[gjs] Don't depend on hash table order in test cases



commit 13de60162c1957bb90c5fcd4ecf5e16843f174c7
Author: Owen W. Taylor <otaylor fishsoup net>
Date:   Thu May 14 23:36:05 2009 -0400

    Don't depend on hash table order in test cases
    
    Using object.toSource() and comparing that to a string is not
    reliable; instead provide our own function to convert an object
    to a reliably ordered string.
    
    http://bugzilla.gnome.org/show_bug.cgi?id=582704
---
 test/js/testEverythingBasic.js |   30 ++++++++++++++++++++++++------
 1 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/test/js/testEverythingBasic.js b/test/js/testEverythingBasic.js
index 771daf5..2c20e99 100644
--- a/test/js/testEverythingBasic.js
+++ b/test/js/testEverythingBasic.js
@@ -228,13 +228,31 @@ function testArrayOut() {
 }
 
 /* GHash type */
+
+// Convert an object to a predictable (not-hash-order-dependent) string
+function objToString(v) {
+    if (typeof(v) == "object") {
+	let keys = [];
+	for (let k in v)
+	    keys.push(k);
+	keys.sort();
+	return "{" + keys.map(function(k) {
+	    return k + ":" + objToString(v[k]);
+	}) + "}";
+    } else if (typeof(v) == "string") {
+	return '"' + v + '"';
+    } else {
+	return v;
+    }
+}
+
 function testGHashOut() {
-    const HASH_STR = '({foo:"bar", baz:"bat", qux:"quux"})';
+    const HASH_STR = '{baz:"bat",foo:"bar",qux:"quux"}';
     assertEquals(null, Everything.test_ghash_null_return());
-    assertEquals(HASH_STR, Everything.test_ghash_nothing_return().toSource());
-    assertEquals(HASH_STR, Everything.test_ghash_nothing_return2().toSource());
-    assertEquals(HASH_STR, Everything.test_ghash_container_return().toSource());
-    assertEquals(HASH_STR, Everything.test_ghash_everything_return().toSource());
+    assertEquals(HASH_STR, objToString(Everything.test_ghash_nothing_return()));
+    assertEquals(HASH_STR, objToString(Everything.test_ghash_nothing_return2()));
+    assertEquals(HASH_STR, objToString(Everything.test_ghash_container_return()));
+    assertEquals(HASH_STR, objToString(Everything.test_ghash_everything_return()));
 }
 
 function testGHashIn() {
@@ -247,7 +265,7 @@ function testGHashIn() {
 }
 
 function testNestedGHashOut() {
-    const HASH_STR = '({wibble:{foo:"bar", baz:"bat", qux:"quux"}})';
+    const HASH_STR = '{wibble:{baz:"bat",foo:"bar",qux:"quux"}}';
     // FIXME uncomment this when it's added to everything module
     //assertEquals(HASH_STR, Everything.test_ghash_nested_everything_return().toSource());
     //assertEquals(HASH_STR, Everything.test_ghash_nested_everything_return2().toSource());



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]