blib/lib/Devel/Trepan/IO/StringArray.pm untested

filter: Statementstmt 0.0% 0 / 81 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 142 CC 7 SCAR 15.7
CC
7
Cov
0%
readline
12.0
new
12.0
new
6.0
CRAP 4.8
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2011 Rocky Bernstein <rocky@cpan.org>
3  
4 # Simulate I/O using lists of strings.
5 use rlib '../../..';
6  
7 use rlib '../../..';
8 package Devel::Trepan::IO::StringArrayInput;
9 use warnings; use strict;
10  
11 use Devel::Trepan::IO;
12  
13 use vars qw(@ISA);
14 @ISA = qw(Devel::Trepan::IO::InputBase);
15  
16 # Simulate I/O using an array of strings. Sort of like StringIO, but
17 # even simplier.
18  
19 sub new($$;$)
20 {
21 my ($class, $inp, $opts) = @_;
22 $opts = {} unless defined $opts;
23 my $self = Devel::Trepan::IO::InputBase->new($inp, $opts);
24 $self->{closed} = 0;
25 bless $self, $class;
26 $self
27 }
28  
29 # this close() interface is defined for class compatibility
30 sub close($)
31 {
32 my $self = shift;
33 $self->{closed} = 1;
34 }
35  
36 sub is_closed($)
37 {
38 my $self = shift;
39 $self->{closed};
40 }
41  
42 sub is_eof($)
43 {
44 my $self = shift;
45 $self->{closed} || !@{$self->{input}};
46 }
47  
48 # Nothing to do here. Interface is for compatibility
49 sub flush($) { ; }
50  
51 # Read a line of input. undef is returned on EOF.
52 # Note that we don't support prompting.
53 sub readline($)
54 {
55 my $self = shift;
56 return undef if $self->is_eof;
57 unless (@{$self->{input}}) {
58 return undef;
59 }
60 my $line = shift @{$self->{input}};
61 return $line ;
62 }
63  
64 sub have_term_readline($)
65 {
66 return 0;
67 }
68  
69 # class << self
70 # # Use this to set where to read from.
71 # sub open(inp, opts={})
72 # if inp.is_a?(Array)
73 # return self.new(inp)
74 # else
75 # raise IOError, "Invalid input type (%s) for %s" % [inp.class, inp]
76 # }
77 # }
78 # }
79 # }
80  
81 # Simulate I/O using an array of strings. Sort of like StringIO, but
82 # even simplier.
83 package Devel::Trepan::IO::StringArrayOutput;
84 use vars qw(@ISA);
85 @ISA = qw(Devel::Trepan::IO::OutputBase);
86  
87 sub new
88 {
89 my ($class, $out, $opts) = @_;
90 $out = [] unless defined $out;
91 $opts = {} unless defined $opts;
92 my $self = Devel::Trepan::IO::OutputBase->new($out, $opts);
93 $self->{closed} = 0;
94 bless $self, $class;
95 return $self;
96 }
97  
98 # Nothing to do here. Interface is for compatibility
99 sub close($)
100 {
101 my $self = shift;
102 $self->{closed} = 1;
103 }
104  
105 sub is_closed($)
106 {
107 my $self = shift;
108 $self->{closed};
109 }
110  
111 sub is_eof()
112 {
113 my $self = shift;
114 $self->{close} || !$self->{output};
115 }
116  
117 # Nothing to do here. Interface is for compatibility
118 sub flush() { ; }
119  
120 # This method the debugger uses to write. In contrast to
121 # writeline, no newline is added to the } to `str'.
122 #
123 sub write($$)
124 {
125 my ($self, $msg) = @_;
126 return undef if $self->{closed};
127 push @{$self->{output}}, $msg;
128 }
129
130 # used to write to a debugger that is connected to this
131 # server; Here, we use the null string '' as an indicator of a
132 # newline.
133 sub writeline($$)
134 {
135 my ($self, $msg) = @_;
136 $self->write($msg);
137 $self->write('');
138 }
139  
140 # class << self
141 # # Use this to set where to write to. output can be a
142 # # file object or a string. This code raises IOError on error.
143 # #
144 # # If another file was previously open upon calling this open,
145 # # that will be stacked and will come back into use after
146 # # a close_write().
147 # sub open(output=[])
148 # if output.is_a?(Array)
149 # return self.new(output)
150 # else
151 # raise IOError, ("Invalid output type (%s) for %s" %
152 # [output.class, output])
153 # }
154 # }
155 # }
156 # }
157  
158 # Demo
159 unless (caller) {
160 my $inp = Devel::Trepan::IO::StringArrayInput->new(
161 ['Now is the time', 'for all good men']);
162 my $line = $inp->readline;
163 print $line, "\n";
164 $line = $inp->readline;
165 print $line, "\n";
166 $line = $inp->readline;
167 print "That's the end the line\n" unless defined $line;
168  
169 my $out = Devel::Trepan::IO::StringArrayOutput->new;
170 $out->writeline("Some output");
171 $out->writeline('Hello, world!');
172 print $out->{output}[0], "\n";
173 print $out->{output}[1], "\n";
174 print $out->{output}[2], "\n";
175 # out.write('Hello');
176 # p out.output
177 # out.writeline(', again.');
178 # p out.output
179 # # io.open_write(sys.stdout)
180 # out.flush_after_write = true
181 # out.write('Last hello')
182 # print "Output is closed? #{out.closed?}"
183 # out.close
184 # p out.output
185 # begin
186 # out.writeline("You won't see me")
187 # rescue
188 # }
189  
190 # # Closing after already closed is okay
191 # out.close
192 # print "Output is closed? #{out.closed?}"
193 # print "Input is closed? #{inp.closed?}"
194 # inp.close
195 # print "Input is closed? #{inp.closed?}"
196 }