1Test2::Manual::Tooling:U:sNeerstCionngt(r3i)buted Perl DToecsutm2e:n:tMaatniuoanl::Tooling::Nesting(3)
2
3
4
6 Test2::Manual::Tooling::Nesting - Tutorial for using other tools within
7 your own.
8
10 Sometimes you find yourself writing the same test pattern over and
11 over, in such cases you may want to encapsulate the logic in a new test
12 function that calls several tools together. This sounds easy enough,
13 but can cause headaches if not done correctly.
14
16 Lets say you find yourself writing the same test pattern over and over
17 for multiple objects:
18
19 my $obj1 = $class1->new;
20 is($obj1->foo, 'foo', "got foo");
21 is($obj1->bar, 'bar', "got bar");
22
23 my $obj2 = $class1->new;
24 is($obj2->foo, 'foo', "got foo");
25 is($obj2->bar, 'bar', "got bar");
26
27 ... 10x more times for classes 2-12
28
29 The naive way to do this is to write a "check_class()" function like
30 this:
31
32 sub check_class {
33 my $class = shift;
34 my $obj = $class->new;
35 is($obj->foo, 'foo', "got foo");
36 is($obj->bar, 'bar', "got bar");
37 }
38
39 check_class($class1);
40 check_class($class2);
41 check_class($class3);
42 ...
43
44 This will appear to work fine, and you might not notice any problems,
45 so long as the tests are passing.
46
47 WHATS WRONG WITH IT?
48 The problems with the naive approach become obvious if things start to
49 fail. The diagnostics that tell you what file and line the failure
50 occurred on will be wrong. The failure will be reported to the line
51 inside "check_class", not to the line where "check_class()" was called.
52 This is problem because it leaves you with no idea which class is
53 failing.
54
55 HOW TO FIX IT
56 Luckily this is extremely easy to fix. You need to acquire a context
57 object at the start of your function, and release it at the end... yes
58 it is that simple.
59
60 use Test2::API qw/context/;
61
62 sub check_class {
63 my $class = shift;
64
65 my $ctx = context();
66
67 my $obj = $class->new;
68 is($obj->foo, 'foo', "got foo");
69 is($obj->bar, 'bar', "got bar");
70
71 $ctx->release;
72 }
73
74 See, that was easy. With these 2 additional lines we know have proper
75 file+line reporting. The nested tools will find the context we acquired
76 here, and know to use it's file and line numbers.
77
78 THE OLD WAY (DO NOT DO THIS ANYMORE)
79
80 With Test::Builder there was a global variables called
81 $Test::Builder::Level which helped solve this problem:
82
83 sub check_class {
84 my $class = shift;
85
86 local $Test::Builder::Level = $Test::Builder::Level + 1;
87
88 my $obj = $class->new;
89 is($obj->foo, 'foo', "got foo");
90 is($obj->bar, 'bar', "got bar");
91 }
92
93 This variable worked well enough (and will still work) but was not very
94 discoverable. Another problem with this variable is that it becomes
95 cumbersome if you have a more deeply nested code structure called the
96 nested tools, you might need to count stack frames, and hope they never
97 change due to a third party module. The context solution has no such
98 caveats.
99
101 Test2::Manual - Primary index of the manual.
102
104 The source code repository for Test2-Manual can be found at
105 https://github.com/Test-More/Test2-Suite/.
106
108 Chad Granum <exodist@cpan.org>
109
111 Chad Granum <exodist@cpan.org>
112
114 Copyright 2018 Chad Granum <exodist@cpan.org>.
115
116 This program is free software; you can redistribute it and/or modify it
117 under the same terms as Perl itself.
118
119 See http://dev.perl.org/licenses/
120
121
122
123perl v5.34.0 2022-03-08Test2::Manual::Tooling::Nesting(3)