2 # Copyright (C) 2006-2008, Parrot Foundation.
7 use lib qw( . lib ../lib ../../lib );
9 use Parrot::Test tests => 6;
13 t/pmc/parrotio.t - test the FileHandle PMC
17 % prove t/pmc/parrotio.t
21 Tests the FileHandle PMC.
25 # L<PDD22/I\/O PMC API/=item new>
26 pir_output_is( <<'CODE', <<'OUT', 'new' );
28 new $P0, ['FileHandle']
29 say "ok 1 - $P0 = new ['FileHandle']"
32 ok 1 - $P0 = new ['FileHandle']
35 # L<PDD22/I\/O PMC API/=item open.*=item close>
36 pir_output_is( <<'CODE', <<'OUT', 'open and close - synchronous', todo => 'not yet implemented' );
38 $P0 = new ['FileHandle']
40 say 'ok 1 - $P0.open($S1)'
43 say 'ok 2 - $P0.close()'
45 $P0.open('README', 'rw')
46 say 'ok 3 - $P0.open($S1, $S2) # rw mode'
50 say 'ok 4 - $P0.open()'
58 $P0.open('bad_file', 'r')
62 $P0.open('new_file', 'w')
63 say 'ok 6 - $P0.open($S1, $S2) # new file, write mode succeeds'
68 say 'ok 5 - $P0.open($S1) # with bad file'
75 ok 3 - $P0.open($S1, $S2) # rw mode
77 ok 5 - $P0.open($S1) # with bad file
78 ok 6 - $P0.open($S1, $S2) # new file, write mode succeeds
81 # should be in the PIR code
85 skip 'no asynch calls yet' => 1;
87 pir_output_is( <<'CODE', <<'OUT', 'open and close - asynchronous' );
89 $P1 = # TT #1204 create a callback here
90 $P0 = new ['FileHandle']
93 say 'ok 1 - $P0.open($S1)'
96 say 'ok 2 - $P0.close($P1)'
98 $P0.open('README', 'rw')
99 say 'ok 3 - $P0.open($S1, $S2)'
103 say 'ok 4 - $P0.open()'
111 ok 3 - $P0.open($S1, $S2)
116 # L<PDD22/I\/O PMC API/=item print.*=item readline>
118 <<'CODE', <<'OUT', 'print, read, and readline - synchronous', todo => 'not yet implemented' );
120 load_bytecode 'String/Utils.pbc'
122 chomp = get_global ['String';'Utils'], 'chomp'
124 $P0 = new ['FileHandle']
127 $S0 = $P0.read(14) # bytes
128 if $S0 == 'This is Parrot' goto ok_1
131 say 'ok 1 - $S0 = $P1.read($I2)'
133 $S0 = $P0.read(9) # bytes
134 if $S0 == ', version' goto ok_2
137 say 'ok 2 - $S0 = $P1.read($I2) # again on same stream'
141 $P0.print("squawk\n")
142 $P1 = new ['Integer']
145 say 'ok 3 - $P0.print(${I,N,S,P}1)'
149 if $S0 == '123456.789000squawk' goto ok_4
152 say 'ok 4 - $S0 = $P1.readline($I2)'
156 if $S0 == '42' goto ok_5
159 say 'ok 5 - $S0 = $P1.readline($I2) # again on same stream'
162 ok 1 - $S0 = $P1.read($I2)
163 ok 2 - $S0 = $P1.read($I2) # again on same stream
164 ok 3 - $P0.print(${I,N,S,P}1)
165 ok 4 - $S0 = $P1.readline($I2)
166 ok 5 - $S0 = $P1.readline($I2) # again on same stream
169 # TT #1204 test reading long chunks, eof, and across newlines
171 # TT #1204 pir_output_is( <<'CODE', <<'OUT', 'print, read, and readline - asynchronous', todo => 'not yet implemented' );
173 # L<PDD22/I\/O PMC API/=item record_separator>
174 pir_output_is( <<'CODE', <<'OUT', 'record_separator', todo => 'not yet implemented' );
176 $P0 = new ['FileHandle']
178 $S0 = $P0.record_separator()
179 if $S0 == "\n" goto ok_1
182 say 'ok 1 - $S0 = $P1.record_separator() # default'
185 $P0.record_separator($S99)
186 $S0 = $P0.record_separator()
187 if $S0 == $S99 goto ok_2
190 say 'ok 2 - $P0.record_separator($S1)'
193 $S0 = $P0.record_separator()
198 if $S0 == '123abc' goto ok_3
201 say 'ok 3 - $P0.record_separator() # .readline works as expected'
204 ok 1 - $S0 = $P1.record_separator() # default
205 ok 2 - $P0.record_separator($S1)
206 ok 3 - $P0.record_separator() # .readline works as expected
209 # L<PDD22/I\/O PMC API/=item buffer_type>
210 pir_output_is( <<'CODE', <<'OUT', 'buffer_type', todo => 'not yet implemented' );
212 .include 'io_buffer_types.pasm'
214 $P0 = new ['FileHandle']
216 $P0.buffer_type('unbuffered')
217 $I0 = $P0.buffer_type()
218 if $I0 == PIO_NONBUF goto ok_1
221 say 'ok 1 - $I0 = $P1.buffer_type() # PIO_NONBUF'
223 $P0.buffer_type(PIO_NONBUF)
224 $S0 = $P0.buffer_type()
225 if $S0 == 'unbuffered' goto ok_2
228 say 'ok 2 - $S0 = $P1.buffer_type() # PIO_NONBUF'
230 $P0.buffer_type('line-buffered')
231 $I0 = $P0.buffer_type()
232 if $I0 == PIO_LINEBUF goto ok_3
235 say 'ok 3 - $I0 = $P1.buffer_type() # PIO_LINEBUF'
237 $P0.buffer_type(PIO_LINEBUF)
238 $S0 = $P0.buffer_type()
239 if $S0 == 'line-buffered' goto ok_4
242 say 'ok 4 - $S0 = $P1.buffer_type() # PIO_LINEBUF'
244 $P0.buffer_type('full-buffered')
245 $I0 = $P0.buffer_type()
246 if $I0 == PIO_FULLBUF goto ok_5
249 say 'ok 5 - $I0 = $P1.buffer_type() # PIO_FULLBUF'
251 $P0.buffer_type(PIO_FULLBUF)
252 $S0 = $P0.buffer_type()
253 if $S0 == 'full-buffered' goto ok_6
256 say 'ok 6 - $S0 = $P1.buffer_type() # PIO_FULLBUF'
259 ok 1 - $I0 = $P1.buffer_type() # PIO_NONBUF
260 ok 2 - $S0 = $P1.buffer_type() # PIO_NONBUF
261 ok 3 - $I0 = $P1.buffer_type() # PIO_LINEBUF
262 ok 4 - $S0 = $P1.buffer_type() # PIO_LINEBUF
263 ok 5 - $I0 = $P1.buffer_type() # PIO_FULLBUF
264 ok 6 - $S0 = $P1.buffer_type() # PIO_FULLBUF
267 # TT #1204 test effects of buffer_type, not just set/get
270 # L<PDD22/I\/O PMC API/=item buffer_size>
271 # NOTES: try setting positive, zero, negative int
272 # perform print and read ops
273 # change buffer size while it contains data
274 # try with all 'buffer_type' modes
277 # L<PDD22/I\/O PMC API/=item get_fd>
278 # NOTES: this is going to be platform dependent
282 # cperl-indent-level: 4
285 # vim: expandtab shiftwidth=4: