forked from pherkin/test-bdd-cucumber-perl
/
pherkin.pm
232 lines (167 loc) · 5.92 KB
/
pherkin.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package App::pherkin;
use strict;
use warnings;
use Getopt::Long;
use Module::Runtime qw(use_module);
use List::Util qw(max);
use Pod::Usage;
use FindBin qw($RealBin $Script);
use Test::BDD::Cucumber::I18n
qw(languages langdef readable_keywords keyword_to_subname);
use Test::BDD::Cucumber::Loader;
use Moose;
has 'tags' => ( is => 'rw', isa => 'ArrayRef', required => 0 );
has 'tag_scheme' => ( is => 'rw', isa => 'ArrayRef', required => 0 );
=head1 NAME
App::pherkin - Run Cucumber tests from the command line
=head1 SYNOPSIS
pherkin
pherkin some/path/features/
=head1 DESCRIPTION
C<pherkin> will search the directory specified (or C<./features/>) for
feature files (any file matching C<*.feature>) and step definition files (any
file matching C<*_steps.pl>), loading the step definitions and then executing
the features.
Steps that pass will be printed in green, those that fail in red, and those
for which there is no step definition as yellow (for TODO), assuming you're
using the default output harness.
=head1 METHODS
=head2 run
The C<App::pherkin> class, which is what the C<pherkin> command uses, makes
use of the C<run()> method, which accepts currently a single path as a string,
or nothing.
Returns a L<Test::BDD::Cucumber::Model::Result> object for all steps run.
=cut
sub run {
my ( $self, @arguments ) = @_;
# localized features will have utf8 in them and options may output utf8 as well
binmode STDOUT, ':utf8';
my ( $options, @feature_files ) = $self->_process_arguments(@arguments);
my ( $executor, @features ) =
Test::BDD::Cucumber::Loader->load( $feature_files[0] || './features/',
$self->tag_scheme );
die "No feature files found" unless @features;
my $harness = $self->_load_harness( $options->{'harness'} );
my $tag_spec;
if ( $self->tag_scheme ) {
$tag_spec = Test::BDD::Cucumber::Model::TagSpec->new(
{ tags => $self->tag_scheme } );
}
$executor->execute( $_, $harness, $tag_spec ) for @features;
$harness->shutdown();
return $harness->result;
}
sub _load_harness {
my ( $self, $harness_module ) = @_;
unless ( $harness_module =~ m/::/ ) {
$harness_module = "Test::BDD::Cucumber::Harness::" . $harness_module;
}
eval { use_module($harness_module) }
|| die "Unable to load harness [$harness_module]: $@";
my $harness = $harness_module->new();
$harness->startup();
return $harness;
}
sub _process_arguments {
my ( $self, @args ) = @_;
local @ARGV = @args;
# Allow -Ilib, -bl
Getopt::Long::Configure('bundling', 'pass_through');
my $includes = [];
my $tags = [];
my $help = 0;
GetOptions(
'I=s@' => \$includes,
'l|lib' => \( my $add_lib ),
'b|blib' => \( my $add_blib ),
'o|output=s' => \( my $harness ),
't|tags=s@' => \$tags,
'i18n=s' => \( my $i18n ),
'h|help' => \$help,
);
pod2usage(
-verbose => 1,
-input => "$RealBin/$Script",
) if ($help);
if ($i18n) {
_print_langdef($i18n) unless $i18n eq 'help';
_print_languages();
}
unshift @$includes, 'lib' if $add_lib;
unshift @$includes, 'blib/lib', 'blib/arch' if $add_blib;
# Munge the output harness
$harness = "TermColor" unless defined $harness;
lib->import(@$includes) if @$includes;
# Store our TagSpecScheme
$self->tag_scheme( $self->_process_tags( @{$tags} ) );
return ( { harness => $harness }, @ARGV );
}
sub _process_tags {
my ( $self, @tags ) = @_;
# This is a bit faffy and possibly suboptimal.
my $tag_scheme = [];
my @ands = ();
# Iterate over our commandline tag strings.
foreach my $tag (@tags) {
my @parts = ();
foreach my $part ( split( ',', $tag ) ) {
# Trim any @ or ~@ from the front of the tag
$part =~ s/^(~?)@//;
# ~@tag => "NOT tag" => [ not => tag ]
if ( defined $1 and $1 eq '~' ) {
push @parts, [ not => $part ];
} else {
push @parts, $part;
}
}
# @tag,@cow => "@tag OR @cow" => [ or => tag, cow ]
# (It's simpler to always stick an 'or' on the front.)
push @ands, [ or => @parts ];
}
# -t @tag -t @cow => "@tag AND @cow" => [ and => tag, cow ]
# (It's simpler to always stick an 'and' on the front.)
$tag_scheme = [ and => @ands ];
return $tag_scheme;
}
sub _print_languages {
my @languages = languages();
my $max_code_length = max map { length } @languages;
my $max_name_length = max map { length( langdef($_)->{name} ) } @languages;
my $max_native_length =
max map { length( langdef($_)->{native} ) } @languages;
my $format =
"| %-${max_code_length}s | %-${max_name_length}s | %-${max_native_length}s |\n";
for my $language ( sort @languages ) {
my $langdef = langdef($language);
printf $format, $language, $langdef->{name}, $langdef->{native};
}
exit;
}
sub _print_langdef {
my ($language) = @_;
my $langdef = langdef($language);
my @keywords = qw(feature background scenario scenario_outline examples
given when then and but);
my $max_length =
max map { length readable_keywords( $langdef->{$_} ) } @keywords;
my $format = "| %-16s | %-${max_length}s |\n";
for my $keyword (
qw(feature background scenario scenario_outline
examples given when then and but )
)
{
printf $format, $keyword, readable_keywords( $langdef->{$keyword} );
}
my $codeformat = "| %-16s | %-${max_length}s |\n";
for my $keyword (qw(given when then )) {
printf $codeformat, $keyword . ' (code)',
readable_keywords( $langdef->{$keyword}, \&keyword_to_subname );
}
exit;
}
=head1 AUTHOR
Peter Sergeant C<pete@clueball.com>
=head1 LICENSE
Copyright 2011-2014, Peter Sergeant; Licensed under the same terms as Perl
=cut
1;