From ed8735a5cc5eea3c0107e0d0a95447ba6fc11d19 Mon Sep 17 00:00:00 2001 From: Sascha Kolewa Date: Mon, 20 Dec 2010 17:43:06 +0100 Subject: Added object comparison to TestCase QML class --- src/imports/testlib/TestCase.qml | 50 +++++++++++++++++++++++++- tests/qmlauto/selftests/tst_compareObjects.qml | 33 +++++++++++++++++ tests/qmlauto/tst_compare.qml | 12 ------- 3 files changed, 82 insertions(+), 13 deletions(-) create mode 100644 tests/qmlauto/selftests/tst_compareObjects.qml delete mode 100644 tests/qmlauto/tst_compare.qml diff --git a/src/imports/testlib/TestCase.qml b/src/imports/testlib/TestCase.qml index 1165c3a..6c73c1d 100644 --- a/src/imports/testlib/TestCase.qml +++ b/src/imports/testlib/TestCase.qml @@ -100,10 +100,13 @@ Item { function qtest_compareInternal(actual, expected) { var success = false + if (typeof actual == "number" && typeof expected == "number") { // Use a fuzzy compare if the two values are floats if (Math.abs(actual - expected) <= 0.00001) success = true + } else if (Array.isArray(actual) && Array.isArray(expected)) { + success = qtest_compareInternalArrays(actual, expected) } else if (typeof actual == "object" && typeof expected == "object") { // Does the expected value look like a vector3d? if ("x" in expected && "y" in expected && "z" in expected) { @@ -112,7 +115,7 @@ Item { Math.abs(actual.z - expected.z) <= 0.00001) { success = true } - } else if (actual == expected) { + } else if (qtest_compareInternalObjects(actual, expected)) { success = true } } else if (actual == expected) { @@ -121,12 +124,57 @@ Item { return success } + // Test for equality any JavaScript type. + // Discussions and reference: http://philrathe.com/articles/equiv + // Test suites: http://philrathe.com/tests/equiv + // Author: Philippe Rathé + function qtest_compareInternalObjects(a, b) { + var i; + var eq = true; // unless we can proove it + var aProperties = [], bProperties = []; // collection of strings + + // comparing constructors is more strict than using instanceof + if (a.constructor !== b.constructor) { + return false; + } + + for (i in a) { // be strict: don't ensures hasOwnProperty and go deep + aProperties.push(i); // collect a's properties + if (!qtest_compareInternal(a[i], b[i])) { + eq = false; + } + } + + for (i in b) { + bProperties.push(i); // collect b's properties + } + + // Ensures identical properties name + return eq && qtest_compareInternal(aProperties.sort(), bProperties.sort()); + + } + + function qtest_compareInternalArrays(actual, expected) { + if (actual.length != expected.length) { + return false + } + + for (var i = 0, len = actual.length; i < len; i++) { + if (!qtest_compareInternal(actual[i], expected[i])) { + return false + } + } + + return true + } + function qtest_formatValue(value) { if (typeof value == "object") { if ("x" in value && "y" in value && "z" in value) { return "Qt.vector3d(" + value.x + ", " + value.y + ", " + value.z + ")" } + return JSON.stringify(value) } return value } diff --git a/tests/qmlauto/selftests/tst_compareObjects.qml b/tests/qmlauto/selftests/tst_compareObjects.qml new file mode 100644 index 0000000..84fe3fe --- /dev/null +++ b/tests/qmlauto/selftests/tst_compareObjects.qml @@ -0,0 +1,33 @@ +import Qt 4.7 +import QtQuickTest 1.0 + +TestCase { + name: "SelfTests_CompareObjects" + + function test_qtest_internalCompareObjects() { + var testObj11 = {a: 1, b: "foo", c: true}; + var testObj12 = {a: 1, b: "foo", c: true}; + var testObj21 = {a: 1, b: "foo", c: true, d: testObj11}; + var testObj22 = {a: 1, b: "foo", c: true, d: testObj11}; + var testObj23 = {a: 1, b: "foo", c: true, d: testObj12}; + + compare(qtest_compareInternalObjects(testObj11, testObj11), true, "Object identity"); + compare(qtest_compareInternalObjects(testObj11, testObj12), true, "Object equality"); + compare(qtest_compareInternalObjects(testObj21, testObj21), true, "Nested object identity"); + compare(qtest_compareInternalObjects(testObj21, testObj22), true, "Nested object equality"); + compare(qtest_compareInternalObjects(testObj21, testObj23), true, "Deep nested object equality"); + } + + function test_qtest_internalCompareObjects_differentObjects() { + var testObj11 = {a: 1, b: "foo", c: true}; + var testObj12 = {a: 2, b: "foo", c: true}; + var testObj13 = {a: 1, b: "bar", c: true}; + var testObj14 = {a: 1, b: "foo", c: false}; + + compare(qtest_compareInternalObjects(testObj11, testObj12), false, "Different number"); + compare(qtest_compareInternalObjects(testObj11, testObj13), false, "Different string"); + compare(qtest_compareInternalObjects(testObj11, testObj14), false, "Different boolean"); + } + + +} diff --git a/tests/qmlauto/tst_compare.qml b/tests/qmlauto/tst_compare.qml deleted file mode 100644 index 579ca41..0000000 --- a/tests/qmlauto/tst_compare.qml +++ /dev/null @@ -1,12 +0,0 @@ -import Qt 4.7 -import QtQuickTest 1.0 - -TestCase { - function test_compare_objects() { - var testObj1 = {a: 1, b: "foo", c: true}; - compare(testObj1, testObj1, "Object identity"); - - var testObj2 = {a: 1, b: "foo", c: true}; - compare(testObj1, testObj2, "Object equality"); - } -} -- cgit v1.2.3