Ticket #1839: stdin-io.t

File stdin-io.t, 2.5 KB (added by rgrjr, 4 years ago)

Test script for reading from stdin

Line 
1#! perl
2# Copyright (C) 2010, Parrot Foundation.
3# $Id:$
4
5use strict;
6use warnings;
7use lib qw( . lib ../lib ../../lib );
8
9use Test::More tests => 3;
10use Parrot::Test;
11use Parrot::Config;
12
13use Parrot::Test::Util 'create_tempfile';
14
15=head1 NAME
16
17t/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
25Tests the Parrot IO operations from stdin.
26
27=cut
28
29sub 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
56pir_stdin_output_is(<<'INPUT', <<'CODE', <<'OUTPUT', 'simple stdin use');
57foo
58INPUT
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
80CODE
81got len 1:  "f"
82got len 2:  "oo"
83got len 1:  "
84"
85got len 0:  ""
86OUTPUT
87
88pir_stdin_output_is(<<'INPUT', <<'CODE', <<'OUTPUT', 'readline from stdin');
89one
90two
91three
92INPUT
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
113CODE
114got len 4:  "one
115"
116got len 4:  "two
117"
118got len 6:  "three
119"
120got len 0:  ""
121OUTPUT
122
123pir_stdin_output_is(<<'INPUT', <<'CODE', <<'OUTPUT', 'readall from stdin');
124one
125two
126three
127INPUT
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
146CODE
147got len 14:  "one
148two
149three
150"
151got len 0:  ""
152OUTPUT