135daf339ed8f56aafd2e436632651bf0d9e4142
1 package Config
::Any
::WTScript
;
5 Config::Any::WTScript - Parse wtscript files.
9 use Config::Any::WTScript;
11 my $tests = Config::Any::WTScript->parse($data);
15 Parses a wtscript file and converts it to a set of test objects.
24 use vars
qw($VERSION @extensions);
26 use base 'Config::Any::Base';
28 use Text::Balanced qw(extract_codeblock extract_delimited);
29 use File
::Slurp
qw(read_file);
31 use constant ST_FILE
=> 0;
32 use constant ST_TEST_BLOCK
=> 1;
34 $VERSION = '0.001_002';
36 # horizontal space regexp
38 # sequence of any chars which doesn't contain ')', space chars and '=>'
39 my $reWORD = qr/(?: (?: [^=)\s] | [^)\s] (?!>) )+ )/x;
41 my $reCOMMENT = qr/(?: \s*? ^ \s* \# .* )+/mx;
43 =head2 extensions(;@extensions)
45 When list of extensions to accept given, replace current list with given list.
47 Return an array of valid extensions (default: C<wts>, C<wtscript>).
51 @extensions = qw(wts wtscript);
55 my ( $class, @new_ext ) = @_;
56 @new_ext and @extensions = @new_ext;
62 Parses wtscript text data passed in a scalar variable C<$data>.
66 A list of two elements - a reference to an array that contains test
67 objects and a reference to a hash that contains test parameters.
76 my $data = read_file
($filename);
78 my ( $tests, $opts ) = eval { _parse
($data) };
85 my $parse_pos = pos($data) || 0;
87 # find reminder of string near error (without surrounding
89 $data =~ /\G $reHS* (.*?) $reHS* $/gmx;
93 $near = 'at the end of line';
97 $near = "near '$near'";
101 my $line_num = () = substr( $data, 0, $parse_pos ) =~ m
|$|gmx
;
102 pos($data) = $parse_pos;
103 $line_num-- if $data =~ /\G \z/gx;
106 Config::WebTest: wtscript parsing error
107 Line $line_num $near: $exc
112 foreach my $test_item (@
$tests)
114 my %test = %$test_item;
120 check
=> { response
=> 200, },
122 my $agent_cfg = $cfg{request
}->{agent
};
124 defined( $test{user_agent
} ) and $agent_cfg->{agent
} = delete $test{user_agent
};
125 defined( $test{handle_redirects
} )
126 and $agent_cfg->{requests_redirectable
} = delete $test{handle_redirects
};
127 defined( $test{proxies
} ) and $agent_cfg->{proxy
} = { @
{ delete $test{proxies
} } };
128 defined( $agent_cfg->{requests_redirectable
} )
129 and looks_like_number
( $agent_cfg->{requests_redirectable
} )
130 and $agent_cfg->{requests_redirectable
} = [qw(GET POST)];
132 $cfg{request
}->{uri
} = delete $test{url
};
133 defined $test{method
} and $cfg{request
}->{method
} = lc delete $test{method
};
134 defined $test{http_headers
}
135 and $cfg{request
}->{http_headers
} = { @
{ delete $test{http_headers
} } };
137 $cfg{check
} = \
%test;
140 push( \
@configs, \
%cfg );
146 sub eval_in_playground
151 package Config::WebTest::PlayGround;
154 local \$^W; # aka no warnings in new perls
160 sub make_sub_in_playground
164 return eval_in_playground
("sub { local \$^W; $code }");
179 # eat whitespace and comments
180 $data =~ /\G $reCOMMENT /gcx;
183 $data =~ /\G \s+/gcx;
185 if ( $state == ST_FILE
)
187 if ( $data =~ /\G \z/gcx )
192 elsif ( $data =~ /\G test_name (?=\W)/gcx )
194 # found new test block start
196 $state = ST_TEST_BLOCK
;
198 # find test block name
199 if ( $data =~ /\G $reHS* = $reHS* (?: \n $reHS*)?/gcx )
201 $test->{test_name
} = _parse_scalar
($data);
203 die "Test name is missing\n"
204 unless defined $test->{test_name
};
209 # expect global test parameter
210 my ( $name, $value ) = _parse_param
($data);
214 _set_test_param
( $opts, $name, $value );
218 die "Global test parameter or test block is expected\n";
222 elsif ( $state == ST_TEST_BLOCK
)
224 if ( $data =~ /\G end_test (?=\W)/gcx )
231 # expect test parameter
232 my ( $name, $value ) = _parse_param
($data);
236 _set_test_param
( $test, $name, $value );
240 die "Test parameter or end_test is expected\n";
246 die "Unknown state\n";
250 return ( $tests, $opts );
259 if ( exists $href->{$name} )
261 $href->{$name} = [ $href->{$name} ]
262 if ref( $href->{$name} )
263 and ref( $href->{$name} ) eq 'ARRAY';
264 push @
{ $href->{$name} }, $value;
268 $href->{$name} = $value;
277 $_[0] =~ /\G
([a
-zA
-Z_
]+) # param name
278 $reHS* = $reHS* (?
: \n $reHS*)?
# = (and optional space chars)
289 my $value = _parse_value
( $_[0] );
290 return unless defined $value;
292 return ( $name, $value );
297 if ( $_[0] =~ /\G \(/gcx )
313 # eat whitespace and comments
314 $_[0] =~ /\G $reCOMMENT /gcx;
317 $_[0] =~ /\G \s+/gcx;
319 # exit loop on closing bracket
320 last if $_[0] =~ /\G \)/gcx;
322 my $value = _parse_value
( $_[0] );
324 die "Missing right bracket\n"
325 unless defined $value;
329 if ( $_[0] =~ /\G $reHS* => $reHS* /gcx )
331 # handles second part of scalar => scalar syntax
332 my $value = _parse_value
( $_[0] );
334 die "Missing right bracket\n"
335 unless defined $value;
346 return _parse_scalar
( $_[0] );
352 my $parse_pos = pos $_[0];
354 if ( $_[0] =~ /\G (['"])/gcx )
358 pos( $_[0] ) = $parse_pos;
359 my ($extracted) = extract_delimited
( $_[0] );
360 die "Can't find string terminator \"$delim\"\n"
363 if ( $delim eq "'" or $extracted !~ /[\$\@\%]/ )
365 # variable interpolation impossible - just evalute string
366 # to get rid of escape chars
367 my $ret = eval_in_playground
($extracted);
370 die "Eval error\n$@\n" if $@
;
376 # variable interpolation possible - evaluate as subroutine
377 # which will be used as callback
378 my $ret = make_sub_in_playground
($extracted);
381 die "Eval error\n$@\n" if $@
;
386 elsif ( $_[0] =~ /\G \{/gcx )
388 pos( $_[0] ) = $parse_pos;
389 my ($extracted) = extract_codeblock
( $_[0] );
390 die "Missing right curly bracket\n"
393 my $ret = make_sub_in_playground
($extracted);
396 die "Eval error\n$@\n" if $@
;
402 $_[0] =~ /\G ((?: $reWORD $reHS+ )* $reWORD )/gcxo;
412 Copyright (c) 2001-2003 Ilya Martynov. All rights reserved.
414 This program is free software; you can redistribute it and/or modify
415 it under the same terms as Perl itself.
419 L<HTTP::WebTest|HTTP::WebTest>
421 L<HTTP::WebTest::API|HTTP::WebTest::API>