Skip to content

Commit 208b4de

Browse files
author
timlinux
committed
First draft at a little perl script to generate a test class with method stubs automatically
git-svn-id: http://svn.osgeo.org/qgis/trunk@5220 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 735cb36 commit 208b4de

File tree

2 files changed

+111
-2
lines changed

2 files changed

+111
-2
lines changed

tests/src/core/Makefile.am

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ GLOBALCXXFLAGS = $(CXXFLAGS) $(EXTRA_CXXFLAGS) $(GDAL_CFLAGS) $(QT_CXXFLAGS) $(P
2424
%.moc.cpp: %.cpp
2525
$(MOC) -o $@ $<
2626

27+
BUILT_SOURCES = $(testqgsapplication_MOC)
28+
29+
CLEANFILES = $(BUILT_SOURCES)
30+
2731
#
2832
# Specify the compilation files for each unit test now
2933
#
@@ -33,6 +37,4 @@ testqgsapplication_SOURCES = testqgsapplication.cpp
3337
testqgsapplication_LDADD = $(GLOBALLDADD)
3438
testqgsapplication_CXXFLAGS = $(GLOBALCXXFLAGS)
3539

36-
BUILT_SOURCES = $(testqgsapplication_MOC)
3740

38-
CLEANFILES = $(BUILT_SOURCES)

tests/src/core/test_builder.pl

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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/core dir
12+
$myDir = fastgetcwd;
13+
print "\n\nChecking that we are in the <qgis dir>/test/src/core/ directory....";
14+
if ($myDir =~ m/tests\/src\/core$/)
15+
{
16+
print "yes\n";
17+
}
18+
else
19+
{
20+
print "no\n";
21+
print $myDir;
22+
print "\nPlease relocate to the /test/src/core/ directory before attempting to run this script.\n";
23+
exit;
24+
}
25+
# get the needed information from the user
26+
print "\n\nEnter the name of the class for which the test will be created.\n";
27+
print "Used mixed case notation.\n";
28+
print "e.g. QgsSymbol\n";
29+
$testClass =<STDIN>;
30+
chop $testClass;
31+
$testClassLowerCaseName = lc($testClass); #todo convert to lower case
32+
print "Create the unit test? [y/n]: ";
33+
$createIt = <STDIN>;
34+
chop $createIt;
35+
36+
if(($createIt eq 'y') || ($createIt eq 'Y'))
37+
{
38+
#
39+
# its a go -- create the unit test and modify the build files
40+
#
41+
system("cp test_template.cpp test$testClassLowerCaseName.cpp");
42+
43+
# Substitute the class name in the file
44+
system("perl -pi -e 's/\\\[testClassLowerCaseName\\\]/$testClassLowerCaseName/g' test$testClassLowerCaseName.cpp");
45+
system("perl -pi -e 's/\\\[testClassCamelCaseName\\\]/$testClass/g' test$testClassLowerCaseName.cpp");
46+
#
47+
# TODO: write a parser to pull out method signatures from class bing tested and create a stub
48+
# for each method in the generated test class to replace teh [TestMethods] placeholder
49+
#
50+
51+
# Add an entry to Makefile.am
52+
open MAKEFILE, "<./Makefile.am" || die 'Unable to open Makefile.am';
53+
open MAKEFILEMOD, ">./Makefile.am.mod" || die 'Unable to create Makefile.am.mod';
54+
# read through Makefile.am and write each line to Makefile.am.mod
55+
while(<MAKEFILE>)
56+
{
57+
if(/^\s*bin_PROGRAMS =*/)
58+
{
59+
# add our application binary name to the next line
60+
print MAKEFILEMOD "\\";
61+
print MAKEFILEMOD "\t\t$testClassLowerCaseName \n";
62+
}
63+
else
64+
{
65+
print MAKEFILEMOD;
66+
}
67+
if(/^\s*BUILT_SOURCES =*/)
68+
{
69+
# add our application binary name to the next line
70+
print MAKEFILEMOD "\\";
71+
print MAKEFILEMOD "\t\t${testClassLowerCaseName}_MOC \n";
72+
}
73+
else
74+
{
75+
print MAKEFILEMOD;
76+
}
77+
}
78+
#before closing the file add the lines for our new test class
79+
print MAKEFILEMOD "test${testClassLowerCaseName}_MOC = test${testClassLowerCaseName}.moc.cpp"
80+
print MAKEFILEMOD "test${testClassLowerCaseName}_SOURCES = ${testClassLowerCaseName}.cpp"
81+
print MAKEFILEMOD "test${testClassLowerCaseName}_LDADD = $(GLOBALLDADD)"
82+
print MAKEFILEMOD "test${testClassLowerCaseName}_CXXFLAGS = $(GLOBALCXXFLAGS)"
83+
84+
# close the Makefile file handles
85+
close MAKEFILEMOD;
86+
close MAKEFILE;
87+
88+
# save Makefile.am in case we die before done moving things around
89+
system("mv Makefile.am Makefile.am.save");
90+
# move the new Makefile.am to where it belongs
91+
system("mv Makefile.am.mod Makefile.am");
92+
# delete the original Makefile.am
93+
unlink("Makefile.am.save");
94+
95+
}
96+
97+
print << "EOP";
98+
99+
Your test unit has been created now as testClassLowerCaseName.cpp.
100+
101+
EOP
102+
103+
}else{
104+
# user cancelled
105+
print "Test unit not created\n";
106+
}
107+

0 commit comments

Comments
 (0)