G'day Bod,

"... reads JSON data from STDIN on a webserver. ... I have had to simulate this."

There's a number of ways a program can read from STDIN:

# By default $ cat > TabNL Tab NL # By redirection $ cat -vet < TabNL Tab^INL$ # By piping $ cat TabNL | cat -vet Tab^INL$ # By using '-' as a special filename $ cat -vet - Tab NL Tab^INL$ # By others I didn't immediately think of

Which of those methods does your module use? Knowing this will allow us to better advise you on ways to perform the simulation. :-)

"Using code I found in an answer on SO ... *STDIN = *DATA; ..."

That's not really simulating STDIN. You're just rebadging an existing filehandle:

$ perl -E ' use strict; use warnings; say "Real \\*STDIN fileno: ", fileno(\*STDIN); say "Real \\*DATA fileno: ", fileno(\*DATA); *STDIN = *DATA; say "Fake \\*STDIN fileno: ", fileno(\*STDIN); __DATA__ some data ' Real \*STDIN fileno: 0 Real \*DATA fileno: 3 Fake \*STDIN fileno: 3

Beyond simulating the input, it would probably help to have some idea of what tests you intend to run.

Here's a test script that simulates JSON being piped to your application. It's subsequently decoded and compared with reference data via is_deeply() (presumably you'd have more useful tests here). Note how you can run the tests on multiple JSON files.

ken@titan ~/tmp/pm_11152777_test_stdin/t $ cat test_json.t #!perl use strict; use warnings; use autodie; use Cwd 'abs_path'; use File::Basename 'dirname'; my $THISDIR; BEGIN { $THISDIR = dirname abs_path __FILE__ } use JSON::MaybeXS; use POSIX '_exit'; use Test::More; my @file_bases = qw{test1 testA}; plan tests => 0+@file_bases; for my $file_base (@file_bases) { my $json_data = ''; my $child_pid = open my $from_kid, '-|'; if ($child_pid) { # parent process (pipe from child): # reads JSON from "effective" STDIN while (my $line = <$from_kid>) { $json_data .= $line; } waitpid $child_pid, 0; } else { # child process (pipe to parent): # writes JSON to STDOUT my $json_file = "$THISDIR/../data/$file_base.json"; open my $json_fh, '<', $json_file; while (my $line = <$json_fh>) { print $line; } _exit 0; } my $perl_data = decode_json($json_data); my $reference_data = do "$THISDIR/../data/$file_base.perl"; is_deeply $perl_data, $reference_data, "Testing '$file_base'"; }

Here's the test data:

ken@titan ~/tmp/pm_11152777_test_stdin/data $ cat test1.json { "key1" : "val1", "key2" : [ "elem1", "elem2", "elem3" ], "key3" : { "name1" : "value1", "name2" : "value2" } } $ cat test1.perl { key1 => 'val1', key2 => [qw{elem1 elem2 elem3}], key3 => {name1 => 'value1', name2 => 'value2'}, }; $ cat testA.json { "keyA" : "valA", "keyB" : [ "elemA", "elemB", "elemC" ], "keyC" : { "nameA" : "valueA", "nameB" : "valueB" } } $ cat testA.perl { keyA => 'valA', keyB => [qw{elemA elemB elemC}], keyC => {nameA => 'valueA', nameB => 'valueB'}, };

And here's an actual test run:

ken@titan ~/tmp/pm_11152777_test_stdin $ prove -v t/test_json.t t/test_json.t .. 1..2 ok 1 - Testing 'test1' ok 2 - Testing 'testA' ok All tests successful. Files=1, Tests=2, 1 wallclock secs ( 0.01 usr 0.03 sys + 0.12 cusr + 0.08 csys = 0.25 CPU) Result: PASS

— Ken


In reply to Re: STDIN typeglob by kcott
in thread STDIN typeglob by Bod

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.