Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 165 lines (132 sloc) 5.141 kb
58135b3 [t/spec] changed 'use v6-alpha;' to 'use v6;'
moritz authored
1 use v6;
eb624f9 [t] Test suite refactoring.
cosimo authored
2
3 use Test;
4
5 # L<S16/"Filehandles, files, and directories"/"open">
6 # L<S16/"Filehandles, files, and directories"/"close">
2417519 [t\spec] smartlink issues
buchetc authored
7 # L<S16/Unfiled/IO.readline>
eb624f9 [t] Test suite refactoring.
cosimo authored
8
9 =begin pod
10
11 I/O tests
12
13 =end pod
14
64cdf98 [t/spec] initial, very basic tests for IO.ins
moritz authored
15 plan 61;
eb624f9 [t] Test suite refactoring.
cosimo authored
16
a29ae0f [t/spec] better tempfile name
moritz authored
17 #?pugs emit if $*OS eq "browser" {
18 #?pugs emit skip_rest "Programs running in browsers don't have access to regular IO.";
19 #?pugs emit exit;
20 #?pugs emit }
eb624f9 [t] Test suite refactoring.
cosimo authored
21
22
25e2988 [t/spec] rakudo doesn't do Int() yet, so use a different way to generate...
moritz authored
23 sub nonce () { return ".{$*PID}." ~ (1..1000).pick() }
a29ae0f [t/spec] better tempfile name
moritz authored
24 my $filename = 'tempfile_filehandles_io' ~ nonce();
eb624f9 [t] Test suite refactoring.
cosimo authored
25
26 # create and write a file
27
28 my $out = open($filename, :w);
207f13f [t] and [t/spec]
moritz authored
29 isa_ok($out, IO);
eb624f9 [t] Test suite refactoring.
cosimo authored
30 $out.say("Hello World");
31329b9 [t/spec]: Fudge io.t for Rakudo.
pmichaud authored
31 $out.say("Foo Bar Baz");
eb624f9 [t] Test suite refactoring.
cosimo authored
32 $out.say("The End");
33 ok($out.close, 'file closed okay');
34
35 # read the file all possible ways
36
37 my $in1 = open($filename);
207f13f [t] and [t/spec]
moritz authored
38 isa_ok($in1, IO);
eb624f9 [t] Test suite refactoring.
cosimo authored
39 my $line1a = readline($in1);
40 is($line1a, "Hello World", 'readline($in) worked (and autochomps)');
64cdf98 [t/spec] initial, very basic tests for IO.ins
moritz authored
41 is $in1.ins, 1, 'read one line (.ins)';
eb624f9 [t] Test suite refactoring.
cosimo authored
42 my $line1b = readline($in1);
43 is($line1b, "Foo Bar Baz", 'readline($in) worked (and autochomps)');
64cdf98 [t/spec] initial, very basic tests for IO.ins
moritz authored
44 is $in1.ins, 2, 'read two lines (.ins)';
eb624f9 [t] Test suite refactoring.
cosimo authored
45 my $line1c = readline($in1);
46 is($line1c, "The End", 'readline($in) worked');
64cdf98 [t/spec] initial, very basic tests for IO.ins
moritz authored
47 is $in1.ins, 3, 'read two lines (.ins)';
eb624f9 [t] Test suite refactoring.
cosimo authored
48 ok($in1.close, 'file closed okay (1)');
49
50 my $in2 = open($filename);
207f13f [t] and [t/spec]
moritz authored
51 isa_ok($in2, IO);
eb624f9 [t] Test suite refactoring.
cosimo authored
52 my $line2a = $in2.readline();
53 is($line2a, "Hello World", '$in.readline() worked');
54 my $line2b = $in2.readline();
55 is($line2b, "Foo Bar Baz", '$in.readline() worked');
56 my $line2c = $in2.readline();
57 is($line2c, "The End", '$in.readline() worked');
58 ok($in2.close, 'file closed okay (2)');
59
60 # L<S02/Files/you now write>
61 my $in3 = open($filename);
207f13f [t] and [t/spec]
moritz authored
62 isa_ok($in3, IO);
eb624f9 [t] Test suite refactoring.
cosimo authored
63 my $line3a = =$in3;
64 is($line3a, "Hello World", 'unary =$in worked');
65 my $line3b = =$in3;
66 is($line3b, "Foo Bar Baz", 'unary =$in worked');
67 my $line3c = =$in3;
68 is($line3c, "The End", 'unary =$in worked');
69 ok($in3.close, 'file closed okay (3)');
70
71 # append to the file
72
73 my $append = open($filename, :a);
207f13f [t] and [t/spec]
moritz authored
74 isa_ok($append, IO);
eb624f9 [t] Test suite refactoring.
cosimo authored
75 $append.say("... Its not over yet!");
76 ok($append.close, 'file closed okay (append)');
77
78 # now read in in list context
79
80 my $in4 = open($filename);
207f13f [t] and [t/spec]
moritz authored
81 isa_ok($in4, IO);
eb624f9 [t] Test suite refactoring.
cosimo authored
82 my @lines4 = readline($in4);
83 is(+@lines4, 4, 'we got four lines from the file');
84 is(@lines4[0], "Hello World", 'readline($in) worked in list context');
85 is(@lines4[1], "Foo Bar Baz", 'readline($in) worked in list context');
86 is(@lines4[2], "The End", 'readline($in) worked in list context');
87 is(@lines4[3], "... Its not over yet!", 'readline($in) worked in list context');
88 ok($in4.close, 'file closed okay (4)');
89
90 my $in5 = open($filename);
207f13f [t] and [t/spec]
moritz authored
91 isa_ok($in5, IO);
eb624f9 [t] Test suite refactoring.
cosimo authored
92 my @lines5 = $in5.readline();
93 is(+@lines5, 4, 'we got four lines from the file');
94 is(@lines5[0], "Hello World", '$in.readline() worked in list context');
95 is(@lines5[1], "Foo Bar Baz", '$in.readline() worked in list context');
96 is(@lines5[2], "The End", '$in.readline() worked in list context');
97 is(@lines5[3], "... Its not over yet!", '$in.readline() worked in list context');
98 ok($in5.close, 'file closed okay (5)');
99
100 my $in6 = open($filename);
207f13f [t] and [t/spec]
moritz authored
101 isa_ok($in6, IO);
eb624f9 [t] Test suite refactoring.
cosimo authored
102 my @lines6 = =$in6;
103 is(+@lines6, 4, 'we got four lines from the file');
104 is(@lines6[0], "Hello World", 'unary =$in worked in list context');
105 is(@lines6[1], "Foo Bar Baz", 'unary =$in worked in list context');
106 is(@lines6[2], "The End", 'unary =$in worked in list context');
107 is(@lines6[3], "... Its not over yet!", 'unary =$in worked in list context');
108 ok($in6.close, 'file closed okay (6)');
109
110 # test reading a file into an array and then closing before
111 # doing anything with the array (in other words, is pugs too lazy)
112 my $in7 = open($filename);
207f13f [t] and [t/spec]
moritz authored
113 isa_ok($in7, IO);
eb624f9 [t] Test suite refactoring.
cosimo authored
114 my @lines7 = readline($in7);
115 ok($in7.close, 'file closed okay (7)');
116 is(+@lines7, 4, 'we got four lines from the file (lazily)');
117 is(@lines7[0], "Hello World", 'readline($in) worked in list context');
118 is(@lines7[1], "Foo Bar Baz", 'readline($in) worked in list context');
119 is(@lines7[2], "The End", 'readline($in) worked in list context');
120 is(@lines7[3], "... Its not over yet!", 'readline($in) worked in list context');
121
122 #now be sure to delete the file as well
123
124 is(unlink($filename), 1, 'file has been removed');
125
126 # new file for testing other types of open() calls
127
128 my $out8 = open($filename, :w);
207f13f [t] and [t/spec]
moritz authored
129 isa_ok($out8, IO);
eb624f9 [t] Test suite refactoring.
cosimo authored
130 $out8.say("Hello World");
131 ok($out8.close, 'file closed okay (out8)');
132
133 my $in8 = open($filename);
207f13f [t] and [t/spec]
moritz authored
134 isa_ok($in8, IO);
eb624f9 [t] Test suite refactoring.
cosimo authored
135 my $line8_1 = readline($in8);
136 is($line8_1, "Hello World", 'readline($in) worked');
137 ok($in8.close, 'file closed okay (in8)');
138
139 my $fh9 = open($filename, :r, :w); # was "<+" ?
207f13f [t] and [t/spec]
moritz authored
140 isa_ok($fh9, IO);
eb624f9 [t] Test suite refactoring.
cosimo authored
141 #my $line9_1 = readline($fh9);
142 #is($line9_1, "Hello World");
143 #$fh9.say("Second line");
144 ok($fh9.close, 'file closed okay (9)');
145
146 #my $in9 = open($filename);
207f13f [t] and [t/spec]
moritz authored
147 #isa_ok($in9, IO);
eb624f9 [t] Test suite refactoring.
cosimo authored
148 #my $line9_1 = readline($in9);
149 #my $line9_2 = readline($in9);
150 #is($line9_1, "Hello World", 'readline($in) worked');
151 #is($line9_2, "Second line", 'readline($in) worked');
152
31329b9 [t/spec]: Fudge io.t for Rakudo.
pmichaud authored
153 #?rakudo skip ':rw on open() unimplemented'
154 {
155 my $fh10 = open($filename, :rw); # was "<+" ?
207f13f [t] and [t/spec]
moritz authored
156 isa_ok($fh10, IO);
31329b9 [t/spec]: Fudge io.t for Rakudo.
pmichaud authored
157 #ok($fh10.close, 'file closed okay (10)');
158 }
eb624f9 [t] Test suite refactoring.
cosimo authored
159
fecfcdd [t/spec] Remove some Win32-specific logic on a test that saw us flunk it...
jnthn authored
160 #?pugs todo 'buggy on Win32'
161 ok(unlink($filename), 'file has been removed');
21f6acc [t/spec]
moritz authored
162 ok $filename !~~ :e, '... and the tempfile is gone, really';
734bc95 [t/spec]
moritz authored
163
164 # vim: ft=perl6
Something went wrong with that request. Please try again.