File Coverage

lib/Devel/Trepan/IO/StringArray.pm
Criterion Covered Total %
statement 41 63 65.0
branch 5 16 31.2
condition n/a
subroutine 11 21 52.3
pod n/a
total 57 100 57.0


line stmt bran cond sub pod time code
1             # -*- coding: utf-8 -*-
2             # Copyright (C) 2011 Rocky Bernstein <rocky@cpan.org>
3              
4             # Simulate I/O using lists of strings.
5 14     14   19705 use rlib '../../..';
  14         51  
  14         84  
6              
7 14     14   4455 use rlib '../../..';
  14         36  
  14         58  
8             package Devel::Trepan::IO::StringArrayInput;
9 14     14   4517 use warnings; use strict;
  14     14   37  
  14         336  
  14         70  
  14         33  
  14         376  
10              
11 14     14   356 use Devel::Trepan::IO;
  14         31  
  14         382  
12              
13 14     14   70 use vars qw(@ISA);
  14         36  
  14         4344  
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 1     1   368 my ($class, $inp, $opts) = @_;
22 1 50       6 $opts = {} unless defined $opts;
23 1         8 my $self = Devel::Trepan::IO::InputBase->new($inp, $opts);
24 1         5 $self->{closed} = 0;
25 1         3 bless $self, $class;
26 1         59 $self
27             }
28              
29             # this close() interface is defined for class compatibility
30             sub close($)
31             {
32 0     0   0 my $self = shift;
33 0         0 $self->{closed} = 1;
34             }
35              
36             sub is_closed($)
37             {
38 0     0   0 my $self = shift;
39 0         0 $self->{closed};
40             }
41              
42             sub is_eof($)
43             {
44 0     0   0 my $self = shift;
45 0 0       0 $self->{closed} || !@{$self->{input}};
  0         0  
46             }
47              
48             # Nothing to do here. Interface is for compatibility
49       0     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 0     0   0 my $self = shift;
56 0 0       0 return undef if $self->is_eof;
57 0 0       0 unless (@{$self->{input}}) {
  0         0  
58 0         0 return undef;
59             }
60 0         0 my $line = shift @{$self->{input}};
  0         0  
61 0         0 return $line ;
62             }
63              
64             sub have_term_readline($)
65             {
66 0     0   0 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 14     14   108 use vars qw(@ISA);
  14         41  
  14         5968  
85             @ISA = qw(Devel::Trepan::IO::OutputBase);
86              
87             sub new
88             {
89 3     3   221 my ($class, $out, $opts) = @_;
90 3 100       15 $out = [] unless defined $out;
91 3 50       13 $opts = {} unless defined $opts;
92 3         19 my $self = Devel::Trepan::IO::OutputBase->new($out, $opts);
93 3         14 $self->{closed} = 0;
94 3         8 bless $self, $class;
95 3         11 return $self;
96             }
97              
98             # Nothing to do here. Interface is for compatibility
99             sub close($)
100             {
101 0     0   0 my $self = shift;
102 0         0 $self->{closed} = 1;
103             }
104              
105             sub is_closed($)
106             {
107 0     0   0 my $self = shift;
108 0         0 $self->{closed};
109             }
110              
111             sub is_eof()
112             {
113 0     0   0 my $self = shift;
114 0 0       0 $self->{close} || !$self->{output};
115             }
116              
117             # Nothing to do here. Interface is for compatibility
118       0     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 18     18   32 my ($self, $msg) = @_;
126 18 50       47 return undef if $self->{closed};
127 18         26 push @{$self->{output}}, $msg;
  18         58  
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 9     9   23 my ($self, $msg) = @_;
136 9         27 $self->write($msg);
137 9         20 $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             }