File: object.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 (212 lines) | stat: -rw-r--r-- 7,024 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# Test the new "object" behaviour.

# This tests:
# * Copy literals, don't use read-only scalars.
# * User-defined booleans
# ** Correct object name in user-defined booleans
# ** Copy literals and user-defined booleans interplay
# ** Deletion of user-defined booleans
# * Detect hash collisions

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

#   ____                    _ _ _                 _     
#  / ___|___  _ __  _   _  | (_) |_ ___ _ __ __ _| |___ 
# | |   / _ \| '_ \| | | | | | | __/ _ \ '__/ _` | / __|
# | |__| (_) | |_) | |_| | | | | ||  __/ | | (_| | \__ \
#  \____\___/| .__/ \__, | |_|_|\__\___|_|  \__,_|_|___/
#            |_|    |___/                               
#

my $jp = JSON::Parse->new ();
$jp->copy_literals (1);
my $stuff = '{"hocus":true,"pocus":false,"focus":null}';
my $out = $jp->run ($stuff);
eval {
    $out->{pocus} = "bad city";
};
ok (! $@, "Can modify literals without error");

$jp->copy_literals (0);
my $stuff2 = '{"hocus":true,"pocus":false,"focus":null}';
my $out2 = $jp->run ($stuff);
eval {
    $out2->{pocus} = "bad city";
};
ok ($@, "Can't modify literals without error");
note ($@);

# User-defined booleans
package Ba::Bi::Bu::Be::Bo;

# https://metacpan.org/source/MAKAMAKA/JSON-PP-2.27300/lib/JSON/PP.pm#L1390

$Ba::Bi::Bu::Be::Bo::true = do { bless \(my $dummy = 1), "JSON::PP::Boolean" };
$Ba::Bi::Bu::Be::Bo::false = do { bless \(my $dummy = 0), "JSON::PP::Boolean" };
$Ba::Bi::Bu::Be::Bo::null = do { bless \(my $dummy), "JSON::PP::Boolean" };

sub true {$Ba::Bi::Bu::Be::Bo::true;}
sub false {$Ba::Bi::Bu::Be::Bo::false;}
sub null {$Ba::Bi::Bu::Be::Bo::null;}

1;
package main;

# $jpub = j-son p-arser with u-ser b-ooleans

my $jpub = JSON::Parse->new ();
my $jpub1 = $jpub->run ($stuff);
eval {
    $jpub1->{hocus} = "bad city";
};
ok ($@, "got error altering literals with default JSON::Parse object");

# Use the same things all the people on CPAN do, switching off the
# warnings.

$jpub->set_true ($Ba::Bi::Bu::Be::Bo::true);
$jpub->no_warn_literals (1);
$jpub->set_false ($Ba::Bi::Bu::Be::Bo::false);
$jpub->no_warn_literals (0);
$jpub->set_null ($Ba::Bi::Bu::Be::Bo::null);
my $jpub2 = $jpub->run ($stuff);
eval {
    $jpub2->{hocus} = "bad city";
};
ok (! $@, "Values are not read-only with user-defined true/false values");

my $jpub3 = $jpub->run ($stuff);
like (ref $jpub3->{hocus}, qr/JSON::PP::Boolean/, "true value correct type");
like (ref $jpub3->{pocus}, qr/JSON::PP::Boolean/, "false value correct type");
like (ref $jpub3->{focus}, qr/JSON::PP::Boolean/, "null value correct type");

# Now test the same thing after switching on copy_literals.

$jpub->no_warn_literals (1);
$jpub->copy_literals (1);
$jpub->no_warn_literals (0);
my $jpub4 = $jpub->run ($stuff);
like (ref $jpub4->{hocus}, qr/JSON::PP::Boolean/, "true value correct type even with copy-literals");
like (ref $jpub4->{pocus}, qr/JSON::PP::Boolean/, "false value correct type even with copy-literals");
like (ref $jpub4->{focus}, qr/JSON::PP::Boolean/, "null value correct type even with copy-literals");

# Now delete all our user-defined booleans

$jpub->delete_true ();
$jpub->delete_false ();
$jpub->delete_null ();

# Test the objects have gone.

my $jpub5 = $jpub->run ($stuff);
unlike (ref $jpub5->{hocus}, qr/JSON::PP::Boolean/, "User true deleted");
unlike (ref $jpub5->{pocus}, qr/JSON::PP::Boolean/, "User false deleted");
unlike (ref $jpub5->{focus}, qr/JSON::PP::Boolean/, "User null deleted");

