App-Actions
view release on metacpan or search on metacpan
t/function.t view on Meta::CPAN
'a stale version is reported with both values');
is_deeply([ validate({ %good, count => '3.5' }, $schema) ], [ '$.count: expected integer, got 3.5' ], 'not an integer');
is_deeply([ validate({ %good, count => "3\n" }, $schema) ], [ qq{\$.count: expected integer, got '3\n'} ],
'a trailing newline is not an integer');
is_deeply([ validate({ %good, state => 'maybe' }, $schema) ], [ q{$.state: 'maybe' is not one of 'ok', 'failed'} ],
'a value outside the enum');
is_deeply([ validate({ %good, flag => 1 }, $schema) ], [ '$.flag: expected boolean, got 1' ], 'a number is not a boolean');
is_deeply([ validate({ %good, items => [ 'a', [] ] }, $schema) ], [ '$.items[1]: expected string, got ARRAY' ],
'each item is checked, with its index');
like(join('', validate({ %good, either => 'x' }, $schema)), qr/\A\$\.either: matches none of the alternative shapes \(first said: /,
'oneOf with no match');
is_deeply([ validate('text', { type => 'object' }) ], [ q{$: expected object, got 'text'} ], 'the root is $');
is_deeply([ validate([], { type => 'object' }, 'doc') ], [ 'doc: expected object, got ARRAY' ], 'a path can be given');
my @several = validate({ a => 1, b => 2 }, { type => 'object', properties => {} });
is(scalar @several, 2, 'every error is reported, not only the first');
is_deeply([ validate(undef, { type => 'string' }) ], [ '$: expected string, got null' ], 'null is not a string');
# A document can be deeply nested; the validator must not be
# confused by a hostile depth, only report it.
# Perl warns about recursion deeper than 100 calls; that is expected here.
local $SIG{__WARN__} = sub { warn @_ unless $_[0] =~ /\ADeep recursion on subroutine "App::Actions::Schema::validate"/ };
my $deep = 'leaf';
$deep = { a => $deep } for 1 .. 200;
my $nest = { type => 'string' };
$nest = { type => 'object', properties => { a => $nest } } for 1 .. 200;
is_deeply([ validate($deep, $nest) ], [], 'a deep document is walked to the leaf');
keeps_topic(sub { validate({ %good }, $schema) }, 'validate');
};
subtest 'assert_valid' => sub {
is(assert_valid({ %good }, $schema, 'doc'), 1, 'a good document passes');
my $error = caught { assert_valid({ name => 1, extra => 1 }, $schema, 'check document') };
ok(App::Actions::Schema::Invalid->caught($error), 'a bad one raises App::Actions::Schema::Invalid');
is("$error", "check document: \$.extra: unexpected key (and 1 more)\n", 'naming the first error and how many more');
# The error text comes from describe_errors(); a failure there must
# not turn a broken document into a passing one.
my $guard = mock_scoped('App::Actions::Schema::describe_errors' => sub { die "describe failed\n" });
throws_ok { assert_valid({}, $schema, 'doc') } qr/\Adescribe failed$/, 'a failure while describing still fails';
};
subtest 'describe_errors' => sub {
is(describe_errors([]), '', 'no errors is empty');
is(describe_errors([ 'one' ]), 'one', 'one error is itself');
is(describe_errors([qw(one two three)]), 'one (and 2 more)', 'more are counted');
};
subtest 'invalid_exit' => sub {
is(App::Actions::Schema->invalid_exit, 70, 'EX_SOFTWARE, the status of a document that breaks its schema');
isnt(App::Actions::Schema->invalid_exit, $_, "distinct from the run status $_") for 0 .. 2;
};
subtest '_is_type' => sub {
my %cases = (
null => [ [ undef ], [ 0, '' ] ],
boolean => [ [ JSON::PP::true(), JSON::PP::false() ], [ 1, 0, 'true' ] ],
array => [ [ [] ], [ {}, 'a' ] ],
object => [ [ {} ], [ [], 'a' ] ],
integer => [ [ 0, -3, '42' ], [ '1.0', '1e3', ' 1', "1\n", undef, [] ] ],
number => [ [ 0, '-1.5', '.5', '1e-3', '3.' ], [ 'NaN', 'Inf', '1e', '', undef ] ],
string => [ [ '', 'x', 0 ], [ undef, [], JSON::PP::true() ] ],
);
for my $type (sort keys %cases) {
my ($yes, $no) = @{ $cases{$type} };
is(App::Actions::Schema::_is_type($_, $type), 1, "$type accepts " . App::Actions::Schema::_show($_)) for @{$yes};
is(App::Actions::Schema::_is_type($_, $type), 0, "$type refuses " . App::Actions::Schema::_show($_)) for @{$no};
}
is(App::Actions::Schema::_is_type('x', 'colour'), 0, 'an unknown type accepts nothing');
};
subtest '_same' => sub {
is(App::Actions::Schema::_same(undef, undef), 1, 'two nulls');
is(App::Actions::Schema::_same('1', 1), 1, 'scalars compare as strings');
is(App::Actions::Schema::_same('1.0', 1), 0, 'so 1.0 is not 1');
is(App::Actions::Schema::_same(undef, ''), 0, 'null is not empty');
is(App::Actions::Schema::_same([], []), 0, 'structures are never the same');
is(App::Actions::Schema::_same(JSON::PP::true(), JSON::PP::true()), 1, 'two trues are the same');
is(App::Actions::Schema::_same(JSON::PP::false(), JSON::PP::false()), 1, 'two falses are the same');
is(App::Actions::Schema::_same(JSON::PP::true(), JSON::PP::false()), 0, 'true is not false');
is(App::Actions::Schema::_same(JSON::PP::true(), 1), 0, 'true is not the number 1');
is(App::Actions::Schema::_same(JSON::PP::false(), undef), 0, 'false is not null');
is_deeply([ validate(JSON::PP::true(), { enum => [ JSON::PP::true() ] }) ], [], 'so an enum of booleans works');
};
subtest '_show' => sub {
is(App::Actions::Schema::_show(undef), 'null', 'null');
is(App::Actions::Schema::_show(JSON::PP::false()), 'false', 'false');
is(App::Actions::Schema::_show(12), '12', 'a number is bare');
is(App::Actions::Schema::_show('12a'), q{'12a'}, 'a string is quoted');
is(App::Actions::Schema::_show({}), 'HASH', 'a structure is named by its kind');
};
};
# ---------------------------------------------------------------------------
# App::Actions::Process
# ---------------------------------------------------------------------------
use App::Actions::Process;
# Shell exit statuses the Process tests expect, by meaning.
Readonly::Hash my %STATUS => (
ok => 0,
failed => 3, # what the test programs exit with when told to fail
interrupted => 130, # 128 + SIGINT
not_started => 255, # the program could not be run at all
not_found => 127, # a forked child that could not exec
);
# A command that runs this Perl, so the tests need nothing installed.
sub perl_command { return [ $^X, '-e', @_ ] }
subtest 'App::Actions::Process' => sub {
subtest 'exit_code' => sub {
is(App::Actions::Process::exit_code(0), $STATUS{ok}, 'success');
is(App::Actions::Process::exit_code(undef), $STATUS{ok}, 'no status is success');
is(App::Actions::Process::exit_code($STATUS{failed} << 8), $STATUS{failed}, 'an exit status');
is(App::Actions::Process::exit_code(2), $STATUS{interrupted}, 'a signal is 128 plus its number');
is(App::Actions::Process::exit_code(2 | 128), $STATUS{interrupted}, 'with or without a core dump');
is(App::Actions::Process::exit_code(-1), $STATUS{not_started}, 'a program that could not start is 255');
returns_ok(App::Actions::Process::exit_code(256), { type => 'integer', min => 0, max => 255 }, 'always a shell status');
( run in 2.338 seconds using v1.01-cache-2.11-cpan-60884a8df76 )