The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

JSON::Assert - Asserts JSONPaths into a JSON data structure for correct values/matches

SYNOPSIS

    use JSON;
    use JSON::Assert;

    my $json = '{ "foo": { "bar": "text" } }';

    # create a JSON::Assert object
    my $json_assert = JSON::Assert->new();

    # assert that there is:
    # - only one <bar> key in the document
    $json_assert->assert_jpath_count($json, '$..bar', 1);
    # - the value of bar is 'text'
    $json_assert->assert_jpath_value_match($json, '$..bar', 'text');
    # - the value of bar matches /^tex/
    $json_assert->assert_jpath_value_match($json, '$..bar', qr{^tex});

DESCRIPTION

This module allows you to test JPaths into a JSON data structure to check that their number or values are what you expect.

To test the number of keys you expect to find, use the assert_jpath_count() method. To test the value of a key, use the assert_jpath_value_match(). This method can test against strings or regexes.

You can also text a value against a number of keys by using the assert_jpath_values_match() method. This can check your value against any number of keys.

Each of these assert methods throws an exception if they are false. Therefore, there are equivalent methods which do not die, but instead return a truth value. They are does_jpath_count(), does_jpath_value_match() and do_jpath_values_match().

SUBROUTINES

Please note that all subroutines listed here that start with assert_* throw an error if the assertion is not true. You'd expect this.

Also note that there are a corresponding number of other methods for each assert_* method which either return true or false and do not throw an error. Please be sure to use the correct version for what you need.

assert_jpath_count($doc, $jpath, $count)

Checks that there are $count keys in the $doc that are returned by the $jpath. Throws an error if this is untrue.

is_jpath_count($doc, $jpath, $count)

Calls the above method but catches any error and instead returns a truth value.

assert_jpath_value_match($doc, $jpath, $match)

Checks that $jpath returns only one key and that the value matches $match.

does_jpath_value_match($doc, $jpath, $match)

Calls the above method but catches any error and instead returns a truth value.

assert_jpath_values_match($doc, $jpath, $match)

Checks that $jpath returns keys and that all the matched values matches $match.

do_jpath_values_match($doc, $jpath, $match)

Calls the above method but catches any error and instead returns a truth value.

assert_json_contains($doc, $jpath, $match)

Checks that $jpath contains the data structure contained within $match.

does_jpath_contains($doc, $jpath, $match)

Calls the above method but catches any error and instead returns a truth value.

EXPORTS

Nothing.

SEE ALSO

Test::JSON::Assert, JSON::Compare

Based on:

Test::XML::Assert, XML::Compare

AUTHOR

Andrew Ruthven

Work

<puck at catalyst dot net dot nz>, http://www.catalyst.net.nz/

Personal

<andrew at etc dot gen dot nz<gt>, http://www.etc.gen.nz/

COPYRIGHT & LICENSE

This software development is sponsored and directed by NZRS Ltd., http://www.nzrs.net.nz/

Part od the work was carried out by Catalyst IT, http://www.catalyst.net.nz/

Copyright (c) 2014-2015, NZRS Limited. All Rights Reserved. This software may be used under the terms of the Artistic License 2.0. Note that this license is compatible with both the GNU GPL and Artistic licenses. A copy of this license is supplied with the distribution in the file COPYING.txt.