xref: /openbsd-src/gnu/usr.bin/perl/cpan/JSON-PP/t/104_sortby.t (revision 256a93a44f36679bee503f12e49566c2183f6181)
1
2use Test::More;
3use strict;
4use warnings;
5BEGIN { plan tests => 3 };
6BEGIN { $ENV{PERL_JSON_BACKEND} = 0; }
7use JSON::PP;
8#########################
9
10my ($js,$obj);
11my $pc = JSON::PP->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);
16is($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);
20is($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);
23is($js, q|{"a":1,"b":2,"c":3,"d":4,"e":5,"f":6,"g":7,"h":8,"i":9}|);
24
25sub JSON::PP::hoge { $JSON::PP::a cmp $JSON::PP::b }
26