symbianunittestfw/tsrc/eunit_to_symbianunit.pl
branchRCL_3
changeset 3 9397a16b6eb8
parent 1 6edeef394eb7
equal deleted inserted replaced
1:6edeef394eb7 3:9397a16b6eb8
     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 #use Cwd 'abs_path';
       
    22 use Cwd;
       
    23 
       
    24 
       
    25 # Main
       
    26 my $root_dir = "";
       
    27 GetOptions("dir=s" => \$root_dir);
       
    28 if ($root_dir eq "") {
       
    29     $root_dir = ".";
       
    30 }
       
    31 convert_cpp_in_directory($root_dir);
       
    32 convert_files_in_directory($root_dir);
       
    33 print "Conversion completed.\n";
       
    34 
       
    35 sub convert_cpp_in_directory {
       
    36     my $dir_name = shift;
       
    37     print "Opening directory: " . $dir_name . "\n";
       
    38     opendir(DIR, $dir_name) || die "Cannot open directory " . $dir_name;
       
    39     chdir($dir_name);
       
    40     my @sourcefiles = readdir(DIR);
       
    41     foreach (@sourcefiles) {
       
    42         if ( $_ =~ /.cpp$/i ) {
       
    43             # make the file writable (0666 is in linux/unix terms rw-)
       
    44             chmod(0666,$_);
       
    45             print "Converting: ";
       
    46             print $_;
       
    47             my $converted_file_content = "";
       
    48             if (/.cpp$/ ) {
       
    49                 $converted_file_content = convert_source_file_content($_);
       
    50             }
       
    51 	    open(my $result_file_handle, ">", $_) or die(". Writing " . $_ . " failed!\n");
       
    52 	    print $result_file_handle $converted_file_content;
       
    53 	    close $result_file_handle;
       
    54             print ". Done\n";
       
    55         }
       
    56         elsif ( /\./ ) {
       
    57             # Other types of files
       
    58         }
       
    59         else {
       
    60             # Directories    
       
    61             convert_cpp_in_directory($_);
       
    62             chdir(".."); # After recursion change back to the current directory
       
    63         }
       
    64     }
       
    65     closedir DIR;
       
    66 }
       
    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) {
       
    75         if ( $_ =~ /.h$/i || $_ =~ /.mmp$/i || $_ =~ /.def$/i) {
       
    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 = "";
       
    81             if (/.h$/) {
       
    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             }
       
    90 	    open(my $result_file_handle, ">", $_) or die(". Writing " . $_ . " failed!\n");
       
    91 	    print $result_file_handle $converted_file_content;
       
    92 	    close $result_file_handle;
       
    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);
       
   112 
       
   113     #check if this source include a separate test table header file
       
   114     #in that case, we need to insert the test table content from header first
       
   115     if ($file_content =~ m/\#include\s*\"(.*)testtable\.h\"/) {
       
   116 	    my $curpath = cwd; 
       
   117             my $table_file_name = $curpath . "/../inc/" . $1 . "testtable.h";
       
   118 	    print "\n    try to merge header file at: " . $table_file_name . "\n";
       
   119             my $tabledef = read_file_content_into_string($table_file_name);
       
   120 	    #remove copyright and other comments
       
   121             $tabledef =~ s/\/\/.*|\/\*[\s\S]*?\*\///g;
       
   122             $tabledef =~ s/#include\s*\".*\"//g;
       
   123             $file_content =~ s/\#include\s*\".*testtable\.h\"/$tabledef/g;  
       
   124 
       
   125 
       
   126     }
       
   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);
       
   131     #print "converted test table: " . $converted_test_table . "\n";
       
   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;
       
   143     $file_content =~ s/#include <eunitdecorators.h>//gi;
       
   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;
       
   153     $file_content =~ s/EUNIT_ASSERT_DESC/SUT_ASSERT_DESC/g;
       
   154     $file_content =~ s/EUNIT_ASSERT_EQUALS_DESC/SUT_ASSERT_EQUALS_DESC/g;
       
   155     $file_content =~ s/EUNIT_PRINT/\/\/EUNIT_PRINT/g;
       
   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;
       
   167     $file_content =~ s/eunitutil.lib//gi;
       
   168     $file_content =~ s/\/epoc32\/include\/Digia\/EUnit//gi;
       
   169     $file_content =~ s/\/epoc32\/include\/platform\/Digia\/EUnit//gi;
       
   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 }