|
| 1 | +#!/usr/bin/perl |
| 2 | +use Cwd; |
| 3 | + |
| 4 | +##################################################### |
| 5 | +# A script to automate creation of a new unit test |
| 6 | +# Authors TSutton |
| 7 | +# April 08, 2006 |
| 8 | +##################################################### |
| 9 | +# $Id: plugin_builder.pl 5212 2006-04-07 23:21:38Z timlinux $ # |
| 10 | + |
| 11 | +#make sure we are in a the tests/src/raster dir |
| 12 | +$myDir = fastgetcwd; |
| 13 | +$sourceDir = "../../../src/raster"; |
| 14 | +print "\n\nChecking that we are in the <qgis dir>/test/src/raster/ directory...."; |
| 15 | +if ($myDir =~ m/tests\/src\/raster$/) |
| 16 | +{ |
| 17 | + print "yes\n"; |
| 18 | +} |
| 19 | +else |
| 20 | +{ |
| 21 | + print "no\n"; |
| 22 | + print $myDir; |
| 23 | + print "\nPlease relocate to the /test/src/raster/ directory before attempting to run this script.\n"; |
| 24 | + exit; |
| 25 | +} |
| 26 | +# get the needed information from the user |
| 27 | +$testClass=""; |
| 28 | +$argCount = $#ARGV+1; |
| 29 | +if ($argCount > 0) |
| 30 | +{ |
| 31 | + $testClass=@ARGV[ 0 ]; |
| 32 | +} |
| 33 | +else |
| 34 | +{ |
| 35 | + print "\n\nEnter the name of the class for which the test will be created.\n"; |
| 36 | + print "Used mixed case notation.\n"; |
| 37 | + print "e.g. QgsSymbol\n"; |
| 38 | + $testClass =<STDIN>; |
| 39 | + chop $testClass; |
| 40 | +} |
| 41 | + |
| 42 | +$testClassLowerCaseName = lc($testClass); #todo convert to lower case |
| 43 | + |
| 44 | +# |
| 45 | +# Check source file is ok |
| 46 | +# |
| 47 | + |
| 48 | +if ($testClass eq "") |
| 49 | +{ |
| 50 | + print "ClassName not supplied ...exiting..."; |
| 51 | + exit; |
| 52 | +} |
| 53 | +print "Checking if source class exists in filesystem ..."; |
| 54 | +if (-e "${sourceDir}/${testClassLowerCaseName}.cpp" ) |
| 55 | +{ |
| 56 | + print "yes\n"; |
| 57 | +} |
| 58 | +else |
| 59 | +{ |
| 60 | + print "no, exiting\n"; |
| 61 | + print "${sourceDir}/${testClassLowerCaseName}.cpp does not exist!\n"; |
| 62 | + exit; |
| 63 | +} |
| 64 | + |
| 65 | + |
| 66 | +print "Stubs will be created for the following methods:\n"; |
| 67 | +open CPPFILE, "<$sourceDir/$testClassLowerCaseName.cpp"|| die 'Unable to open header file $testClassLowerCaseName.cpp'; |
| 68 | +$stubString=""; |
| 69 | +$lastLine=""; |
| 70 | +while(<CPPFILE>) |
| 71 | +{ |
| 72 | + if(m/${testClass}::[A-Za-z0-9]*\(/) |
| 73 | + { |
| 74 | + #get the matched part of the line |
| 75 | + $line = $&; |
| 76 | + #strip off the :: |
| 77 | + $line =~ s/:://g; |
| 78 | + #strip off the ( |
| 79 | + $line =~ s/\(//g; |
| 80 | + if ($lastLine eq $line) |
| 81 | + { |
| 82 | + #duplicate entry |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + #add it to our stub code |
| 87 | + $stubString = $stubString . " void $line()\n\{\n\n\};\n"; |
| 88 | + #show the user the list |
| 89 | + print $line; |
| 90 | + print "\n"; |
| 91 | + } |
| 92 | + $lastLine=$line; |
| 93 | + } |
| 94 | +} |
| 95 | +$createIt="n"; |
| 96 | +if ($argCount eq 0) |
| 97 | +{ |
| 98 | + print "-----------------------------\n"; |
| 99 | + print "Create the unit test? [y/n]: "; |
| 100 | + $createIt = <STDIN>; |
| 101 | + chop $createIt; |
| 102 | +} |
| 103 | +else |
| 104 | +{ |
| 105 | + $createIt="y"; |
| 106 | +} |
| 107 | + |
| 108 | +if(($createIt eq 'y') || ($createIt eq 'Y')) |
| 109 | +{ |
| 110 | + # |
| 111 | + # its a go -- create the unit test and modify the build files |
| 112 | + # |
| 113 | + system("cp test_template.cpp test$testClassLowerCaseName.cpp"); |
| 114 | + |
| 115 | + # Substitute the class name in the file |
| 116 | + system("perl -pi -e 's/\\\[testClassLowerCaseName\\\]/$testClassLowerCaseName/g' test$testClassLowerCaseName.cpp"); |
| 117 | + system("perl -pi -e 's/\\\[testClassCamelCaseName\\\]/$testClass/g' test$testClassLowerCaseName.cpp"); |
| 118 | + system("perl -pi -e 's/\\\[TestMethods\\\]/$stubString/g' test$testClassLowerCaseName.cpp"); |
| 119 | + # Add an entry to Makefile.am |
| 120 | + open MAKEFILE, "<./Makefile.am" || die 'Unable to open Makefile.am'; |
| 121 | + open MAKEFILEMOD, ">./Makefile.am.mod" || die 'Unable to create Makefile.am.mod'; |
| 122 | + # read through Makefile.am and write each line to Makefile.am.mod |
| 123 | + while(<MAKEFILE>) |
| 124 | + { |
| 125 | + if(/^\s*bin_PROGRAMS =*/) |
| 126 | + { |
| 127 | + # add our application binary name to the next line |
| 128 | + print MAKEFILEMOD; |
| 129 | + print MAKEFILEMOD "\t\ttest$testClassLowerCaseName \\\n"; |
| 130 | + } |
| 131 | + elsif(/^\s*BUILT_SOURCES =*/) |
| 132 | + { |
| 133 | + # add our application binary name to the next line |
| 134 | + print MAKEFILEMOD; |
| 135 | + print MAKEFILEMOD "\t\t\$(test${testClassLowerCaseName}_MOC) \\\n"; |
| 136 | + } |
| 137 | + else |
| 138 | + { |
| 139 | + print MAKEFILEMOD; |
| 140 | + } |
| 141 | + } |
| 142 | + #before closing the file add the lines for our new test class |
| 143 | + print MAKEFILEMOD "\n"; |
| 144 | + print MAKEFILEMOD "test${testClassLowerCaseName}_MOC = test${testClassLowerCaseName}.moc.cpp\n"; |
| 145 | + print MAKEFILEMOD "test${testClassLowerCaseName}_SOURCES = test${testClassLowerCaseName}.cpp\n"; |
| 146 | + print MAKEFILEMOD "test${testClassLowerCaseName}_LDADD = \$(GLOBALLDADD)\n"; |
| 147 | + print MAKEFILEMOD "test${testClassLowerCaseName}_CXXFLAGS = \$(GLOBALCXXFLAGS)\n"; |
| 148 | + |
| 149 | + # close the Makefile file handles |
| 150 | + close MAKEFILEMOD; |
| 151 | + close MAKEFILE; |
| 152 | + |
| 153 | + # save Makefile.am in case we die before done moving things around |
| 154 | + system("mv Makefile.am Makefile.am.save"); |
| 155 | + # move the new Makefile.am to where it belongs |
| 156 | + system("mv Makefile.am.mod Makefile.am"); |
| 157 | + # delete the original Makefile.am |
| 158 | + unlink("Makefile.am.save"); |
| 159 | + |
| 160 | + |
| 161 | + print << "EOP"; |
| 162 | +
|
| 163 | +Your test unit has been created now as ${testClassLowerCaseName}.cpp. |
| 164 | +
|
| 165 | +EOP |
| 166 | + |
| 167 | +} |
| 168 | +else |
| 169 | +{ |
| 170 | + # user cancelled |
| 171 | + print "Test unit not created\n"; |
| 172 | +} |
| 173 | + |
0 commit comments