Test::XML(3pm)

πŸ’– Test::XML πŸ’–

okay bestie, so this is Test::XML β€” it's a Perl thing for comparing XML in your tests, and honestly? it's giving ✨peace of mind✨. it lives in the User Contributed Perl Documentation, so you already know it's the people's module. 🫢


✨ SYNOPSIS ✨

like, here's the whole vibe in one code block:

use Test::XML tests => 3;
is_xml( '<foo />', '<foo></foo>' );   # PASS
is_xml( '<foo />', '<bar />' );       # FAIL
isnt_xml( '<foo />', '<bar />' );     # PASS
is_well_formed_xml('<foo/>');               # PASS
is_well_formed_xml('<foo>');                # FAIL

see how <foo /> and <foo></foo> are literally the same thing to this module? that's the whole point bestie, semantic comparison, no cap. πŸ’…


🌸 DESCRIPTION 🌸

so this module is a lil collection of generic XML testing tools. and if you're looking for stuff that's specific to particular XML modules, keep scrolling β€” there's a list of those other modules further down. iykyk. πŸ¦‹


πŸŽ€ FUNCTIONS πŸŽ€

πŸ’… is_xml ( GOT, EXPECTED [, TESTNAME ] )

this one takes GOT and EXPECTED β€” both of them strings of XML β€” and compares them. and the way it compares them? semantically. meaning it will straight-up ignore syntax differences that don't actually mean anything in XML, like:

  • different quote characters for attributes
  • the order of attributes
  • empty tag styles

returns true or false depending on whether the test succeeded. iconic behaviour, honestly. ✨

πŸ’… isnt_xml( GOT, MUST_NOT_BE [, TESTNAME ] )

basically is_xml()'s chaotic twin. it does the same kinda thing, except it fails if GOT and MUST_NOT_BE are identical. so it passes when they're not the same. periodt.

πŸ’… is_well_formed_xml( XML [, TESTNAME ] )

this one just checks whether a given XML string is parseable as XML. that's it. is it valid? is it well-formed? vibe check complete. 🧚

πŸ’… is_good_xml ( XML [, TESTNAME ] )

an alias for is_well_formed_xml(). same energy, different name. πŸ’œ


πŸ‘‘ NOTES πŸ‘‘

okay so there are a few features of XML::SemanticDiff that might genuinely surprise you if nobody warned you first. consider this your warning. 🚨

  • Leading and trailing whitespace is always stripped, even in elements with character content.
  • Whitespace inside character content is always stripped down to a single space.
  • In mixed content elements (ie: an element with both text and elements beneath it), all text is treated as a single value.
  • The order of elements is ignored.
lowkey important: that last one means element order doesn't matter to this module. if you were relying on order, that's a you problem bestie. 😭

🌈 SEE ALSO 🌈

  • Test::XML::SAX
  • Test::XML::Twig
  • Test::More
  • XML::SemanticDiff

go read those too, they're all part of the same little friend group. 🫢


⭐ AUTHOR ⭐

Dominic Mitchell, <cpan2 (at) semantico.com>


Copyright 2002 by semantico

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


and that's the whole page bestie!! πŸŽ€ this was perl v5.34.0, dated 2022-06-18, and yes it's the User Contributed Perl Documentation version of Test::XML(3pm). slay, go write some tests, mwah πŸ’‹βœ¨

πŸ“¦ where this page comes from