summaryrefslogtreecommitdiffstats
path: root/vespaclient/src/perl/test/Yahoo/Vespa/JsonTest.pl
blob: 687fd6dc56de7e462530422c883684587dd4092b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#
# Tests of the Json wrapper library..
# 

use Test::More;

use strict;

BEGIN {
    use_ok( 'Yahoo::Vespa::Json' );
    *Json:: = *Yahoo::Vespa::Json:: # Alias namespace
}
require_ok( 'Yahoo::Vespa::Json' );

&testSimpleJson();

done_testing();

exit(0);

sub testSimpleJson {
    my $json = <<EOS;
{
  "foo" : "bar",
  "map" : {
    "abc" : "def",
    "num" : 13.0
  },
  "array" : [
    { "val1" : 3 },
    { "val2" : 6 }
  ]
}
EOS
    my $parsed = Json::parse($json);
    is( $parsed->{'foo'}, 'bar', 'json test 1' );
    is( $parsed->{'map'}->{'abc'}, 'def', 'json test 2' );
    is( $parsed->{'map'}->{'num'}, 13.0, 'json test 3' );
    my $prettyPrint = <<EOS;
{
   "array" : [
      {
         "val1" : 3
      },
      {
         "val2" : 6
      }
   ],
   "map" : {
      "num" : 13,
      "abc" : "def"
   },
   "foo" : "bar"
}
EOS
    is( Json::encode($parsed), $prettyPrint, 'simple json test - encode' );
    my @keys = sort keys %{$parsed->{'map'}};
    is( scalar @keys, 2, 'simple json test - map keys' );
    is( $keys[0], 'abc', 'simple json test - map key 1' );
    is( $keys[1], 'num', 'simple json test - map key 2' );

    @keys = @{ $parsed->{'array'} };
    is( scalar @keys, 2, 'simple json test - list keys' );
    is( $keys[0]->{'val1'}, 3, 'simple json test - list key 1' );
    is( $keys[1]->{'val2'}, 6, 'simple json test - list key 2' );
}