Last active
December 14, 2015 03:39
-
-
Save jberger/5022507 to your computer and use it in GitHub Desktop.
Very preliminary implementation for multiple tests on websocket messages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$t->websocket_ok('/ws') | |
->message_ok | |
->json_message_subtest(sub{ | |
my ($t, $json) = @_; | |
ok $json->[1] >= 0; | |
ok $json->[1] <= 3; | |
})->finish_ok; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Test::Mojo; | |
use Mojo::JSON 'j'; | |
sub json_message_subtest { | |
shift->_message_subtest(shift, 1); | |
} | |
sub message_subtest { | |
shift->_message_subtest(shift, 0); | |
} | |
sub _message_subtest { | |
local $Test::Builder::Level = $Test::Builder::Level + 3; | |
my ($t, $cb, $is_json) = @_; | |
my $msg = $t->message || die "Must call message_ok first"; | |
$t->$cb( $is_json ? j($msg->[1]) : $msg->[1] ); | |
return $t; | |
} | |
# or this, using Test::More::subtest | |
# message/json is in $_ rather than @_ which this doesn't have access to | |
sub Test::Mojo::_message_subtest { | |
local $Test::Builder::Level = $Test::Builder::Level + 2; | |
my ($t, $is_json) = @_; | |
my $msg = $t->message || die "Must call message_ok first"; | |
$msg = $is_json ? j($msg->[1]) : $msg->[1]; | |
my $cb = pop; | |
my $name = shift || $is_json ? "JSON message subtest" : "message subtest"; | |
Test::More->can('subtest')->( $name => $cb ) for $msg; | |
return $t; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment