Skip to content

Commit

Permalink
added more methods for JSON testing
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 12, 2014
1 parent 0f054c7 commit 94e5fe6
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 6 deletions.
4 changes: 3 additions & 1 deletion Changes
@@ -1,5 +1,7 @@

5.13 2014-07-11
5.13 2014-07-12
- Added json_like, json_message_like, json_message_unlike and json_unlike
methods to Test::Mojo.
- Improved HTML5.1 compliance of Mojo::DOM::HTML.
- Fixed Mojo::Reactor::Poll bug where watchers were active after they have
been removed. (jberger)
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/UserAgent.pm
Expand Up @@ -109,7 +109,7 @@ sub _connect {
# Connection established
$stream->on(
timeout => sub { $self->_error($id, 'Inactivity timeout', 1) });
$stream->on(close => sub { $self->_finish($id, 1) });
$stream->on(close => sub { $self && $self->_finish($id, 1) });
$stream->on(error => sub { $self && $self->_error($id, pop) });
$stream->on(read => sub { $self->_read($id, pop) });
$self->$cb;
Expand Down
54 changes: 54 additions & 0 deletions lib/Test/Mojo.pm
Expand Up @@ -161,6 +161,12 @@ sub json_is {
return $self->_test('is_deeply', $self->tx->res->json($p), $data, $desc);
}

sub json_like {
my ($self, $p, $regex, $desc) = @_;
$desc ||= encode 'UTF-8', qq{similar match for JSON Pointer "$p"};
return $self->_test('like', $self->tx->res->json($p), $regex, $desc);
}

sub json_message_has {
my ($self, $p, $desc) = @_;
$desc ||= encode 'UTF-8', qq{has value for JSON Pointer "$p"};
Expand All @@ -180,6 +186,24 @@ sub json_message_is {
return $self->_test('is_deeply', $self->_json(get => $p), $data, $desc);
}

sub json_message_like {
my ($self, $p, $regex, $desc) = @_;
$desc ||= encode 'UTF-8', qq{similar match for JSON Pointer "$p"};
return $self->_test('like', $self->_json(get => $p), $regex, $desc);
}

sub json_message_unlike {
my ($self, $p, $regex, $desc) = @_;
$desc ||= encode 'UTF-8', qq{no similar match for JSON Pointer "$p"};
return $self->_test('unlike', $self->_json(get => $p), $regex, $desc);
}

sub json_unlike {
my ($self, $p, $regex, $desc) = @_;
$desc ||= encode 'UTF-8', qq{no similar match for JSON Pointer "$p"};
return $self->_test('unlike', $self->tx->res->json($p), $regex, $desc);
}

sub message_is {
my ($self, $value, $desc) = @_;
return $self->_message('is', $value, $desc || 'exact match for message');
Expand Down Expand Up @@ -676,6 +700,14 @@ Opposite of L</"json_has">.
Check the value extracted from JSON response using the given JSON Pointer with
L<Mojo::JSON::Pointer>, which defaults to the root value if it is omitted.
=head2 json_like
$t = $t->json_like('/foo/1' => qr/^\d+$/);
$t = $t->json_like('/foo/1' => qr/^\d+$/, 'right value');
Check the value extracted from JSON response using the given JSON Pointer with
L<Mojo::JSON::Pointer> for similar match.
=head2 json_message_has
$t = $t->json_message_has('/foo');
Expand All @@ -701,6 +733,28 @@ Check the value extracted from JSON WebSocket message using the given JSON
Pointer with L<Mojo::JSON::Pointer>, which defaults to the root value if it is
omitted.
=head2 json_message_like
$t = $t->json_message_like('/foo/1' => qr/^\d+$/);
$t = $t->json_message_like('/foo/1' => qr/^\d+$/, 'right value');
Check the value extracted from JSON WebSocket message using the given JSON
Pointer with L<Mojo::JSON::Pointer> for similar match.
=head2 json_message_unlike
$t = $t->json_message_unlike('/foo/1' => qr/^\d+$/);
$t = $t->json_message_unlike('/foo/1' => qr/^\d+$/, 'different value');
Opposite of L</"json_message_like">.
=head2 json_unlike
$t = $t->json_unlike('/foo/1' => qr/^\d+$/);
$t = $t->json_unlike('/foo/1' => qr/^\d+$/, 'different value');
Opposite of L</"json_like">.
=head2 message_is
$t = $t->message_is({binary => $bytes});
Expand Down
5 changes: 4 additions & 1 deletion t/mojolicious/lite_app.t
Expand Up @@ -920,7 +920,10 @@ $t->get_ok('/json')->status_is(200)->header_is(Server => 'Mojolicious (Perl)')
->content_type_is('application/json')->json_is({foo => [1, -2, 3, 'b☃r']})
->json_is('/foo' => [1, -2, 3, 'b☃r'])
->json_is('/foo/3', 'b☃r', 'right value')->json_has('/foo')
->json_hasnt('/bar');
->json_hasnt('/bar')->json_like('/foo/3' => qr/r$/)
->json_unlike('/foo/3' => qr/b$/)
->json_like('/foo/3' => qr/^b/, 'right value')
->json_unlike('/foo/3' => qr/^r/, 'different value');

# JSON ("null")
$t->get_ok('/json' => json => undef)->status_is(200)
Expand Down
10 changes: 7 additions & 3 deletions t/mojolicious/websocket_lite_app.t
Expand Up @@ -209,9 +209,13 @@ $t->websocket_ok('/json')->send_ok({json => {test => 23, snowman => '☃'}})
->json_message_is('/2' => 3, 'right value')
->json_message_hasnt('/5', 'not five elements')
->send_ok({json => {'' => [1, 2, 3]}})
->message_ok->json_message_is('/☃', [1, 2, 3])->send_ok({json => 'works'})
->message_ok->json_message_is('works')->send_ok({json => undef})
->message_ok->json_message_is(undef)->finish_ok;
->message_ok->json_message_is('/☃', [1, 2, 3])
->json_message_like('/☃/1' => qr/\d/)
->json_message_unlike('/☃/1' => qr/[a-z]/)
->json_message_like('/☃/2' => qr/3/, 'right value')
->json_message_unlike('/☃/2' => qr/2/, 'different value')
->send_ok({json => 'works'})->message_ok->json_message_is('works')
->send_ok({json => undef})->message_ok->json_message_is(undef)->finish_ok;

# Plain request
$t->get_ok('/plain')->status_is(200)->content_is('Nothing to see here!');
Expand Down

0 comments on commit 94e5fe6

Please sign in to comment.