The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Test2::Tools::JSON::Pointer - Compare parts of JSON string to data structure using JSON pointers VERSION

VERSION

version 0.02

SYNOPSIS

use utf8;
use Test2::V0;
use Test2::Tools::JSON::Pointer;

is(
  '{"a":"龍"}',
  json '/a' => "龍",
);

is(
  '{"a":[1,2,3],"b":{"x":"y"}',
  json '/a' => [1,2,3],
);

is(
  '{"a":[1,2,3],"b":{"x":"y"}',
  json '/b' => hash {
    field 'x' => 'y';
  },
);

done_testing;

with files:

use Test2::V0;
use Test2::Tools::JSON::Pointer;
use Path::Tiny qw( path );

is(
  # will also work with Path::Class::File
  path('myjsonfile.json'),
  json '/a' => [1,2,3],
);

done_testing;

DESCRIPTION

This module provides a comparison for a JSON string with a JSON pointer. This module was inspired by Test::Mojo, which provides a mechanism for checking the JSON response from a HTTP body. This module provides a generic way to write tests for JSON using a JSON pointer with or without the context of Mojolicious or HTTP. It also has a Test2::Suite style interface.

This module expects a Perl string in Perl's internal representation (utf-8), NOT raw encoded bytes. Thus if you are reading files they need to be read in with the appropriate encoding. If you are testing against the content of a HTTP::Response object you want to use decoded_content.

FUNCTIONS

json

is(
  $json,
  json($pointer, $check)
);

is(
  $json,
  json($check),
);

Compare $json to the given Test2::Suite $check after decoding the string into a deep reference (array or hash) and starting at the position of the given $pointer. If $json is an instance of Path::Tiny or Path::Class::File, then it will read from that file first to get the JSON.

SEE ALSO

Test2::Tools::JSON

Provides a similar check without JSON pointers.

Test::Deep::JSON

Provides a similar check in a Test::Deep context.

Test::Mojo

Among many other capabilities, this great testing library allows you to make checks against JSON on an HTTP response object with JSON pointers.

AUTHOR

Graham Ollis <plicease@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2018 by Graham Ollis.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.