-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild-tests.pl
307 lines (242 loc) · 8.16 KB
/
build-tests.pl
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
#-------------------------------------------------------------------------------
# Generates perl unit tests from the toml/json files in BurntSush/toml-test
# without having to add special casing to TOML::Tiny to conform to their
# annotated JSON format.
#-------------------------------------------------------------------------------
use strict;
use warnings;
no warnings 'experimental';
use v5.18;
use Data::Dumper;
use JSON::PP;
use File::Copy;
use File::Find;
use File::Spec;
# We want to read unicde as characters from toml-test source files. That makes
# things simpler for us when we parse them and generate perl source in the
# generated test file.
binmode STDIN, ':encoding(UTF-8)';
binmode STDOUT, ':encoding(UTF-8)';
sub slurp{
open my $fh, '<', $_[0] or die $!;
local $/;
<$fh>;
}
# Removes type annotations from BurntSushi/toml-test JSON files and returns the
# cleaned up data structure to which the associated TOML file should be parsed.
sub deturd_json{
state $json = JSON::PP->new->utf8(1);
my $annotated = $json->decode(slurp(shift));
my $cleanish = deannotate($annotated);
local $Data::Dumper::Varname = 'expected';
local $Data::Dumper::Deparse = 1;
local $Data::Dumper::Sortkeys = 1;
local $Data::Dumper::Useqq = 1;
return Dumper($cleanish);
}
# Recursively deannotates and inflates values from toml-test JSON data
# structures into a format more in line with TOML::Tiny's parser outout. For
# integer and float values, a Test2::Tools::Compare validator is generated to
# compare using Math::Big(Int|Float)->beq, since TOML's float and int types are
# 64 bits. Datetimes are converted to a common, normalized string format.
sub deannotate{
my $data = shift;
for (ref $data) {
when ('HASH') {
if (exists $data->{type} && exists $data->{value} && keys(%$data) == 2) {
for ($data->{type}) {
return $data->{value} eq 'true' ? 1 : 0 when /bool/;
return [ map{ deannotate($_) } @{$data->{value}} ] when /array/;
when (/integer/) {
my $src = qq{
use Test2::Tools::Compare qw(validator);
validator('Math::BigInt->new("$data->{value}")->beq(\$_)' => sub{
require Math::BigInt;
my \$got = Math::BigInt->new(\$_);
Math::BigInt->new("$data->{value}")->beq(\$got);
});
};
my $result = eval $src;
$@ && die $@;
return $result;
}
when (/float/) {
my $src;
if ($data->{value} eq 'nan') {
$src = qq{
use Test2::Tools::Compare qw(validator);
validator('Math::BigFloat->new(\$_)->is_nan' => sub{
require Math::BigFloat;
Math::BigFloat->new(\$_)->is_nan;
});
};
}
elsif ($data->{value} eq 'inf') {
$src = qq{
use Test2::Tools::Compare qw(validator);
validator('Math::BigFloat->new(\$_)->is_inf' => sub{
require Math::BigFloat;
Math::BigFloat->new(\$_)->is_inf;
});
};
}
elsif ($data->{value} eq '-inf') {
$src = qq{
use Test2::Tools::Compare qw(validator);
validator('Math::BigFloat->new(\$_)->is_inf' => sub{
require Math::BigFloat;
Math::BigFloat->new(\$_)->is_inf;
});
};
}
else {
$src = qq{
use Test2::Tools::Compare qw(validator);
validator('Math::BigFloat->new("$data->{value}")->beq(\$_)' => sub{
require Math::BigFloat;
my \$got = Math::BigFloat->new(\$_);
Math::BigFloat->new("$data->{value}")->beq(\$got);
});
};
}
my $result = eval $src;
$@ && die $@;
return $result;
}
default{ return $data->{value} }
}
}
my %object;
$object{$_} = deannotate($data->{$_}) for keys %$data;
return \%object;
}
when ('ARRAY') {
return [ map{ deannotate($_) } @$data ];
}
default{
return $data;
}
}
}
sub find_tests{
my $src = shift;
my %tests;
find {
no_chdir => 1,
wanted => sub {
return unless /\.toml$/;
my $abs = File::Spec->rel2abs( $_ );
my $rel = File::Spec->abs2rel( $abs, $src );
my $toml = $rel;
my $test = substr $rel, 0, -5;
$tests{$test} = $toml;
},
} => $src;
return %tests;
}
sub build_pospath_test_files{
my $src = shift;
my $dest = shift;
$src = "$src/tests/valid";
$dest = "$dest/t/toml-test/valid";
print "Generating positive path tests from $src\n";
my %TOML = find_tests( $src );
for (sort keys %TOML) {
copy("$src/$TOML{$_}", "$dest/$TOML{$_}");
my $json = substr( $TOML{$_}, 0, -4 ) . 'json';
my $data = deturd_json("$src/$json");
my $test = "$dest/$_.t";
my ( undef, $path ) = File::Spec->splitpath( $test );
unless (-f $test) {
system('mkdir', '-p', $path) == 0 || die $?;
}
open my $fh, '>', $test or die $!;
print $fh qq{# File automatically generated from BurntSushi/toml-test
use utf8;
use Test2::V0;
use Data::Dumper;
use Math::BigInt;
use Math::BigFloat;
use TOML::Tiny;
local \$Data::Dumper::Sortkeys = 1;
local \$Data::Dumper::Useqq = 1;
binmode STDIN, ':encoding(UTF-8)';
binmode STDOUT, ':encoding(UTF-8)';
open my \$fh, '<', "$dest/$TOML{$_}" or die \$!;
binmode \$fh, ':encoding(UTF-8)';
my \$toml = do{ local \$/; <\$fh>; };
close \$fh;
my $data
my \$actual = from_toml(\$toml);
is(\$actual, \$expected1, '$_ - from_toml') or do{
diag 'TOML INPUT:';
diag "\$toml";
diag '';
diag 'EXPECTED:';
diag Dumper(\$expected1);
diag '';
diag 'ACTUAL:';
diag Dumper(\$actual);
};
my \$regenerated = to_toml \$actual;
my \$reparsed = eval{ scalar from_toml \$regenerated };
my \$error = \$@;
ok(!\$error, '$_ - to_toml - no errors')
or diag \$error;
is(\$reparsed, \$expected1, '$_ - to_toml') or do{
diag "ERROR: \$error" if \$error;
diag '';
diag 'PARSED FROM TEST SOURCE TOML:';
diag Dumper(\$actual);
diag '';
diag 'REGENERATED TOML:';
diag \$regenerated;
diag '';
diag 'REPARSED FROM REGENERATED TOML:';
diag Dumper(\$reparsed);
};
done_testing;};
close $fh;
}
}
sub build_negpath_test_files{
my $src = shift;
my $dest = shift;
$src = "$src/tests/invalid";
$dest = "$dest/t/toml-test/invalid";
print "Generating negative path tests from $src\n";
my %TOML = find_tests( $src );
for (sort keys %TOML) {
copy("$src/$TOML{$_}", "$dest/$TOML{$_}");
my $test = "$dest/$_.t";
my ( undef, $path ) = File::Spec->splitpath( $test );
unless (-f $test) {
system('mkdir', '-p', $path) == 0 || die $?;
}
open my $fh, '>', $test or die $!;
print $fh qq{# File automatically generated from BurntSushi/toml-test
use utf8;
use Test2::V0;
use TOML::Tiny;
binmode STDIN, ':encoding(UTF-8)';
binmode STDOUT, ':encoding(UTF-8)';
open my \$fh, '<', "$dest/$TOML{$_}" or die \$!;
binmode \$fh, ':raw';
my \$toml = do{ local \$/; <\$fh>; };
close \$fh;
ok dies(sub{ scalar from_toml(\$toml, strict => 1) }), 'strict_mode dies on $_';
done_testing;};
close $fh;
}
}
my $usage = "usage: build-tests \$toml-test-repo-path \$toml-tiny-repo-path\n";
my $toml_test_path = shift @ARGV || die $usage;
my $toml_tiny_path = shift @ARGV || die $usage;
-d $toml_test_path || die "invalid path to BurntSushi/toml-test: $toml_test_path\n";
-d "$toml_test_path/tests" || die "invalid path to BurntSushi/toml-test: $toml_test_path\n";
-d $toml_tiny_path || die "invalid path to TOML::Tiny repo: $toml_tiny_path\n";
-d "$toml_tiny_path/t" || die "invalid path to TOML::Tiny repo: $toml_tiny_path\n";
print "Checking out master branch of BurntSushi/toml-test and pulling the latest commits\n";
system("cd $toml_test_path && git checkout master && git pull") == 0 || die $!;
build_pospath_test_files($toml_test_path, $toml_tiny_path);
build_negpath_test_files($toml_test_path, $toml_tiny_path);