| 1 | #! perl |
|---|
| 2 | # Copyright (C) 2010, Parrot Foundation. |
|---|
| 3 | # $Id:$ |
|---|
| 4 | |
|---|
| 5 | use strict; |
|---|
| 6 | use warnings; |
|---|
| 7 | use lib qw( . lib ../lib ../../lib ); |
|---|
| 8 | |
|---|
| 9 | use Test::More tests => 3; |
|---|
| 10 | use Parrot::Test; |
|---|
| 11 | use Parrot::Config; |
|---|
| 12 | |
|---|
| 13 | use Parrot::Test::Util 'create_tempfile'; |
|---|
| 14 | |
|---|
| 15 | =head1 NAME |
|---|
| 16 | |
|---|
| 17 | t/pmc/stdin-io.t - IO Ops from stdin |
|---|
| 18 | |
|---|
| 19 | =head1 SYNOPSIS |
|---|
| 20 | |
|---|
| 21 | % prove t/pmc/stdin-io.t |
|---|
| 22 | |
|---|
| 23 | =head1 DESCRIPTION |
|---|
| 24 | |
|---|
| 25 | Tests the Parrot IO operations from stdin. |
|---|
| 26 | |
|---|
| 27 | =cut |
|---|
| 28 | |
|---|
| 29 | sub pir_stdin_output_is { |
|---|
| 30 | my ($input_string, $code, $expected_output, $test_name) = @_; |
|---|
| 31 | |
|---|
| 32 | my $stuff = sub { |
|---|
| 33 | # Put the string on a file. |
|---|
| 34 | my $string = shift; |
|---|
| 35 | |
|---|
| 36 | my (undef, $file) = create_tempfile(UNLINK => 1); |
|---|
| 37 | open(my $out, ">$file") or die "bug"; |
|---|
| 38 | print $out $string; |
|---|
| 39 | return $file; |
|---|
| 40 | }; |
|---|
| 41 | |
|---|
| 42 | # Write the input and code strings. |
|---|
| 43 | my $input_file = $stuff->($input_string); |
|---|
| 44 | my $code_file = $stuff->($code); |
|---|
| 45 | |
|---|
| 46 | # Slurp and compare the output. |
|---|
| 47 | my $result = do { |
|---|
| 48 | local $/; |
|---|
| 49 | open(my $in, "./parrot '$code_file' < '$input_file' |") |
|---|
| 50 | or die "bug"; |
|---|
| 51 | <$in>; |
|---|
| 52 | }; |
|---|
| 53 | Test::More::is($result, $expected_output, $test_name); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | pir_stdin_output_is(<<'INPUT', <<'CODE', <<'OUTPUT', 'simple stdin use'); |
|---|
| 57 | foo |
|---|
| 58 | INPUT |
|---|
| 59 | .sub test :main |
|---|
| 60 | .local pmc stdin |
|---|
| 61 | stdin = getstdin |
|---|
| 62 | read_str(stdin, 1) |
|---|
| 63 | read_str(stdin, 2) |
|---|
| 64 | read_str(stdin, 5) |
|---|
| 65 | read_str(stdin, 5) |
|---|
| 66 | .end |
|---|
| 67 | .sub read_str |
|---|
| 68 | .param pmc stream |
|---|
| 69 | .param int n_chars |
|---|
| 70 | .local string input_string |
|---|
| 71 | .local int len |
|---|
| 72 | input_string = stream.'read'(n_chars) |
|---|
| 73 | len = length input_string |
|---|
| 74 | print "got len " |
|---|
| 75 | print len |
|---|
| 76 | print ': "' |
|---|
| 77 | print input_string |
|---|
| 78 | print "\"\n" |
|---|
| 79 | .end |
|---|
| 80 | CODE |
|---|
| 81 | got len 1: "f" |
|---|
| 82 | got len 2: "oo" |
|---|
| 83 | got len 1: " |
|---|
| 84 | " |
|---|
| 85 | got len 0: "" |
|---|
| 86 | OUTPUT |
|---|
| 87 | |
|---|
| 88 | pir_stdin_output_is(<<'INPUT', <<'CODE', <<'OUTPUT', 'readline from stdin'); |
|---|
| 89 | one |
|---|
| 90 | two |
|---|
| 91 | three |
|---|
| 92 | INPUT |
|---|
| 93 | .sub test :main |
|---|
| 94 | .local pmc stdin |
|---|
| 95 | stdin = getstdin |
|---|
| 96 | read_str(stdin) |
|---|
| 97 | read_str(stdin) |
|---|
| 98 | read_str(stdin) |
|---|
| 99 | read_str(stdin) |
|---|
| 100 | .end |
|---|
| 101 | .sub read_str |
|---|
| 102 | .param pmc stream |
|---|
| 103 | .local string input_string |
|---|
| 104 | .local int len |
|---|
| 105 | input_string = stream.'readline'() |
|---|
| 106 | len = length input_string |
|---|
| 107 | print "got len " |
|---|
| 108 | print len |
|---|
| 109 | print ': "' |
|---|
| 110 | print input_string |
|---|
| 111 | print "\"\n" |
|---|
| 112 | .end |
|---|
| 113 | CODE |
|---|
| 114 | got len 4: "one |
|---|
| 115 | " |
|---|
| 116 | got len 4: "two |
|---|
| 117 | " |
|---|
| 118 | got len 6: "three |
|---|
| 119 | " |
|---|
| 120 | got len 0: "" |
|---|
| 121 | OUTPUT |
|---|
| 122 | |
|---|
| 123 | pir_stdin_output_is(<<'INPUT', <<'CODE', <<'OUTPUT', 'readall from stdin'); |
|---|
| 124 | one |
|---|
| 125 | two |
|---|
| 126 | three |
|---|
| 127 | INPUT |
|---|
| 128 | .sub test :main |
|---|
| 129 | .local pmc stdin |
|---|
| 130 | stdin = getstdin |
|---|
| 131 | read_str(stdin) |
|---|
| 132 | read_str(stdin) |
|---|
| 133 | .end |
|---|
| 134 | .sub read_str |
|---|
| 135 | .param pmc stream |
|---|
| 136 | .local string input_string |
|---|
| 137 | .local int len |
|---|
| 138 | input_string = stream.'readall'() |
|---|
| 139 | len = length input_string |
|---|
| 140 | print "got len " |
|---|
| 141 | print len |
|---|
| 142 | print ': "' |
|---|
| 143 | print input_string |
|---|
| 144 | print "\"\n" |
|---|
| 145 | .end |
|---|
| 146 | CODE |
|---|
| 147 | got len 14: "one |
|---|
| 148 | two |
|---|
| 149 | three |
|---|
| 150 | " |
|---|
| 151 | got len 0: "" |
|---|
| 152 | OUTPUT |
|---|