summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSascha Kolewa <sascha.kolewa@nokia.com>2010-12-20 17:43:06 +0100
committerSascha Kolewa <sascha.kolewa@nokia.com>2010-12-21 10:08:22 +0100
commited8735a5cc5eea3c0107e0d0a95447ba6fc11d19 (patch)
tree9f74276aca3dc796204a169798e43daf67267ebe
parent7497c2f9161991f5ec1ef25101788f8ca0f2bdce (diff)
Added object comparison to TestCase QML class
-rw-r--r--src/imports/testlib/TestCase.qml50
-rw-r--r--tests/qmlauto/selftests/tst_compareObjects.qml33
-rw-r--r--tests/qmlauto/tst_compare.qml12
3 files changed, 82 insertions, 13 deletions
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é <prathe@gmail.com>
+ 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");
- }
-}