[go: up one dir, main page]

Menu

[80c337]: / tests / create_tests  Maximize  Restore  History

Download this file

174 lines (145 with data), 4.3 kB

#!/bin/sh

# Did they pass any arguments?  If not, teach them how to use this script.
if [ $# -eq 0 -o $# -gt 3 ]
then
    echo Usage: $0 classname \[makeargs \[qmakeargs\]\]
    echo Creates a directory test_classname, places the code for a unit test in that
    echo directory, makes the unit test, and runs it.
    echo Assumes that classname.\{h,cpp\} exist and define a class to test.
    echo The makeargs parameter \(if given\) will be passed to make.  Similar for qmakeargs.
    echo To pass qmakeargs and not makeargs, use $0 classname \"\" qmakeargs
    exit 1
fi
# Gather all parameters:
CLASS=$1
if [ $# -gt 1 ]
then
    MAKEARGS=$2
else
    MAKEARGS=""
fi
if [ $# -gt 2 ]
then
    QMAKEARGS=$3
else
    QMAKEARGS=""
fi
echo Running create_test on class $CLASS with make arguments $MAKEARGS and qmake arguments $QMAKEARGS

# Did they pass 

mkdir test_$CLASS
cd test_$CLASS

cat <<EOT >test_$CLASS.cpp

#include <QtTest>
#include "$CLASS.h"

/** \brief A Qt unit test of everything defined in $CLASS.h.
 *  
 *  It aims to test both member functions of classes and non-member functions, whatever is
 *  defined in $CLASS.h and implemented in $CLASS.cpp.
 *  This page therefore links to $CLASS.h according to the convetion described in the page
 *  \link docconv Conventions in the Lurch Documentation\endlink.
 *
 *  According to the Qt unit testing framework's design, all public slots in this
 *  class will be run when the executable file generated from compiling this file is run.
 */
class test_$CLASS : public QObject
{
    Q_OBJECT

private slots:

    /** A dummy test; change this documentation when you've made it a real test routine.
     *  Here you would explain what this routine is testing and why.
     *  Be sure to mention explicitly what routines from $CLASS.h this function is testing,
     *  and then go to those routines and link to this one as the test function for them.
     */
    void test_1 ();

};

void test_$CLASS::test_1 ()
{
    // replace this with creation of some objects,
    // performance of some computations,
    // and then a call like this one:
    QCOMPARE( 1+1, 2 ); // actual, expected
}

QTEST_MAIN(test_$CLASS)

#include "test_$CLASS.moc"

EOT

cat <<EOT >run_test.sh
#!/bin/sh

MAKEARGS=""
QMAKEARGS=""
if [ \$# -ge 1 ]
then
    MAKEARGS=\$1
    if [ \$# -ge 2 ]
    then
        QMAKEARGS=\$2
        if [ \$# -ge 3 ]
        then
            echo Too many arguments to \$0
            echo Usage: \$0 \[makeargs \[qmakeargs\]\]
            echo \(You can use \"\" for makeargs.\)
            exit 1
        fi
    fi
fi

if [ ! -f Makefile ]
then
qmake \$QMAKEARGS
fi

if make \$MAKEARGS
then
    ./test_$CLASS
else
    echo "************************************************"
    echo COULD NOT RUN ./test_$CLASS BECAUSE MAKE FAILED!
    echo "************************************************"
    exit 1
fi

EOT

chmod 700 run_test.sh

echo -----------------------------------------------
echo -
echo -  This script created a directory test_$CLASS
echo -  and placed in that directory the file
echo -  test_$CLASS.cpp.  It will now create a corresp-
echo -  onding .pro file, run qmake, make, and the
echo -  resulting executable: just one dummy test.
echo -
echo -  It also created a link to the documentation
echo -  for the file $CLASS.h, to explain that it is
echo -  testing the classes and functions in that
echo -  file.
echo -
echo -  If this is not what you wanted, delete the
echo -  directory test_$CLASS and all this will go away.
echo -
echo -----------------------------------------------

cat <<EOT >test_$CLASS.pro

CONFIG += qtestlib
macx:CONFIG -= app_bundle
win32:CONFIG += CONSOLE
TEMPLATE = app
TARGET = 
DEPENDPATH += .
INCLUDEPATH += .

include( ../../lob.pri )
# include( ../../lobscript.pri ) # uncomment if needed

HEADERS += $CLASS.h test_$CLASS.moc
SOURCES += test_$CLASS.cpp $CLASS.cpp

unix {
    UI_DIR = .ui
    MOC_DIR = .moc
    OBJECTS_DIR = .obj
}

EOT
qmake $QMAKEARGS
make $MAKEARGS
./test_$CLASS

echo -----------------------------------------------
echo -
echo -                N  O  T  E  :
echo -
echo -  You must go to the file $CLASS.h and manually
echo -  add a link back to the test_$CLASS class.
echo -  Follow the convention in the docconv page.
echo -
echo -----------------------------------------------