From 1b5057f72ac19ed26f90cc5e90ed68fb1959513a Mon Sep 17 00:00:00 2001 From: Sergiotarxz Date: Sun, 11 Feb 2024 04:00:50 +0100 Subject: [PATCH] Adding tests for json_* subroutines in Test::Mojo. --- t/test/mojo.t | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/t/test/mojo.t b/t/test/mojo.t index f4f00cfdb3..26d897659f 100644 --- a/t/test/mojo.t +++ b/t/test/mojo.t @@ -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; @@ -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();