Test::JSON(3pm)

πŸ’– Test::JSON β€” like, the manual, but make it make sense πŸ’–

okay bestie, so this is Test::JSON(3pm) β€” a lil Perl module for testing JSON data, and honestly? it ate. let's get into it πŸ¦‹


✨ NAME ✨

Test::JSON β€” Test JSON data. that's it, that's the vibe. πŸ’…

πŸŽ€ VERSION πŸŽ€

Version 0.11 ✨ (yes queen, we're on 0.11)

🌸 SYNOPSIS 🌸

bestie, here's the whole entire move:

use Test::JSON;

is_valid_json $json,                 '... json is well formed';
is_json       $json, $expected_json, '... and it matches what we expected';

that's it. two little functions and your JSON test game is immaculate. 🫢

πŸ’… EXPORT πŸ’…

this module hands you these two for free when you use it:

  • is_valid_json
  • is_json

no :all needed, no begging, they just show up. main character behaviour. πŸ‘‘

πŸ¦‹ DESCRIPTION πŸ¦‹

so. JavaScript Object Notation (JSON) is a lightweight data interchange format β€” you already knew that, ikyk 😌

what Test::JSON does is make it super easy to check two things:

  1. that you actually built valid JSON βœ…
  2. that it matches the output you expected βœ…

go peep <http://www.json.org/> if you want the full JSON lore, fr fr. 🌈

πŸ”₯ TESTS πŸ”₯

is_valid_json

is_valid_json $json, '... json is well formed';

the test passes if the string you passed in is valid JSON. that's the whole deal. valid? pass. not valid? not pass. periodt. πŸ’–

is_json

is_json $json, $expected_json, '... and it matches what we expected';

the test passes if both JSON strings are valid JSON and they evaluate to the same data structure. like, same data structure, not just same letters. it's giving "understood the assignment". 🧚

and okay this is the part that's actually so good β€” Test::Differences is used to give you easy diagnostics of why the JSON structures did not match. so when it fails you're not just sitting there like 😭 you get:

  Failed test '... and identical JSON should match'
  in t/10testjson.t at line 14.
+----+---------------------------+---------------------------+
| Elt|Got                        |Expected                   |
+----+---------------------------+---------------------------+
|   0|{                          |{                          |
|   1|  bool => '1',             |  bool => '1',             |
|   2|  description => bless( {  |  description => bless( {  |
|   3|    value => undef         |    value => undef         |
|   4|  }, 'JSON::NotString' ),  |  }, 'JSON::NotString' ),  |
|   5|  id => '1',               |  id => '1',               |
*   6|  name => 'foo'            |  name => 'fo'             *
|   7|}                          |}                          |
+----+---------------------------+---------------------------+

see that lil * on line 6? 'foo' vs 'fo'. caught in 4k, no cap. πŸ“Έ the diff literally points at the difference so you can fix it instead of spiralling. obsessed. πŸ’œ

πŸ‘‘ AUTHOR πŸ‘‘

Curtis "Ovid" Poe, "<ovid@cpan.org>" β€” an absolute legend πŸ†

πŸ’€ BUGS πŸ’€

please report any bugs or feature requests to "bug-test-json@rt.cpan.org", or through the web interface at <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-JSON>.

and here's the cute part: I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. so like, you get receipts. 🫢✨

🌈 SEE ALSO 🌈

this test module uses JSON::Any and Test::Differences. go say hi to them, they're doing the heavy lifting fr. πŸ’

πŸ“ ACKNOWLEDGEMENTS πŸ“

The development of this module was sponsored by Kineticode, <http://www.kineticode.com/>, the leading provider of services for the Bricolage content management system, <http://www.bricolage.cc/>. sponsors, we love you, thank you for funding the vibes πŸ’–

Thanks to Makamaka Hannyaharamitu "makamaka@cpan.org" for a patch to make this work with JSON 2.0. πŸ› οΈ iconic.

Thanks to Stevan Little for suggesting a switch to JSON::Any. This makes it easier for this module to work with whatever JSON module you have installed. flexible queen behaviour. πŸ¦‹

Copyright 2005-2007 Curtis "Ovid" Poe, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. slay, and, importantly, legally slay. πŸ’œ


and that's the whole page bestie!! this was Test::JSON version 0.11, and the page itself is from perl v5.36.0, dated 2022-10-14. love u, go write some valid JSON πŸ’–βœ¨πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from