File: rfc7159.t

package info (click to toggle)
libjson-parse-perl 0.62-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 572 kB
  • sloc: ansic: 3,614; perl: 475; makefile: 12
file content (58 lines) | stat: -rw-r--r-- 1,622 bytes parent folder | download | duplicates (2)
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
# This tests for the new behaviour of the JSON specification as of RFC
# 7159 where a single item without braces {} or square brackets [] is
# also valid as JSON.

use FindBin '$Bin';
use lib "$Bin";
use JPT;

my $stringonly = '"this"';
my $j;
eval {
    $j = parse_json ($stringonly);
};
ok (! $@, "no errors parsing rfc7159 json");
is ($j, 'this', "Got correct value as well");
ok (valid_json ($stringonly), "And it's valid json too");

my $numberonly = '3.14';
my $j2;
eval {
    $j2 = parse_json ($numberonly);
};
ok (! $@, "no errors parsing rfc7159 json");
cmp_ok (abs ($j2 - $numberonly), '<', 0.0001, "got number back");
ok (valid_json ($numberonly), "And it's valid JSON too");

my $numberonly2 = '0.14';
my $jx;
eval {
    $jx = parse_json ($numberonly2);
};
ok (! $@, "no errors parsing rfc7159 json $numberonly2");
cmp_ok (abs ($jx - ($numberonly2 + 0.0)), '<', 0.0001, "got number back $numberonly2");
ok (valid_json ($numberonly2), "And it's valid JSON too");

my $numberws = '  5.55e10  ';
ok (valid_json ($numberws), "$numberws validated");
my $literalws = '   true  ';
ok (valid_json ($literalws), "'$literalws' validates");
my $j3;
eval {
    $j3 = parse_json ($literalws);
};
ok (! $@, "no errors parsing '$literalws'");
ok ($j3, "'$literalws' gives a true value");
is ($j3, 1, "'$literalws' is equal to one");
my $literal = 'null';
ok (valid_json ($literal), "'$literal' validates");
my $j4;
eval {
    $j4 = parse_json ($literal);
};
ok (! $@, "no errors parsing '$literal'");
ok (! $j4, "bare literal null is false value");
ok (! defined ($j4), "bare literal null is undefined");

 
done_testing ();