From a2d6a46af8a209f0d401145b2051edda2b735a97 Mon Sep 17 00:00:00 2001 From: Rhys Weatherley Date: Sun, 3 Oct 2010 14:25:48 +1000 Subject: Move the logging functions to a helper library --- QtTest/TestCase.qml | 46 ++++++++++------------------------- QtTest/testlogger.js | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 33 deletions(-) create mode 100644 QtTest/testlogger.js diff --git a/QtTest/TestCase.qml b/QtTest/TestCase.qml index caee691..0309812 100644 --- a/QtTest/TestCase.qml +++ b/QtTest/TestCase.qml @@ -1,4 +1,5 @@ import Qt 4.7 +import "testlogger.js" as TestLogger Item { id: testCase @@ -18,9 +19,6 @@ Item { // Internal private state property string currentTestCase - property int numPassed - property int numFailed - property int numSkipped property bool expectingFail property string expectFailMsg property bool prevWhen: true @@ -29,13 +27,10 @@ Item { if (!msg) msg = ""; if (expectingFail) { - if (expectFailMsg) - print("XFAIL : " + currentTestCase + " " + expectFailMsg + " " + msg) - else - print("XFAIL : " + currentTestCase + " " + msg) + TestLogger.log_expect_fail(currentTestCase, expectFailMsg, msg) throw new Error("QtTest::expect_fail") } else { - print("FAIL! : " + currentTestCase + " " + msg) + TestLogger.log_fail(currentTestCase, msg) throw new Error("QtTest::fail") } } @@ -64,7 +59,7 @@ Item { } function skip(msg) { - print("SKIP : " + currentTestCase + " " + msg) + TestLogger.log_skip(currentTestCase, msg) throw new Error("QtTest::skip") } @@ -88,31 +83,21 @@ Item { try { testCaseResult = testCase[prop](arg) if (expectingFail) { - ++numFailed success = false - print("XPASS : " + currentTestCase) + TestLogger.log_expect_fail_pass(currentTestCase) } else if (!dataDriven) { - print("PASS : " + currentTestCase) + TestLogger.log_pass(currentTestCase) } } catch (e) { testCaseResult = [] - if (e.message == "QtTest::fail") { - ++numFailed + if (e.message == "QtTest::fail") success = false - } else if (e.message == "QtTest::skip") { - ++numSkipped - } else if (e.message == "QtTest::expect_fail") { - ++numFailed - } } return success } function run() { var success = true - numPassed = 0 - numFailed = 0 - numSkipped = 0 running = true for (var prop in testCase) { if (prop.indexOf("test_") != 0) @@ -133,14 +118,13 @@ Item { successThis = false } if (!haveData) - print("WARNING: no data supplied for " + prop + "() by " + datafunc + "()") + console.log("WARNING: no data supplied for " + prop + "() by " + datafunc + "()") if (successThis) { var prefix; if (name) prefix = name + "::" currentTestCase = prefix + prop + "()" - print("PASS : " + currentTestCase) - ++numPassed + TestLogger.log_pass(currentTestCase) } else { success = false } @@ -148,9 +132,7 @@ Item { success = false } } else { - if (runInternal(prop, false)) - ++numPassed - else + if (!runInternal(prop, false)) success = false } } @@ -171,12 +153,10 @@ Item { Component.onCompleted: { prevWhen = when if (when && !completed && !running) { - print("********* Start testing of " + name + " *********") + console.log("********* Start testing of " + name + " *********") var success = run() - print("Totals: " + numPassed + " passed, " + - numFailed + " failed, " + - numSkipped + " skipped"); - print("********* Finished testing of " + name + " *********") + TestLogger.log_print_totals() + console.log("********* Finished testing of " + name + " *********") Qt.quit() // XXX - how do we set the exit value? } } diff --git a/QtTest/testlogger.js b/QtTest/testlogger.js new file mode 100644 index 0000000..21acbb5 --- /dev/null +++ b/QtTest/testlogger.js @@ -0,0 +1,69 @@ + +.pragma library + +// We need a global place to store the results that can be +// shared between multiple TestCase instances. Because QML +// creates a separate scope for every inclusion of this file, +// we hijack the global "Qt" object to store our data. +function log_init_results() +{ + if (!Qt.testResults) { + Qt.testResults = { + numPassed: 0, + numFailed: 0, + numSkipped: 0 + } + } +} + +function log_fail(testcase, msg) +{ + log_init_results() + if (!msg) + msg = "" + console.log("FAIL! : " + testcase + " " + msg) + ++Qt.testResults.numFailed +} + +function log_expect_fail(testcase, expectmsg, msg) +{ + log_init_results() + if (!msg) + msg = "" + if (expectmsg) + console.log("XFAIL : " + testcase + " " + expectmsg + " " + msg) + else + console.log("XFAIL : " + testcase + " " + msg) + ++Qt.testResults.numPassed +} + +function log_expect_fail_pass(testcase) +{ + log_init_results() + console.log("XPASS : " + testcase) + ++Qt.testResults.numFailed +} + +function log_skip(testcase, msg) +{ + log_init_results() + if (!msg) + msg = "" + console.log("SKIP : " + testcase + " " + msg) + ++Qt.testResults.numSkipped +} + +function log_pass(testcase) +{ + log_init_results() + console.log("PASS : " + testcase) + ++Qt.testResults.numPassed +} + +function log_print_totals() +{ + log_init_results() + console.log("Totals: " + Qt.testResults.numPassed + " passed, " + + Qt.testResults.numFailed + " failed, " + + Qt.testResults.numSkipped + " skipped"); +} -- cgit v1.2.3