1#!/usr/bin/perl 2 3# Testing for Data::JavaScript::Anon 4 5use strict; 6BEGIN { 7 $| = 1; 8 $^W = 1; 9} 10 11use Test::More; 12use Data::JavaScript::Anon; 13 14# Thoroughly test the numeric tests 15my @numbers = qw{ 16 0 1 2 3 4 5 6 7 8 9 10 11 123455 +1 +0 +5 +10 +123213 -0 -1 -5 -10 -12321133 17 0.0 .0 1.0 1.1 10.1 10.01 111111.111111 +111111.111111 -1111111.000100 18 1e0 1e1 1e2 1e10 1e+1 1e+2 1e+0 1e+10 1e-0 1e-1 1e-10 19 2e+0002 31.31e-000200430 +41.420010E+222211 -5111.050E-5151 20 0x2131 0xaaad32 -0x21312 +0x212 +0X212 21 01 02 03 04 05 01251 002123 00000 22 }; 23my @not_numbers = qw{ 24 a 09 +09 -09 08 +08 -08 ++1 +-34 25 3com 2131.231fd2132 +0x21x 26}; 27push @not_numbers, "0\n", "1\n"; 28 29my @keywords = qw{ 30 abstract boolean break byte case catch char class const continue 31 debugger default delete do double else enum export extends false final 32 finally float for function goto if implements import in instanceof int 33 interface long native new null package private protected public return 34 short static super switch synchronized this throw throws transient true 35 try typeof var void volatile while with 36}; 37 38my @hash_keys = ( 39 # CPAN #7183: 40 { input => "0596000278", output => '"0596000278"', 41 desc => 'correctly escapes 0-leading non-octal' }, 42 43 # CPAN #19915: 44 { input => '0', output => '0', 45 desc => q[doesn't delete string '0'] }, 46 47 { input => '', output => '""', 48 desc => q[doesn't delete empty string] }, 49 50 { input => "foo\n", output => '"foo\n"', 51 desc => q[correctly quotes identifier+newline] }, 52 53 { input => "1\n", output => '"1\n"', 54 desc => q[correctly quotes number+newline] }, 55); 56 57plan tests => (@numbers + @not_numbers + @keywords + @hash_keys + 6); 58 59foreach ( @numbers ) { 60 ok( Data::JavaScript::Anon->is_a_number( $_ ), "$_ is a number" ); 61} 62foreach ( @not_numbers ) { 63 ok( ! Data::JavaScript::Anon->is_a_number( $_ ), "$_ is not a number" ); 64} 65 66# Test that keywords come out quoted 67foreach ( @keywords ) { 68 is( Data::JavaScript::Anon->anon_hash_key($_), '"' . $_ . '"', 69 "anon_hash_key correctly quotes keyword $_ used as hash key" ); 70} 71 72# Test that hash keys aren't bent, folded, spindled, or mutilated 73foreach ( @hash_keys ) { 74 is( Data::JavaScript::Anon->anon_hash_key($_->{input}), $_->{output}, 75 "anon_hash_key $_->{desc}" ); 76} 77 78my $o = Data::JavaScript::Anon->new( quote_char => "'" ); 79isa_ok( $o, 'Data::JavaScript::Anon', 'isa Data::JavaScript::Anon object'); 80my $rv = $o->anon_dump( [ "a\nb", "a\rb", "a b", "a\'b", "a\bb" ] ); 81is( $rv, '[ \'a\nb\', \'a\rb\', \'a b\', \'a\\\'b\', \'a\010b\' ]', 'changing default quote character'); 82 83# Do a simple test of most of the code in a single go 84undef $rv; 85$rv = Data::JavaScript::Anon->anon_dump( [ 'a', 1, { a => { a => 1, } }, \"foo" ] ); 86is( $rv, '[ "a", 1, { a: { a: 1 } }, "foo" ]', 87 'Generates expected output for simple combination struct' ); 88 89# Test for CPAN bug #11882 (forward slash not being escaped) 90is( Data::JavaScript::Anon->anon_scalar( 'C:\\devel' ), '"C:\\\\devel"', 91 'anon_scalar correctly escapes forward slashes' ); 92 93# Also make sure double quotes are escaped 94is( Data::JavaScript::Anon->anon_scalar( 'foo"bar' ), '"foo\\"bar"', 95 'anon_scalar correctly escapes double quotes' ); 96 97# Test for generalised case of CPAN bug # (newline not being escaped) 98$rv = Data::JavaScript::Anon->anon_dump( [ "a\nb", "a\rb", "a b", "a\"b", "a\bb" ] ); 99is( $rv, '[ "a\nb", "a\rb", "a\tb", "a\\"b", "a\010b" ]', 'escape tabs, newlines, CRs and control chars'); 100