# Now test that copy-literals is still working.

my $jpub6 = $jpub->run ($stuff);
eval {
    $jpub6->{hocus} = "bad city";
};
ok (! $@, "Values are not read-only, copy literals still works");

# Finally switch off copy-literals and check that things are back to
# the default behaviour.

$jpub->copy_literals (0);
my $jpub7 = $jpub->run ($stuff);
unlike (ref $jpub7->{hocus}, qr/JSON::PP::Boolean/, "User true deleted");
unlike (ref $jpub7->{pocus}, qr/JSON::PP::Boolean/, "User false deleted");
unlike (ref $jpub7->{focus}, qr/JSON::PP::Boolean/, "User null deleted");
eval {
    $jpub7->{hocus} = "bad city";
};
ok ($@, "Values are read-only again");
# Check it's the right error, "Modification of a readonly value".
like ($@, qr/Modification/, "Error message looks good");
note ($@);

# Check that this doesn't make a warning, we want the user to be able
# to set "null" to "undef".

my $warning;
$SIG{__WARN__} = sub { $warning = "@_"; };
$jpub->set_true (undef);
ok ($warning, "Warning on setting true to non-true value");
$jpub->set_true (0);
ok ($warning, "Warning on setting true to non-true value");
$jpub->set_true ('');
ok ($warning, "Warning on setting true to non-true value");
$warning = undef;
$jpub->set_false (undef);
ok (! $warning, "no warning when setting user-defined false");
$warning = undef;
$jpub->set_false (0);
ok (! $warning, "no warning when setting user-defined false");
$warning = undef;
$jpub->set_false ('');
ok (! $warning, "no warning when setting user-defined false");
$warning = undef;
# https://www.youtube.com/watch?v=g4ouPGGLI6Q
$jpub->set_false ('Yodeadodoyodeadodoyodeadodoyodeadodoyodeadodoyodeadodoyo-bab-baaaaa Ahhhhhh-aaahhhh-aaaaaa-aaaaAAA! Ohhhhhh-ooohhh-oooooo-oooOOO!');
ok ($warning, "warning when setting user-defined false to a true value");
note ($warning);
$warning = undef;
$jpub->set_null (undef);
$jpub->set_null (0);
$jpub->set_null ('');
ok (! $warning, "no warning when setting user-defined null");

#  ____       _            _               _ _ _     _                 
# |  _ \  ___| |_ ___  ___| |_    ___ ___ | | (_)___(_) ___  _ __  ___ 
# | | | |/ _ \ __/ _ \/ __| __|  / __/ _ \| | | / __| |/ _ \| '_ \/ __|
# | |_| |  __/ ||  __/ (__| |_  | (_| (_) | | | \__ \ | (_) | | | \__ \
# |____/ \___|\__\___|\___|\__|  \___\___/|_|_|_|___/_|\___/|_| |_|___/
#                                                                     
#

my $stuff3 = '{"hocus":1,"pocus":2,"hocus":3,"focus":4}';

my $jp3 = JSON::Parse->new ();
eval {
    $jp3->run ($stuff3);
};
ok (!$@, "Did not detect collision in default setting");

$jp3->detect_collisions (1);
eval {
    $jp3->run ($stuff3);
};
ok ($@, "Detected collision");
note ($@);

$jp3->detect_collisions (0);
eval {
    $jp3->run ($stuff3);
};
ok (!$@, "Did not detect collision after reset to 0");

SKIP: {
    eval "require 5.14;";
    if ($@) {
	skip "diagnostics_hash requires perl 5.14 or later", 1;
    }
    #  ____  _                             _   _        _               _     
    # |  _ \(_) __ _  __ _ _ __   ___  ___| |_(_) ___  | |__   __ _ ___| |__  
    # | | | | |/ _` |/ _` | '_ \ / _ \/ __| __| |/ __| | '_ \ / _` / __| '_ \ 
    # | |_| | | (_| | (_| | | | | (_) \__ \ |_| | (__  | | | | (_| \__ \ | | |
    # |____/|_|\__,_|\__, |_| |_|\___/|___/\__|_|\___| |_| |_|\__,_|___/_| |_|
    #                |___/                                                    
    
    my $jp4 = JSON::Parse->new ();
    $jp4->diagnostics_hash (1);
    eval {
	$jp4->run ("{{{{{");
    };
    ok (ref $@ eq 'HASH', "Got hash diagnostics");
};

done_testing ();