Subject: | Bugs with '0' as anon_hash_key and "0\n" as number; patch+test included |
Data::JavaScript::Anon->anon_hash_key incorrectly returns undef for string '0', causing an
uninitialized-value warning, and a syntax error in the generated JavaScript.
Data::JavaScript::Anon->is_a_number incorrectly returns true for strings containing a number
followed by a newline character.
I've attached a patch with failing tests, and fixes for both bugs.
--
Aaron Crane
Subject: | dja.diff |
diff -urN Data-JavaScript-Anon-0.9.orig/lib/Data/JavaScript/Anon.pm Data-JavaScript-Anon-0.9/lib/Data/JavaScript/Anon.pm
--- Data-JavaScript-Anon-0.9.orig/lib/Data/JavaScript/Anon.pm 2006-06-04 03:48:02.000000000 +0100
+++ Data-JavaScript-Anon-0.9/lib/Data/JavaScript/Anon.pm 2006-06-15 11:44:45.000000000 +0100
@@ -25,10 +25,10 @@
# The final combination of all posibilities for a straight number
# The string to match must have no extra characters
- $RE_NUMERIC = qr/^(?:\+|\-)??(?:$real|$_hex|$_oct)$/;
+ $RE_NUMERIC = qr/^(?:\+|\-)??(?:$real|$_hex|$_oct)\z/;
# The numeric for of the hash key is similar, but without the + or - allowed
- $RE_NUMERIC_HASHKEY = qr/^(?:$real|$_hex|$_oct)$/;
+ $RE_NUMERIC_HASHKEY = qr/^(?:$real|$_hex|$_oct)\z/;
%KEYWORD = map { $_ => 1 } qw{
abstract boolean break byte case catch char class const
@@ -177,7 +177,7 @@
# Turn a single perl value into a javascript hash key
sub anon_hash_key {
my $class = shift;
- my $value = _STRING($_[0]) ? shift : return undef;
+ my $value = _STRING($_[0]) || $_[0] eq '0' ? shift : return undef;
# Quote if it's a keyword
return '"' . $value . '"' if $KEYWORD{$value};
diff -urN Data-JavaScript-Anon-0.9.orig/t/02_results.t Data-JavaScript-Anon-0.9/t/02_results.t
--- Data-JavaScript-Anon-0.9.orig/t/02_results.t 2006-06-04 03:48:02.000000000 +0100
+++ Data-JavaScript-Anon-0.9/t/02_results.t 2006-06-15 11:39:56.000000000 +0100
@@ -32,10 +32,10 @@
0x2131 0xaaad32 -0x21312 +0x212 +0X212
01 02 03 04 05 01251 002123 00000
};
-my @not_numbers = qw{
+my @not_numbers = ("0\n", qw{
a 09 +09 -09 ++1 +-34
3com 2131.231fd2132 +0x21x
-};
+});
my @keywords = qw{
abstract boolean break byte case catch char class const continue
@@ -46,7 +46,7 @@
try typeof var void volatile while with
};
-plan tests => (@numbers + @not_numbers + @keywords + 5);
+plan tests => (@numbers + @not_numbers + @keywords + 6);
foreach ( @numbers ) {
ok( Data::JavaScript::Anon->is_a_number( $_ ), "$_ is a number" );
@@ -84,5 +84,9 @@
$rv = Data::JavaScript::Anon->anon_dump( [ "a\nb", "a\rb", "a b", "a\"b", "a\bb" ] );
is( $rv, '[ "a\nb", "a\rb", "a\tb", "a\\"b", "a\010b" ]', 'escape tabs, newlines, CRs and control chars');
+# Test that hash keys which are numeric zero don't turn into empty string
+$rv = Data::JavaScript::Anon->anon_dump( { 0 => 'zero' } );
+is( $rv, '{ 0: "zero" }', q[numeric-zero hash keys don't disappear]);
+
1;