|
| 1 | +use v6; |
| 2 | +use Test; |
| 3 | + |
| 4 | +use lib 't/spec/packages'; |
| 5 | +use Test::Util; |
| 6 | + |
| 7 | +plan 7; |
| 8 | + |
| 9 | +sub create-temporary-file { |
| 10 | + my $filename = $*TMPDIR ~ '/tmp.' ~ $*PID ~ '-' ~ time; |
| 11 | + return $filename, open($filename, :w); |
| 12 | +} |
| 13 | + |
| 14 | +my ( $tmp-file-name, $tmp-file-handle ) = create-temporary-file; |
| 15 | +my $output; |
| 16 | +my @lines; |
| 17 | + |
| 18 | +$tmp-file-handle.say: 'one'; |
| 19 | +$tmp-file-handle.say: 'two'; |
| 20 | +$tmp-file-handle.say: 'three'; |
| 21 | + |
| 22 | +$tmp-file-handle.close; |
| 23 | + |
| 24 | +my @args = $tmp-file-name; |
| 25 | + |
| 26 | +$output = Test::Util::run('say get()', :@args); |
| 27 | + |
| 28 | +is $output, "one\n", 'get() should read from $*ARGFILES, which reads from files in @*ARGS'; |
| 29 | + |
| 30 | +$output = Test::Util::run('say get()', "foo\nbar\nbaz\n"); |
| 31 | + |
| 32 | +is $output, "foo\n", 'get($*ARGFILES) reads from $*IN if no files are in @*ARGS'; |
| 33 | + |
| 34 | +$output = Test::Util::run('while get() -> $line { say $line }', :@args); |
| 35 | +@lines = lines($output); |
| 36 | + |
| 37 | +is-deeply @lines, [<one two three>], 'calling get() several times should work'; |
| 38 | + |
| 39 | +$output = Test::Util::run('while get() -> $line { say $line }', "foo\nbar\nbaz\n", :@args); |
| 40 | +@lines = lines($output); |
| 41 | + |
| 42 | +is-deeply @lines, [<one two three>], '$*ARGFILES should not use $*IN if files are in @*ARGS'; |
| 43 | + |
| 44 | +$output = Test::Util::run('.say for lines()', :@args); |
| 45 | +@lines = lines($output); |
| 46 | + |
| 47 | +is-deeply @lines, [<one two three>], 'lines() should read from $*ARGFILES, which reads from files in @*ARGS'; |
| 48 | + |
| 49 | +$output = Test::Util::run('.say for lines()', "foo\nbar\nbaz\n"); |
| 50 | +@lines = lines($output); |
| 51 | + |
| 52 | +is-deeply @lines, [<foo bar baz>], 'lines($*ARGFILES) reads from $*IN if no files are in @*ARGS'; |
| 53 | + |
| 54 | +$output = Test::Util::run('.say for lines()', "foo\nbar\nbaz\n", :@args); |
| 55 | +@lines = lines($output); |
| 56 | + |
| 57 | +is-deeply @lines, [<one two three>], '$*ARGFILES should not use $*IN if files are in @*ARGS'; |
| 58 | + |
| 59 | +$tmp-file-name.IO.unlink; |
0 commit comments