Skip to content

Commit 1b5057f

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

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

t/test/mojo.t

Lines changed: 85 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,86 @@ 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"'];
226+
$t->get_ok('/json')->status_is(200)->json_has('/test/0', 'some description');
227+
is_deeply \@args, ['ok', !!1, 'some description'];
228+
$t->get_ok('/json')->status_is(200)->json_has('/test0');
229+
is_deeply \@args, ['ok', !!0, 'has value for JSON Pointer "/test0"'];
230+
};
231+
232+
subtest 'json_hasnt' => sub {
233+
$t->get_ok('/json')->status_is(200)->json_hasnt('/test/0');
234+
is_deeply \@args, ['ok', !!0, 'has no value for JSON Pointer "/test/0"'];
235+
$t->get_ok('/json')->status_is(200)->json_hasnt('/test/0', 'some description');
236+
is_deeply \@args, ['ok', !!0, 'some description'];
237+
$t->get_ok('/json')->status_is(200)->json_hasnt('/test0');
238+
is_deeply \@args, ['ok', !!1, 'has no value for JSON Pointer "/test0"'];
239+
};
240+
241+
subtest 'json_is' => sub {
242+
$t->get_ok('/json')->status_is(200)->json_is('/test' => ['hello', 'bye']);
243+
is_deeply \@args, ['is_deeply', (['hello', 'bye']) x 2, 'exact match for JSON Pointer "/test"'];
244+
$t->get_ok('/json')->status_is(200)->json_is('/test' => ['hello', 'bye'], 'some description');
245+
is_deeply \@args, ['is_deeply', (['hello', 'bye']) x 2, 'some description'];
246+
$t->get_ok('/json')->status_is(200)->json_is('/test' => ['hello', 'goodbye']);
247+
is_deeply \@args, ['is_deeply', ['hello', 'bye'], ['hello', 'goodbye'], 'exact match for JSON Pointer "/test"'];
248+
};
249+
250+
subtest 'json_like' => sub {
251+
$t->get_ok('/json')->status_is(200)->json_like('/test/0' => qr/^he/);
252+
is_deeply \@args, ['like', 'hello', qr/^he/, 'similar match for JSON Pointer "/test/0"'];
253+
$t->get_ok('/json')->status_is(200)->json_like('/test/0' => qr/^he/, 'some description');
254+
is_deeply \@args, ['like', 'hello', qr/^he/, 'some description'];
255+
};
256+
257+
subtest 'json_unlike' => sub {
258+
$t->get_ok('/json')->status_is(200)->json_unlike('/test/0' => qr/^he/);
259+
is_deeply \@args, ['unlike', 'hello', qr/^he/, 'no similar match for JSON Pointer "/test/0"'];
260+
$t->get_ok('/json')->status_is(200)->json_unlike('/test/0' => qr/^he/, 'some description');
261+
is_deeply \@args, ['unlike', 'hello', qr/^he/, 'some description'];
262+
};
263+
264+
subtest 'json_message_has' => sub {
265+
$t->message([text => encode_json($example_json)])->json_message_has('/test');
266+
is_deeply \@args, ['ok', !!1, 'has value for JSON Pointer "/test"'];
267+
$t->message([text => encode_json($example_json)])->json_message_has('/test', 'some description');
268+
is_deeply \@args, ['ok', !!1, 'some description'];
269+
$t->message([text => encode_json($example_json)])->json_message_has('/test0');
270+
is_deeply \@args, ['ok', undef, 'has value for JSON Pointer "/test0"'];
271+
};
272+
273+
subtest 'json_message_hasnt' => sub {
274+
$t->message([text => encode_json($example_json)])->json_message_hasnt('/test');
275+
is_deeply \@args, ['ok', !!0, 'has no value for JSON Pointer "/test"'];
276+
$t->message([text => encode_json($example_json)])->json_message_hasnt('/test', 'some description');
277+
is_deeply \@args, ['ok', !!0, 'some description'];
278+
$t->message([text => encode_json($example_json)])->json_message_hasnt('/test0');
279+
is_deeply \@args, ['ok', !!1, 'has no value for JSON Pointer "/test0"'];
280+
};
281+
282+
subtest 'json_message_is' => sub {
283+
$t->message([text => encode_json($example_json)])->json_message_is('/test', => ['hello', 'bye']);
284+
is_deeply \@args, ['is_deeply', (['hello', 'bye']) x 2, 'exact match for JSON Pointer "/test"'];
285+
$t->message([text => encode_json($example_json)])->json_message_is('/test', => ['hello', 'bye'], 'some description');
286+
is_deeply \@args, ['is_deeply', (['hello', 'bye']) x 2, 'some description'];
287+
$t->message([text => encode_json($example_json)])->json_message_is('/test', => ['hello', 'goodbye'],);
288+
is_deeply \@args, ['is_deeply', ['hello', 'bye'], ['hello', 'goodbye'], 'exact match for JSON Pointer "/test"'];
289+
};
290+
291+
subtest 'json_message_like' => sub {
292+
$t->message([text => encode_json($example_json)])->json_message_like('/test/0', => qr/^he/);
293+
is_deeply \@args, ['like', 'hello', qr/^he/, 'similar match for JSON Pointer "/test/0"'];
294+
$t->message([text => encode_json($example_json)])->json_message_like('/test/0', => qr/^he/, 'some description');
295+
is_deeply \@args, ['like', 'hello', qr/^he/, 'some description'];
296+
};
297+
298+
subtest 'json_message_unlike' => sub {
299+
$t->message([text => encode_json($example_json)])->json_message_unlike('/test/0', => qr/^he/);
300+
is_deeply \@args, ['unlike', 'hello', qr/^he/, 'no similar match for JSON Pointer "/test/0"'];
301+
$t->message([text => encode_json($example_json)])->json_message_unlike('/test/0', => qr/^he/, 'some description');
302+
is_deeply \@args, ['unlike', 'hello', qr/^he/, 'some description'];
303+
};
304+
220305
done_testing();

0 commit comments

Comments
 (0)