Debian Codegraph @

cpan/Test2-Suite/lib/Test2/Workflow/Task.pm

raw
1 package Test2::Workflow::Task;
2 use strict;
3 use warnings;
4
5 our $VERSION = '0.000162';
6
7 use Test2::API();
8 use Test2::Event::Exception();
9
10 use List::Util qw/min max/;
11 use Scalar::Util qw/blessed/;
12 use Carp qw/croak/;
13 our @CARP_NOT = qw/Test2::Util::HashBase/;
14
15 use base 'Test2::Workflow::BlockBase';
16 use Test2::Util::HashBase qw/name flat async iso todo skip scaffold events is_root/;
17
18 for my $attr (FLAT, ISO, ASYNC, TODO, SKIP, SCAFFOLD) {
19 my $old = __PACKAGE__->can("set_$attr");
20 my $new = sub {
21 my $self = shift;
22 my $out = $self->$old(@_);
23 $self->verify_scaffold;
24 return $out;
25 };
26
27 no strict 'refs';
28 no warnings 'redefine';
29 *{"set_$attr"} = $new;
30 }
31
32 sub init {
33 my $self = shift;
34
35 $self->{+EVENTS} ||= [];
36
37 {
38 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
39 $self->SUPER::init();
40 }
41
42 $self->throw("the 'name' attribute is required")
43 unless $self->{+NAME};
44
45 $self->throw("the 'flat' attribute cannot be combined with 'iso' or 'async'")
46 if $self->{+FLAT} && ($self->{+ISO} || $self->{+ASYNC});
47
48 $self->set_subname($self->package . "::<$self->{+NAME}>");
49
50 $self->verify_scaffold;
51 }
52
53 sub clone {
54 my $self = shift;
55 return bless {%$self}, blessed($self);
56 }
57
58 sub verify_scaffold {
59 my $self = shift;
60
61 return unless $self->{+SCAFFOLD};
62
63 croak "The 'flat' attribute must be true for scaffolding"
64 if defined($self->{+FLAT}) && !$self->{+FLAT};
65
66 $self->{+FLAT} = 1;
67
68 for my $attr (ISO, ASYNC, TODO, SKIP) {
69 croak "The '$attr' attribute cannot be used on scaffolding"
70 if $self->{$attr};
71 }
72 }
73
74 sub exception {
75 my $self = shift;
76 my ($err) = @_;
77
78 my $hub = Test2::API::test2_stack->top;
79
80 my $trace = $self->trace($hub);
81
82 $hub->send(
83 Test2::Event::Exception->new(
84 trace => $trace,
85 error => $err,
86 ),
87 );
88 }
89
90 sub filter {
91 my $self = shift;
92 my ($filter) = @_;
93
94 return unless $filter;
95 return if $self->{+IS_ROOT};
96 return if $self->{+SCAFFOLD};
97
98 if (my $name = $filter->{name}) {
99 my $ok = 0;
100 unless(ref($name)) {
101 $ok ||= $self->{+NAME} eq $name;
102 $ok ||= $self->subname eq $name;
103 }
104 if (ref($name) eq 'Regexp') {
105 $ok ||= $self->{+NAME} =~ $name;
106 $ok ||= $self->subname =~ $name;
107 }
108 elsif ($name =~ m{^/}) {
109 my $pattern = eval "qr$name" or die "'$name' does not appear to be a valid pattern";
110 $ok ||= $self->{+NAME} =~ $pattern;
111 $ok ||= $self->subname =~ $pattern;
112 }
113
114 return {skip => "Does not match name filter '$name'"}
115 unless $ok;
116 }
117
118 if (my $file = $filter->{file}) {
119 return {skip => "Does not match file filter '$file'"}
120 unless $self->file eq $file;
121 }
122
123 if (my $line = $filter->{line}) {
124 my $lines = $self->lines;
125
126 return {skip => "Does not match line filter '$line' (no lines)"}
127 unless $lines && @$lines;
128
129 my $min = min(@$lines);
130 my $max = max(@$lines);
131
132 return {skip => "Does not match line filter '$min <= $line <= $max'"}
133 unless $min <= $line && $max >= $line;
134 }
135
136 return;
137 }
138
139 1;
140
141 __END__
142
143 =pod
144
145 =encoding UTF-8
146
147 =head1 NAME
148
149 Test2::Workflow::Task - Encapsulation of a Task
150
151 =head1 SOURCE
152
153 The source code repository for Test2-Workflow can be found at
154 F<https://github.com/Test-More/Test2-Suite/>.
155
156 =head1 MAINTAINERS
157
158 =over 4
159
160 =item Chad Granum E<lt>[email protected]<gt>
161
162 =back
163
164 =head1 AUTHORS
165
166 =over 4
167
168 =item Chad Granum E<lt>[email protected]<gt>
169
170 =back
171
172 =head1 COPYRIGHT
173
174 Copyright 2018 Chad Granum E<lt>[email protected]<gt>.
175
176 This program is free software; you can redistribute it and/or
177 modify it under the same terms as Perl itself.
178
179 See F<http://dev.perl.org/licenses/>
180
181 =cut
182