Skip to content

Commit

Permalink
Adding tests for json_* subroutines in Test::Mojo.
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiotarxz committed Feb 11, 2024
1 parent 37a4c39 commit 1b5057f
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions t/test/mojo.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ use Mojo::Base -strict;
use Test::More;
use Test::Mojo;
use Mojolicious::Lite;
use Mojo::JSON qw/encode_json decode_json/;

any '/' => {text => 'Hello Test!'};
my $example_json = {test => ['hello', 'bye']};
any '/json' => {json => $example_json};

my $t = Test::Mojo->new;

Expand Down Expand Up @@ -217,4 +220,86 @@ subtest 'header_unlike' => sub {
is_deeply \@args, ['unlike', 'text/html;charset=UTF-8', qr/image\/png/, 'some description'], 'right result';
};

subtest 'json_has' => sub {
$t->get_ok('/json')->status_is(200)->json_has('/test/0');
is_deeply \@args, ['ok', !!1, 'has value for JSON Pointer "/test/0"'];
$t->get_ok('/json')->status_is(200)->json_has('/test/0', 'some description');
is_deeply \@args, ['ok', !!1, 'some description'];
$t->get_ok('/json')->status_is(200)->json_has('/test0');
is_deeply \@args, ['ok', !!0, 'has value for JSON Pointer "/test0"'];
};

subtest 'json_hasnt' => sub {
$t->get_ok('/json')->status_is(200)->json_hasnt('/test/0');
is_deeply \@args, ['ok', !!0, 'has no value for JSON Pointer "/test/0"'];
$t->get_ok('/json')->status_is(200)->json_hasnt('/test/0', 'some description');
is_deeply \@args, ['ok', !!0, 'some description'];
$t->get_ok('/json')->status_is(200)->json_hasnt('/test0');
is_deeply \@args, ['ok', !!1, 'has no value for JSON Pointer "/test0"'];
};

subtest 'json_is' => sub {
$t->get_ok('/json')->status_is(200)->json_is('/test' => ['hello', 'bye']);
is_deeply \@args, ['is_deeply', (['hello', 'bye']) x 2, 'exact match for JSON Pointer "/test"'];
$t->get_ok('/json')->status_is(200)->json_is('/test' => ['hello', 'bye'], 'some description');
is_deeply \@args, ['is_deeply', (['hello', 'bye']) x 2, 'some description'];
$t->get_ok('/json')->status_is(200)->json_is('/test' => ['hello', 'goodbye']);
is_deeply \@args, ['is_deeply', ['hello', 'bye'], ['hello', 'goodbye'], 'exact match for JSON Pointer "/test"'];
};

subtest 'json_like' => sub {
$t->get_ok('/json')->status_is(200)->json_like('/test/0' => qr/^he/);
is_deeply \@args, ['like', 'hello', qr/^he/, 'similar match for JSON Pointer "/test/0"'];
$t->get_ok('/json')->status_is(200)->json_like('/test/0' => qr/^he/, 'some description');
is_deeply \@args, ['like', 'hello', qr/^he/, 'some description'];
};

subtest 'json_unlike' => sub {
$t->get_ok('/json')->status_is(200)->json_unlike('/test/0' => qr/^he/);
is_deeply \@args, ['unlike', 'hello', qr/^he/, 'no similar match for JSON Pointer "/test/0"'];
$t->get_ok('/json')->status_is(200)->json_unlike('/test/0' => qr/^he/, 'some description');
is_deeply \@args, ['unlike', 'hello', qr/^he/, 'some description'];
};

subtest 'json_message_has' => sub {
$t->message([text => encode_json($example_json)])->json_message_has('/test');
is_deeply \@args, ['ok', !!1, 'has value for JSON Pointer "/test"'];
$t->message([text => encode_json($example_json)])->json_message_has('/test', 'some description');
is_deeply \@args, ['ok', !!1, 'some description'];
$t->message([text => encode_json($example_json)])->json_message_has('/test0');
is_deeply \@args, ['ok', undef, 'has value for JSON Pointer "/test0"'];
};

subtest 'json_message_hasnt' => sub {
$t->message([text => encode_json($example_json)])->json_message_hasnt('/test');
is_deeply \@args, ['ok', !!0, 'has no value for JSON Pointer "/test"'];
$t->message([text => encode_json($example_json)])->json_message_hasnt('/test', 'some description');
is_deeply \@args, ['ok', !!0, 'some description'];
$t->message([text => encode_json($example_json)])->json_message_hasnt('/test0');
is_deeply \@args, ['ok', !!1, 'has no value for JSON Pointer "/test0"'];
};

subtest 'json_message_is' => sub {
$t->message([text => encode_json($example_json)])->json_message_is('/test', => ['hello', 'bye']);
is_deeply \@args, ['is_deeply', (['hello', 'bye']) x 2, 'exact match for JSON Pointer "/test"'];
$t->message([text => encode_json($example_json)])->json_message_is('/test', => ['hello', 'bye'], 'some description');
is_deeply \@args, ['is_deeply', (['hello', 'bye']) x 2, 'some description'];
$t->message([text => encode_json($example_json)])->json_message_is('/test', => ['hello', 'goodbye'],);
is_deeply \@args, ['is_deeply', ['hello', 'bye'], ['hello', 'goodbye'], 'exact match for JSON Pointer "/test"'];
};

subtest 'json_message_like' => sub {
$t->message([text => encode_json($example_json)])->json_message_like('/test/0', => qr/^he/);
is_deeply \@args, ['like', 'hello', qr/^he/, 'similar match for JSON Pointer "/test/0"'];
$t->message([text => encode_json($example_json)])->json_message_like('/test/0', => qr/^he/, 'some description');
is_deeply \@args, ['like', 'hello', qr/^he/, 'some description'];
};

subtest 'json_message_unlike' => sub {
$t->message([text => encode_json($example_json)])->json_message_unlike('/test/0', => qr/^he/);
is_deeply \@args, ['unlike', 'hello', qr/^he/, 'no similar match for JSON Pointer "/test/0"'];
$t->message([text => encode_json($example_json)])->json_message_unlike('/test/0', => qr/^he/, 'some description');
is_deeply \@args, ['unlike', 'hello', qr/^he/, 'some description'];
};

done_testing();

0 comments on commit 1b5057f

Please sign in to comment.