lib/VSGDR/UnitTest/mergeGDRTests.pl untested

filter: Statementstmt 0.0% 0 / 197 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 15 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 291 SCAR 6.9
CC
1
Cov
0%
END
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/bin/perl
2  
3 use strict;
4 use warnings;
5 use autodie qw(:all);
6 # throw exceptions on their use
7 no indirect ':fatal';
8 use 5.010;
9  
10  
11 use Carp;
12 use Text::Diff ;
13  
14 #DONE: 1. Add support to merging tests with compatible init/teardown sections
15 #TODO: 1. Add support to merging tests with compatible init/teardown conditions
16  
17 use VSGDR::UnitTest::TestSet::Test;
18 use VSGDR::UnitTest::TestSet::Test::TestCondition;
19 use VSGDR::UnitTest::TestSet::Representation;
20 use VSGDR::UnitTest::TestSet::Resx;
21  
22 our @opt_infile;
23 our $opt_outfile;
24 our $opt_version;
25 our $opt_classname;
26 our $opt_namespace;
27  
28 use Getopt::Euclid qw( :vars<opt_> );
29 use List::MoreUtils qw{firstidx} ;
30 use version ; our $VERSION = qv('1.3.3');
31 use Data::Dumper;
32 use File::Basename;
33 use Smart::Comments;
34  
35 my %ValidParserMakeArgs = ( vb => "NET::VB"
36 , cs => "NET::CS"
37 , xls => "XLS"
38 , xml => "XML"
39 ) ;
40 my %ValidParserMakeArgs2 = ( vb => "NET2::VB"
41 , cs => "NET2::CS"
42 ) ;
43
44 #my @validSuffixes = keys %ValidParserMakeArgs ;
45 my @validSuffixes = map { '.'.$_ } keys %ValidParserMakeArgs ;
46  
47  
48 my $version = $opt_version ;
49  
50  
51 my @infiles = @opt_infile ;
52 my $outFile = $opt_outfile ;
53  
54 my($outfname, $outdirectories, $outsfx) = fileparse($outFile, @validSuffixes);
55 croak 'Invalid output file' unless defined $outsfx ;
56  
57 $outsfx = substr(lc $outsfx,1) ;
58
59 my $outpfx = $outfname;
60  
61 my $classname = undef ;
62 $classname = $opt_classname if defined $opt_classname ;
63 $classname = $outpfx unless defined $classname ;
64  
65 my $namespace = undef ;
66 $namespace = $opt_namespace if defined $opt_namespace ;
67 $namespace = $outpfx unless defined $namespace ;
68  
69  
70 croak 'Invalid output file' unless exists $ValidParserMakeArgs{$outsfx} ;
71 my $outResxFile = "${outpfx}.resx" ;
72  
73 ### check output files can be written to
74 # yes so it's a race-condition anyway
75  
76 croak 'Output file cannot be written to' unless -f $outFile or ! -e $outFile ;
77 croak 'Output resource file cannot be written to' unless -f $outResxFile or ! -e $outResxFile ;
78  
79 my @cleanupScripts = () ;
80 my @initScripts = () ;
81  
82  
83 my %Parsers = () ;
84 # if output is needed in ssdt unit test format add in a .net2 parser to the list
85 if ($version == 1) {
86 $Parsers{${outsfx}} = VSGDR::UnitTest::TestSet::Representation->make( { TYPE => $ValidParserMakeArgs{${outsfx}} } );
87 }
88 else {
89 $Parsers{"${outsfx}2"} = VSGDR::UnitTest::TestSet::Representation->make( { TYPE => $ValidParserMakeArgs2{${outsfx}} } );
90 }
91  
92 my @testSets = () ;
93 my %test_testScripts = () ;
94  
95 ### Creating output files
96  
97 my $o_resx = VSGDR::UnitTest::TestSet::Resx->new() ;
98 for my $testFile (@infiles) { ### testfile .. done
99  
100 my($infname, $indirectories, $insfx) = fileparse($testFile, @validSuffixes);
101 croak 'Invalid input file' unless $insfx;
102
103 $insfx = substr(lc $insfx,1) ;
104  
105 croak 'Invalid input file' unless exists $ValidParserMakeArgs{$insfx} ;
106
107 $Parsers{${insfx}} = VSGDR::UnitTest::TestSet::Representation->make( { TYPE => $ValidParserMakeArgs{${insfx}} } )
108 unless exists $Parsers{${insfx}} ;
109 # if input is in a .net language, add in a .net2 parser to the list
110 if ( firstidx { $_ eq ${insfx} } ['cs','vb'] != -1 ) {
111 $Parsers{"${insfx}2"} = VSGDR::UnitTest::TestSet::Representation->make( { TYPE => $ValidParserMakeArgs2{${insfx}} } )
112 unless exists $Parsers{"${insfx}2"} ;
113 }
114  
115 ### Parsing test file
116  
117 my $testSet = undef ;
118  
119 eval {
120 $testSet = $Parsers{$insfx}->deserialise($testFile);
121 } ;
122 if ( not defined $testSet ) {
123 if ( exists $Parsers{"${insfx}2"}) {
124 eval {
125 $testSet = $Parsers{"${insfx}2"}->deserialise($testFile);
126 }
127 }
128 else {
129 croak 'Parsing failed.';
130 }
131 }
132  
133
134 push @testSets, $testSet ;
135 $testSet = undef ;
136  
137 # my $resx_data = ''; { $/ = undef ; open my $AA, "<", "${inpfx}.resx"; $resx_data = <$AA> ; close $AA ; } ;
138 # my $rh_testScripts = $o_resx->parse($resx_data) ;
139  
140 ### Parsing script file
141  
142 my $inpfx = $infname ;
143 my $rh_testScripts = $o_resx->deserialise("${inpfx}.resx") ;
144  
145 push @cleanupScripts, $rh_testScripts->{testCleanupAction} if exists $rh_testScripts->{testCleanupAction} ;
146 push @initScripts, $rh_testScripts->{testInitializeAction} if exists $rh_testScripts->{testInitializeAction} ;
147  
148 #warn Dumper $rh_testScripts ;
149 #warn Dumper keys %$rh_testScripts ;
150  
151 map { $test_testScripts{$_} = $$rh_testScripts{$_} } keys %{$rh_testScripts} ;
152  
153 }
154  
155 #warn Dumper keys %test_testScripts ;
156 #warn Dumper scalar @testSets ;
157 my @cleanupActions = grep { defined($_->cleanupAction()) } @testSets ;
158 my @initActions = grep { defined($_->initializeAction()) } @testSets ;
159  
160 my %testNames = () ;
161 my %conditionNames = () ;
162  
163  
164 my %lc_testNames = () ;
165 my %lc_conditionNames = () ;
166  
167 #exit;
168  
169 croak 'Some tests have initialisation and/or cleanup actions, and some do not. The tests cannot be merged.'
170 if ( scalar @cleanupActions and ( scalar @testSets != scalar @cleanupActions ) )
171 or ( scalar @initActions and ( scalar @testSets != scalar @initActions ) ) ;
172  
173  
174 if (@testSets > 1) {
175  
176 if ( scalar(@initScripts) ) {
177 my $firstInitSQL = $initScripts[0] ;
178 my $fi = $firstInitSQL;
179 $fi =~ s{\s+}{\ }xgmsi ;
180 shift @initScripts ;
181  
182 local $_ = undef ;
183 while ( my $sc = shift @initScripts) {
184 $sc =~ s{\s+}{\ }xgmsi ;
185 croak 'Different initialisation scripts exist, Tests cannot be merged.'
186 if diff(\$sc,\$fi) ;
187 }
188 }
189  
190 if (scalar(@cleanupScripts)) {
191 my $firstCleanupSQL = $cleanupScripts[0] ;
192 my $fc = $firstCleanupSQL;
193 $fc =~ s{\s+}{[ ]}xgmsi ;
194 shift @cleanupScripts ;
195  
196 while ( $_ = shift @cleanupScripts) {
197 $_ =~ s{\s+}{[ ]}xgmsi ;
198 croak 'Different cleanup scripts exist, Tests cannot be merged.'
199 if diff(\$_,\$fc) ;
200 }
201 }
202 } ;
203  
204  
205 ### Pulling out test information
206  
207  
208 foreach my $testSet (@testSets) { ### testset .. done
209 my $ra_tests = $testSet->tests() ;
210 my @t = map { my $s=$_->testName() ; $testNames{$s}++ ; $lc_testNames{lc($s)}++ ; $s ; }
211 @$ra_tests ;
212 foreach my $test (@$ra_tests) { ### test .. done
213 my $ra_conditions = $test->conditions() ;
214 my @c = map { my $s=$_->conditionName();$conditionNames{$s}++ ; $lc_conditionNames{lc($s)}++ ; $s ; }
215 @$ra_conditions ;
216 }
217 } ;
218  
219  
220 my @dupTestNames = grep { $lc_testNames{$_} > 1 } keys %lc_testNames ;
221 my @dupConditionNames = grep { $lc_conditionNames{$_} > 1 } keys %lc_conditionNames ;
222  
223  
224 ### Checking for clashes
225  
226 {
227 local $" =", ";
228 croak "Duplicate Test names exist:- @dupTestNames, Tests cannot be merged."
229 if scalar @dupTestNames ;
230 croak "Duplicate Condition names exist: @dupConditionNames, Tests cannot be merged."
231 if scalar @dupConditionNames ;
232 }
233 my $mergedTestSet = VSGDR::UnitTest::TestSet->new( { NAMESPACE => $namespace
234 , CLASSNAME => $classname
235 }
236 ) ;
237 my @testA = () ;
238 foreach my $testSet (@testSets) {
239 my $ra_tests = $testSet->tests() ;
240 push @testA,@$ra_tests ;
241  
242 } ;
243  
244 ### Building merged test set
245  
246 $mergedTestSet->initializeConditions([]) ;
247 $mergedTestSet->cleanupConditions([]) ;
248 $mergedTestSet->initializeAction($mergedTestSet->initializeActionLiteral()) if scalar @initActions ;
249 $mergedTestSet->cleanupAction($mergedTestSet->cleanupActionLiteral()) if scalar @cleanupActions ;
250 $mergedTestSet->tests(\@testA) ;
251  
252 ### Serialising parser
253  
254 if ($version == 1) {
255 $Parsers{$outsfx}->serialise($opt_outfile,$mergedTestSet);
256 }
257 else {
258 $Parsers{"${outsfx}2"}->serialise($opt_outfile,$mergedTestSet);
259 }
260  
261  
262 ### Cloning scripts
263  
264 my $o_resx_clone = $o_resx->clone() ;
265 $o_resx_clone->scripts(\%test_testScripts);
266 unlink $outResxFile if -f $outResxFile ;
267  
268 ### Serialising scripts
269  
270 $o_resx_clone->serialise($outResxFile,$o_resx_clone);
271  
272 exit ;
273  
274 END {}
275  
276 __DATA__