Subject: | Problem with JSON::Path::set |
JSON::Path::set does not work correctly when using JSON paths such as
'$.store.book[2].author', which does not include wildcards (i.e., '*').
I added an additional test (please find the attached file) for checking
JSON::Path::set and obtained failures as follows:
==============================
% make test
cp lib/JSON/Path.pm blib/lib/JSON/Path.pm
PERL_DL_NONLAZY=1 /opt/local/bin/perl5.16 "-MExtUtils::Command::MM" "-e"
"test_harness(0, 'inc', 'blib/lib', 'blib/arch')" t/01basic.t
t/02zeroth.t t/03shortcuts.t t/04map.t t/05set.t
t/01basic.t ...... ok
t/02zeroth.t ..... ok
t/03shortcuts.t .. ok
t/04map.t ........ ok
t/05set.t ........ 1/?
# Failed test at t/05set.t line 81.
# got: 'Herman Melville'
# expected: 'Anon'
# Looks like you failed 1 test of 6.
t/05set.t ........ Dubious, test returned 1 (wstat 256, 0x100)
Failed 2/6 subtests
Test Summary Report
-------------------
t/05set.t (Wstat: 256 Tests: 5 Failed: 1)
Failed test: 6
Non-zero exit status: 1
Parse errors: Tests out of sequence. Found (6) but expected (5)
Bad plan. You planned 6 tests but ran 5.
Files=5, Tests=27, 1 wallclock secs ( 0.05 usr 0.02 sys + 0.43 cusr 0.06
csys = 0.56 CPU)
Result: FAIL
Failed 1/5 test programs. 1/27 subtests failed.
==============================
Subject: | 05set.t |
use Test::More;
use JSON::Path -all;
use JSON;
my $object = from_json(<<'JSON');
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
JSON
my $titles = '$.store.book[*].title';
my $jpath = JSON::Path->new ($titles);
is_deeply(
[ $jpath->values($object) ],
[ "Sayings of the Century",
"Sword of Honour",
"Moby Dick",
"The Lord of the Rings" ]
);
is(
$jpath->set($object => 'TBD', 2),
2,
);
is_deeply(
[ $jpath->values($object) ],
[ "TBD",
"TBD",
"Moby Dick",
"The Lord of the Rings" ]
);
my $author = '$.store.book[2].author';
$jpath = JSON::Path->new ($author);
is(
$jpath->value($object),
"Herman Melville",
);
is(
$jpath->set($object => 'Anon'),
1,
);
is(
$jpath->value($object),
'Anon',
);
done_testing();