diff options
| author | Yichun Zhang (agentzh) <agentzh@gmail.com> | 2014-02-18 14:33:37 -0800 |
|---|---|---|
| committer | Yichun Zhang (agentzh) <agentzh@gmail.com> | 2014-02-18 14:33:37 -0800 |
| commit | cdb1a73615415e88ac8ef1b2eeec216fe72b9794 (patch) | |
| tree | 6d87656c1852ab07dbf348d4dc340a08775eab17 /tests | |
| parent | 5fd54895cebd6bdf1e53718ed0691014ce0d2794 (diff) | |
| download | lua-cjson-2.1.0.1.tar.gz lua-cjson-2.1.0.1.tar.bz2 lua-cjson-2.1.0.1.zip | |
feature: applied Jiale Zhi's patch to add the new config function encode_empty_table_as_object so that we can encode empty Lua tables into empty JSON arrays.2.1.0.1
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/TestLua.pm | 70 | ||||
| -rw-r--r-- | tests/agentzh.t | 41 |
2 files changed, 111 insertions, 0 deletions
diff --git a/tests/TestLua.pm b/tests/TestLua.pm new file mode 100644 index 0000000..cd22d83 --- /dev/null +++ b/tests/TestLua.pm | |||
| @@ -0,0 +1,70 @@ | |||
| 1 | package TestLua; | ||
| 2 | |||
| 3 | use Test::Base -Base; | ||
| 4 | use IPC::Run3; | ||
| 5 | use Cwd; | ||
| 6 | |||
| 7 | use Test::LongString; | ||
| 8 | |||
| 9 | our @EXPORT = qw( run_tests ); | ||
| 10 | |||
| 11 | $ENV{LUA_CPATH} = "../?.so;;"; | ||
| 12 | $ENV{LUA_PATH} = "../lua/?.lua;;"; | ||
| 13 | #$ENV{LUA_PATH} = ($ENV{LUA_PATH} || "" ) . ';' . getcwd . "/runtime/?.lua" . ';;'; | ||
| 14 | |||
| 15 | sub run_test ($) { | ||
| 16 | my $block = shift; | ||
| 17 | #print $json_xs->pretty->encode(\@new_rows); | ||
| 18 | #my $res = #print $json_xs->pretty->encode($res); | ||
| 19 | my $name = $block->name; | ||
| 20 | |||
| 21 | my $lua = $block->lua or | ||
| 22 | die "No --- lua specified for test $name\n"; | ||
| 23 | |||
| 24 | my $luafile = "test_case.lua"; | ||
| 25 | |||
| 26 | open my $fh, ">$luafile" or | ||
| 27 | die "Cannot open $luafile for writing: $!\n"; | ||
| 28 | |||
| 29 | print $fh $lua; | ||
| 30 | close $fh; | ||
| 31 | |||
| 32 | my ($res, $err); | ||
| 33 | |||
| 34 | my @cmd; | ||
| 35 | |||
| 36 | if ($ENV{TEST_LUA_USE_VALGRIND}) { | ||
| 37 | @cmd = ('valgrind', '-q', '--leak-check=full', 'lua', 'test_case.lua'); | ||
| 38 | } else { | ||
| 39 | @cmd = ('lua', 'test_case.lua'); | ||
| 40 | } | ||
| 41 | |||
| 42 | run3 \@cmd, undef, \$res, \$err; | ||
| 43 | my $rc = $?; | ||
| 44 | |||
| 45 | #warn "res:$res\nerr:$err\n"; | ||
| 46 | |||
| 47 | if (defined $block->err) { | ||
| 48 | $err =~ /.*:.*:.*: (.*\s)?/; | ||
| 49 | $err = $1; | ||
| 50 | is $err, $block->err, "$name - err expected"; | ||
| 51 | |||
| 52 | } elsif ($rc) { | ||
| 53 | die "Failed to execute --- lua for test $name: $err\n"; | ||
| 54 | |||
| 55 | } else { | ||
| 56 | #is $res, $block->out, "$name - output ok"; | ||
| 57 | is $res, $block->out, "$name - output ok"; | ||
| 58 | } | ||
| 59 | |||
| 60 | is $rc, ($block->exit || 0), "$name - exit code ok"; | ||
| 61 | #unlink 'test_case.lua' or warn "could not delete \'test_case.lua\':$!"; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub run_tests () { | ||
| 65 | for my $block (blocks()) { | ||
| 66 | run_test($block); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | 1; | ||
diff --git a/tests/agentzh.t b/tests/agentzh.t new file mode 100644 index 0000000..aeebd67 --- /dev/null +++ b/tests/agentzh.t | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | use TestLua; | ||
| 2 | |||
| 3 | plan tests => 2 * blocks(); | ||
| 4 | |||
| 5 | run_tests(); | ||
| 6 | |||
| 7 | __DATA__ | ||
| 8 | |||
| 9 | === TEST 1: empty tables as objects | ||
| 10 | --- lua | ||
| 11 | local cjson = require "cjson" | ||
| 12 | print(cjson.encode({})) | ||
| 13 | print(cjson.encode({dogs = {}})) | ||
| 14 | --- out | ||
| 15 | {} | ||
| 16 | {"dogs":{}} | ||
| 17 | |||
| 18 | |||
| 19 | |||
| 20 | === TEST 2: empty tables as arrays | ||
| 21 | --- lua | ||
| 22 | local cjson = require "cjson" | ||
| 23 | cjson.encode_empty_table_as_object(false) | ||
| 24 | print(cjson.encode({})) | ||
| 25 | print(cjson.encode({dogs = {}})) | ||
| 26 | --- out | ||
| 27 | [] | ||
| 28 | {"dogs":[]} | ||
| 29 | |||
| 30 | |||
| 31 | |||
| 32 | === TEST 3: empty tables as objects (explicit) | ||
| 33 | --- lua | ||
| 34 | local cjson = require "cjson" | ||
| 35 | cjson.encode_empty_table_as_object(true) | ||
| 36 | print(cjson.encode({})) | ||
| 37 | print(cjson.encode({dogs = {}})) | ||
| 38 | --- out | ||
| 39 | {} | ||
| 40 | {"dogs":{}} | ||
| 41 | |||
