<commit_before>#! perl
# Copyright (C) 2010, Parrot Foundation.
# $Id$

use strict;
use warnings;
use lib qw( . lib ../lib ../../lib );

use Test::More tests => 3;
use Parrot::Test;
use Parrot::Config;

use Parrot::Test::Util 'create_tempfile';

=head1 NAME

t/pmc/stdin-io.t - IO Ops from stdin

=head1 SYNOPSIS

    % prove t/pmc/io_stdin.t

=head1 DESCRIPTION

Tests the Parrot IO operations from stdin.

=cut

sub pir_stdin_output_is {
    my ($input_string, $code, $expected_output, $test_name) = @_;

    my $stuff = sub {
        # Put the string on a file.
        my $string = shift;

        my (undef, $file) = create_tempfile(UNLINK => 1);
        open(my $out, '>', "$file") or die "bug";
        print $out $string;
        return $file;
    };

    # Write the input and code strings.
    my $input_file = $stuff->($input_string);
    my $code_file = $stuff->($code);

    # Slurp and compare the output.
    my $result = do {
        local $/;
        open(my $in, "./parrot '$code_file' < '$input_file' |")
            or die "bug";
        <$in>;
    };
    Test::More::is($result, $expected_output, $test_name);
}

pir_stdin_output_is(<<'INPUT', <<'CODE', <<'OUTPUT', 'simple stdin use');
foo
INPUT
.sub test :main
    .local pmc stdin
    stdin = getstdin
    read_str(stdin, 1)
    read_str(stdin, 2)
    read_str(stdin, 5)
    read_str(stdin, 5)
.end
.sub read_str
    .param pmc stream
    .param int n_chars
    .local string input_string
    .local int len
    input_string = stream.'read'(n_chars)
    len = length input_string<commit_msg>[t] Use three-argument form of perl open<commit_after>#! perl
# Copyright (C) 2010, Parrot Foundation.
# $Id$

use strict;
use warnings;
use lib qw( . lib ../lib ../../lib );

use Test::More tests => 3;
use Parrot::Test;
use Parrot::Config;

use Parrot::Test::Util 'create_tempfile';

=head1 NAME

t/pmc/stdin-io.t - IO Ops from stdin

=head1 SYNOPSIS

    % prove t/pmc/io_stdin.t

=head1 DESCRIPTION

Tests the Parrot IO operations from stdin.

=cut

sub pir_stdin_output_is {
    my ($input_string, $code, $expected_output, $test_name) = @_;

    my $stuff = sub {
        # Put the string on a file.
        my $string = shift;

        my (undef, $file) = create_tempfile(UNLINK => 1);
        open(my $out, '>', "$file") or die "bug";
        print $out $string;
        return $file;
    };

    # Write the input and code strings.
    my $input_file = $stuff->($input_string);
    my $code_file = $stuff->($code);

    # Slurp and compare the output.
    my $result = do {
        local $/;
        open(my $in, '-|', "./parrot '$code_file' < '$input_file'")
            or die "bug";
        <$in>;
    };
    Test::More::is($result, $expected_output, $test_name);
}

pir_stdin_output_is(<<'INPUT', <<'CODE', <<'OUTPUT', 'simple stdin use');
foo
INPUT
.sub test :main
    .local pmc stdin
    stdin = getstdin
    read_str(stdin, 1)
    read_str(stdin, 2)
    read_str(stdin, 5)
    read_str(stdin, 5)
.end
.sub read_str
    .param pmc stream
    .param int n_chars
    .local string input_string
    .local int len
    input_string = stream.'read'(n_chars)
    len = length input_string
{'fraction_non_alphanumeric': 0.14583992407465993, 'fraction_numerical': 0.008225245175577349, 'mean_word_length': 2.7069167643610785, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2047797', 'n_tokens_mistral': 1194, 'n_tokens_neox': 1146, 'n_words': 329}