1 |
#!/usr/bin/perl -w |
#!/usr/bin/perl -w |
2 |
|
|
|
use Test::More tests => 7; |
|
|
use Test::Exception; |
|
|
use Cwd qw/abs_path/; |
|
|
use blib; |
|
3 |
use strict; |
use strict; |
4 |
|
use lib 'lib'; |
5 |
|
|
6 |
|
use Test::More tests => 14; |
7 |
|
|
8 |
|
use JSON; |
9 |
|
|
10 |
BEGIN { |
BEGIN { |
11 |
|
use_ok( 'WebPAC::Test' ); |
12 |
use_ok( 'WebPAC::Output::JSON' ); |
use_ok( 'WebPAC::Output::JSON' ); |
13 |
} |
} |
14 |
|
|
15 |
ok(my $abs_path = abs_path($0), "abs_path"); |
my $path = "$abs_path/out/test.js"; |
16 |
$abs_path =~ s#/[^/]*$#/#; # |
|
17 |
diag "abs_path: $abs_path"; |
ok(my $out = new WebPAC::Output::JSON({ path => $path, %LOG }), "new"); |
|
|
|
|
ok(my $out = new WebPAC::Output::JSON({ |
|
|
path => "$abs_path/out/test.js", |
|
|
}), "new"); |
|
18 |
|
|
19 |
ok( $out->init, 'init' ); |
ok( $out->init, 'init' ); |
20 |
|
|
21 |
my $ds = { |
my $ds = { |
22 |
'Source' => { |
'Source' => { |
23 |
'name' => 'Izvor: ', |
'name' => 'Izvor: ', |
|
'tag' => 'Source', |
|
24 |
'display' => [ 'foo' ] |
'display' => [ 'foo' ] |
25 |
}, |
}, |
26 |
'ID' => { |
'ID' => { |
27 |
'name' => 'ID', |
'display' => 'id', |
28 |
'tag' => 'IDths', |
}, |
29 |
'search' => [ 'bar' ], |
'Array' => { |
30 |
'lookup_key' => [ 'bar' ] |
'display' => [ qw/a1 a2 s3 a4 a5/ ], |
31 |
}, |
}, |
|
'filename' => [ 'out/thes/001.html' ], |
|
|
'name' => 'filename', |
|
|
'tag' => 'filename' |
|
32 |
}; |
}; |
33 |
|
|
34 |
throws_ok { $out->add( ) } qr/need id/, 'add without params'; |
throws_ok { $out->add( ) } qr/need id/, 'add without params'; |
36 |
|
|
37 |
ok( $out->add( 42, $ds ), 'add' ); |
ok( $out->add( 42, $ds ), 'add' ); |
38 |
|
|
39 |
|
ok( $out->add( 99, { foo => { display => 'foo' } } ), 'add another' ); |
40 |
|
|
41 |
|
ok( $out->finish ); |
42 |
|
|
43 |
|
ok( -e $out->path, "created $path" ); |
44 |
|
|
45 |
|
cmp_ok( $out->path, 'eq', $path, 'path' ); |
46 |
|
|
47 |
|
ok( my $items = read_file( $path ), 'read_file' ); |
48 |
|
|
49 |
|
ok( $items = jsonToObj( $items ), 'parse JSON' ); |
50 |
|
|
51 |
|
diag dump( $items ) if $debug; |
52 |
|
|
53 |
|
is_deeply( $items, { |
54 |
|
items => [ |
55 |
|
{ array => ["a1", "a2", "s3", "a4", "a5"], id => "id", source => ["foo"] }, |
56 |
|
{ foo => "foo" }, |
57 |
|
], |
58 |
|
}, 'same' ); |