Debian Codegraph @

t/104_sortby.t

raw
1
2 use Test::More;
3 use strict;
4 use warnings;
5 BEGIN { plan tests => 3 };
6 BEGIN { $ENV{PERL_JSON_BACKEND} ||= "JSON::backportPP"; }
7 use JSON -support_by_pp;
8 #########################
9
10 my ($js,$obj);
11 my $pc = JSON->new;
12
13 $obj = {a=>1, b=>2, c=>3, d=>4, e=>5, f=>6, g=>7, h=>8, i=>9};
14
15 $js = $pc->sort_by(1)->encode($obj);
16 is($js, q|{"a":1,"b":2,"c":3,"d":4,"e":5,"f":6,"g":7,"h":8,"i":9}|);
17
18
19 $js = $pc->sort_by(sub { $JSON::PP::a cmp $JSON::PP::b })->encode($obj);
20 is($js, q|{"a":1,"b":2,"c":3,"d":4,"e":5,"f":6,"g":7,"h":8,"i":9}|);
21
22 $js = $pc->sort_by('hoge')->encode($obj);
23 is($js, q|{"a":1,"b":2,"c":3,"d":4,"e":5,"f":6,"g":7,"h":8,"i":9}|);
24
25 sub JSON::PP::hoge { $JSON::PP::a cmp $JSON::PP::b }