summaryrefslogtreecommitdiffstats
path: root/src
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 /src
parent7497c2f9161991f5ec1ef25101788f8ca0f2bdce (diff)
Added object comparison to TestCase QML class
Diffstat (limited to 'src')
-rw-r--r--src/imports/testlib/TestCase.qml50
1 files changed, 49 insertions, 1 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
}