forked from stereocat/Cisco-AccessList-Parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_tests.PL
executable file
·302 lines (254 loc) · 9.23 KB
/
generate_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
#!/usr/bin/perl
use strict;
use warnings;
use YAML;
use Path::Class;
use Data::Dumper;
sub generate_case {
my $field_data = shift;
my $fieldseq = shift;
my $test_field = shift @$fieldseq;
if ( !defined($test_field) ) {
return [
{ data => "",
valid => "1",
todo => undef
}
];
}
if ( !exists( $field_data->{$test_field} ) ) {
die "Unknown field name: $test_field";
}
# data by later fields was generated recursively.
my $later_test_case = generate_case( $field_data, $fieldseq );
my $result = [];
foreach my $field ( @{ $field_data->{$test_field} } ) {
my $curr_data = $field->{data};
my $curr_valid = $field->{valid};
my $curr_todo = $field->{todo};
my $curr_expected_msg
= exists( $field->{expected_msg} )
? $field->{expected_msg}
: undef;
my $curr_struct
= exists( $field->{struct} ) ? $field->{struct} : undef;
foreach my $later_field (@$later_test_case) {
my $later_data = $later_field->{data};
my $later_valid = $later_field->{valid};
my $later_todo = $later_field->{todo};
my $later_expected_msg
= exists( $later_field->{expected_msg} )
? $later_field->{expected_msg}
: undef;
my $later_struct
= exists( $later_field->{struct} )
? $later_field->{struct}
: undef;
my $data = join( q{ }, $curr_data, $later_data );
$data =~ s/\s+$//;
my $valid = $curr_valid * $later_valid;
my $todo = defined($curr_todo) ? $curr_todo : $later_todo;
# expected message
my $expected_msg = [];
push @$expected_msg, $curr_expected_msg
if ( defined($curr_expected_msg) );
push @$expected_msg, @$later_expected_msg
if ( defined($later_expected_msg) );
# struct
# print "# valid: $valid, ";
# print "curr: $test_field / $curr_data, data: $data\n";
# print "# curr_struct: $curr_struct\n";
# print "# later_struct: ", Dumper $later_struct, "\n";
my $struct = [];
if ( defined($curr_struct) && $curr_struct =~ /##entry##/ ) {
my $later_struct_str
= defined($later_struct)
? join( q{, }, @$later_struct )
: q{};
my $curr_struct_str = $curr_struct;
$curr_struct_str =~ s/##entry##/$later_struct_str/g;
push @$struct, $curr_struct_str;
# print "## later: ", $later_struct_str, "\n";
# print "## curr :", $curr_struct_str, "\n";
}
elsif ( defined($curr_struct) && $curr_struct =~ /##right##/ ) {
my $curr_struct_str = $curr_struct;
my $right_str = q{};
# print "## later_struct: ", Dumper $later_struct;
$right_str = shift @$later_struct
if(defined($later_struct) && @$later_struct > 0);
$curr_struct_str =~ s/##right##/$right_str/g;
# print "## curr :", $curr_struct_str, "\n";
# print "## right:", $right_str, "\n";
push @$struct, $curr_struct_str;
push @$struct, @$later_struct
if ( defined($later_struct) );
## the data will used for pattern generation
## by other current fields. DO NOT delete.
unshift @$later_struct, $right_str;
}
else {
push @$struct, $curr_struct
if ( defined($curr_struct) );
push @$struct, @$later_struct
if ( defined($later_struct) );
}
# test data construction
push(
@$result,
{ data => $data,
valid => $valid,
todo => $todo,
expected_msg => $expected_msg,
struct => $struct
}
);
}
}
return $result;
}
############################################################
sub generate_test_script {
my $code = shift;
my $field_data_dir = shift;
my $test_name = shift;
my $generate_test_script = shift;
my $test_script_file = file( $field_data_dir, $test_name . '.t' );
open my $tsfh, ">", $test_script_file
or die "Cannot open $test_script_file: $!";
print $tsfh $code;
foreach my $testcase (@$generate_test_script) {
my $data = $testcase->{'data'};
my $todo = defined( $testcase->{'todo'} ) ? $testcase->{'todo'} : "";
my $valid = $testcase->{'valid'};
my $expected_msg = join( q{,}, @{ $testcase->{'expected_msg'} } );
my $struct = $valid>0 ? join( q{}, @{ $testcase->{'struct'} } ) : "";
# print Dumper $struct;
my $record = <<"EOL";
===
--- data: $data
--- todo: $todo
--- valid: $valid
--- struct: $struct
--- expected_msg: [$expected_msg]
EOL
print $tsfh $record;
}
close $tsfh;
print "# Generate Test Script: $test_script_file\n";
}
############################################################
# read __DATA__ token; test running program
my $test_runner_code = do { local $/; <DATA> };
foreach my $test_conf_file (<t/*.conf>) {
my $test_conf_list = YAML::LoadFile($test_conf_file)
or die "Cannot Load Test Conf File: $test_conf_file";
print "# Load Test Conf File: $test_conf_file\n";
foreach my $test_case (@$test_conf_list) {
# data check
if ( !exists( $test_case->{testname} ) ) {
die "test case name: not specified in conf file: $test_conf_list";
}
if ( !exists( $test_case->{casedata} ) ) {
die "data file: not specified in conf file: $test_conf_file";
}
if ( !exists( $test_case->{fieldseq} ) ) {
die "fieldseq-data: not contained in conf file: $test_conf_file";
}
# data construction sequence data list
my $fieldseq = $test_case->{fieldseq};
# load data file (input)
my $field_data_file = $test_case->{casedata};
my $field_data_dir = file($test_conf_file)->parent;
my $field_data
= YAML::LoadFile( file( $field_data_dir, $field_data_file ) );
# generated test case
my $generated_test_case = generate_case( $field_data, $fieldseq );
# save generated test case to dat file
generate_test_script(
$test_runner_code, $field_data_dir,
$test_case->{testname}, $generated_test_case
);
}
}
############################################################
#
# This program generate test-case recursively.
#
# Fieldseq: A B C
# A1 B1 C1
# A2 B2
#
# 1st: C-[]
# => [C]
# 2nd: B1-[C], B2-[C]
# => [B1-C, B2-C]
# 3rd: A1-[B1-C, B2-C], A2-[B1-C, B2-C]
# => [A1-B1-C, A1-B2-C, A2-B1-C, A2-B2-C]
#
############################################################
__DATA__
#!/usr/bin/perl
use strict;
use warnings;
use Test::Base;
use Test::Warn;
use YAML::XS;
use Data::Dumper;
use Cisco::AccessList::Parser;
my $p = Cisco::AccessList::Parser->new();
our $TODO;
plan tests => 1 * blocks;
sub check_acl {
my $p = shift;
my $block = shift;
my $data = $block->data();
my $valid = $block->valid();
my $todo = $block->todo();
my ($acl, $objgrp, $errors);
if($valid > 0) {
my $str_hashref = YAML::XS::Load( $block->struct() );
( $acl, $objgrp ) = $p->parse( 'input' => $data );
#is( keys(%$acl) + keys(%$objgrp), $valid, $data );
my $got;
if ( defined($acl) ) {
$got = $acl;
}
else {
$got = $objgrp;
}
is_deeply( $got, $str_hashref, $data );
# print "## str: ", $block->struct(), "\n";
# print "## str: ", Dumper $str_hashref, "\n";
# print "## str-p: ", Dumper $acl, "\n";
}
else {
# print "## msg: ", $block->expected_msg(), "\n";
my $msg_hashref = YAML::XS::Load( $block->expected_msg() );
# If multiple error messages was generated,
# this test only certify a messages at first of them.
my $exist_err = shift @$msg_hashref;
warnings_exist {
( $acl, $objgrp, $errors ) = $p->parse( 'input' => $data );
if (@$errors) {
warn @$errors;
}
} [$exist_err], $data;
}
}
run {
my $block = shift;
my $data = $block->data();
my $valid = $block->valid();
my $todo = $block->todo();
if ( !( defined($todo) && length($todo) > 0 ) ) {
check_acl($p, $block);
}
else {
TODO: {
local $TODO = $todo;
check_acl($p, $block);
}
}
};
__DATA__