changeset 5:fdbad39a9170

Added basic test framework.
author Atul Varma <varmaa@toolness.com>
date Fri, 10 Apr 2009 07:47:39 -0700
parents 4c91125795dc
children 25d079125b95
files browser-couch.js index.html tests.js
diffstat 3 files changed, 89 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/browser-couch.js	Fri Apr 10 05:17:45 2009 -0700
+++ b/browser-couch.js	Fri Apr 10 07:47:39 2009 -0700
@@ -91,7 +91,6 @@
     var docIdIndex = {};
 
     if (dbName in storage && storage[dbName].value) {
-      console.log(storage[dbName].value);
       var db = JSON.parse(storage[dbName].value);
       documents = db.documents;
       docIdIndex = db.docIdIndex;
@@ -180,34 +179,3 @@
     };
   }
 };
-
-var gDb;
-
-BrowserCouch.get(
-  "blarg",
-  function(db) {
-    gDb = db;
-    console.log(db);
-    db.put(
-      [{id: "monkey",
-        content: "hello there dude"},
-       {id: "chunky",
-        content: "hello there dogen"}],
-      function() {
-        db.view(
-          {map: function(doc, emit) {
-             var words = doc.content.split(" ");
-             for (var i = 0; i < words.length; i++)
-               emit(words[i], 1);
-           },
-           reduce: function(keys, values) {
-             var totals = {};
-             for (var i = 0; i < keys.length; i++)
-               totals[keys[i]] = values[i].length;
-             return totals;
-           },
-           callback: function(result) {
-             console.log(result);
-           }});
-      });
-  });
--- a/index.html	Fri Apr 10 05:17:45 2009 -0700
+++ b/index.html	Fri Apr 10 07:47:39 2009 -0700
@@ -3,10 +3,14 @@
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 <head>
   <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
-  <title>Browser Couch!</title>
+  <title>Browser Couch Tests</title>
 </head>
 <body>
-Blah.
+<h1>Browser Couch Tests</h1>
 </body>
 <script src="browser-couch.js"></script>
+<script src="tests.js"></script>
+<script>
+Tests.run();
+</script>
 </html>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests.js	Fri Apr 10 07:47:39 2009 -0700
@@ -0,0 +1,83 @@
+var Tests = {
+  run: function(container, console, setTimeout) {
+    if (!container)
+      container = this;
+    if (!console) {
+      if (!window.console)
+        throw new Error("window.console unavailable");
+      else
+        console = window.console;
+    }
+    if (!setTimeout)
+      setTimeout = window.setTimeout;
+
+    var tests = [];
+
+    for (name in container)
+      if (name.indexOf("test") == "0") {
+        var test = {
+          name: name,
+          func: container[name],
+          isAsync: name.indexOf("_async") != -1,
+          console: console,
+          assertEqual: function assertEqual(a, b) {
+            if (a != b)
+              throw new Error(a + " != " + b);
+          }
+        };
+        tests.push(test);
+      }
+
+    var nextTest = 0;
+
+    function runNextTest() {
+      if (nextTest < tests.length) {
+        var test = tests[nextTest];
+        console.log("Running " + test.name + "...");
+        test.done = function() {
+          console.log("OK");
+          setTimeout(runNextTest, 0);
+        };
+        test.func(test);
+        if (!test.isAsync)
+          test.done();
+        nextTest++;
+      } else
+        console.log("All tests passed.");
+    }
+
+    runNextTest();
+  },
+  testBasic_async: function(self) {
+    BrowserCouch.get(
+      "blarg",
+      function(db) {
+        db.put(
+          [{id: "monkey",
+            content: "hello there dude"},
+           {id: "chunky",
+            content: "hello there dogen"}],
+          function() {
+            db.view(
+              {map: function(doc, emit) {
+                 var words = doc.content.split(" ");
+                 for (var i = 0; i < words.length; i++)
+                   emit(words[i], 1);
+               },
+               reduce: function(keys, values) {
+                 var totals = {};
+                 for (var i = 0; i < keys.length; i++)
+                   totals[keys[i]] = values[i].length;
+                 return totals;
+               },
+               callback: function(result) {
+                 self.assertEqual(result.hello, 2);
+                 self.assertEqual(result.there, 2);
+                 self.assertEqual(result.dude, 1);
+                 self.assertEqual(result.dogen, 1);
+                 self.done();
+               }});
+          });
+      });
+  }
+};