X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=t%2F5-output-json.t;h=90e92abbd9f5814573d94c47d6d6526993110191;hb=7451ddd0e2f18949b3e27e9f3689675ec5ae8ceb;hp=e85b191cfe87ea0c0a3ae966b0f88ed22061faa6;hpb=4a20ef460976fb95c11a82e44c1de03990ce6d68;p=webpac2 diff --git a/t/5-output-json.t b/t/5-output-json.t index e85b191..90e92ab 100755 --- a/t/5-output-json.t +++ b/t/5-output-json.t @@ -1,40 +1,34 @@ #!/usr/bin/perl -w -use Test::More tests => 7; -use Test::Exception; -use Cwd qw/abs_path/; -use blib; use strict; +use blib; + +use Test::More tests => 14; + +use JSON; BEGIN { +use_ok( 'WebPAC::Test' ); use_ok( 'WebPAC::Output::JSON' ); } -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, %LOG }), "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 +36,23 @@ throws_ok { $out->add( 42 ) } qr/need ds/, 'add without ds'; 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' );