diff options
author | Jedrzej Nowacki <jedrzej.nowacki@nokia.com> | 2010-02-02 12:00:21 (GMT) |
---|---|---|
committer | Jedrzej Nowacki <jedrzej.nowacki@nokia.com> | 2010-02-02 12:24:37 (GMT) |
commit | 3cecf1548ff6f6255bd3d6163758fe7950c0a2f9 (patch) | |
tree | 002769e35f680f41680c927b8fdeb3bb7cc7e0f2 /tests | |
parent | 7ced77b654eb46af1221a55e315795c1be1753a0 (diff) | |
download | Qt-3cecf1548ff6f6255bd3d6163758fe7950c0a2f9.zip Qt-3cecf1548ff6f6255bd3d6163758fe7950c0a2f9.tar.gz Qt-3cecf1548ff6f6255bd3d6163758fe7950c0a2f9.tar.bz2 |
Introduce of QScriptValue autotest generator.
The patch contain the QScriptValue autotest suite generator. Based on an
input it can generate expected tests results and simple tests cases.
The results are kept inside generated autotest implementation file
(cpp), which could be included by main test file (tst_qscriptvalue.cpp).
Generator gives great coverage for isXXX, toXXX, comparison methods.
The generator should be used manually and it is not compiled by default.
Reviewed-by: Kent Hansen
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/qscriptvalue/testgen/data.txt | 116 | ||||
-rwxr-xr-x | tests/auto/qscriptvalue/testgen/gen.py | 203 | ||||
-rw-r--r-- | tests/auto/qscriptvalue/testgen/main.cpp | 40 | ||||
-rw-r--r-- | tests/auto/qscriptvalue/testgen/testgen.pro | 18 | ||||
-rw-r--r-- | tests/auto/qscriptvalue/testgen/testgenerator.cpp | 644 | ||||
-rw-r--r-- | tests/auto/qscriptvalue/testgen/testgenerator.h | 55 |
6 files changed, 1076 insertions, 0 deletions
diff --git a/tests/auto/qscriptvalue/testgen/data.txt b/tests/auto/qscriptvalue/testgen/data.txt new file mode 100644 index 0000000..8e7026e --- /dev/null +++ b/tests/auto/qscriptvalue/testgen/data.txt @@ -0,0 +1,116 @@ +# Data set for QScriptValue autotest. + +# Each line is a c++ code that should return a QScriptValue object. +# Lines that are empty or start with '#' will be ignored + +QScriptValue() + +#Unbound values +QScriptValue(QScriptValue::UndefinedValue) +QScriptValue(QScriptValue::NullValue) +QScriptValue(true) +QScriptValue(false) +QScriptValue(int(122)) +QScriptValue(uint(124)) +QScriptValue(0) +QScriptValue(0.0) +QScriptValue(123.0) +QScriptValue(6.37e-8) +QScriptValue(-6.37e-8) +QScriptValue(0x43211234) +QScriptValue(0x10000) +QScriptValue(0x10001) +QScriptValue(qSNaN()) +QScriptValue(qQNaN()) +QScriptValue(qInf()) +QScriptValue(-qInf()) +QScriptValue("NaN") +QScriptValue("Infinity") +QScriptValue("-Infinity") +QScriptValue("ciao") +QScriptValue(QString::fromLatin1("ciao")) +QScriptValue(QString("")) +QScriptValue(QString()) +QScriptValue(QString("0")) +QScriptValue(QString("123")) +QScriptValue(QString("12.4")) + +#Unbound values (bound to a null engine) +QScriptValue(0, QScriptValue::UndefinedValue) +QScriptValue(0, QScriptValue::NullValue) +QScriptValue(0, true) +QScriptValue(0, false) +QScriptValue(0, int(122)) +QScriptValue(0, uint(124)) +QScriptValue(0, 0) +QScriptValue(0, 0.0) +QScriptValue(0, 123.0) +QScriptValue(0, 6.37e-8) +QScriptValue(0, -6.37e-8) +QScriptValue(0, 0x43211234) +QScriptValue(0, 0x10000) +QScriptValue(0, 0x10001) +QScriptValue(0, qSNaN()) +QScriptValue(0, qQNaN()) +QScriptValue(0, qInf()) +QScriptValue(0, -qInf()) +QScriptValue(0, "NaN") +QScriptValue(0, "Infinity") +QScriptValue(0, "-Infinity") +QScriptValue(0, "ciao") +QScriptValue(0, QString::fromLatin1("ciao")) +QScriptValue(0, QString("")) +QScriptValue(0, QString()) +QScriptValue(0, QString("0")) +QScriptValue(0, QString("123")) +QScriptValue(0, QString("12.3")) + +#Bound values +QScriptValue(engine, QScriptValue::UndefinedValue) +QScriptValue(engine, QScriptValue::NullValue) +QScriptValue(engine, true) +QScriptValue(engine, false) +QScriptValue(engine, int(122)) +QScriptValue(engine, uint(124)) +QScriptValue(engine, 0) +QScriptValue(engine, 0.0) +QScriptValue(engine, 123.0) +QScriptValue(engine, 6.37e-8) +QScriptValue(engine, -6.37e-8) +QScriptValue(engine, 0x43211234) +QScriptValue(engine, 0x10000) +QScriptValue(engine, 0x10001) +QScriptValue(engine, qSNaN()) +QScriptValue(engine, qQNaN()) +QScriptValue(engine, qInf()) +QScriptValue(engine, -qInf()) +QScriptValue(engine, "NaN") +QScriptValue(engine, "Infinity") +QScriptValue(engine, "-Infinity") +QScriptValue(engine, "ciao") +QScriptValue(engine, QString::fromLatin1("ciao")) +QScriptValue(engine, QString("")) +QScriptValue(engine, QString()) +QScriptValue(engine, QString("0")) +QScriptValue(engine, QString("123")) +QScriptValue(engine, QString("1.23")) + +# evaluate +engine->evaluate("[]") +engine->evaluate("{}") +engine->evaluate("Object.prototype") +engine->evaluate("Date.prototype") +engine->evaluate("Array.prototype") +engine->evaluate("Function.prototype") +engine->evaluate("Error.prototype") +engine->evaluate("Object") +engine->evaluate("Array") +engine->evaluate("Number") +engine->evaluate("Function") +engine->evaluate("(function() { return 1; })") +engine->evaluate("(function() { return 'ciao'; })") +engine->evaluate("(function() { throw new Error('foo'); })") +engine->evaluate("/foo/") +engine->evaluate("new Object()") +engine->evaluate("new Array()") +engine->evaluate("new Error()") diff --git a/tests/auto/qscriptvalue/testgen/gen.py b/tests/auto/qscriptvalue/testgen/gen.py new file mode 100755 index 0000000..fddb853 --- /dev/null +++ b/tests/auto/qscriptvalue/testgen/gen.py @@ -0,0 +1,203 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +#Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) + +#This library is free software; you can redistribute it and/or +#modify it under the terms of the GNU Library General Public +#License as published by the Free Software Foundation; either +#version 2 of the License, or (at your option) any later version. + +#This library is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +#Library General Public License for more details. + +#You should have received a copy of the GNU Library General Public License +#along with this library; see the file COPYING.LIB. If not, write to +#the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, +#Boston, MA 02110-1301, USA. + +from __future__ import with_statement +from string import Template + +class Options(): + """Option manager. It parse and check all paramteres, set internal variables.""" + def __init__(self, args): + import logging as log + log.basicConfig() + #comand line options parser + from optparse import OptionParser + #load some directory searching stuff + import os.path, sys + + opt = OptionParser("%prog [options] path_to_input_file path_to_output_file.") + + self._o, self._a = opt.parse_args(args) + + try: + if not (os.path.exists(self._a[0])): + raise Exception("Path doesn't exist") + if len(self._a) != 2: + raise IndexError("Only two files!") + self._o.ipath = self._a[0] + self._o.opath = self._a[1] + except IndexError: + log.error("Bad usage. Please try -h or --help") + sys.exit(1) + except Exception: + log.error("Path '" + self._a[0] + " or " + self._a[1] + "' don't exist") + sys.exit(2) + + def __getattr__(self, attr): + """map all options properties into this object (remove one level of indirection)""" + return getattr(self._o, attr) + + +mainTempl = Template("""/* + Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +//////////////////////////////////////////////////////////////// +// THIS FILE IS AUTOGENERATED, ALL MODIFICATIONS WILL BE LAST // +//////////////////////////////////////////////////////////////// + +#include "testgenerator.h" + +#include <QtCore/qdatastream.h> +#include <QtCore/qdatetime.h> +#include <QtCore/qdebug.h> +#include <QtCore/qfile.h> +#include <QtCore/qnumeric.h> +#include <QtCore/qvariant.h> +#include <QtCore/qvector.h> +#include <QtScript/qscriptvalue.h> +#include <QtScript/qscriptengine.h> + + + +typedef bool (QScriptValue::*ComparisionType) (const QScriptValue&) const; +static QVector<bool> compare(ComparisionType compare, QScriptValue value, const QScriptValueList& values) { + QVector<bool> result; + result.reserve(${count}); + + QScriptValueList::const_iterator i = values.constBegin(); + for (; i != values.constEnd(); ++i) { + result << (value.*compare)(*i); + } + return result; +} + +static void dump(QDataStream& out, QScriptValue& value, const QString& expression, const QScriptValueList& allValues) +{ + out << QString(expression); + + out << value.isValid(); + out << value.isBool(); + out << value.isBoolean(); + out << value.isNumber(); + out << value.isFunction(); + out << value.isNull(); + out << value.isString(); + out << value.isUndefined(); + out << value.isVariant(); + out << value.isQObject(); + out << value.isQMetaObject(); + out << value.isObject(); + out << value.isDate(); + out << value.isRegExp(); + out << value.isArray(); + out << value.isError(); + + out << value.toString(); + out << value.toNumber(); + out << value.toBool(); + out << value.toBoolean(); + out << value.toInteger(); + out << value.toInt32(); + out << value.toUInt32(); + out << value.toUInt16(); + + out << compare(&QScriptValue::equals, value, allValues); + out << compare(&QScriptValue::strictlyEquals, value, allValues); + out << compare(&QScriptValue::lessThan, value, allValues); + out << compare(&QScriptValue::instanceOf, value, allValues); + + out << qscriptvalue_cast<QString>(value); + out << qscriptvalue_cast<qsreal>(value); + out << qscriptvalue_cast<bool>(value); + out << qscriptvalue_cast<qint32>(value); + out << qscriptvalue_cast<quint32>(value); + out << qscriptvalue_cast<quint16>(value); +} + +void TestGenerator::prepareData() +{ + QScriptEngine* engine = new QScriptEngine; + + QScriptValueList allValues; + allValues << ${values}; + QVector<QString> allDataTags; + allDataTags.reserve(${count}); + allDataTags << ${dataTags}; + QDataStream out(&m_tempFile); + out << allDataTags; + + for(unsigned i = 0; i < ${count}; ++i) + dump(out, allValues[i], allDataTags[i], allValues); + + delete engine; +} +""") +qsvTempl = Template(""" + { + QScriptValue value = ${expr}; + dump(out, value, "${expr_esc}", allValues); + }""") + + + +if __name__ == '__main__': + import sys + o = Options(sys.argv[1:]) + out = [] + qsv = [] + # load input file + with open(o.ipath) as f: + for row in f.readlines(): + qsv.append(row) + + #skip comments and empty lines + qsv = filter(lambda w: len(w.strip()) and not w.startswith('#'), qsv) + + escape = lambda w: w.replace('\\','\\\\').replace('"','\\"') + + for row in qsv: + row = row.replace('\n','') + row_esc = escape(row) + out.append(qsvTempl.substitute(expr = row, expr_esc = row_esc)) + + result = mainTempl.substitute(dump= "".join(out) \ + , values = (11 * ' ' + '<< ').join(qsv) \ + , count = len(qsv) \ + , dataTags = (11 * ' ' + '<< ').join(map(lambda w: '"' + escape(w.replace('\n','')) + '"\n', qsv))) + + with open(o.opath, 'w') as f: + f.write(result) + + diff --git a/tests/auto/qscriptvalue/testgen/main.cpp b/tests/auto/qscriptvalue/testgen/main.cpp new file mode 100644 index 0000000..389a725 --- /dev/null +++ b/tests/auto/qscriptvalue/testgen/main.cpp @@ -0,0 +1,40 @@ +/* + Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "testgenerator.h" +#include <QtCore/qdebug.h> +#include <QtCore/qfile.h> +#include <QtCore/qstringlist.h> +#include <QtCore/QCoreApplication> + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + + if (argc != 2) { + qWarning() << "./prog outputfile"; + exit(1); + } + + //Procced + TestGenerator gen(a.arguments()[1]); + gen.run(); + + return 0; +} diff --git a/tests/auto/qscriptvalue/testgen/testgen.pro b/tests/auto/qscriptvalue/testgen/testgen.pro new file mode 100644 index 0000000..47709a8 --- /dev/null +++ b/tests/auto/qscriptvalue/testgen/testgen.pro @@ -0,0 +1,18 @@ +QT += core script +TARGET = testgen +CONFIG += console +CONFIG -= app_bundle +TEMPLATE = app + +SOURCES += main.cpp \ + testgenerator.cpp +HEADERS += testgenerator.h + + +INPUT_DATASET = data.txt +dataset.name = Generating QScraiptValue autotest's dataset +dataset.output = autogenerated.cpp +dataset.commands = python gen.py data.txt autogenerated.cpp +dataset.input = INPUT_DATASET +dataset.variable_out = SOURCES +QMAKE_EXTRA_COMPILERS += dataset diff --git a/tests/auto/qscriptvalue/testgen/testgenerator.cpp b/tests/auto/qscriptvalue/testgen/testgenerator.cpp new file mode 100644 index 0000000..199e84b --- /dev/null +++ b/tests/auto/qscriptvalue/testgen/testgenerator.cpp @@ -0,0 +1,644 @@ +/* + Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "testgenerator.h" + +#include <QtCore/qdatastream.h> +#include <QtCore/qdatetime.h> +#include <QtCore/qdebug.h> +#include <QtCore/qnumeric.h> +#include <QtCore/qstringlist.h> +#include <QtCore/qtextstream.h> +#include <QtCore/qvariant.h> +#include <QtScript/qscriptvalue.h> + +void TestGenerator::save(const QString& data) +{ + QTextStream out(&m_ofile); + out << data; +} + +static QString escape(QString txt) +{ + return txt.replace("\\","\\\\").replace("\"","\\\"").replace("\n","\\n"); +} + +template<typename T> +QString prepareToInsert(T value) {return QString::fromAscii("\"") + escape(value) + "\"";} +template<> +QString prepareToInsert<qsreal>(qsreal value) +{ + if (qIsNaN(value)) + return "qQNaN()"; + if (qIsInf(value)) + return "qInf()"; + return QString::number(value, 'g', 16); +} +template<> +QString prepareToInsert<qint32>(qint32 value) {return QString::number(value);} +template<> +QString prepareToInsert<quint32>(quint32 value) {return QString::number(value);} +template<> +QString prepareToInsert<quint16>(quint16 value) {return QString::number(value);} +template<> +QString prepareToInsert<bool>(bool value) {return value ? "true" : "false";} +template<> +QString prepareToInsert<QString>(QString value) {return QString::fromAscii("\"") + escape(value) + "\"";} + +template<typename T> +QString typeName() {return QString();} +template<> +QString typeName<qsreal>() {return "qsreal";} +template<> +QString typeName<qint32>() {return "qint32";} +template<> +QString typeName<quint32>() {return "quint32";} +template<> +QString typeName<quint16>() {return "quint16";} +template<> +QString typeName<bool>() {return "bool";} +template<> +QString typeName<QString>() {return "QString";} + +static QString generateIsXXXDef(const QString& name, const QList<QString>& list) +{ + static const QString templ("void tst_QScriptValue::%1_initData()\n"\ + "{\n"\ + " QTest::addColumn<bool>(\"expected\");\n"\ + " initScriptValues();\n"\ + "}\n"\ + "\n"\ + "void tst_QScriptValue::%1_makeData(const char* expr)\n"\ + "{\n"\ + " static QSet<QString> %1;\n"\ + " if (%1.isEmpty()) {\n"\ + " %1%2\n"\ + " }\n"\ + " newRow(expr) << %1.contains(expr);\n"\ + "}\n"\ + "\n"\ + "void tst_QScriptValue::%1_test(const char*, const QScriptValue& value)\n"\ + "{\n"\ + " QFETCH(bool, expected);\n"\ + " QCOMPARE(value.%1(), expected);\n"\ + "}\n"\ + "\n"\ + "DEFINE_TEST_FUNCTION(%1)\n"\ + "\n"); + + if (!list.size()) { + qWarning() << name << ": nothing to add!" ; + return QString(); + } + + QString result = templ; + QStringList set; + foreach(QString t, list) { + t = escape(t); + t.append('\"'); + t.prepend('\"'); + set.append(QString(" << ")); + set.append(t); + set.append("\n "); + } + set.append(";"); + return result.arg(name, set.join(QString())); +} + +template<typename T> +static QString generateToXXXDef(const QString& name, const QList<QPair<QString, T> >& list) +{ + static const QString templ = "\n"\ + "void tst_QScriptValue::%1_initData()\n"\ + "{\n"\ + " QTest::addColumn<%2>(\"expected\");\n"\ + " initScriptValues();\n"\ + "}\n"\ + "\n"\ + "void tst_QScriptValue::%1_makeData(const char* expr)\n"\ + "{\n"\ + " static QHash<QString, %2> %1;\n"\ + " if (%1.isEmpty()) {\n"\ + "%3"\ + " }\n"\ + " newRow(expr) << %1.value(expr);\n"\ + "}\n"\ + "\n"\ + "void tst_QScriptValue::%1_test(const char*, const QScriptValue& value)\n"\ + "{\n"\ + " QFETCH(%2, expected);\n"\ + " QCOMPARE(value.%1(), expected);\n"\ + "}\n"\ + "\n"\ + "DEFINE_TEST_FUNCTION(%1)\n"; + QString result = templ; + + typename QList<QPair<QString, T> >::const_iterator i = list.constBegin(); + QStringList set; + for(; i != list.constEnd(); ++i) { + QPair<QString, T> t = *i; + t.first = escape(t.first); + set.append(QString(" ")); + set.append(name); + set.append(".insert(\""); + set.append(t.first); + set.append(QString::fromAscii("\", ")); + set.append(prepareToInsert<T>(t.second)); + set.append(QString::fromAscii(");\n")); + } + return result.arg(name, typeName<T>(), set.join(QString())); +} + + +template<> +QString generateToXXXDef<qsreal>(const QString& name, const QList<QPair<QString, qsreal> >& list) +{ + static const QString templ = "\n"\ + "void tst_QScriptValue::%1_initData()\n"\ + "{\n"\ + " QTest::addColumn<%2>(\"expected\");\n"\ + " initScriptValues();\n"\ + "}\n"\ + "\n"\ + "void tst_QScriptValue::%1_makeData(const char* expr)\n"\ + "{\n"\ + " static QHash<QString, %2> %1;\n"\ + " if (%1.isEmpty()) {\n"\ + "%3"\ + " }\n"\ + " newRow(expr) << %1.value(expr);\n"\ + "}\n"\ + "\n"\ + "void tst_QScriptValue::%1_test(const char*, const QScriptValue& value)\n"\ + "{\n"\ + " QFETCH(%2, expected);\n"\ + "%666" + " if (qIsInf(expected)) {\n"\ + " QVERIFY(qIsInf(value.%1()));\n"\ + " return;\n"\ + " }\n"\ + " QCOMPARE(value.%1(), expected);\n"\ + "}\n"\ + "\n"\ + "DEFINE_TEST_FUNCTION(%1)\n"; + QString result = templ; + + QList<QPair<QString, qsreal> >::const_iterator i = list.constBegin(); + QStringList set; + for(; i != list.constEnd(); ++i) { + QPair<QString, qsreal> t = *i; + t.first = escape(t.first); + set.append(QString(" ")); + set.append(name); + set.append(".insert(\""); + set.append(t.first); + set.append(QString::fromAscii("\", ")); + set.append(prepareToInsert<qsreal>(t.second)); + set.append(QString::fromAscii(");\n")); + } + // toInteger shouldn't return NaN, so it would be nice to catch the case. + QString hook; + if (name == "toNumber") { + hook = + " if (qIsNaN(expected)) {\n"\ + " QVERIFY(qIsNaN(value.toNumber()));\n"\ + " return;\n"\ + " }\n"; + } + return result.arg(name, typeName<qsreal>(), set.join(QString()), hook); +} + +template<typename T> +static QString generateCastDef(const QList<QPair<QString, T> >& list) +{ + static const QString templ = "\n"\ + "void tst_QScriptValue::qscriptvalue_cast%2_initData()\n"\ + "{\n"\ + " QTest::addColumn<%2>(\"expected\");\n"\ + " initScriptValues();\n"\ + "}\n"\ + "\n"\ + "void tst_QScriptValue::qscriptvalue_cast%2_makeData(const char* expr)\n"\ + "{\n"\ + " static QHash<QString, %2> value;\n"\ + " if (value.isEmpty()) {\n"\ + "%3"\ + " }\n"\ + " newRow(expr) << value.value(expr);\n"\ + "}\n"\ + "\n"\ + "void tst_QScriptValue::qscriptvalue_cast%2_test(const char*, const QScriptValue& value)\n"\ + "{\n"\ + " QFETCH(%2, expected);\n"\ + " QCOMPARE(qscriptvalue_cast<%2>(value), expected);\n"\ + "}\n"\ + "\n"\ + "DEFINE_TEST_FUNCTION(qscriptvalue_cast%2)\n"; + QString result = templ; + + typename QList<QPair<QString, T> >::const_iterator i = list.constBegin(); + QStringList set; + for(; i != list.constEnd(); ++i) { + QPair<QString, T> t = *i; + t.first = escape(t.first); + set.append(QString(" ")); + set.append("value.insert(\""); + set.append(t.first); + set.append(QString::fromAscii("\", ")); + set.append(prepareToInsert<T>(t.second)); + set.append(QString::fromAscii(");\n")); + } + return result.arg(typeName<T>(), set.join(QString())); +} + +template<> +QString generateCastDef<qsreal>(const QList<QPair<QString, qsreal> >& list) +{ + static const QString templ = "\n"\ + "void tst_QScriptValue::qscriptvalue_cast%2_initData()\n"\ + "{\n"\ + " QTest::addColumn<%2>(\"expected\");\n"\ + " initScriptValues();\n"\ + "}\n"\ + "\n"\ + "void tst_QScriptValue::qscriptvalue_cast%2_makeData(const char* expr)\n"\ + "{\n"\ + " static QHash<QString, %2> value;\n"\ + " if (value.isEmpty()) {\n"\ + "%3"\ + " }\n"\ + " newRow(expr) << value.value(expr);\n"\ + "}\n"\ + "\n"\ + "void tst_QScriptValue::qscriptvalue_cast%2_test(const char*, const QScriptValue& value)\n"\ + "{\n"\ + " QFETCH(%2, expected);\n"\ + " if (qIsNaN(expected)) {\n" + " QVERIFY(qIsNaN(qscriptvalue_cast<%2>(value)));\n" + " return;\n" + " }\n"\ + " if (qIsInf(expected)) {\n" + " QVERIFY(qIsInf(qscriptvalue_cast<%2>(value)));\n" + " return;\n" + " }\n" + " QCOMPARE(qscriptvalue_cast<%2>(value), expected);\n"\ + "}\n"\ + "\n"\ + "DEFINE_TEST_FUNCTION(qscriptvalue_cast%2)\n"; + QString result = templ; + + QList<QPair<QString, qsreal> >::const_iterator i = list.constBegin(); + QStringList set; + for(; i != list.constEnd(); ++i) { + QPair<QString, qsreal> t = *i; + t.first = escape(t.first); + set.append(QString(" ")); + set.append("value.insert(\""); + set.append(t.first); + set.append(QString::fromAscii("\", ")); + set.append(prepareToInsert<qsreal>(t.second)); + set.append(QString::fromAscii(");\n")); + } + return result.arg(typeName<qsreal>(), set.join(QString())); +} + +static QString generateCompareDef(const QString& comparisionType, const QList<QString> tags) +{ + static const QString templ = "\n"\ + "void tst_QScriptValue::%1_initData()\n"\ + "{\n"\ + " QTest::addColumn<QScriptValue>(\"other\");\n"\ + " QTest::addColumn<bool>(\"expected\");\n"\ + " initScriptValues();\n"\ + "}\n"\ + "\n"\ + "void tst_QScriptValue::%1_makeData(const char *expr)\n"\ + "{\n"\ + " static QSet<QString> equals;\n"\ + " if (equals.isEmpty()) {\n"\ + "%2\n"\ + " }\n"\ + " QHash<QString, QScriptValue>::const_iterator it;\n"\ + " for (it = m_values.constBegin(); it != m_values.constEnd(); ++it) {\n"\ + " QString tag = QString::fromLatin1(\"%20 <=> %21\").arg(expr).arg(it.key());\n"\ + " newRow(tag.toLatin1()) << it.value() << equals.contains(tag);\n"\ + " }\n"\ + "}\n"\ + "\n"\ + "void tst_QScriptValue::%1_test(const char *, const QScriptValue& value)\n"\ + "{\n"\ + " QFETCH(QScriptValue, other);\n"\ + " QFETCH(bool, expected);\n"\ + " QCOMPARE(value.%1(other), expected);\n"\ + "}\n"\ + "\n"\ + "DEFINE_TEST_FUNCTION(%1)\n"; + Q_ASSERT(comparisionType == "strictlyEquals" + || comparisionType == "equals" + || comparisionType == "lessThan" + || comparisionType == "instanceOf"); + QString result = templ; + + QStringList set; + foreach(const QString& tmp, tags) { + set.append(" equals.insert(\"" + escape(tmp) + "\");"); + } + return result.arg(comparisionType, set.join("\n")); +} + +static QString generateInitDef(const QVector<QString>& allDataTags) +{ + static const QString templ = "/*\n"\ + " Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)\n"\ + "\n"\ + " This library is free software; you can redistribute it and/or\n"\ + " modify it under the terms of the GNU Library General Public\n"\ + " License as published by the Free Software Foundation; either\n"\ + " version 2 of the License, or (at your option) any later version.\n"\ + "\n"\ + " This library is distributed in the hope that it will be useful,\n"\ + " but WITHOUT ANY WARRANTY; without even the implied warranty of\n"\ + " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"\ + " Library General Public License for more details.\n"\ + "\n"\ + " You should have received a copy of the GNU Library General Public License\n"\ + " along with this library; see the file COPYING.LIB. If not, write to\n"\ + " the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,\n"\ + " Boston, MA 02110-1301, USA.\n"\ + "*/\n"\ + "\n"\ + "#include \"tst_qscriptvalue.h\"\n\n"\ + "#define DEFINE_TEST_VALUE(expr) m_values.insert(QString::fromLatin1(#expr), expr)\n"\ + "\n"\ + "void tst_QScriptValue::initScriptValues()\n"\ + "{\n"\ + " m_values.clear();\n"\ + " if (engine) \n"\ + " delete engine;\n"\ + " engine = new QScriptEngine;\n"\ + "%1\n}\n\n"; + QString result = templ; + QStringList set; + foreach(const QString tag, allDataTags) { + set.append(" DEFINE_TEST_VALUE(" + tag + ");"); + } + + return result.arg(set.join("\n")); +} + +static void squashTags(QString dataTag, const QVector<bool>& results, QList<QString>& tags, QVector<QString> dataTags) +{ + for(int i = 0; i < results.count(); ++i) { + if (results.at(i)) + tags.append(dataTag + " <=> " + dataTags[i]); + } +} + + +QString TestGenerator::generateTest() +{ + // All data tags keept in one place. + QVector<QString> dataTags; + + // Data tags for values that return true in isXXX call + QList<QString> isValidList; + QList<QString> isBoolList; + QList<QString> isBooleanList; + QList<QString> isNumberList; + QList<QString> isFunctionList; + QList<QString> isNullList; + QList<QString> isStringList; + QList<QString> isUndefinedList; + QList<QString> isVariantList; + QList<QString> isQObjectList; + QList<QString> isQMetaObjectList; + QList<QString> isObjectList; + QList<QString> isDateList; + QList<QString> isRegExpList; + QList<QString> isArrayList; + QList<QString> isErrorList; + + // List of pairs data tag and value returned from toXXX call + QList<QPair<QString, QString> > toStringList; + QList<QPair<QString, qsreal> > toNumberList; + QList<QPair<QString, bool> > toBoolList; + QList<QPair<QString, bool> > toBooleanList; + QList<QPair<QString, qsreal> > toIntegerList; + QList<QPair<QString, qint32> > toInt32List; + QList<QPair<QString, quint32> > toUInt32List; + QList<QPair<QString, quint16> > toUInt16List; + + // List of complex tags returning true + QList<QString> equalsList; + QList<QString> strictlyEqualsList; + QList<QString> lessThanList; + QList<QString> instanceOfList; + + QList<QPair<QString, QString> > castStringList; + QList<QPair<QString, qsreal> > castSRealList; + QList<QPair<QString, bool> > castBoolList; + QList<QPair<QString, qint32> > castInt32List; + QList<QPair<QString, quint32> > castUInt32List; + QList<QPair<QString, quint16> > castUInt16List; + + // Load. + m_tempFile.seek(0); + QDataStream in(&m_tempFile); + in >> dataTags; + Q_ASSERT(in.status() == in.Ok); + + while(!in.atEnd()) + { + bool isValidRes; + bool isBoolRes; + bool isBooleanRes; + bool isNumberRes; + bool isFunctionRes; + bool isNullRes; + bool isStringRes; + bool isUndefinedRes; + bool isVariantRes; + bool isQObjectRes; + bool isQMetaObjectRes; + bool isObjectRes; + bool isDateRes; + bool isRegExpRes; + bool isArrayRes; + bool isErrorRes; + + QString toStringRes; + qsreal toNumberRes; + bool toBoolRes; + bool toBooleanRes; + qsreal toIntegerRes; + qint32 toInt32Res; + quint32 toUInt32Res; + quint16 toUInt16Res; + //toVariantRes; + //toDateTimeRes; + + QVector<bool> equalsRes; + QVector<bool> strictlyEqualsRes; + QVector<bool> lessThanRes; + QVector<bool> instanceOfRes; + + QString castStringRes; + qsreal castSRealRes; + bool castBoolRes; + qint32 castInt32Res; + quint32 castUInt32Res; + quint16 castUInt16Res; + + QString dataTag; + in >> dataTag; + in >> isValidRes; + in >> isBoolRes; + in >> isBooleanRes; + in >> isNumberRes; + in >> isFunctionRes; + in >> isNullRes; + in >> isStringRes; + in >> isUndefinedRes; + in >> isVariantRes; + in >> isQObjectRes; + in >> isQMetaObjectRes; + in >> isObjectRes; + in >> isDateRes; + in >> isRegExpRes; + in >> isArrayRes; + in >> isErrorRes; + + if (isValidRes) isValidList.append(dataTag); + if (isBoolRes) isBoolList.append(dataTag); + if (isBooleanRes) isBooleanList.append(dataTag); + if (isNumberRes) isNumberList.append(dataTag); + if (isFunctionRes) isFunctionList.append(dataTag); + if (isNullRes) isNullList.append(dataTag); + if (isStringRes) isStringList.append(dataTag); + if (isUndefinedRes) isUndefinedList.append(dataTag); + if (isVariantRes) isVariantList.append(dataTag); + if (isQObjectRes) isQObjectList.append(dataTag); + if (isQMetaObjectRes) isQMetaObjectList.append(dataTag); + if (isObjectRes) isObjectList.append(dataTag); + if (isDateRes) isDateList.append(dataTag); + if (isRegExpRes) isRegExpList.append(dataTag); + if (isArrayRes) isArrayList.append(dataTag); + if (isErrorRes) isErrorList.append(dataTag); + + in >> toStringRes; + in >> toNumberRes; + in >> toBoolRes; + in >> toBooleanRes; + in >> toIntegerRes; + in >> toInt32Res; + in >> toUInt32Res; + in >> toUInt16Res; + //in >> toVariantRes; + //in >> toDateTimeRes; + + toStringList.append(QPair<QString, QString>(dataTag, toStringRes)); + toNumberList.append(QPair<QString, qsreal>(dataTag, toNumberRes)); + toBoolList.append(QPair<QString, bool>(dataTag, toBoolRes)); + toBooleanList.append(QPair<QString, bool>(dataTag, toBooleanRes)); + toIntegerList.append(QPair<QString, qsreal>(dataTag, toIntegerRes)); + toInt32List.append(QPair<QString, qint32>(dataTag, toInt32Res)); + toUInt32List.append(QPair<QString, quint32>(dataTag, toUInt32Res)); + toUInt16List.append(QPair<QString, quint16>(dataTag, toUInt16Res)); + + in >> equalsRes; + in >> strictlyEqualsRes; + in >> lessThanRes; + in >> instanceOfRes; + + squashTags(dataTag, equalsRes, equalsList, dataTags); + squashTags(dataTag, strictlyEqualsRes, strictlyEqualsList, dataTags); + squashTags(dataTag, lessThanRes, lessThanList, dataTags); + squashTags(dataTag, instanceOfRes, instanceOfList, dataTags); + + in >> castStringRes; + in >> castSRealRes; + in >> castBoolRes; + in >> castInt32Res; + in >> castUInt32Res; + in >> castUInt16Res; + + castStringList.append(QPair<QString, QString>(dataTag, castStringRes)); + castSRealList.append(QPair<QString, qsreal>(dataTag, castSRealRes)); + castBoolList.append(QPair<QString, bool>(dataTag, castBoolRes)); + castInt32List.append(QPair<QString, qint32>(dataTag, castInt32Res)); + castUInt32List.append(QPair<QString, quint32>(dataTag, castUInt32Res)); + castUInt16List.append(QPair<QString, quint16>(dataTag, castUInt16Res)); + + Q_ASSERT(in.status() == in.Ok); + } + + Q_ASSERT(in.atEnd()); + + // Generate. + QStringList result; + result.append(generateInitDef(dataTags)); + result.append(generateIsXXXDef("isValid", isValidList)); + result.append(generateIsXXXDef("isBool", isBoolList)); + result.append(generateIsXXXDef("isBoolean", isBooleanList)); + result.append(generateIsXXXDef("isNumber", isNumberList)); + result.append(generateIsXXXDef("isFunction", isFunctionList)); + result.append(generateIsXXXDef("isNull", isNullList)); + result.append(generateIsXXXDef("isString", isStringList)); + result.append(generateIsXXXDef("isUndefined", isUndefinedList)); + result.append(generateIsXXXDef("isVariant", isVariantList)); + result.append(generateIsXXXDef("isQObject", isQObjectList)); + result.append(generateIsXXXDef("isQMetaObject", isQMetaObjectList)); + result.append(generateIsXXXDef("isObject", isObjectList)); + result.append(generateIsXXXDef("isDate", isDateList)); + result.append(generateIsXXXDef("isRegExp", isRegExpList)); + result.append(generateIsXXXDef("isArray", isArrayList)); + result.append(generateIsXXXDef("isError", isErrorList)); + + result.append(generateToXXXDef<QString>("toString", toStringList)); + result.append(generateToXXXDef<qsreal>("toNumber", toNumberList)); + result.append(generateToXXXDef<bool>("toBool", toBoolList)); + result.append(generateToXXXDef<bool>("toBoolean", toBooleanList)); + result.append(generateToXXXDef<qsreal>("toInteger", toIntegerList)); + result.append(generateToXXXDef<qint32>("toInt32", toInt32List)); + result.append(generateToXXXDef<quint32>("toUInt32", toUInt32List)); + result.append(generateToXXXDef<quint16>("toUInt16", toUInt16List)); + + result.append(generateCompareDef("equals", equalsList)); + result.append(generateCompareDef("strictlyEquals", strictlyEqualsList)); + result.append(generateCompareDef("lessThan", lessThanList)); + result.append(generateCompareDef("instanceOf", instanceOfList)); + + result.append(generateCastDef(castStringList)); + result.append(generateCastDef(castSRealList)); + result.append(generateCastDef(castBoolList)); + result.append(generateCastDef(castInt32List)); + result.append(generateCastDef(castUInt32List)); + result.append(generateCastDef(castUInt16List)); + + return result.join("\n"); +} + + + + + + + + + diff --git a/tests/auto/qscriptvalue/testgen/testgenerator.h b/tests/auto/qscriptvalue/testgen/testgenerator.h new file mode 100644 index 0000000..d3a2196 --- /dev/null +++ b/tests/auto/qscriptvalue/testgen/testgenerator.h @@ -0,0 +1,55 @@ +/* + Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef TESTGENERATOR_H +#define TESTGENERATOR_H + +#include <QtCore/qdebug.h> +#include <QtCore/qfile.h> +#include <QtCore/qtemporaryfile.h> + +class TestGenerator { +public: + TestGenerator(QString& outputpath) + : m_ofile(outputpath) + { + // Open output file + if (!m_ofile.open(QIODevice::WriteOnly | QIODevice::Text)) { + qWarning() << "Can't open output file: " << outputpath; + exit(2); + } + m_tempFile.open(); + } + + void run() + { + prepareData(); + Q_ASSERT(m_tempFile.size()); + save(generateTest()); + } + + void prepareData(); + QString generateTest(); + void save(const QString& data); +private: + QFile m_ofile; + QTemporaryFile m_tempFile; +}; + +#endif // TESTGENERATOR_H |