remove test dependency on Test::Without::Module (RT#115394)
[p5sagit/JSON-MaybeXS.git] / t / legacy.t
1 use strict;
2 use warnings;
3
4 # hide Cpanel::JSON::XS
5 use lib map {
6     my ( $m, $c ) = ( $_, qq{die "Can't locate $_ (hidden)\n"} );
7     sub { return unless $_[1] eq $m; open my $fh, "<", \$c; return $fh }
8 } qw{Cpanel/JSON/XS.pm};
9
10 use Test::More 0.88;
11 use JSON::MaybeXS qw/:legacy/;
12
13 my $in = '[1, 2, 3, 4]';
14
15 my $arr = from_json($in);
16 my $j = to_json($arr);
17 is($j, '[1,2,3,4]');
18 is(ref($arr), 'ARRAY');
19
20 my $json = 'JSON::MaybeXS';
21 diag "using invocant: $json";
22 like(
23     do { eval { $json->from_json($in) }; $@ },
24     qr/from_json should not be called as a method/,
25     'blessed invocant detected in from_json',
26 );
27
28 like(
29     do { eval { $json->to_json($arr, { blah => 1 } ) }; $@ },
30     qr/to_json should not be called as a method/,
31     'blessed invocant detected in to_json',
32 );
33
34 done_testing;
35
36 __END__
37
38   to_json
39        $json_text = to_json($perl_scalar)
40
41     Converts the given Perl data structure to a json string.
42
43     This function call is functionally identical to:
44
45        $json_text = JSON->new->encode($perl_scalar)
46
47   from_json
48        $perl_scalar = from_json($json_text)
49
50     The opposite of "to_json": expects a json string and tries to parse it,
51     returning the resulting reference.
52
53     This function call is functionally identical to:
54
55         $perl_scalar = JSON->decode($json_text)