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


version 0.2


  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


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.


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.


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.


Jakob Voss


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.