TEST2::MANUAL::TOOLING::NESTING(3pm)
π¦ Test2::Manual::Tooling::Nesting(3pm) π¦
okay bestie, this page is from the Perl Programmers Reference Guide, and itβs literally a tutorial for using other tools within your own. no cap. π
β¨ NAME β¨
Test2::Manual::Tooling::Nestingβ Tutorial for using other tools within your own.
πΈ DESCRIPTION πΈ
okay so sometimes youβre out here writing the exact same test pattern over and over, and in those cases you might want to wrap the logic in a shiny new test function that calls a bunch of tools together. Sounds easy enough, right? It can still cause headaches if you donβt do it correctly, bestie. π
β¨ NAIVE WAY β¨
Letβs say you find yourself writing the same test pattern over and over for multiple objects:
my $obj1 = $class1->new;
is($obj1->foo, 'foo', "got foo");
is($obj1->bar, 'bar', "got bar");
my $obj2 = $class1->new;
is($obj2->foo, 'foo', "got foo");
is($obj2->bar, 'bar', "got bar");
... 10x more times for classes 2-12
The naive way to do this is to write a check_class() function like this:
sub check_class {
my $class = shift;
my $obj = $class->new;
is($obj->foo, 'foo', "got foo");
is($obj->bar, 'bar', "got bar");
}
check_class($class1);
check_class($class2);
check_class($class3);
...
This will appear to work fine, and you might not notice any problems, so long as the tests are passing. π
π WHATS WRONG WITH IT? π
The problems with the naive approach become obvious the moment things start to fail. The diagnostics that tell you what file and line the failure occurred on will be wrong β the failure gets reported to the line inside check_class, not to the line where check_class() was called. Thatβs a problem because it leaves you with no idea which class is failing. π
π«Ά HOW TO FIX IT π«Ά
Luckily this is extremely easy to fix. You need to acquire a context object at the start of your function, and release it at the endβ¦ yes it is that simple. β¨
use Test2::API qw/context/;
sub check_class {
my $class = shift;
my $ctx = context();
my $obj = $class->new;
is($obj->foo, 'foo', "got foo");
is($obj->bar, 'bar', "got bar");
$ctx->release;
}
See, that was easy. With these 2 additional lines we now have proper file+line reporting. The nested tools will find the context we acquired here, and know to use its file and line numbers. periodt. π
π THE OLD WAY (DO NOT DO THIS ANYMORE) π
With Test::Builder there was a global variable called $Test::Builder::Level which helped solve this problem:
sub check_class {
my $class = shift;
local $Test::Builder::Level = $Test::Builder::Level + 1;
my $obj = $class->new;
is($obj->foo, 'foo', "got foo");
is($obj->bar, 'bar', "got bar");
}
This variable worked well enough (and will still work) but was not very discoverable. Another problem with this variable is that it becomes cumbersome if you have a more deeply nested code structure called the nested tools β you might need to count stack frames, and hope they never change due to a third party module. The context solution has no such caveats. π
π SEE ALSO π
Test2::Manualβ Primary index of the manual.
π SOURCE π
The source code repository for Test2-Manual can be found at https://github.com/Test-More/test-more/.
π MAINTAINERS π
- Chad Granum β
<exodist@cpan.org>
πΈ AUTHORS πΈ
- Chad Granum β
<exodist@cpan.org>
π COPYRIGHT π
Copyright Chad Granum <exodist@cpan.org>.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See http://dev.perl.org/licenses/
okay thatβs the whole page, bestie π signed off by perl v5.42.3, dated 2026-11-03, and filed under Test2::...:Nesting(3pm). mwah π«Άπ
π¦ where this page comes from
- alpine 3.24: perl-doc 5.42.3-r0 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms