Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 51 lines (41 sloc) 1.27 kb
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
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;
# COPYRIGHT
Something went wrong with that request. Please try again.