-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path06_transformation.t
292 lines (232 loc) · 10.7 KB
/
06_transformation.t
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
#!perl
use 5.006001;
use strict;
use warnings;
use English qw< -no_match_vars >;
use File::Basename qw< basename >;
use File::Spec::Functions qw< catdir catfile >;
use PPI::Document q< >;
use PPI::Document::File q< >;
use Perl::ToPerl6::Utils qw< :characters >;
use Perl::ToPerl6::Transformation q< >;
use Test::More tests => 67;
#-----------------------------------------------------------------------------
use lib catdir( qw< t 06_transformation.d lib > );
use TransformationTest; # this is solely to test the import() method; has diagnostics
use TransformationTest2; # this is solely to test the import() method; no diagnostics
#-----------------------------------------------------------------------------
# method tests
{
can_ok('Perl::ToPerl6::Transformation', 'sort_by_location');
can_ok('Perl::ToPerl6::Transformation', 'sort_by_necessity');
can_ok('Perl::ToPerl6::Transformation', 'new');
can_ok('Perl::ToPerl6::Transformation', 'location');
can_ok('Perl::ToPerl6::Transformation', 'diagnostics');
can_ok('Perl::ToPerl6::Transformation', 'description');
can_ok('Perl::ToPerl6::Transformation', 'explanation');
can_ok('Perl::ToPerl6::Transformation', 'filename');
can_ok('Perl::ToPerl6::Transformation', 'source');
can_ok('Perl::ToPerl6::Transformation', 'transformer');
can_ok('Perl::ToPerl6::Transformation', 'get_format');
can_ok('Perl::ToPerl6::Transformation', 'set_format');
can_ok('Perl::ToPerl6::Transformation', 'to_string');
} # end scope block
#-----------------------------------------------------------------------------
# Constructor Failures:
{
eval { Perl::ToPerl6::Transformation->new('desc', 'expl'); };
ok($EVAL_ERROR, 'new, wrong number of args');
eval { Perl::ToPerl6::Transformation->new('desc', 'expl', {}, 'necessity'); };
ok($EVAL_ERROR, 'new, bad arg');
} # end scope block
#-----------------------------------------------------------------------------
# Accessor tests
{
my $pkg = __PACKAGE__;
my $code = 'Hello World;';
my $document = PPI::Document->new(\$code);
my $no_diagnostics_msg = qr/ \s* No [ ] diagnostics [ ] available \s* /xms;
my $viol = Perl::ToPerl6::Transformation->new( 'Foo', 'Bar', $document, 99, );
is( $viol->description(), 'Foo', 'description');
is( $viol->explanation(), 'Bar', 'explanation');
is( $viol->line_number(), 1, 'line_number');
is( $viol->logical_line_number(), 1, 'logical_line_number');
is( $viol->column_number(), 1, 'column_number');
is( $viol->visual_column_number(), 1, 'visual_column_number');
is( $viol->necessity(), 99, 'necessity');
is( $viol->source(), $code, 'source');
is( $viol->transformer(), $pkg, 'transformer');
is( $viol->element_class(), 'PPI::Document', 'element class');
like( $viol->diagnostics(), qr/ \A $no_diagnostics_msg \z /xms, 'diagnostics');
{
my $old_format = Perl::ToPerl6::Transformation::get_format();
Perl::ToPerl6::Transformation::set_format('%l,%c,%m,%e,%p,%d,%r');
my $expect = qr/\A 1,1,Foo,Bar,$pkg,$no_diagnostics_msg,\Q$code\E \z/xms;
like($viol->to_string(), $expect, 'to_string');
like("$viol", $expect, 'stringify');
Perl::ToPerl6::Transformation::set_format($old_format);
}
$viol = Perl::ToPerl6::Transformation->new('Foo', [28], $document, 99);
is($viol->explanation(), 'See page 28 of PBP', 'explanation');
$viol = Perl::ToPerl6::Transformation->new('Foo', [28,30], $document, 99);
is($viol->explanation(), 'See pages 28,30 of PBP', 'explanation');
} # end scope block
{
my $pkg = __PACKAGE__;
my $code = 'Say goodbye to the document;';
my $document = PPI::Document->new(\$code);
my $words = $document->find('PPI::Token::Word');
my $word = $words->[0];
my $no_diagnostics_msg = qr/ \s* No [ ] diagnostics [ ] available \s* /xms;
my $viol = Perl::ToPerl6::Transformation->new( 'Foo', 'Bar', $word, 99, );
# Make bye-bye with the document. This will end up stripping the guts out
# of the PPI::Token::Word instance, so it is useless to us after the
# document is gone. We need to make sure that we've copied the data out
# that we'll need.
undef $document;
undef $words;
undef $word;
is( $viol->description(), 'Foo', 'description after dropping document');
is( $viol->explanation(), 'Bar', 'explanation after dropping document');
is( $viol->line_number(), 1, 'line_number after dropping document');
is( $viol->logical_line_number(), 1, 'logical_line_number after dropping document');
is( $viol->column_number(), 1, 'column_number after dropping document');
is( $viol->visual_column_number(), 1, 'visual_column_number after dropping document');
is( $viol->necessity(), 99, 'necessity after dropping document');
is( $viol->source(), $code, 'source after dropping document');
is( $viol->transformer(), $pkg, 'transformer after dropping document');
is( $viol->element_class(), 'PPI::Token::Word', 'element class after dropping document');
like(
$viol->diagnostics(),
qr/ \A $no_diagnostics_msg \z /xms,
'diagnostics after dropping document',
);
} # end scope block
#-----------------------------------------------------------------------------
# Import tests
{
like(
TransformationTest->get_transformation()->diagnostics(),
qr/ \A \s* This [ ] is [ ] a [ ] test [ ] diagnostic [.] \s*\z /xms,
'import diagnostics',
);
} # end scope block
#-----------------------------------------------------------------------------
# Transformation sorting
SKIP: {
my $code = <<'END_PERL';
my $foo = 1; my $bar = 2;
my $baz = 3;
END_PERL
my $document = PPI::Document->new(\$code);
my @children = $document->schildren();
my @transformations =
map { Perl::ToPerl6::Transformation->new($EMPTY, $EMPTY, $_, 0) }
$document, @children;
my @sorted = Perl::ToPerl6::Transformation->sort_by_location( reverse @transformations);
is_deeply(\@sorted, \@transformations, 'sort_by_location');
my @severities = (5, 3, 4, 0, 2, 1);
@transformations =
map { Perl::ToPerl6::Transformation->new($EMPTY, $EMPTY, $document, $_) }
@severities;
@sorted = Perl::ToPerl6::Transformation->sort_by_necessity( @transformations );
is_deeply( [map {$_->necessity()} @sorted], [sort @severities], 'sort_by_necessity');
}
#-----------------------------------------------------------------------------
# Transformation formatting
{
my $format = '%l; %c; %m; %e; %s; %r; %P; %p; %d';
my $expected = join q{; }, (
1, 1, # line, col
'desc', 'expl',
1, # necessity
'print;', # source near token[0]
'Perl::ToPerl6::Transformer::Test', 'Test', # long, short
' diagnostic',
);
Perl::ToPerl6::Transformation::set_format($format);
is(Perl::ToPerl6::Transformation::get_format(), $format, 'set/get_format');
my $code = "print;\n";
my $document = PPI::Document->new(\$code);
$document->index_locations();
# my $p = Perl::ToPerl6::Transformer::Test->new();
my @t = $document->tokens();
# my $v = $p->transform($t[0]);
# ok($v, 'got a transformation');
# is($v->to_string(), $expected, 'to_string()');
}
#-----------------------------------------------------------------------------
# More formatting
{
# Alias subroutines, because I'm lazy
my $get_format = *Perl::ToPerl6::Transformation::get_format;
my $set_format = *Perl::ToPerl6::Transformation::set_format;
my $fmt_literal = 'Found %m in file %f on line %l\n'; ## no mogrify (RequireInterpolationOfMetachars)
my $fmt_interp = "Found %m in file %f on line %l\n"; #Same, but double-quotes
is($set_format->($fmt_literal), $fmt_interp, 'set_format by spec');
is($get_format->(), $fmt_interp, 'get_format by spec');
my $fmt_predefined = "%m at %f line %l\n";
is($set_format->(3), $fmt_predefined, 'set_format by number');
is($get_format->(), $fmt_predefined, 'get_format by number');
my $fmt_default = "%m at line %l, column %c. %e. (Necessity: %s)\n";
is($set_format->(999), $fmt_default, 'set_format by invalid number');
is($get_format->(), $fmt_default, 'get_format by invalid number');
is($set_format->(undef), $fmt_default, 'set_format with undef');
is($get_format->(), $fmt_default, 'get_format with undef');
}
#-----------------------------------------------------------------------------
{
my @given = ( qw(foo bar. .baz.. nuts!), [], {} );
my @want = ( qw(foo bar .baz nuts!), [], {} );
my @have = Perl::ToPerl6::Transformation::_chomp_periods(@given);
is_deeply(\@have, \@want, 'Chomping periods');
} # end scope block
#-----------------------------------------------------------------------------
{
my $filename = catfile( qw< t 06_transformation.d source Line.pm > );
my $document = PPI::Document::File->new($filename);
my @words = @{ $document->find('PPI::Token::Word') };
is(
(scalar @words),
2,
'Got the expected number of words in the line directive example document.',
);
my %expected = (
'%F' => basename($filename),
'%f' => $filename,
'%G' => basename($filename),
'%g' => $filename,
'%l' => '1',
'%L' => '1',
);
_test_file_and_line_formats($words[0], \%expected);
@expected{ qw< %F %f > } = ('Thingy.pm') x 2;
$expected{'%l'} = 57;
$expected{'%L'} = 3;
_test_file_and_line_formats($words[1], \%expected);
}
sub _test_file_and_line_formats {
my ($word, $expected) = @_;
my $transformation = Perl::ToPerl6::Transformation->new($EMPTY, $EMPTY, $word, 0);
foreach my $format ( sort keys %{$expected} ) {
Perl::ToPerl6::Transformation::set_format($format);
is(
$transformation->to_string(),
$expected->{$format},
"Got expected value for $format for " . $word->content(),
);
}
return;
}
#-----------------------------------------------------------------------------
# ensure we return true if this test is loaded by
# t/06_transformation.t_without_optional_dependencies.t
1;
# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# fill-column: 78
# indent-tabs-mode: nil
# c-indentation-style: bsd
# End:
# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :