author | Johnson Ma <johnson.ma@nokia.com> |
Thu, 13 May 2010 17:42:48 +0800 | |
changeset 3 | a5f55a5789f3 |
parent 1 | bbd31066657e |
permissions | -rw-r--r-- |
0 | 1 |
# |
2 |
# Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 |
# All rights reserved. |
|
4 |
# This component and the accompanying materials are made available |
|
5 |
# under the terms of "Eclipse Public License v1.0" |
|
6 |
# which accompanies this distribution, and is available |
|
7 |
# at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 |
# |
|
9 |
# Initial Contributors: |
|
10 |
# Nokia Corporation - initial contribution. |
|
11 |
# |
|
12 |
# Contributors: |
|
13 |
# |
|
14 |
# Description: |
|
15 |
# |
|
16 |
||
17 |
#!/usr/bin/perl |
|
18 |
use strict; |
|
19 |
use warnings; |
|
20 |
use Getopt::Long; |
|
1
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
21 |
#use Cwd 'abs_path'; |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
22 |
use Cwd; |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
23 |
|
0 | 24 |
|
25 |
# Main |
|
26 |
my $root_dir = ""; |
|
27 |
GetOptions("dir=s" => \$root_dir); |
|
28 |
if ($root_dir eq "") { |
|
29 |
$root_dir = "."; |
|
30 |
} |
|
1
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
31 |
convert_cpp_in_directory($root_dir); |
0 | 32 |
convert_files_in_directory($root_dir); |
33 |
print "Conversion completed.\n"; |
|
34 |
||
1
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
35 |
sub convert_cpp_in_directory { |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
36 |
my $dir_name = shift; |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
37 |
print "Opening directory: " . $dir_name . "\n"; |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
38 |
opendir(DIR, $dir_name) || die "Cannot open directory " . $dir_name; |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
39 |
chdir($dir_name); |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
40 |
my @sourcefiles = readdir(DIR); |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
41 |
foreach (@sourcefiles) { |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
42 |
if ( $_ =~ /.cpp$/i ) { |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
43 |
# make the file writable (0666 is in linux/unix terms rw-) |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
44 |
chmod(0666,$_); |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
45 |
print "Converting: "; |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
46 |
print $_; |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
47 |
my $converted_file_content = ""; |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
48 |
if (/.cpp$/ ) { |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
49 |
$converted_file_content = convert_source_file_content($_); |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
50 |
} |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
51 |
open(my $result_file_handle, ">", $_) or die(". Writing " . $_ . " failed!\n"); |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
52 |
print $result_file_handle $converted_file_content; |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
53 |
close $result_file_handle; |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
54 |
print ". Done\n"; |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
55 |
} |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
56 |
elsif ( /\./ ) { |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
57 |
# Other types of files |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
58 |
} |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
59 |
else { |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
60 |
# Directories |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
61 |
convert_cpp_in_directory($_); |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
62 |
chdir(".."); # After recursion change back to the current directory |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
63 |
} |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
64 |
} |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
65 |
closedir DIR; |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
66 |
} |
0 | 67 |
|
68 |
sub convert_files_in_directory { |
|
69 |
my $dir_name = shift; |
|
70 |
print "Opening directory: " . $dir_name . "\n"; |
|
71 |
opendir(DIR, $dir_name) || die "Cannot open directory " . $dir_name; |
|
72 |
chdir($dir_name); |
|
73 |
my @sourcefiles = readdir(DIR); |
|
74 |
foreach (@sourcefiles) { |
|
1
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
75 |
if ( $_ =~ /.h$/i || $_ =~ /.mmp$/i || $_ =~ /.def$/i) { |
0 | 76 |
# make the file writable (0666 is in linux/unix terms rw-) |
77 |
chmod(0666,$_); |
|
78 |
print "Converting: "; |
|
79 |
print $_; |
|
80 |
my $converted_file_content = ""; |
|
1
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
81 |
if (/.h$/) { |
0 | 82 |
$converted_file_content = convert_source_file_content($_); |
83 |
} |
|
84 |
elsif (/.mmp$/) { |
|
85 |
$converted_file_content = convert_mmp_file_content($_); |
|
86 |
} |
|
87 |
else { |
|
88 |
$converted_file_content = convert_def_file_content($_); |
|
89 |
} |
|
1
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
90 |
open(my $result_file_handle, ">", $_) or die(". Writing " . $_ . " failed!\n"); |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
91 |
print $result_file_handle $converted_file_content; |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
92 |
close $result_file_handle; |
0 | 93 |
print ". Done\n"; |
94 |
} |
|
95 |
elsif ( /\./ ) { |
|
96 |
# Other types of files |
|
97 |
} |
|
98 |
else { |
|
99 |
# Directories |
|
100 |
convert_files_in_directory($_); |
|
101 |
chdir(".."); # After recursion change back to the current directory |
|
102 |
} |
|
103 |
} |
|
104 |
closedir DIR; |
|
105 |
} |
|
106 |
||
107 |
||
108 |
sub convert_source_file_content { |
|
109 |
my $file_name = shift; |
|
110 |
||
111 |
my $file_content = read_file_content_into_string($file_name); |
|
1
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
112 |
|
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
113 |
#check if this source include a separate test table header file |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
114 |
#in that case, we need to insert the test table content from header first |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
115 |
if ($file_content =~ m/\#include\s*\"(.*)testtable\.h\"/) { |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
116 |
my $curpath = cwd; |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
117 |
my $table_file_name = $curpath . "/../inc/" . $1 . "testtable.h"; |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
118 |
print "\n try to merge header file at: " . $table_file_name . "\n"; |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
119 |
my $tabledef = read_file_content_into_string($table_file_name); |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
120 |
#remove copyright and other comments |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
121 |
$tabledef =~ s/\/\/.*|\/\*[\s\S]*?\*\///g; |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
122 |
$tabledef =~ s/#include\s*\".*\"//g; |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
123 |
$file_content =~ s/\#include\s*\".*testtable\.h\"/$tabledef/g; |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
124 |
|
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
125 |
|
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
126 |
} |
0 | 127 |
|
128 |
# Convert the EUnit test table to SymbianUnit tests and move it to the constructor |
|
129 |
my $symbianunit_constructor_content = "BASE_CONSTRUCT"; |
|
130 |
my $converted_test_table = convert_eunit_test_table($file_content); |
|
1
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
131 |
#print "converted test table: " . $converted_test_table . "\n"; |
0 | 132 |
$symbianunit_constructor_content .= $converted_test_table; |
133 |
$file_content =~ s/CEUnitTestSuiteClass::ConstructL\(.*\)\;/$symbianunit_constructor_content/g; |
|
134 |
||
135 |
# Remove the EUnit test table |
|
136 |
$file_content =~ s/EUNIT_BEGIN_TEST_TABLE(.*)EUNIT_END_TEST_TABLE//ms; |
|
137 |
$file_content =~ s/\/\/ TEST TABLE//ms; |
|
138 |
||
139 |
# Do rest of the conversions |
|
140 |
$file_content =~ s/#include <eunitmacros.h>/#include <symbianunittestmacros.h>/gi; |
|
141 |
$file_content =~ s/#include <ceunittestsuiteclass.h>/#include <symbianunittest.h>/gi; |
|
142 |
$file_content =~ s/#include <ceunittestsuite.h>/#include <symbianunittestsuite.h>/gi; |
|
1
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
143 |
$file_content =~ s/#include <eunitdecorators.h>//gi; |
0 | 144 |
$file_content =~ s/CEUnitTestSuiteClass/CSymbianUnitTest/g; |
145 |
$file_content =~ s/CEUnitTestSuite/CSymbianUnitTestSuite/g; |
|
146 |
$file_content =~ s/MEUnitTest/MSymbianUnitTestInterface/g; |
|
147 |
$file_content =~ s/EUNIT_DECLARE_TEST_TABLE;//g; |
|
148 |
$file_content =~ s/EUNIT_ASSERT_SPECIFIC_LEAVE/SUT_ASSERT_LEAVE_WITH/g; |
|
149 |
$file_content =~ s/EUNIT_ASSERT_LEAVE/SUT_ASSERT_LEAVE/g; |
|
150 |
$file_content =~ s/EUNIT_ASSERT_EQUALS/SUT_ASSERT_EQUALS/g; |
|
151 |
$file_content =~ s/EUNIT_ASSERT_NO_LEAVE//g; |
|
152 |
$file_content =~ s/EUNIT_ASSERT/SUT_ASSERT/g; |
|
1
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
153 |
$file_content =~ s/EUNIT_ASSERT_DESC/SUT_ASSERT_DESC/g; |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
154 |
$file_content =~ s/EUNIT_ASSERT_EQUALS_DESC/SUT_ASSERT_EQUALS_DESC/g; |
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
155 |
$file_content =~ s/EUNIT_PRINT/\/\/EUNIT_PRINT/g; |
0 | 156 |
|
157 |
return $file_content; |
|
158 |
} |
|
159 |
||
160 |
||
161 |
sub convert_mmp_file_content { |
|
162 |
my $file_name = shift; |
|
163 |
||
164 |
my $file_content = read_file_content_into_string($file_name); |
|
165 |
||
166 |
$file_content =~ s/eunit.lib/symbianunittestfw.lib/gi; |
|
1
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
167 |
$file_content =~ s/eunitutil.lib//gi; |
0 | 168 |
$file_content =~ s/\/epoc32\/include\/Digia\/EUnit/\/epoc32\/include\/symbianunittest/gi; |
1
bbd31066657e
publish symbianunittest v1.1.0
Johnson Ma <johnson.ma@nokia.com>
parents:
0
diff
changeset
|
169 |
$file_content =~ s/\/epoc32\/include\/platform\/Digia\/EUnit/\/epoc32\/include\/symbianunittest/gi; |
0 | 170 |
$file_content =~ s/TARGETPATH(.*)\/DigiaEUnit\/Tests//gi; |
171 |
$file_content =~ s/UID(.*)0x1000af5a/MACRO SYMBIAN_UNIT_TEST\nUID 0x20022E76/gi; |
|
172 |
||
173 |
return $file_content; |
|
174 |
} |
|
175 |
||
176 |
||
177 |
sub convert_def_file_content { |
|
178 |
my $file_name = shift; |
|
179 |
||
180 |
my $file_content = read_file_content_into_string($file_name); |
|
181 |
||
182 |
$file_content =~ s/MEUnitTest/MSymbianUnitTestInterface/g; |
|
183 |
||
184 |
return $file_content; |
|
185 |
} |
|
186 |
||
187 |
||
188 |
sub read_file_content_into_string { |
|
189 |
my $file_name = shift; |
|
190 |
||
191 |
open(my $src_file_handle, "<", $file_name) or die("\nFile not found!\n"); |
|
192 |
my @file_content_array = <$src_file_handle>; |
|
193 |
my $file_content = ""; |
|
194 |
foreach (@file_content_array) { |
|
195 |
$file_content .= $_; |
|
196 |
} |
|
197 |
close $src_file_handle; |
|
198 |
||
199 |
return $file_content; |
|
200 |
} |
|
201 |
||
202 |
||
203 |
sub convert_eunit_test_table { |
|
204 |
my $file_content = shift; |
|
205 |
my $converted_test_table = "\n"; |
|
206 |
if ($file_content =~ /EUNIT_BEGIN_TEST_TABLE(.*)EUNIT_END_TEST_TABLE/ms) { |
|
207 |
my $test_table_content = $1; |
|
208 |
my @test_list = split(/EUNIT_TEST/, $test_table_content); |
|
209 |
shift @test_list; # Remove the test table parameters before the first test |
|
210 |
foreach (@test_list) { |
|
211 |
if ($_ =~ /\((.*)\)/ms) { |
|
212 |
$converted_test_table .= "\n"; |
|
213 |
$converted_test_table .= convert_eunit_test_entry($1); |
|
214 |
} |
|
215 |
} |
|
216 |
} |
|
217 |
return $converted_test_table; |
|
218 |
} |
|
219 |
||
220 |
||
221 |
sub convert_eunit_test_entry { |
|
222 |
# Parameters for EUNIT_TEST: |
|
223 |
# text1, text2, text3, text4, setupFunc, runFunc, teardownFunc |
|
224 |
my $eunit_test_parameters = shift; |
|
225 |
# Remove whitespaces, tabs and line breaks |
|
226 |
$eunit_test_parameters =~ s/\s//g; |
|
227 |
my @test_parameter_array = split(/,/, $eunit_test_parameters); |
|
228 |
my $result = ""; |
|
229 |
if (@test_parameter_array == 7) { |
|
230 |
if ($test_parameter_array[4] ne "SetupL" || |
|
231 |
$test_parameter_array[6] ne "Teardown") { |
|
232 |
# Non-default setup or teardown used |
|
233 |
$result = " ADD_SUT_WITH_SETUP_AND_TEARDOWN( "; |
|
234 |
$result .= $test_parameter_array[4]; |
|
235 |
$result .= ", "; |
|
236 |
$result .= $test_parameter_array[5]; |
|
237 |
$result .= ", "; |
|
238 |
$result .= $test_parameter_array[6]; |
|
239 |
$result .= " )"; |
|
240 |
} |
|
241 |
else { |
|
242 |
$result = " ADD_SUT( "; |
|
243 |
$result .= $test_parameter_array[5]; |
|
244 |
$result .= " )"; |
|
245 |
} |
|
246 |
} |
|
247 |
return $result; |
|
248 |
} |