NAME

Test::Deep::JType - Test::Deep helpers for JSON::Typist data

VERSION

version 0.006

OVERVIEW

Test::Deep is a very useful library for testing data structures. Test::Deep::JType extends it with routines for testing JSON::Typist-annotated data.

By default, Test::Deep's cmp_deeply will interpret plain numbers and strings as shorthand for shallow(...) tests, meaning that the corresponding input data will also need to be a plain number or string. That means that this test won't work:

my $json  = q[ { "key": "value" } ];
my $data  = decode_json($json);
my $typed = JSON::Typist->new->apply_types( $data );

cmp_deeply($typed, { key => "value" });

...because "value" will refuse to match an object. You could wrap each string or number to be compared in str() or num() respectively, but this can be a hassle, as well as a lot of clutter.

jcmp_deeply is exported by Test::Deep::JType, and behaves just like cmp_deeply, but plain numbers and strings are wrapped in str() tests rather than shallow ones, so they always compare with eq.

To test that the input data matches the right type, other routines are exported that check type as well as content.

FUNCTIONS

jcmp_deeply

This behaves just like Test::Deep's jcmp_deeply but wraps plain scalar and number expectations in str, meaning they're compared with eq only, instead of also asserting that the found value must not be an object.

jstr

jnum

jbool

jtrue

jfalse

These routines are plain old Test::Deep-style assertions that check not only for data equivalence, but also that the data is the right type.

jstr, jnum, and jbool take arguments, which are passed to the non-j version of the test used in building the j-style version. In other words, you can write:

jcmp_deeply(
  $got,
  {
    name => jstr("Ricardo"),
    age  => jnum(38.2, 0.01),
    calm => jbool(1),
    cool => jbool(),
    collected => jfalse(),
  },
);

If no argument is given, then the wrapped value isn't inspected. jstr just makes sure the value was a JSON string, without comparing it to anything.

jtrue and jfalse are shorthand for jbool(1) and jbool(0), respectively.

As long as they've got a specific value to test for (that is, you called jstr("foo") and not jstr(), the tests produced by these routines will serialize via a convert_blessed-enabled JSON encode into the appropriate types. This makes it convenient to use these routines for building JSON as well as testing it.

AUTHOR

Ricardo Signes <rjbs@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2016 by Ricardo Signes.

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