[Checkins] SVN: Sandbox/J1m/dozodb/src/zc/dozodb/test_setup.js Wrote a js pretty printer because the one in dojo.toJson doesn't

Jim Fulton jim at zope.com
Mon Jan 10 17:21:01 EST 2011


Log message for revision 119478:
  Wrote a js pretty printer because the one in dojo.toJson doesn't
  normalize property order and I need that for doc tests.
  
  Updated the xhr stub to use it.
  

Changed:
  U   Sandbox/J1m/dozodb/src/zc/dozodb/test_setup.js

-=-
Modified: Sandbox/J1m/dozodb/src/zc/dozodb/test_setup.js
===================================================================
--- Sandbox/J1m/dozodb/src/zc/dozodb/test_setup.js	2011-01-10 21:59:51 UTC (rev 119477)
+++ Sandbox/J1m/dozodb/src/zc/dozodb/test_setup.js	2011-01-10 22:21:01 UTC (rev 119478)
@@ -1,8 +1,75 @@
 
 var last_xhr;
 
+function pprint(pname, ob, indent) {
+    var print = ! indent, out = [], i, name, names;
+
+    if (! indent) {
+        indent = pname ? '  ' : '';
+    }
+    if (pname) {
+        out.push(pname+': ');
+    }
+    else {
+        out.push(indent);
+    }
+    if (dojo.isArray(ob)) {
+        if (ob.length == 0) {
+            out.push('[]');
+        }
+        else {
+            out.push('[\n');
+            for(i = 0; i < ob.length; i++) {
+                out.push(pprint('', ob[i], indent+'  '));
+                out.push(',\n');
+            }
+            out.pop();
+            out.push(' ]');
+        }
+    }
+    else if (dojo.isFunction(ob) || ! ob || ! dojo.isObject(ob)) {
+        out.push(dojo.toJson(ob));
+    }
+    else {
+        names = [];
+        for (name in ob) {
+            if (ob.hasOwnProperty(name)) {
+                names.push(name);
+            }
+        }
+        if (names.length == 0)
+            out.push('{}');
+        else {
+            names.sort();
+            if (pname) {
+                out.push('{\n');
+                i = indent+'  ';
+            }
+            else {
+                out.push('{');
+                i = ' ';
+            }
+            dojo.forEach(
+                names, function (name) {
+                    out.push(i);
+                    out.push(pprint(name, ob[name], indent+'  '));
+                    i = indent+'  ';
+                    out.push(',\n');
+                });
+            out.pop();
+            out.push(' }');
+            }
+    }
+    out = out.join('');
+    if (print) {
+        console.log(out);
+        return undefined;
+    }
+    return out;
+}
+
 dojo.xhr = function (method, args, hasBody) {
-    var i, name, aa=[];
+    var i, name, nonfun={};
     last_xhr = {};
     for (name in args) {
         if (args.hasOwnProperty(name)) {
@@ -10,15 +77,11 @@
                 last_xhr[name] = args[name];
             }
             else {
-                aa.push([name, args[name]]);
+                nonfun[name] = args[name];
             }
         }
     }
-    aa.sort();
-    console.log("xhr "+method);
-    for (i=0; i < aa.length; i++) {
-        console.log("  "+aa[i][0]+': '+aa[i][1]);
-    }
+    pprint('xhr '+method, nonfun);
     if (hasBody ? method !== 'POST' : method !== 'GET') {
         console.error('Bad hasBody: '+ hasBody);
     }
@@ -30,4 +93,8 @@
     return dojo.xhr("POST", args, true);
 };
 
-
+function assert(cond, message) {
+    if (! cond) {
+        console.error('Assertion Failed: '+message);
+    }
+}



More information about the checkins mailing list