App-AnteREPL

 view release on metacpan or  search on metacpan

t/screen-bounds.t  view on Meta::CPAN

use 5.016;
use strict;
use warnings;
use Test::More;
use FindBin;
use lib "$FindBin::Bin/../lib";
use App::AnteREPL;
use Encode qw(decode);

{
    package BoundedScreen;
    our @ISA = ('App::AnteREPL::Screen');
    sub key { shift @{ $_[0]{keys} } || 'EOF' }
    sub _cursor_time { exists $_[0]{clock} ? $_[0]{clock} : $_[0]->SUPER::_cursor_time }
}
sub screen {
    my ($w, $h) = @_;
    my $s = BoundedScreen->new;
    @{$s}{qw(width height)} = ($w, $h);
    return $s;
}
sub texts { [map { $_->{text} } @{ $_[0]{rows} }] }
sub bounded {
    my ($s, $label) = @_;
    cmp_ok scalar(@{$s->{rows}}), '<=', $s->{height}, "$label: rows fit";
    ok !grep(length($_->{text}) > $s->{width}, @{$s->{rows}}), "$label: columns fit";
    cmp_ok $s->{row}, '<', $s->{height}, "$label: cursor row fits";
    cmp_ok $s->{col}, '<', $s->{width}, "$label: cursor column fits";
    is_deeply [@{$s}{qw(top left)}], [0, 0], "$label: no hidden viewport";
}
sub fails (&) { my ($code) = @_; local $@; eval { $code->(); 1 }; return $@; }

my $s = screen(40, 4);
$s->append("row $_\n") for 1 .. 20;
is_deeply texts($s), ['row 18', 'row 19', 'row 20', ''], 'scrolling discards rows beyond the visible screen';
$s->edit_key('PAGEUP') for 1 .. 3;
is $s->{row}, 0, 'PageUp cannot reach discarded rows';
$s->edit_key('LEFT');
is_deeply [@{$s}{qw(row col)}], [0, 0], 'Left cannot leave the upper left corner';
ok !$s->{resize_bank}, 'ordinary scrolling does not create a recovery bank';
bounded($s, 'scroll');

$s = screen(10, 8);
my $code = 'say "a long wrapped line"';
$s->edit_key(['TEXT', $_]) for split //, $code;
is(($s->entry)[0], $code, 'soft wrapping preserves Perl source without inserting newlines');
bounded($s, 'typing across right edge');
$s->edit_key('TOP');
$s->edit_key(['TEXT', 'prefix']);
is(($s->entry)[0], 'prefix' . $code, 'insertion pushes characters into the next wrapped row');
bounded($s, 'insert into wrapped input');

$s = screen(10, 8);
$s->edit_key(['TEXT', 'abcdefghij']);
$s->edit_key('BACKSPACE');
is(($s->entry)[0], 'abcdefghi', 'Backspace crosses a soft wrap without deleting a source newline');

$s = screen(10, 8);
$s->append('abcdefghij');
$s->append('klm');
$s->append("\n");
is_deeply texts($s), ['abcdefghij', 'klm', ''], 'partial output writes wrap without losing or duplicating characters';
bounded($s, 'wrapped output');

my $r = App::AnteREPL->new;
$r->dispatch('5     say "a long listed line"');
$r->dispatch('100 say "other"');
$s = screen(12, 20);
$r->{screen} = $s;
$r->dispatch('LIST 5');
$s->edit_key('TOP');
is(($s->entry)[0], '5     say "a long listed line";', 'wrapped LIST preserves source indentation and removes only number padding');
my $source = $r->source;
$r->dispatch(($s->entry)[0]);



( run in 1.436 second using v1.01-cache-2.11-cpan-4e491c91c6e )