Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare JSON with Test::Deep?

When I run the following code:

use strict;
use warnings;
use Test::Deep;
use Test::Deep::JSON;

cmp_deeply(json("{}"), json("{}"));

I get

Found a special comparison in $data
You can only use specials in the expects structure at /usr/local/share/perl/5.26.1/Test/Deep.pm line 348.
    Test::Deep::descend(Test::Deep::JSON=HASH(0x5559ec3b3810), Test::Deep::JSON=HASH(0x5559ecb236a8)) called at /usr/local/share/perl/5.26.1/Test/Deep.pm line 221
    Test::Deep::cmp_details(Test::Deep::JSON=HASH(0x5559ec3b3810), Test::Deep::JSON=HASH(0x5559ecb236a8)) called at /usr/local/share/perl/5.26.1/Test/Deep.pm line 202
    Test::Deep::cmp_deeply(Test::Deep::JSON=HASH(0x5559ec3b3810), Test::Deep::JSON=HASH(0x5559ecb236a8)) called at jsondiff.pl line 6

Why is that and how can I compare two JSON strings?

like image 490
Robert Avatar asked Feb 20 '26 07:02

Robert


1 Answers

use Test::More tests => 1;

use Test::Deep;
use Test::Deep::JSON;

my $json_to_check = '{}';

cmp_deeply($json_to_check, json({}));

If you want to start with JSON, just decode it before passing it to json.

use feature qw( state );

use Test::More tests => 1;

use Test::Deep;
use Test::Deep::JSON;

use Cpanel::JSON::XS qw( );

sub from_json {
   state $json = Cpanel::JSON::XS->new();
   return $json->decode($_[0]);
}

my $json_to_check = '{}';

cmp_deeply($json_to_check, json(from_json('{}')));
like image 132
ikegami Avatar answered Feb 23 '26 01:02

ikegami



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!