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

Test::JSON::Entails - Test whether one JSON or Perl structure entails/subsumes another

VERSION

version 0.2

SYNOPSIS

use Test::JSON::Entails;

entails $json, { foo => 1 }, "JSON contains a foo element with value 1";
entails $json, '{}', "JSON is a valid JSON object (no array)";

my $bar = { foo => 42, bar => 23 };
my $foo = { foo => 42 };

subsumes $bar => $foo, 'bar subsumes foo';  # $foo and $bar may be blessed

DESCRIPTION

Sometimes you want to compare JSON objects not for exact equivalence but for whether one structure subsumes the other. The other way round, one structure can be entailed by another. For instance

{ "foo": 1, "bar": [ "x" ] }

is entailed by any of the following structures:

{ "foo": 1, "bar": [ "x" ], "doz": 2 }       # additional hash element
{ "foo": 1, "bar": [ "x", "y" ], "doz": 2 }  # additional array element

This module exports the testing method entails and its alias subsumes to check such entailments. You can pass, JSON strings with encoded JSON objects, Perl hash references, and blessed hash references.

LIMITATIONS

This module does not distinguish between numbers and strings, neither between true and 1 or false and 0. Circular references in passed objects are not detected.

SEE ALSO

This module reuses some code from Test::JSON, created by Curtis "Ovid" Poe. If you need more granular comparision of data structures, you should better use Test::Deep.

AUTHOR

Jakob Voss

COPYRIGHT AND LICENSE

This software is copyright (c) 2012 by Jakob Voss.

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