Scaffold/App/Test/YAMLDriven.pm

filter: Statementstmt 42.1% 51 / 121 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 60.7% 17 / 28 Podpod 100% 9 / 9 totaltotal 39.9% 77 / 193 CC 17 SCAR 24.8
CC
17
Cov
34%
__ANON__
42.0
read_test_defs
20.0
execute_test_def
20.0
CRAP 11.9
« Scaffold/Environment.pm ↑ Summary Scaffold/Storable.pm »
1 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Class::Scaffold::App::Test::YAMLDriven;
6 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 $Class::Scaffold::App::Test::YAMLDriven::VERSION = '1.102280';
8 }
9 # ABSTRACT: Base class for YAML-driven test programs
10 1 ▶use Class::Scaffold::App;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Error::Hierarchy::Util 'assert_defined';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use List::Util 'shuffle';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use String::FlexMatch; # in case some tests need it
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use parent 'Class::Scaffold::App::Test';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 $Error::Debug = 1; # to trigger a stacktrace on an exception
21 1 __PACKAGE__->mk_abstract_accessors(qw(run_subtest plan_test))
22 ->mk_hash_accessors(qw(test_def))->mk_scalar_accessors(
23 qw(
24 testdir testname expect run_num runs current_test_def
25 )
26 );
27 1 ▶use constant SHARED => '00shared.yaml';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use constant GETOPT => (
Subroutine BEGIN: called CC=1 SCAR=0.0
29 qw/
30 shuffle reverse
31 /
32 1 );
33  
34 # 'runs' is the number of stage runs per test file ensure idempotency
35 1 ▶use constant DEFAULTS => (
Subroutine BEGIN: called CC=1 SCAR=0.0
36 runs => 1,
37 testdir => '.',
38 1 );
39  
40 0 ▶sub app_code {
Subroutine app_code: not called CC=3 SCAR=24.8
Pod documented
41 0 my $self = shift;
42 0 $self->SUPER::app_code(@_);
43 0 $self->read_test_defs;
44 0 plan tests => $self->make_plan;
45 0 for my $testname ($self->ordered_test_def_keys) {
46 0 ▶ next if $testname eq SHARED;
Branch: 1 branch
BranchTrueFalseTotal
if $testname eq "00shared.yaml" 0 0 0
47 0 $self->execute_test_def($testname);
48 }
49 }
50  
51 0 ▶sub read_test_defs {
Subroutine read_test_defs: not called CC=4 SCAR=30.0
Pod documented
52 0 my $self = shift;
53  
54 # It's possible to pass args to the test program. If there are any
55 # such args, then in order for a test file to be used its name has to
56 # contain one of the args as a substring. For example, to only run
57 # the policy tests whose name contains 'unnamed' or '99', you'd use:
58 #
59 # perl t/10Policy.t unnamed 99
60 0 my $name_filter = join '|' => map { "\Q$_\E" } @ARGV;
61 0 my $testdir = $self->testdir;
62  
63 # First collect the files to process into a hash, then process that
64 # hash sorted by name. This separation is necessary because some test
65 # files depend on others, but find() doesn't ensure that the files are
66 # returned in sorted order.
67 0 my %file;
68 find(
69 0 ▶ sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
70 0 ▶ return unless -f && /\.yaml$/;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $_ and /\.yaml$/ 0 0 0
Condition0%0/3
-f $_ and /\.yaml$/
!ll&&!rl&&r
000
MC/DC0%0/2
-f $_ and /\.yaml$/
-f $_ /\.yaml$/
Truth table0%0/3
A and B
A= -f $_B= /\.yaml$/
ABresult
0X0
100
111
71 0 (my $name = $File::Find::name) =~ s!^$testdir/!!;
72 return
73 0 ▶ if $name ne SHARED && $name_filter && $name !~ /$name_filter/o;
Branch: 1 branch
BranchTrueFalseTotal
if $name ne "00shared.yaml" and $name_filter and not $name =~ /$name_filter/o 0 0 0
Condition0%0/6
$name ne "00shared.yaml" and $name_filter and not $name =~ /$name_filter/o
!ll&&!rl&&r
000
$name ne "00shared.yaml" and $name_filter
!ll&&!rl&&r
000
MC/DC0%0/3
$name ne "00shared.yaml" and $name_filter and not $name =~ /$name_filter/o
$name ne "00shared.yaml" $name_filter not $name =~ /$name_filter/o
Truth table0%0/4
A and B and not(C)
A= $name ne "00shared.yaml"B= $name_filterC= not $name =~ /$name_filter/o
ABCresult
0XX0
10X0
1100
1111
74 0 $file{$name} = $File::Find::name;
75 },
76 0 $testdir
77 );
78 0 for my $name (sort keys %file) {
79 0 note "Loading test file $name";
80 ( my $tests_yaml =
81 0 do { local (@ARGV, $/) = $file{$name}; <> }
82 0 ) =~ s/%%PID%%/sprintf("%06d", $$)/ge;
83 0 $tests_yaml =~ s/%%CNT%%/sprintf("%03d", ++(our $cnt))/ge;
84  
85 # Quick regex check whether the test wants to be skipped. To use
86 # Load() on a test that wants to be skipped would be a bad idea as it
87 # might be work in progress; it will be skipped for a reason.
88 0 ▶ if ($tests_yaml =~ /^skip:\s*1/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tests_yaml =~ /^skip:\s*1/m) { } 0 0 0
89 0 note 'Test wants to be skipped, no activation';
90 } else {
91  
92 # support for value classes
93 0 local $Class::Value::SkipChecks = 1;
94  
95 # require(), not use(), YAML classes because YAML and YAML::Active
96 # might conflict.
97 0 my $test_def;
98 0 ▶ if ($tests_yaml =~ /^use_yaml_active:\s*1/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tests_yaml =~ /^use_yaml_active:\s*1/m) { } 0 0 0
99 0 note 'Loading with YAML::Active.pm';
100 0 require YAML::Active;
101 0 $test_def = YAML::Active::Load($tests_yaml);
102 } else {
103 0 require YAML;
104  
105 # Erik P. Ostlyngen writes:
106 #
107 # There seems to be a difference in the behaviour of YAML and
108 # YAML::XS when it comes to wide characters. YAML::Load()
109 # wants the string to be a perl wide character string whereas
110 # YAML::XS::Load() wants a string of bytes which it tries to
111 # utf-8 decode afterwards.
112 #
113 # This is a problem in Class::Scaffold::App::Test::YAMLDriven
114 # because it uses both of the two YAML modules. So if we're
115 # writing our tests with the use_yaml_active tag, we can
116 # include utf-8 in the document. But if we instead use YAML
117 # with the marshall classes, we cannot use utf-8 directly.
118 #
119 # I think it would be a good idea to support utf-8 encoding
120 # both in yaml-active and yaml-marshall documents and in the
121 # same way. This could easily be fixed with [decode_utf8()].
122 0 $test_def = YAML::Load(decode_utf8($tests_yaml));
123  
124 # note explain $test_def;
125 }
126 0 $self->test_def($name => $test_def);
127 }
128 }
129 }
130  
131 0 ▶sub ordered_test_def_keys {
Subroutine ordered_test_def_keys: not called CC=3 SCAR=24.8
Pod documented
132 0 my $self = shift;
133 0 my @tests;
134 0 ▶ if ($self->opt->{shuffle}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->opt->{'shuffle'}) { } 0 0 0
elsif ($self->opt->{'reverse'}) { } 0 0 0
135 0 note 'test order: shuffle';
136 0 @tests = shuffle $self->test_def_keys;
137 } elsif ($self->opt->{reverse}) {
138 0 note 'test order: reverse';
139 0 @tests = reverse sort $self->test_def_keys;
140 } else {
141 0 note 'test order: sort';
142 0 @tests = sort $self->test_def_keys;
143  
144 # Perl::Critic complains about "return sort ... "
145 }
146 0 @tests;
147 }
148  
149 0 ▶sub should_skip_testname {
Subroutine should_skip_testname: not called CC=2 SCAR=17.9
Pod documented
150 0 my ($self, $testname) = @_;
151 0 ▶ return 'wants to be skipped' if $self->test_def($testname)->{skip};
Branch: 1 branch
BranchTrueFalseTotal
if $self->test_def($testname)->{"skip"} 0 0 0
152 0 return undef;
153 }
154  
155 0 ▶sub make_plan {
Subroutine make_plan: not called CC=1 SCAR=6.9
Pod documented
156 0 my $self = shift;
157  
158 # Each YAML file produces either a skip or a subtest, except for the
159 # shared file, which is expected to only contain YAML::Active objects for
160 # setup.
161 0 $self->runs * (grep { $_ ne SHARED } $self->test_def_keys);
162 }
163  
164 0 ▶sub execute_test_def {
Subroutine execute_test_def: not called CC=4 SCAR=30.0
Pod documented
165 0 my ($self, $testname) = @_;
166 0 assert_defined $testname, 'called without testname';
167  
168 # In case subclasses need to do special things, like multiple tickets in a
169 # test definition:
170 0 $self->current_test_def($self->test_def($testname));
171 0 ▶ $self->expect($self->current_test_def->{expect} || {});
Condition0%0/2
$self->current_test_def->{'expect'} || {}
l!l
00
MC/DC0%0/1
$self->current_test_def->{'expect'} || {}
$self->current_test_def->{'expect'}
Truth table0%0/2
A
A= $self->current_test_def->{'expect'}
Aresult
11
00
172 0 for my $run (1 .. $self->runs) {
173 0 $self->run_num($run);
174 0 $self->testname(
175 sprintf('%s run %d of %d', $testname, $run, $self->runs));
176  
177 # If the current test def specifies that it wants to be skipped, just
178 # pass.
179 0 ▶ if (my $reason = $self->should_skip_testname($testname)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $reason = $self->should_skip_testname($testname)) { } 0 0 0
180 0 $self->todo_skip_test($reason);
181 } else {
182 0 $self->run_test;
183 }
184 }
185 }
186  
187 0 ▶sub run_test {
Subroutine run_test: not called CC=1 SCAR=6.9
Pod documented
188 0 my $self = shift;
189 0 ▶ subtest $self->testname, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
190 0 plan tests => $self->plan_test($self->current_test_def, $self->run_num);
191 0 $self->run_subtest;
192 0 };
193 }
194  
195 0 ▶sub named_test {
Subroutine named_test: not called CC=1 SCAR=6.9
Pod documented
196 0 my ($self, $suffix) = @_;
197 0 sprintf '%s: %s', $self->testname, $suffix;
198 }
199  
200 0 ▶sub todo_skip_test {
Subroutine todo_skip_test: not called CC=1 SCAR=6.9
Pod documented
201 0 my ($self, $reason) = @_;
202 0 Test::Builder->new->todo_skip($reason, 1);
203 }
204 1 1;
205  
206  
207 __END__
« Scaffold/Environment.pm ↑ Summary Scaffold/Storable.pm »