App-AnteREPL
view release on metacpan or search on metacpan
use 5.016;
use strict;
use warnings;
use Test::More;
use FindBin;
use Cwd qw(abs_path);
use lib "$FindBin::Bin/../lib";
use App::AnteREPL;
my $script = abs_path("$FindBin::Bin/../script/anterepl");
sub capture (&) {
my ($code) = @_;
my $output = '';
open my $fh, '>', \$output or die $!;
local *STDOUT = $fh;
$code->();
return $output;
}
{
package TestScreen;
our @ISA = ('App::AnteREPL::Screen');
sub key { shift @{ $_[0]{keys} } || 'EOF' }
sub feed { my ($self, @keys) = @_; $self->{keys} = \@keys; return $self->read_entry; }
}
sub typing { map { ['TEXT', $_] } split //, $_[0] }
my $repl = App::AnteREPL->new;
$repl->dispatch('$FOO={bar=>1};');
my $dump = capture { $repl->dispatch('DUMP $FOO') };
like $dump, qr/\$VAR1 = \{/, 'dump uses Data::Dumper output';
like $dump, qr/'bar' => 1/, 'dump sees current workspace';
{
local %ENV = (PERL_ANTE_REPL_TEST => 'visible');
like capture { $repl->dispatch('DUMP \%ENV') }, qr/'PERL_ANTE_REPL_TEST' => 'visible'/, 'dump accepts an environment reference';
}
like capture { $repl->dispatch('DUMP [1, 2];') }, qr/1,\s+2/s, 'dump accepts expressions, optional semicolon';
my $peek = capture { $repl->dispatch('PEEK $FOO') };
like $peek, qr/SV = /, 'peek uses Devel::Peek';
like $peek, qr/FLAGS = \(ROK\)/, 'peek sees a reference scalar';
like $peek, qr/PVHV/, 'peek follows the hash reference';
like $peek, qr/Elt "bar"/, 'peek inspects actual contents';
like $peek, qr/REFCNT = 1/, 'peek does not add an artificial reference to the named scalar';
like capture { $repl->dispatch('PEEK $FOO, 10;') }, qr/IV = 1/, 'peek accepts the optional depth/element limit';
my $failed = eval { $repl->dispatch('PEEK my $x = ;'); 1 };
ok !$failed, 'peek syntax errors are reported';
like capture { $repl->dispatch('say "still works"') }, qr/still works/, 'stdout restored after failed peek';
for my $code ('$FOO = {', '$FOO = {bar => [1,', 'say "open', 'say q{open', '($x +') {
ok App::AnteREPL::needs_more($code), "continue incomplete input: $code";
}
for my $code ('$FOO = {bar=>[1,2]}', 'say "{[()]"', 'say q{nested {braces}}',
'$FOO = { # } ignored', '10 if (1) {', '10: my $x = [', 'EDIT', '! echo "{"',
'say $#array', '$FOO = {bar => 1}; # [', 'say 12 / 4', '$x =~ /[{}]/') {
if ($code eq '$FOO = { # } ignored') { ok App::AnteREPL::needs_more($code), 'comments do not close a data structure'; }
else { ok !App::AnteREPL::needs_more($code), "complete/special input: $code"; }
}
ok !App::AnteREPL::needs_more('$x = {]'), 'mismatched delimiters are handed to Perl for an error';
my $screen = TestScreen->new;
my $line = $screen->feed(typing('10 say "first"'), 'ENTER');
is $line, '10 say "first"', 'screen submits typed row without a prompt';
$repl->dispatch($line);
$screen->append($repl->listing);
is $screen->{rows}[1]{text}, '10 say "first";', 'LIST output is part of editable screen memory';
$line = $screen->feed('UP', 'END', ('LEFT') x 3, ['TEXT','!'], 'ENTER');
is $line, '10 say "firs!t";', 'moving up inserts into the actual displayed output row by default';
is capture { $repl->run }, "first\n", 'editing the screen alone does not update stored source';
$repl->dispatch($line);
is capture { $repl->run }, "firs!t\n", 'Enter resubmission changes stored source';
my $multi = TestScreen->new;
my $definition = $multi->feed(typing('$FOO = {'), 'ENTER', typing('bar => 2,'), 'ENTER',
typing('names => [qw(Alice Bob)],'), 'ENTER', typing('};'), 'ENTER');
is $definition, "\$FOO = {\nbar => 2,\nnames => [qw(Alice Bob)],\n};", 'open delimiters join consecutive screen rows';
$repl->dispatch($definition);
is $repl->evaluate('$FOO->{bar}', 0), 2, 'complete multiline definition evaluates';
is $repl->evaluate('$FOO->{names}[1]', 0), 'Bob', 'nested data structure preserved';
my $again = $multi->feed('UP', 'UP', 'UP', 'HOME', ('RIGHT') x 7, ['TEXT','3'], 'ENTER');
like $again, qr/bar => 32,/, 'an earlier multiline entry inserts text without overwriting';
$repl->dispatch($again);
is $repl->evaluate('$FOO->{bar}', 0), 32, 'resubmission evaluates the whole earlier entry';
my $paste = TestScreen->new;
{
my $typed = TestScreen->new;
my $r = App::AnteREPL->new;
$typed->{repl} = $r;
for my $case (
['$_ = "red"', ''],
['EVAL s /red/blue/', ''],
( run in 0.556 second using v1.01-cache-2.11-cpan-c32d3c3b8f1 )