Test::Object(3)

πŸ’… Test::Object πŸ’…

okay bestie, so this is Test::Object, a page from the User Contributed Perl Documentation section of the manual (that's the (3) part, iykyk) β€” and honestly? it's giving "quietly iconic testing helper" energy. ✨

🌸 NAME 🌸

Test::Object β€” thoroughly testing objects via registered handlers 🫢

πŸŽ€ VERSION πŸŽ€

version 0.08 β€” yes, she's vintage, and yes, she still slays.

✨ SYNOPSIS ✨

ok so here's the whole vibe, in two acts. first you register your test handlers back in your test module:

  ###################################################################
  # In your test module, register test handlers again class names   #
  ###################################################################

  package My::ModuleTester;

  use Test::More;
  use Test::Object;

  # Foo::Bar is a subclass of Foo
  Test::Object->register(
        class => 'Foo',
        tests => 5,
        code  => \&foo_ok,
        );
  Test::Object->register(
        class => 'Foo::Bar',
        # No fixed number of tests
        code  => \&foobar_ok,
        );

  sub foo_ok {
        my $object = shift;
        ok( $object->foo, '->foo returns true' );
  }

  sub foobar_ok {
        my $object = shift;
        is( $object->foo, 'bar', '->foo returns "bar"' );
  }

  1;

then, in your actual test script, you point it at a real object and let it cook:

  ###################################################################
  # In test script, test object against all registered classes      #
  ###################################################################

  #!/usr/bin/perl -w

  use Test::More 'no_plan';
  use Test::Object;
  use My::ModuleTester;

  my $object = Foo::Bar->new;
  isa_ok( $object, 'Foo::Bar' );
  object_ok( $object );

main character behaviour, no cap. πŸ‘‘


πŸ’– DESCRIPTION πŸ’–

so picture this: you've got a deep, deep tree of classes 🌳 β€” like, you're testing a module four or five subclasses down, and the whole point is that it should behave correctly not just as itself but as all its parent classes too. that's the whole assignment, fr fr.

why bother? because you gotta make sure the subclass implementation hasn't somehow "broken" the object's behaviour in a more general sense. like, not me finding out at 2am that my subclass quietly ruined everything πŸ’€.

so Test::Object is a testing package designed to let you easily test what you believe is a valid object against the expected behaviour of all the classes in its inheritance tree β€” in one single call. one call! iconic. πŸŽ€

how does the magic happen? you "register" tests with Test::Object β€” and those tests come in the form of CODE or function references β€” with each test associated with a particular class. πŸ¦‹

then when you call object_ok in your test script, Test::Object checks the object against all the registered tests. for each class that your object responds to $object->isa($class) for, the appropriate testing function gets called. literally every single match, every time. ✨

and here's the cute part: doing it this way means adapter objects β€” and other things that respond to isa differently than the default β€” can still get tested against the classes they're advertising themselves as. so inclusive. so thoughtful. 🫢

⚠️ real talk though: this also means more than one test might be "counted" for each call to object_ok. so you should account for this correctly in your expected test count. don't get caught out by the math, bestie 😭

🌈 SUPPORT 🌈

got bugs? don't suffer in silence, submit them via the CPAN bug tracker, located at:

<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Object>

for other issues, just contact the author. they're nice, promise. πŸ’œ


πŸ‘‘ AUTHOR πŸ‘‘

Adam Kennedy <adamk@cpan.org> β€” the icon behind all of this πŸ’…


πŸ¦‹ SEE ALSO πŸ¦‹

go read them, they're all part of the ecosystem, okurrr 🌸


Copyright 2005, 2006 Adam Kennedy.

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

The full text of the license can be found in the LICENSE file included with this module.


🧚 aaaand that's the whole page! this one came from perl v5.42.0, dated 2017-12-24 β€” so when you're out here testing your objects, just know she's been serving thoroughness since way back. slay, periodt. πŸ’–πŸŽ€βœ¨
πŸ“¦ where this page comes from