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

NAME

Test::JSON::Type - Test JSON data with types.

SYNOPSIS

 use Test::JSON::Type;

 is_json_type($json, $json_expected, $test_name);
 is_json_type_struct($json, $expected_type_hr, $test_name);

SUBROUTINES

is_json_type

 is_json_type($json, $json_expected, $test_name);

This decodes $json and $json_expected JSON strings to Perl structure and return data type structure defined by Cpanel::JSON::XS::Type. And compare these structures, if are same.

Result is success or failure of this comparison. In case of failure print difference in test.

is_json_type_struct

 is_json_type_struct($json, $expected_type_hr, $test_name);

This decoded $json JSON string to Perl structure and return data type structure defined by Cpanel::JSON::XS::Type. Compare this structure with $expected_type_hr, if are same.

Result is success or failure of this comparison. In case of failure print difference in test.

ERRORS

 is_json_type():
         JSON string isn't valid.
                 Error: %s
         JSON string to compare is required.
         Expected JSON string isn't valid.
                 Error: %s
         Expected JSON string to compare is required.
 is_json_type_struct():
         JSON string isn't valid.
                 Error: %s
         JSON string to compare is required.

EXAMPLE1

 use strict;
 use warnings;

 use Test::JSON::Type;
 use Test::More 'tests' => 2;

 my $json_blank1 = '{}';
 my $json_blank2 = '{}';
 is_json_type($json_blank1, $json_blank2, 'Blank JSON strings.');

 my $json_struct1 = <<'END';
 {
   "bool": true,
   "float": 0.23,
   "int": 1,
   "null": null,
   "string": "bar"
 }
 END
 my $json_struct2 = <<'END';
 {
   "bool": false,
   "float": 1.23,
   "int": 2,
   "null": null,
   "string": "foo"
 }
 END
 is_json_type($json_struct1, $json_struct2, 'Structured JSON strings.');

 # Output:
 # 1..2
 # ok 1 - Blank JSON strings.
 # ok 2 - Structured JSON strings.

EXAMPLE2

 use strict;
 use warnings;

 use Test::JSON::Type;
 use Test::More 'tests' => 1;

 my $json_struct_err1 = <<'END';
 {
   "int": 1,
   "string": "1"
 }
 END
 my $json_struct_err2 = <<'END';
 {
   "int": 1,
   "string": 1
 }
 END
 is_json_type($json_struct_err1, $json_struct_err2, 'Structured JSON strings with error.');

 # Output:
 # 1..1
 # not ok 1 - Structured JSON strings with error.
 # #   Failed test 'Structured JSON strings with error.'
 # #   at ./ex2.pl line 21.
 # # +----+--------------------------------+-----------------------------+
 # # | Elt|Got                             |Expected                     |
 # # +----+--------------------------------+-----------------------------+
 # # |   0|{                               |{                            |
 # # |   1|  int => 'JSON_TYPE_INT',       |  int => 'JSON_TYPE_INT',    |
 # # *   2|  string => 'JSON_TYPE_STRING'  |  string => 'JSON_TYPE_INT'  *
 # # |   3|}                               |}                            |
 # # +----+--------------------------------+-----------------------------+
 # # Looks like you failed 1 test of 1.

EXAMPLE3

 use strict;
 use warnings;

 use Test::JSON::Type;
 use Test::More 'tests' => 1;

 my $json_struct_err1 = <<'END';
 {
   "int": 1,
   "array": ["1", 1]
 }
 END
 my $json_struct_err2 = <<'END';
 {
   "int": 1,
   "array": 1
 }
 END
 is_json_type($json_struct_err1, $json_struct_err2, 'Structured JSON strings with error.');

 # Output:
 # 1..1
 # not ok 1 - Structured JSON strings with error.
 # #   Failed test 'Structured JSON strings with error.'
 # #   at ./ex3.pl line 21.
 # # +----+--------------------------+----+-----------------------------+
 # # | Elt|Got                       | Elt|Expected                     |
 # # +----+--------------------------+----+-----------------------------+
 # # |   0|{                         |   0|{                            |
 # # *   1|  array => [              *   1|  array => 'JSON_TYPE_INT',  *
 # # *   2|    'JSON_TYPE_STRING',   *    |                             |
 # # *   3|    'JSON_TYPE_INT'       *    |                             |
 # # *   4|  ],                      *    |                             |
 # # |   5|  int => 'JSON_TYPE_INT'  |   2|  int => 'JSON_TYPE_INT'     |
 # # |   6|}                         |   3|}                            |
 # # +----+--------------------------+----+-----------------------------+
 # # Looks like you failed 1 test of 1.

DEPENDENCIES

Cpanel::JSON::XS, Cpanel::JSON::XS::Type, English, Error::Pure, Readonly, Test::Builder::Module, Test::Differences.

SEE ALSO

Test::JSON

Test JSON data

Test::JSON::More

JSON Test Utility

REPOSITORY

https://github.com/michal-josef-spacek/Test-JSON-Type

AUTHOR

Michal Josef Špaček mailto:skim@cpan.org

http://skim.cz

LICENSE AND COPYRIGHT

© 2021-2022 Michal Josef Špaček

BSD 2-Clause License

VERSION

0.02