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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
|
package Test::Synopsis::Expectation;
use 5.008005;
use strict;
use warnings;
use parent qw/Test::Builder::Module/;
my @test_more_exports;
BEGIN { @test_more_exports = (qw/done_testing/) }
use PPI::Tokenizer;
use ExtUtils::Manifest qw/maniread/;
use Test::More import => \@test_more_exports;
use Test::Synopsis::Expectation::Pod;
our $VERSION = "0.12";
our @EXPORT = (@test_more_exports, qw/all_synopsis_ok synopsis_ok/);
my $prepared = '';
my $ignorings = [];
sub prepare {
$prepared = shift;
}
sub set_ignorings {
$ignorings = shift;
$ignorings = [$ignorings] if ref $ignorings ne 'ARRAY';
return $ignorings;
}
sub all_synopsis_ok {
my $builder = __PACKAGE__->builder;
my @files = _list_up_files_from_manifest($builder);
for my $file (@files) {
_synopsis_ok($file);
}
}
sub synopsis_ok {
my ($files) = @_;
$files = [$files] if ref $files ne 'ARRAY';
for my $file (@$files) {
_synopsis_ok($file);
}
}
sub _synopsis_ok {
my ($file) = @_;
local $Test::Builder::Level = $Test::Builder::Level + 1;
my $parser = Test::Synopsis::Expectation::Pod->new;
$parser->parse_file($file);
my $block_num = 1;
for my $target_code (@{$parser->{target_codes}}) {
my ($expectations, $code) = _analyze_target_code($target_code);
_check_syntax($code, $block_num, $file);
for my $expectation (@$expectations) {
_check_with_expectation($expectation, $block_num, $file);
}
$block_num++;
}
}
sub _check_syntax {
package Test::Synopsis::Expectation::Sandbox;
eval $_[0]; ## no critic
if ($@) {
Test::More::fail("Syntax OK: $_[2] (SYNOPSIS Block: $_[1])");
}
else {
Test::More::pass("Syntax OK: $_[2] (SYNOPSIS Block: $_[1])");
}
}
sub _check_with_expectation {
package Test::Synopsis::Expectation::Sandbox;
# $_[0] is expectation
my $got = eval $_[0]->{code}; ## no critic
my $expected = eval $_[0]->{expected}; ## no critic
my $method = $_[0]->{method};
my $test_name = "$_[2] (SYNOPSIS Block: $_[1], Line: $_[0]->{line_num})";
if ($method eq 'is') {
Test::More::is($got, $expected, $test_name);
} elsif ($method eq 'isa') {
Test::More::isa_ok($got, $expected, $test_name);
} elsif ($method eq 'like') {
Test::More::like($got, $expected, $test_name);
} elsif ($method eq 'is_deeply') {
Test::More::is_deeply($got, $expected, $test_name);
} elsif ($method eq 'success') {
Test::More::ok($got, $test_name);
}
}
sub _analyze_target_code {
my ($target_code) = @_;
my $deficient_brace = 0;
my $code = $prepared || ''; # code for test
my @expectations; # store expectations for test
my $line_num = 1;
for my $line (split /\n\r?/, $target_code) {
my $tokens = PPI::Tokenizer->new(\$line)->all_tokens;
if (grep {$_->{content} eq '...' && $_->isa('PPI::Token::Operator')} @$tokens) {
next;
}
for my $ignoring (@$ignorings) {
$line =~ s/\Q$ignoring\E//g;
}
$code .= "$line\n";
# Count the number of left braces to complete deficient right braces
$deficient_brace++ if (grep {$_->{content} eq '{' && $_->isa('PPI::Token::Structure')} @$tokens);
$deficient_brace-- if (grep {$_->{content} eq '}' && $_->isa('PPI::Token::Structure')} @$tokens);
# Extract comment statement
# Tokens contain one comment token on a line, at the most
if (my ($comment) = grep {$_->isa('PPI::Token::Comment')} @$tokens) {
# Accept special comment for this module
# e.g.
# # => is 42
my ($expectation) = $comment->{content} =~ /#\s*=>\s*(.+)/;
next unless $expectation;
# Accept test methods as string
my $method;
if ($expectation =~ s/^(?:(is|isa|is_deeply|like)\s|(success))//) {
$method = $1 || $2;
}
push @expectations, +{
'method' => $method || 'is',
'expected' => $expectation,
'code' => $code . ('}' x $deficient_brace),
'line_num' => $line_num,
};
}
$line_num++;
}
return (\@expectations, $code);
}
sub _list_up_files_from_manifest {
my ($builder) = @_;
my $manifest = $ExtUtils::Manifest::MANIFEST;
if ( not -f $manifest ) {
$builder->plan( skip_all => "$manifest doesn't exist" );
}
return grep { m!\Alib/.*\.pm\Z! } keys %{ maniread() };
}
1;
__END__
=encoding utf-8
=for stopwords isa yada-yada
=head1 NAME
Test::Synopsis::Expectation - Test that SYNOPSIS code produces expected results
=head1 SYNOPSIS
use Test::Synopsis::Expectation;
synopsis_ok('eg/sample.pod');
done_testing;
Following, SYNOPSIS of F<eg/sample.pod>
=for test_synopsis_expectation_no_test
my $num;
$num = 1; # => 1
++$num; # => is 2
use PPI::Tokenizer;
my $tokenizer = PPI::Tokenizer->new(\'code'); # => isa 'PPI::Tokenizer'
my $str = 'Hello, I love you'; # => like qr/ove/
my $obj = {
foo => ["bar", "baz"],
}; # => is_deeply { foo => ["bar", "baz"] }
my $bool = 1; # => success
=head1 DESCRIPTION
This module checks that a module's SYNOPSIS section is syntactically correct,
and will also check that it produces the expected results,
based on annotations you add in comments.
=head1 FUNCTIONS
=over 4
=item * synopsis_ok($files)
This function tests SYNOPSIS codes of each files.
This function expects file names as an argument as ARRAYREF or SCALAR.
(This function is exported)
=item * all_synopsis_ok()
This function tests SYNOPSIS codes of the all of library files.
This function uses F<MANIFEST> to list up the target files of testing.
(This function is exported)
=item * prepare($code_str)
Register the executable codes to prepare for evaluation.
If you use like;
use Test::Synopsis::Expectation;
Test::Synopsis::Expectation::prepare('my $foo = 1;');
synopsis_ok('path/to/target.pm');
done_testing;
### Following, SYNOPSIS of `target.pm`
$foo; # => 1
Then, SYNOPSIS of F<target.pm> is the same as;
my $foo = 1;
$foo; # => 1
(This function is not exported)
=item * set_ignorings
Set the procedures which would like to ignore.
use Test::Synopsis::Expectation;
Test::Synopsis::Expectation::set_ignorings(['++$num;']);
synopsis_ok(*DATA);
done_testing;
__DATA__
=head1 SYNOPSIS
my $num;
$num = 1; # => 1
++$num;
$num; # => 1
In the above example, C<++$num;> will be ignored.
=back
=head1 NOTATION OF EXPECTATION
Comment that starts at C<# =E<gt>> then this module treats the comment as test statement.
=over 4
=item * # => is
my $foo = 1; # => is 1
This way is equivalent to the next.
my $foo = 1;
is $foo, 1;
This carries out the same behavior as C<Test::More::is>.
=item * # =>
my $foo = 1; # => 1
This notation is the same as C<# =E<gt> is>
=item * # => isa
use Foo::Bar;
my $instance = Foo::Bar->new; # => isa 'Foo::Bar'
This way is equivalent to the next.
use Foo::Bar;
my $instance = Foo::Bar->new;
isa_ok $instance, 'Foo::Bar';
This carries out the same behavior as C<Test::More::isa_ok>.
=item * # => like
my $str = 'Hello, I love you'; # => like qr/ove/
This way is equivalent to the next.
my $str = 'Hello, I love you';
like $str, qr/ove/;
This carries out the same behavior as C<Test::More::like>.
=item * # => is_deeply
my $obj = {
foo => ["bar", "baz"],
}; # => is_deeply { foo => ["bar", "baz"] }
This way is equivalent to the next.
my $obj = {
foo => ["bar", "baz"],
};
is_deeply $obj, { foo => ["bar", "baz"] };
This carries out the same behavior as C<Test::More::is_deeply>.
=item * # => success
my $bool = 1;
$bool; # => success
This way checks value as boolean.
If target value of testing is 0 then this test will fail. Otherwise, it will pass.
=back
=head1 ANNOTATIONS
=over 4
=item * =for test_synopsis_expectation_no_test
The code block behind this annotation will not be tested.
my $sum;
$sum = 1; # => 1
=for test_synopsis_expectation_no_test
my $sum;
$sum = 1; # => 2
In this example, the first code block will be tested, but the second will not.
=back
=head1 RESTRICTION
=head2 Test case must be one line
The following is valid;
my $obj = {
foo => ["bar", "baz"],
}; # => is_deeply { foo => ["bar", "baz"] }
However, the following is invalid;
my $obj = {
foo => ["bar", "baz"],
}; # => is_deeply {
# foo => ["bar", "baz"]
# }
So test case must be one line.
=head2 Not put test cases inside of for(each)
# Example of not working
for (1..10) {
my $foo = $_; # => 10
}
This example doesn't work. On the contrary, it will be error (Probably nobody uses such as this way... I think).
=head1 NOTES
=head2 yada-yada operator
This module ignores yada-yada operators that is in SYNOPSIS code.
Thus, following code is runnable.
my $foo;
...
$foo = 1; # => 1
=head1 SEE ALSO
L<Test::Synopsis> - simpler module, which just checks the syntax of your SYNOPSIS section.
L<Dist::Zilla::Plugin::Test::Synopsis> - a plugin for L<Dist::Zilla> users, which adds a release test
to your distribution, based on L<Test::Synopsis>.
=head1 REPOSITORY
L<https://github.com/moznion/Test-Synopsis-Expectation>
=head1 LICENSE
Copyright (C) moznion.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 AUTHOR
moznion E<lt>moznion@gmail.comE<gt>
=cut
|