|
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; |
|
21 |
|
22 # Main |
|
23 my $root_dir = ""; |
|
24 GetOptions("dir=s" => \$root_dir); |
|
25 if ($root_dir eq "") { |
|
26 $root_dir = "."; |
|
27 } |
|
28 convert_files_in_directory($root_dir); |
|
29 print "Conversion completed.\n"; |
|
30 |
|
31 |
|
32 sub convert_files_in_directory { |
|
33 my $dir_name = shift; |
|
34 print "Opening directory: " . $dir_name . "\n"; |
|
35 opendir(DIR, $dir_name) || die "Cannot open directory " . $dir_name; |
|
36 chdir($dir_name); |
|
37 my @sourcefiles = readdir(DIR); |
|
38 foreach (@sourcefiles) { |
|
39 if ( $_ =~ /.cpp$/i || $_ =~ /.h$/i || $_ =~ /.mmp$/i || $_ =~ /.def$/i) { |
|
40 # make the file writable (0666 is in linux/unix terms rw-) |
|
41 chmod(0666,$_); |
|
42 print "Converting: "; |
|
43 print $_; |
|
44 my $converted_file_content = ""; |
|
45 if (/.cpp$/ || /.h$/) { |
|
46 $converted_file_content = convert_source_file_content($_); |
|
47 } |
|
48 elsif (/.mmp$/) { |
|
49 $converted_file_content = convert_mmp_file_content($_); |
|
50 } |
|
51 else { |
|
52 $converted_file_content = convert_def_file_content($_); |
|
53 } |
|
54 open(my $result_file_handle, ">", $_) or die(". Writing " . $_ . " failed!\n"); |
|
55 print $result_file_handle $converted_file_content; |
|
56 close $result_file_handle; |
|
57 print ". Done\n"; |
|
58 } |
|
59 elsif ( /\./ ) { |
|
60 # Other types of files |
|
61 } |
|
62 else { |
|
63 # Directories |
|
64 convert_files_in_directory($_); |
|
65 chdir(".."); # After recursion change back to the current directory |
|
66 } |
|
67 } |
|
68 closedir DIR; |
|
69 } |
|
70 |
|
71 |
|
72 sub convert_source_file_content { |
|
73 my $file_name = shift; |
|
74 |
|
75 my $file_content = read_file_content_into_string($file_name); |
|
76 |
|
77 # Convert the EUnit test table to SymbianUnit tests and move it to the constructor |
|
78 my $symbianunit_constructor_content = "BASE_CONSTRUCT"; |
|
79 my $converted_test_table = convert_eunit_test_table($file_content); |
|
80 $symbianunit_constructor_content .= $converted_test_table; |
|
81 $file_content =~ s/CEUnitTestSuiteClass::ConstructL\(.*\)\;/$symbianunit_constructor_content/g; |
|
82 |
|
83 # Remove the EUnit test table |
|
84 $file_content =~ s/EUNIT_BEGIN_TEST_TABLE(.*)EUNIT_END_TEST_TABLE//ms; |
|
85 $file_content =~ s/\/\/ TEST TABLE//ms; |
|
86 |
|
87 # Do rest of the conversions |
|
88 $file_content =~ s/#include <eunitmacros.h>/#include <symbianunittestmacros.h>/gi; |
|
89 $file_content =~ s/#include <ceunittestsuiteclass.h>/#include <symbianunittest.h>/gi; |
|
90 $file_content =~ s/#include <ceunittestsuite.h>/#include <symbianunittestsuite.h>/gi; |
|
91 $file_content =~ s/CEUnitTestSuiteClass/CSymbianUnitTest/g; |
|
92 $file_content =~ s/CEUnitTestSuite/CSymbianUnitTestSuite/g; |
|
93 $file_content =~ s/MEUnitTest/MSymbianUnitTestInterface/g; |
|
94 $file_content =~ s/EUNIT_DECLARE_TEST_TABLE;//g; |
|
95 $file_content =~ s/EUNIT_ASSERT_SPECIFIC_LEAVE/SUT_ASSERT_LEAVE_WITH/g; |
|
96 $file_content =~ s/EUNIT_ASSERT_LEAVE/SUT_ASSERT_LEAVE/g; |
|
97 $file_content =~ s/EUNIT_ASSERT_EQUALS/SUT_ASSERT_EQUALS/g; |
|
98 $file_content =~ s/EUNIT_ASSERT_NO_LEAVE//g; |
|
99 $file_content =~ s/EUNIT_ASSERT/SUT_ASSERT/g; |
|
100 |
|
101 return $file_content; |
|
102 } |
|
103 |
|
104 |
|
105 sub convert_mmp_file_content { |
|
106 my $file_name = shift; |
|
107 |
|
108 my $file_content = read_file_content_into_string($file_name); |
|
109 |
|
110 $file_content =~ s/eunit.lib/symbianunittestfw.lib/gi; |
|
111 $file_content =~ s/\/epoc32\/include\/Digia\/EUnit/\/epoc32\/include\/symbianunittest/gi; |
|
112 $file_content =~ s/TARGETPATH(.*)\/DigiaEUnit\/Tests//gi; |
|
113 $file_content =~ s/UID(.*)0x1000af5a/MACRO SYMBIAN_UNIT_TEST\nUID 0x20022E76/gi; |
|
114 |
|
115 return $file_content; |
|
116 } |
|
117 |
|
118 |
|
119 sub convert_def_file_content { |
|
120 my $file_name = shift; |
|
121 |
|
122 my $file_content = read_file_content_into_string($file_name); |
|
123 |
|
124 $file_content =~ s/MEUnitTest/MSymbianUnitTestInterface/g; |
|
125 |
|
126 return $file_content; |
|
127 } |
|
128 |
|
129 |
|
130 sub read_file_content_into_string { |
|
131 my $file_name = shift; |
|
132 |
|
133 open(my $src_file_handle, "<", $file_name) or die("\nFile not found!\n"); |
|
134 my @file_content_array = <$src_file_handle>; |
|
135 my $file_content = ""; |
|
136 foreach (@file_content_array) { |
|
137 $file_content .= $_; |
|
138 } |
|
139 close $src_file_handle; |
|
140 |
|
141 return $file_content; |
|
142 } |
|
143 |
|
144 |
|
145 sub convert_eunit_test_table { |
|
146 my $file_content = shift; |
|
147 my $converted_test_table = "\n"; |
|
148 if ($file_content =~ /EUNIT_BEGIN_TEST_TABLE(.*)EUNIT_END_TEST_TABLE/ms) { |
|
149 my $test_table_content = $1; |
|
150 my @test_list = split(/EUNIT_TEST/, $test_table_content); |
|
151 shift @test_list; # Remove the test table parameters before the first test |
|
152 foreach (@test_list) { |
|
153 if ($_ =~ /\((.*)\)/ms) { |
|
154 $converted_test_table .= "\n"; |
|
155 $converted_test_table .= convert_eunit_test_entry($1); |
|
156 } |
|
157 } |
|
158 } |
|
159 return $converted_test_table; |
|
160 } |
|
161 |
|
162 |
|
163 sub convert_eunit_test_entry { |
|
164 # Parameters for EUNIT_TEST: |
|
165 # text1, text2, text3, text4, setupFunc, runFunc, teardownFunc |
|
166 my $eunit_test_parameters = shift; |
|
167 # Remove whitespaces, tabs and line breaks |
|
168 $eunit_test_parameters =~ s/\s//g; |
|
169 my @test_parameter_array = split(/,/, $eunit_test_parameters); |
|
170 my $result = ""; |
|
171 if (@test_parameter_array == 7) { |
|
172 if ($test_parameter_array[4] ne "SetupL" || |
|
173 $test_parameter_array[6] ne "Teardown") { |
|
174 # Non-default setup or teardown used |
|
175 $result = " ADD_SUT_WITH_SETUP_AND_TEARDOWN( "; |
|
176 $result .= $test_parameter_array[4]; |
|
177 $result .= ", "; |
|
178 $result .= $test_parameter_array[5]; |
|
179 $result .= ", "; |
|
180 $result .= $test_parameter_array[6]; |
|
181 $result .= " )"; |
|
182 } |
|
183 else { |
|
184 $result = " ADD_SUT( "; |
|
185 $result .= $test_parameter_array[5]; |
|
186 $result .= " )"; |
|
187 } |
|
188 } |
|
189 return $result; |
|
190 } |