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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
package Test::BDD::Cucumber::Parser;
$Test::BDD::Cucumber::Parser::VERSION = '0.26';
=head1 NAME
Test::BDD::Cucumber::Parser - Parse Feature files
=head1 VERSION
version 0.26
=head1 DESCRIPTION
Parse Feature files in to a set of data classes
=head1 SYNOPSIS
# Returns a Test::BDD::Cucumber::Model::Feature object
my $feature = Test::BDD::Cucumber::Parser->parse_file(
't/data/features/basic_parse.feature' );
=head1 METHODS
=head2 parse_string
=head2 parse_file
Both methods accept a single string as their argument, and return a
L<Test::BDD::Cucumber::Model::Feature> object on success.
=cut
use strict;
use warnings;
use File::Slurp;
use Test::BDD::Cucumber::Model::Document;
use Test::BDD::Cucumber::Model::Feature;
use Test::BDD::Cucumber::Model::Scenario;
use Test::BDD::Cucumber::Model::Step;
use Test::BDD::Cucumber::Model::TagSpec;
use Test::BDD::Cucumber::I18n qw(langdef);
use Test::BDD::Cucumber::Errors qw/parse_error_from_line/;
# https://github.com/cucumber/cucumber/wiki/Multiline-Step-Arguments
# https://github.com/cucumber/cucumber/wiki/Scenario-outlines
sub parse_string {
my ( $class, $string, $tag_scheme ) = @_;
return $class->_construct( Test::BDD::Cucumber::Model::Document->new({
content => $string
}), $tag_scheme );
}
sub parse_file {
my ( $class, $string, $tag_scheme) = @_;
return $class->_construct( Test::BDD::Cucumber::Model::Document->new({
content => scalar( read_file( $string, { binmode => ':utf8' } ) ),
filename => $string
}), $tag_scheme );
}
sub _construct {
my ( $class, $document, $tag_scheme ) = @_;
my $feature = Test::BDD::Cucumber::Model::Feature->new({ document => $document });
my @lines = $class->_remove_next_blanks( @{ $document->lines } );
$feature->language($class->_extract_language(\@lines));
my $self = { langdef =>
langdef($feature->language) };
bless $self, $class;
$self->_extract_scenarios(
$self->_extract_conditions_of_satisfaction(
$self->_extract_feature_name(
$feature, @lines
)));
return $feature;
}
sub _extract_language {
my ($self, $lines)=@_;
# return default language if we don't see the language directive on the first line
return 'en' unless $lines->[0]->raw_content =~ m{^\s*#\s*language:\s+(.+)$};
# remove the language directive if we saw it ...
shift @$lines;
# ... and return the language it declared
return $1;
}
sub _remove_next_blanks {
my ( $self, @lines ) = @_;
while ($lines[0] && $lines[0]->is_blank) {
shift( @lines );
}
return @lines;
}
sub _extract_feature_name {
my ( $self, $feature, @lines ) = @_;
my @feature_tags = ();
while ( my $line = shift( @lines ) ) {
next if $line->is_comment;
last if $line->is_blank;
if ( $line->content =~ m/^(?:$self->{langdef}->{feature}): (.+)/ ) {
$feature->name( $1 );
$feature->name_line( $line );
$feature->tags( \@feature_tags );
last;
# Feature-level tags
} elsif ( $line->content =~ m/^\s*\@\w/ ) {
my @tags = $line->content =~ m/\@([^\s]+)/g;
push( @feature_tags, @tags );
} else {
die parse_error_from_line( "Malformed feature line", $line );
}
}
return $feature, $self->_remove_next_blanks( @lines );
}
sub _extract_conditions_of_satisfaction {
my ( $self, $feature, @lines ) = @_;
while ( my $line = shift(@lines) ) {
next if $line->is_comment || $line->is_blank;
my $langdef = $self->{langdef};
if ( $line->content =~ m/^((?:$langdef->{background}):|(?:$langdef->{scenario}):|@)/) {
unshift( @lines, $line );
last;
} else {
push( @{ $feature->satisfaction }, $line );
}
}
return $feature, $self->_remove_next_blanks(@lines);
}
sub _extract_scenarios {
my ( $self, $feature, @lines ) = @_;
my $scenarios = 0;
my @scenario_tags;
while ( my $line = shift(@lines) ) {
next if $line->is_comment || $line->is_blank;
my $langdef = $self->{langdef};
if ( $line->content =~ m/^((?:$langdef->{background})|(?:$langdef->{scenario}))(?: Outline)?: ?(.+)?/) {
my ( $type, $name ) = ( $1, $2 );
# Only one background section, and it must be the first
if ( $scenarios++ && $type =~ m/^($langdef->{background})/ ) {
die parse_error_from_line( "Background not allowed after scenarios",
$line);
}
# Create the scenario
my $scenario = Test::BDD::Cucumber::Model::Scenario->new(
{
( $name ? ( name => $name ) : () ),
background => $type =~ m/^($langdef->{background})/ ? 1 : 0,
line => $line,
tags => [ @{ $feature->tags }, @scenario_tags ]
}
);
@scenario_tags = ();
# Attempt to populate it
@lines = $self->_extract_steps( $feature, $scenario, @lines );
if ( $type =~ m/^($langdef->{background})/ ) {
$feature->background($scenario);
} else {
push( @{ $feature->scenarios }, $scenario );
}
# Scenario-level tags
} elsif ( $line->content =~ m/^\s*\@\w/ ) {
my @tags = $line->content =~ m/\@([^\s]+)/g;
push( @scenario_tags, @tags );
} else {
die parse_error_from_line( "Malformed scenario line", $line );
}
}
return $feature, $self->_remove_next_blanks(@lines);
}
sub _extract_steps {
my ( $self, $feature, $scenario, @lines ) = @_;
my $langdef = $self->{langdef};
my @givens = split( /\|/, $langdef->{given} );
my $last_verb = $givens[-1];
while ( my $line = shift(@lines) ) {
next if $line->is_comment;
last if $line->is_blank;
# Conventional step?
if ( $line->content =~
m/^((?:$langdef->{given})|(?:$langdef->{and})|(?:$langdef->{when})|(?:$langdef->{then})|(?:$langdef->{but})) (.+)/
)
{
my ( $verb, $text ) = ( $1, $2 );
my $original_verb = $verb;
$verb = 'Given' if $verb =~ m/^($langdef->{given})$/;
$verb = 'When' if $verb =~ m/^($langdef->{when})$/;
$verb = 'Then' if $verb =~ m/^($langdef->{then})$/;
$verb = $last_verb
if $verb =~ m/^($langdef->{and})$/
or $verb =~ m/^($langdef->{but}$)/;
$last_verb = $verb;
my $step = Test::BDD::Cucumber::Model::Step->new(
{
text => $text,
verb => $verb,
line => $line,
verb_original => $original_verb,
}
);
@lines =
$self->_extract_step_data( $feature, $scenario, $step, @lines );
push( @{ $scenario->steps }, $step );
# Outline data block...
} elsif ( $line->content =~ m/^($langdef->{examples}):$/ ) {
return $self->_extract_table( 6, $scenario,
$self->_remove_next_blanks(@lines) );
} else {
die parse_error_from_line( "Malformed step line", $line );
}
}
return $self->_remove_next_blanks(@lines);
}
sub _extract_step_data {
my ( $self, $feature, $scenario, $step, @lines ) = @_;
return unless @lines;
if ( $lines[0]->content eq '"""' ) {
return $self->_extract_multiline_string(
$feature, $scenario, $step, @lines );
} elsif ( $lines[0]->content =~ m/^\s*\|/ ) {
return $self->_extract_table( 6, $step, @lines );
} else {
return @lines;
}
}
sub _extract_multiline_string {
my ( $self, $feature, $scenario, $step, @lines ) = @_;
my $data = '';
my $start = shift( @lines );
my $indent = $start->indent;
# Check we still have the minimum indentation
while ( my $line = shift( @lines ) ) {
if ( $line->content eq '"""' ) {
$step->data( $data );
return $self->_remove_next_blanks( @lines );
}
my $content = $line->content_remove_indentation( $indent );
# Unescape it
$content =~ s/\\(.)/$1/g;
push( @{ $step->data_as_strings }, $content );
$content .= "\n";
$data .= $content;
}
return;
}
sub _extract_table {
my ( $self, $indent, $target, @lines ) = @_;
my @columns;
my $data = [];
$target->data($data);
while ( my $line = shift( @lines ) ) {
next if $line->is_comment;
return ($line, @lines) if index( $line->content, '|' );
my @rows = $self->_pipe_array( $line->content );
if ( $target->can('data_as_strings') ) {
my $t_content = $line->content;
$t_content =~ s/^\s+//;
push( @{ $target->data_as_strings }, $t_content );
}
if ( @columns ) {
die parse_error_from_line( "Inconsistent number of rows in table", $line )
unless @rows == @columns;
$target->columns( [ @columns ] ) if $target->can('columns');
my $i = 0;
my %data_hash = map { $columns[$i++] => $_ } @rows;
push( @$data, \%data_hash );
} else {
@columns = @rows;
}
}
return;
}
sub _pipe_array {
my ( $self, $string ) = @_;
my @atoms = split(/\|/, $string);
shift( @atoms );
return map { my $atom = $_; $atom =~ s/^\s+//; $atom =~ s/\s+$//; $atom } @atoms;
}
1;
=head1 AUTHOR
Peter Sergeant C<pete@clueball.com>
=head1 LICENSE
Copyright 2011-2014, Peter Sergeant; Licensed under the same terms as Perl
=cut
|