added test file
[webpac2] / t / 5-output-json.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use lib 'lib';
5
6 use Test::More tests => 14;
7
8 use JSON;
9
10 BEGIN {
11 use_ok( 'WebPAC::Test' );
12 use_ok( 'WebPAC::Output::JSON' );
13 }
14
15 my $path = "$abs_path/out/test.js";
16
17 ok(my $out = new WebPAC::Output::JSON({ path => $path, %LOG }), "new");
18
19 ok( $out->init, 'init' );
20
21 my $ds = {
22         'Source' => {
23                 'name' => 'Izvor: ',
24                 'display' => [ 'foo' ]
25         },
26         'ID' => {
27                 'display' => 'id',
28         },
29         'Array' => {
30                 'display' => [ qw/a1 a2 s3 a4 a5/ ],
31         },
32 };
33
34 throws_ok { $out->add( ) } qr/need id/, 'add without params';
35 throws_ok { $out->add( 42 ) } qr/need ds/, 'add without ds';
36
37 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' );