Skip to content

Commit 35fc7b6

Browse files
committed
Adding tests for json_* subroutines in Test::Mojo.
1 parent 37a4c39 commit 35fc7b6

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

t/test/mojo.t

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ use Mojo::Base -strict;
33
use Test::More;
44
use Test::Mojo;
55
use Mojolicious::Lite;
6+
use Mojo::JSON qw/encode_json decode_json/;
67

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

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

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

223+
subtest 'json_has' => sub {
224+
$t->get_ok('/json')->status_is(200)->json_has('/test/0');
225+
is_deeply \@args, ['ok', !!1, 'has value for JSON Pointer "/test/0"'], 'Checking json_has is true.';
226+
$t->get_ok('/json')->status_is(200)->json_has('/test/0', 'some description');
227+
is_deeply \@args, ['ok', !!1, 'some description'],
228+
'Checking json_has is true and the test message is correctly printed.';
229+
$t->get_ok('/json')->status_is(200)->json_has('/test0');
230+
is_deeply \@args, ['ok', !!0, 'has value for JSON Pointer "/test0"'], 'Checking json_has is false.';
231+
};
232+
233+
subtest 'json_hasnt' => sub {
234+
$t->get_ok('/json')->status_is(200)->json_hasnt('/test/0');
235+
is_deeply \@args, ['ok', !!0, 'has no value for JSON Pointer "/test/0"'], 'Checking json_hasnt is false.';
236+
$t->get_ok('/json')->status_is(200)->json_hasnt('/test/0', 'some description');
237+
is_deeply \@args, ['ok', !!0, 'some description'], 'Checking correct test message in json_hasnt.';
238+
$t->get_ok('/json')->status_is(200)->json_hasnt('/test0');
239+
is_deeply \@args, ['ok', !!1, 'has no value for JSON Pointer "/test0"'], 'Checking json_hasnt is true.';
240+
};
241+
242+
subtest 'json_is' => sub {
243+
$t->get_ok('/json')->status_is(200)->json_is('/test' => ['hello', 'bye']);
244+
is_deeply \@args, ['is_deeply', (['hello', 'bye']) x 2, 'exact match for JSON Pointer "/test"'], 'json_is true case.';
245+
$t->get_ok('/json')->status_is(200)->json_is('/test' => ['hello', 'bye'], 'some description');
246+
is_deeply \@args, ['is_deeply', (['hello', 'bye']) x 2, 'some description'], 'Test message for json_is matches.';
247+
$t->get_ok('/json')->status_is(200)->json_is('/test' => ['hello', 'goodbye']);
248+
is_deeply \@args, ['is_deeply', ['hello', 'bye'], ['hello', 'goodbye'], 'exact match for JSON Pointer "/test"'],
249+
'Correct parameters json_is.';
250+
};
251+
252+
subtest 'json_like' => sub {
253+
$t->get_ok('/json')->status_is(200)->json_like('/test/0' => qr/^he/);
254+
is_deeply \@args, ['like', 'hello', qr/^he/, 'similar match for JSON Pointer "/test/0"'],
255+
'json_like matches arguments and contents.';
256+
$t->get_ok('/json')->status_is(200)->json_like('/test/0' => qr/^he/, 'some description');
257+
is_deeply \@args, ['like', 'hello', qr/^he/, 'some description'], 'json_like prints the correct test message.';
258+
};
259+
260+
subtest 'json_unlike' => sub {
261+
$t->get_ok('/json')->status_is(200)->json_unlike('/test/0' => qr/^he/);
262+
is_deeply \@args, ['unlike', 'hello', qr/^he/, 'no similar match for JSON Pointer "/test/0"'],
263+
'json_unlike matches arguments and contents.';
264+
$t->get_ok('/json')->status_is(200)->json_unlike('/test/0' => qr/^he/, 'some description');
265+
is_deeply \@args, ['unlike', 'hello', qr/^he/, 'some description'], 'Correct message printed in json_unlike.';
266+
};
267+
268+
subtest 'json_message_has' => sub {
269+
$t->message([text => encode_json($example_json)])->json_message_has('/test');
270+
is_deeply \@args, ['ok', !!1, 'has value for JSON Pointer "/test"'], 'json_message_has is true.';
271+
$t->message([text => encode_json($example_json)])->json_message_has('/test', 'some description');
272+
is_deeply \@args, ['ok', !!1, 'some description'], 'json_message_has prints the correct test message.';
273+
$t->message([text => encode_json($example_json)])->json_message_has('/test0');
274+
is_deeply \@args, ['ok', undef, 'has value for JSON Pointer "/test0"'], 'json_message_has is false.';
275+
};
276+
277+
subtest 'json_message_hasnt' => sub {
278+
$t->message([text => encode_json($example_json)])->json_message_hasnt('/test');
279+
is_deeply \@args, ['ok', !!0, 'has no value for JSON Pointer "/test"'], 'json_message_hasnt is false.';
280+
$t->message([text => encode_json($example_json)])->json_message_hasnt('/test', 'some description');
281+
is_deeply \@args, ['ok', !!0, 'some description'], 'json_message_hasnt prints the correct test message.';
282+
$t->message([text => encode_json($example_json)])->json_message_hasnt('/test0');
283+
is_deeply \@args, ['ok', !!1, 'has no value for JSON Pointer "/test0"'], 'json_message_hasnt is false.';
284+
};
285+
286+
subtest 'json_message_is' => sub {
287+
$t->message([text => encode_json($example_json)])->json_message_is('/test', => ['hello', 'bye']);
288+
is_deeply \@args, ['is_deeply', (['hello', 'bye']) x 2, 'exact match for JSON Pointer "/test"'],
289+
'json_message_is works in the true case.';
290+
$t->message([text => encode_json($example_json)])->json_message_is('/test', => ['hello', 'bye'], 'some description');
291+
is_deeply \@args, ['is_deeply', (['hello', 'bye']) x 2, 'some description'],
292+
'json_message_is prints the correct test message.';
293+
$t->message([text => encode_json($example_json)])->json_message_is('/test', => ['hello', 'goodbye'],);
294+
is_deeply \@args, ['is_deeply', ['hello', 'bye'], ['hello', 'goodbye'], 'exact match for JSON Pointer "/test"'],
295+
'Correct parameters in json_message_is.';
296+
};
297+
298+
subtest 'json_message_like' => sub {
299+
$t->message([text => encode_json($example_json)])->json_message_like('/test/0', => qr/^he/);
300+
is_deeply \@args, ['like', 'hello', qr/^he/, 'similar match for JSON Pointer "/test/0"'],
301+
'json_message_like works as expected.';
302+
$t->message([text => encode_json($example_json)])->json_message_like('/test/0', => qr/^he/, 'some description');
303+
is_deeply \@args, ['like', 'hello', qr/^he/, 'some description'],
304+
'json_message_like prints the correct test message.';
305+
};
306+
307+
subtest 'json_message_unlike' => sub {
308+
$t->message([text => encode_json($example_json)])->json_message_unlike('/test/0', => qr/^he/);
309+
is_deeply \@args, ['unlike', 'hello', qr/^he/, 'no similar match for JSON Pointer "/test/0"'],
310+
'json_message_unlike works as expected.';
311+
$t->message([text => encode_json($example_json)])->json_message_unlike('/test/0', => qr/^he/, 'some description');
312+
is_deeply \@args, ['unlike', 'hello', qr/^he/, 'some description'],
313+
'json_message_unlike prints the correct test message.';
314+
};
315+
220316
done_testing();

0 commit comments

Comments
 (0)