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