--- trunk/t/5-output-json.t 2007/08/23 20:28:09 881 +++ trunk/t/5-output-json.t 2007/08/23 20:56:59 883 @@ -1,8 +1,11 @@ #!/usr/bin/perl -w -use Test::More tests => 7; +use Test::More tests => 14; use Test::Exception; use Cwd qw/abs_path/; +use JSON; +use File::Slurp; +use Data::Dump qw/dump/; use blib; use strict; @@ -10,31 +13,28 @@ use_ok( 'WebPAC::Output::JSON' ); } +my $debug = shift @ARGV; + ok(my $abs_path = abs_path($0), "abs_path"); $abs_path =~ s#/[^/]*$#/#; # diag "abs_path: $abs_path"; +my $path = "$abs_path/out/test.js"; -ok(my $out = new WebPAC::Output::JSON({ - path => "$abs_path/out/test.js", -}), "new"); +ok(my $out = new WebPAC::Output::JSON({ path => $path }), "new"); ok( $out->init, 'init' ); my $ds = { 'Source' => { 'name' => 'Izvor: ', - 'tag' => 'Source', 'display' => [ 'foo' ] - }, + }, 'ID' => { - 'name' => 'ID', - 'tag' => 'IDths', - 'search' => [ 'bar' ], - 'lookup_key' => [ 'bar' ] - }, - 'filename' => [ 'out/thes/001.html' ], - 'name' => 'filename', - 'tag' => 'filename' + 'display' => 'id', + }, + 'Array' => { + 'display' => [ qw/a1 a2 s3 a4 a5/ ], + }, }; throws_ok { $out->add( ) } qr/need id/, 'add without params'; @@ -42,4 +42,23 @@ ok( $out->add( 42, $ds ), 'add' ); +ok( $out->add( 99, { foo => { display => 'foo' } } ), 'add another' ); + +ok( $out->finish ); + +ok( -e $out->path, "created $path" ); + +cmp_ok( $out->path, 'eq', $path, 'path' ); + +ok( my $items = read_file( $path ), 'read_file' ); + +ok( $items = jsonToObj( $items ), 'parse JSON' ); + +diag dump( $items ) if $debug; +is_deeply( $items, { + items => [ + { array => ["a1", "a2", "s3", "a4", "a5"], id => "id", source => ["foo"] }, + { foo => "foo" }, + ], +}, 'same' );