1Test2::Manual::Tooling:U:sFeirrsCtoTnotorli(b3u)ted PerlTeDsotc2u:m:eMnatnautailo:n:Tooling::FirstTool(3)
2
3
4

NAME

6       Test2::Manual::Tooling::FirstTool - Write your first tool with Test2.
7

DESCRIPTION

9       This tutorial will help you write your very first tool by cloning the
10       "ok()" tool.
11

COMPLETE CODE UP FRONT

13           package Test2::Tools::MyOk;
14           use strict;
15           use warnings;
16
17           use Test2::API qw/context/;
18
19           use base 'Exporter';
20           our @EXPORT = qw/ok/;
21
22           sub ok($;$@) {
23               my ($bool, $name, @diag) = @_;
24
25               my $ctx = context();
26
27               return $ctx->pass_and_release($name) if $bool;
28               return $ctx->fail_and_release($name, @diag);
29           }
30
31           1;
32

LINE BY LINE

34       sub ok($;$@) {
35           In this case we are emulating the "ok()" function exported by
36           Test2::Tools::Basic.
37
38           "ok()" and similar test tools use prototypes to enforce argument
39           parsing. Your test tools do not necessarily need prototypes, like
40           any perl function you need to make the decision based on how it is
41           used.
42
43           The prototype requires at least 1 argument, which will be forced
44           into a scalar context. The second argument is optional, and is also
45           forced to be scalar, it is the name of the test. Any remaining
46           arguments are treated as diagnostics messages that will only be
47           used if the test failed.
48
49       my ($bool, $name, @diag) = @_;
50           This line does not need much explanation, we are simply grabbing
51           the args.
52
53       my $ctx = context();
54           This is a vital line in ALL tools. The context object is the
55           primary API for test tools. You MUST get a context if you want to
56           issue any events, such as making assertions. Further, the context
57           is responsible for making sure failures are attributed to the
58           correct file and line number.
59
60           Note: A test function MUST always release the context when it is
61           done, you cannot simply let it fall out of scope and be garbage
62           collected. Test2 does a pretty good job of yelling at you if you
63           make this mistake.
64
65           Note: You MUST NOT ever store or pass around a real context object.
66           If you wish to hold on to a context for any reason you must use
67           clone to make a copy "my $copy = $ctx->clone". The copy may be
68           passed around or stored, but the original MUST be released when you
69           are done with it.
70
71       return $ctx->pass_and_release($name) if $bool;
72           When $bool is true, this line uses the context object to issue a
73           Test2::Event::Pass event. Along with issuing the event this will
74           also release the context object and return true.
75
76           This is short form for:
77
78               if($bool) {
79                   $ctx->pass($name);
80                   $ctx->release;
81                   return 1;
82               }
83
84       return $ctx->fail_and_release($name, @diag);
85           This line issues a Test2::Event::Fail event, releases the context
86           object, and returns false. The fail event will include any
87           diagnostics messages from the @diag array.
88
89           This is short form for:
90
91               $ctx->fail($name, @diag);
92               $ctx->release;
93               return 0;
94

CONTEXT OBJECT DOCUMENTATION

96       Test2::API::Context is the place to read up on what methods the context
97       provides.
98

SEE ALSO

100       Test2::Manual - Primary index of the manual.
101

SOURCE

103       The source code repository for Test2-Manual can be found at
104       https://github.com/Test-More/Test2-Suite/.
105

MAINTAINERS

107       Chad Granum <exodist@cpan.org>
108

AUTHORS

110       Chad Granum <exodist@cpan.org>
111
113       Copyright 2018 Chad Granum <exodist@cpan.org>.
114
115       This program is free software; you can redistribute it and/or modify it
116       under the same terms as Perl itself.
117
118       See http://dev.perl.org/licenses/
119
120
121
122perl v5.32.0                      2020-12-1T6est2::Manual::Tooling::FirstTool(3)
Impressum