Game-Senet

 view release on metacpan or  search on metacpan

t/33-synopsis.t  view on Meta::CPAN

        return $claim unless @lines;
        shift @lines;
    }
    return undef;
}

my $START = 'dldldldldl/........../.......... l 5';
my $OVER = 'my $game = Game::Senet->new(seed => \'any bytes at all\'); '
         . '$game->play(($game->legal)[0]) until $game->is_over;';

my %CLAIMS = ('Game::Senet::Engine' => 3, 'Game::Senet::Notation' => 3, 'Game::Senet::Variant' => 3);

my @SYNOPSES = (
    [ 'Game::Senet', '', '',
        [ qr/^light threw [01]{4}, worth [1-5]$/m, qr/^(?:light|dark) won$/m ] ],
    [ 'Game::Senet::Bot', '', 'print "over, @levels\n" if $game->is_over;',
        [ qr/^over, 1 2 3$/m ] ],
    [ 'Game::Senet::Engine', '', 'print "$h\n", defined $other ? "parsed\n" : "not parsed: $err\n";',
        [ "$START\n0000000000066666\nmarked\noff\n20\nparsed\n" ] ],
    [ 'Game::Senet::Error', 'my $game = Game::Senet->new(seed => \'any bytes at all\');', '',
        [ qr/\Ano_piece: \S.*\n\z/ ] ],
    [ 'Game::Senet::Move', "my \$board = Game::Senet::Engine->new(position => '$START');", '',
        [ qr/\A(?:light from \d+ to \d+(?:, swapping)?\n)+\z/ ] ],
    [ 'Game::Senet::Notation', 'my $text = Game::Senet->new(seed => \'any bytes at all\')->to_text;',
        'print "$move->{from} $move->{to}\n";',
        [ qr/\A12-15\n2: 22-20<x\n4: -\+\n\[variant kendall [^\n]*\]\n.*^12 15\n\z/ms ] ],
    [ 'Game::Senet::Result', $OVER, '',
        [ qr/\A(?:light|dark) won by \w+\n\z/ ] ],
    [ 'Game::Senet::Rules', '', '',
        [ qr/\A(?:light|dark) to move\n\z/ ] ],
    [ 'Game::Senet::Sticks', '', 'print "@$sticks $throw $roll $again\n";',
        [ "1 1 1 0 3 3 3\n" ] ],
    [ 'Game::Senet::Variant', '', '',
        [ "kendall\nkendall pieces=7\nwalk\n" ] ],
);

for my $row (@SYNOPSES) {
    my ($module, $before, $after, $wants) = @$row;
    subtest $module => sub {
        my $code = synopsis_of($module);
        ok(defined $code && $code =~ /\S/, 'it has a synopsis');
        my ($ok, $error, $said) = run_capturing("$before\n$code\n$after");
        ok($ok, 'the synopsis runs, as it is written') or diag($error);
        my @claims = claims_in($code);
        my $missing = in_order($said, @claims);
        ok(!defined $missing, scalar(@claims) . ' values its comments promise, printed in order')
            or diag("never printed: $missing");
        cmp_ok(scalar @claims, '==', $CLAIMS{$module} // 0, '  as many as the synopsis has');
        for my $want (@$wants) {
            ref $want ? like($said, $want, 'it prints what it says it does')
                      : is($said, $want, 'it prints this and nothing else');
        }
        my (undef, undef, $again) = run_capturing("$before\n$code\n$after");
        is($again, $said, 'and run twice it prints the same: the seed is in it');
    };
}

subtest 'Game::Senet::Terminal' => sub {
    my $code = synopsis_of('Game::Senet::Terminal');
    ok(defined $code && $code =~ /->run\(\@ARGV\)/, 'it has a synopsis, and it runs the program');
    local @ARGV = ('--version');
    $exited = undef;
    my ($ok, $error, $said) = run_capturing($code);
    is($error, "exit was called\n", 'the first line runs and leaves through exit, as a program does');
    is($exited, 0, '  with 0');
    like($said, qr/\Asenet \d+\.\d+\n\z/, '  having done what --version asks');
    ok(eval("package Local::Compiled; no strict 'vars'; my \$compiled = sub { $code }; 1"), 'and the whole of it compiles')
        or diag($@);
};

done_testing();



( run in 0.626 second using v1.01-cache-2.11-cpan-ac8be02dad9 )