1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
package Test::JavaScript::More;
use strict;
use warnings;
use Data::Dumper;
use JavaScript;
use Test::More;
sub _new {
my $pkg = shift;
$pkg = ref $pkg || $pkg;
my $self = bless {}, $pkg;
$self->_init(@_);
$self->_add_test_methods();
return $self;
}
sub _init {
my $self = shift;
my $runtime = JavaScript::Runtime->new;
$self->{context} = ( $runtime->create_context() );
}
sub _add_test_methods {
my $self = shift;
my $context = $self->{context};
$context->bind_value( 'todo' => sub { $Test::More::TODO = $_[0] } );
$context->bind_value( 'plan' => sub { plan( tests => $_[0] ) } );
$context->bind_value( 'ok' => sub { ok($_[0], $_[1]) } );
$context->bind_value( 'is' => sub { is($_[0], $_[1], $_[2]) } );
$context->bind_value( 'diag' => sub { diag(@_) });
$context->bind_value( 'BAIL_OUT' => sub { BAIL_OUT(@_) } );
$context->bind_value( 'sprintf' => sub { sprintf(@_) } );
$context->eval(<<'end_js'); die $@ if $@;
function throws_ok(fn, match, test) {
if (!(match && match.exec)) BAIL_OUT("usage: throws_ok( function, match, test )");
var died = null; try { fn() } catch(e) { died = "" + e };
if (died) {
if (match.exec( died )) ok(1, test);
else {
diag("error '"+died+"' doesn't match '"+match+"'");
ok(0, test);
}
} else {
diag("no error thrown");
ok(0, test);
}
}
function like( string, match, test ) {
if (match.exec( string )) {
ok(1, test);
} else {
diag(sprintf("string '%s' doesn't match %s", string, "" + match));
ok(0, test);
}
}
function unlike( string, match, test ) {
if (match.exec( string )) {
diag(sprintf("string '%s' matches %s", string, match));
ok(0, test);
} else {
ok(1, test);
}
}
end_js
}
sub import {
my ($class, %args) = @_;
my $self = $class->_new(%args);
my $context = $self->{context};
# Now figure out where we are, and read in the actual test script
my ($package, $filename, $line) = caller;
exit unless (-f $filename);
open my $fh, "<", $filename
or die "Can't open $filename: $!";
my $string = "";
# Read lines up to the line where this import method is called. We
# replace the content on these lines with newlines rather than discard
# them entirely, in order to have line numbers matching.
while (<$fh>) {
$string .= "\n";
last if $line == $.
}
# .. the rest of the file is assumed to be JavaScript.
$string .= join "", <$fh>;
$context->eval($string, $filename);
if ($@) {
die "test error: ".( ref($@) ? $@->as_string : $@ );
}
exit;
}
1;
__END__
=head1 NAME
Test::JavaScript::More - test javascript code
=head1 SYNOPSIS
#!/usr/bin/perl
use Test::JavaScript::More;
// from here on the script is all JavaScript
plan(1);
function foo() { return "foo"; }
ok( foo(), "returns true" );
is( foo(), "foo", "more specifically, 'foo'" );
=head1 DESCRIPTION
This module provides Test::More-like function for JavaScript (the
current implementation just imports some of Test::More's functions into
JavaScript space).
Everything below the C<use Test::JavaScript::More> line will be
interpreted as JavaScript. You can subclass this module and set up your
context to include library code.
The line numbers will match up. Any lines in the test file up to and
including the C<use Test::JavaScript::More> are replaced with newlines
in the compiled JavaScript.
=head1 JAVASCRIPT FUNCTIONS
This module exports a bunch lot of functions to JavaScript:
=head2 Testing Functions
=over
=item plan( number_of_tests)
Plan the number of tests. The same as Test::More's C<plan>.
=item ok( true_or_false, label )
Is this okay? The same as Test::More's C<ok>.
=item is( value, expected, label )
Is this this? The same as Test::More's C<is>.
=item diag( string )
Produce some diagnostic output (i.e. text with # in front of it.) The
same as Test:More's C<diag>.
=item like( string, match, label )
Does this string match the regular expression?
=item unlike( string, match, label )
Does this string not match the regular expression?
=item throws_ok( function, match, label )
Does this function throw an exception that matches this regular expression
=back
=head2 Toto Testing
=over
=item todo( true_or_false )
Set'todo' mode (or not.) If set to true, all subseqent tests
run now will be todo tests. You have to manually set this back to false
to stop being in todo being in 'todo' mode.
=back
=head1 BUGS
C<throws_ok>, C<like> and C<unlike> do their diagnostic error messages
the wrong way round.
=head1 AUTHOR
Documentation and original crazy scheme for reading in the caller file
by Mark Fowler. Testing functionality by Tom Insam and Stig Brautaset.
Extracted from an internal Fotango test module by Stig Brautaset and Ash
Berlin.
=head1 LICENCE AND COPYRIGHT
Copyright (c) 2007, Fotango and Claes Jakobsson C<< <claesjac@cpan.org> >>. All rights reserved.
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlartistic>.
=head1 DISCLAIMER OF WARRANTY
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
NECESSARY SERVICING, REPAIR, OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
=cut
|