1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
use 5.006;
use strict;
use warnings;
use Test::More 0.96;
use Capture::Tiny 0.12 qw/capture/;
use File::Temp qw/tempfile/;
use IO::Prompt::Tiny qw/prompt/;
delete $ENV{PERL_MM_USE_DEFAULT}; # need our own, ignore external one
sub _set_tempfile {
my $text = shift;
my $temp = tempfile;
select $temp;
local $| = 1;
select STDOUT;
print {$temp} $text;
seek $temp, 0, 0;
return $temp;
}
sub _prompt {
my @args = @_;
my ( $out, $err, $result ) = capture { scalar prompt(@args) };
return $result;
}
{
no warnings 'redefine';
local *IO::Prompt::Tiny::_is_interactive = sub { 1 }; # fake it for testing
local *STDIN = _set_tempfile("yes");
is( _prompt( "Yes or no?", "no" ), "yes", "prompt read from STDIN" );
is( _prompt( "Yes or no?", "no" ),
"no", "prompt returned default when input exhausted" );
};
{
no warnings 'redefine';
local *IO::Prompt::Tiny::_is_interactive = sub { 1 }; # fake it for testing
local *STDIN = _set_tempfile("yes");
local $ENV{PERL_MM_USE_DEFAULT} = 1;
is( _prompt( "Yes or no?", "no" ),
"no", "prompt returned default under PERL_MM_USE_DEFAULT" );
};
{
local *STDIN = _set_tempfile("yes");
is( _prompt( "Yes or no?", "no" ),
"no", "prompt returned default when not interactive" );
};
done_testing;
#
# This file is part of IO-Prompt-Tiny
#
# This software is Copyright (c) 2012 by David Golden.
#
# This is free software, licensed under:
#
# The Apache License, Version 2.0, January 2004
#
|