Просмотр исходного кода

collecting testing helpers to jsunit-goodies.js

Herbert Vojčík 13 лет назад
Родитель
Сommit
869f14f45d
2 измененных файлов с 86 добавлено и 47 удалено
  1. 85 0
      lib-support/jsunit-goodies.js
  2. 1 47
      lib-support/useful.js

+ 85 - 0
lib-support/jsunit-goodies.js

@@ -0,0 +1,85 @@
+/* appjet:version 0.1 */
+/* appjet:library */
+// Copyright (c) 2010, Herbert Vojčík
+// Licensed by MIT license (http://www.opensource.org/licenses/mit-license.php)
+
+/**@overview This library contains various utility functions to support unit-testing.
+ */
+
+/**
+ * Returns stub function that logs arguments called
+ * into its property called log and returns retval.
+ * Useful primarily for testing.
+ */
+function stub(retval) {
+    var result = function() {
+        result.log.push(Array.slice(arguments));
+        return retval;
+    };
+    result.log = [];
+    return result;
+}
+
+function picker () {
+  var args = Array.prototype.slice.call(arguments);
+  return function (self) {
+    return args.map(function (prop) {
+      var ch = prop[0], rest = prop.substring(1);
+      return ch === "+" ? +self[rest] : ch === "?" ? !!self[rest] : self[prop];
+    });
+  };
+};
+
+function mockLib(lib, exports) {
+  var old = appjet._native.importApp;
+  appjet._native.importApp = function () { return exports; };
+  try { import({}, lib); } finally { appjet._native.importApp = old; }
+}
+
+/* appjet:server */
+import("lib-support/jsunit");
+var _l = import({}, "lib-support/jsunit-goodies");
+
+// ==== Test cases ====
+
+page.testSuite = [
+
+	function logIsEmptyForNewStub() {
+	    var f = _l.stub();
+	    assertArrayEquals("Log is not empty array", [], f.log);
+	},
+	
+	function stubIsAFunction() {
+	    var f = _l.stub();
+	    assertEquals("Stub is not a function", "function", typeof f);
+	},
+	
+	function stubWithoutRetvalReturnsUndefined() {
+	    var f = _l.stub();
+	    assertEquals("Stub doesn't return undefined", undefined, f(3, "hello"));
+	},
+	
+	function stubWithRetvalReturnsRetval() {
+	    var f = _l.stub("world");
+	    assertEquals("Stub returns incorrect value", "world", f(3, "hello"));
+	},
+	
+	function stub_CalledMultipleTimes_ReturnsAlwaysRetval_AndLogsArguments() {
+	    var f = _l.stub("hi");
+	    var r1 = f("hello", 14, "world");
+	    assertArrayEquals("Log is incorrect 1", [["hello", 14, "world"]], f.log);
+	    var r2 = f("!", f);
+	    assertArrayEquals("Log is incorrect 2", [["hello", 14, "world"], ["!", f]], f.log);
+	    f();
+	    assertArrayEquals("Log is incorrect 3", [["hello", 14, "world"], ["!", f], []], f.log);
+	    assertEquals("Stub returns incorrect value 1", "hi", r1);
+	    assertEquals("Stub returns incorrect value 2", "hi", r2);
+	},
+  
+  //TODO tests for the rest
+
+];
+
+// ==== Test runner ====
+import("lib-support/runner");
+runTestsByDefault();

+ 1 - 47
lib-support/useful.js

@@ -12,20 +12,6 @@
  */
 function global() { return {}.valueOf.call(); }
 
-/**
- * Returns stub function that logs arguments called
- * into its property called log and returns retval.
- * Useful primarily for testing.
- */
-function stub(retval) {
-    var result = function() {
-        result.log.push(Array.slice(arguments));
-        return retval;
-    };
-    result.log = [];
-    return result;
-}
-
 /**
  * Read-only wrapper on an object.
  */
@@ -306,39 +292,7 @@ page.testSuite = [
 	    assertEquals("yes was changed", "true", dst.yes);
 	},
 	
-	function logIsEmptyForNewStub() {
-	    var f = _l.stub();
-	    assertArrayEquals("Log is not empty array", [], f.log);
-	},
-	
-	function stubIsAFunction() {
-	    var f = _l.stub();
-	    assertEquals("Stub is not a function", "function", typeof f);
-	},
-	
-	function stubWithoutRetvalReturnsUndefined() {
-	    var f = _l.stub();
-	    assertEquals("Stub doesn't return undefined", undefined, f(3, "hello"));
-	},
-	
-	function stubWithRetvalReturnsRetval() {
-	    var f = _l.stub("world");
-	    assertEquals("Stub returns incorrect value", "world", f(3, "hello"));
-	},
-	
-	function stub_CalledMultipleTimes_ReturnsAlwaysRetval_AndLogsArguments() {
-	    var f = _l.stub("hi");
-	    var r1 = f("hello", 14, "world");
-	    assertArrayEquals("Log is incorrect 1", [["hello", 14, "world"]], f.log);
-	    var r2 = f("!", f);
-	    assertArrayEquals("Log is incorrect 2", [["hello", 14, "world"], ["!", f]], f.log);
-	    f();
-	    assertArrayEquals("Log is incorrect 3", [["hello", 14, "world"], ["!", f], []], f.log);
-	    assertEquals("Stub returns incorrect value 1", "hi", r1);
-	    assertEquals("Stub returns incorrect value 2", "hi", r2);
-	},
-  
-  //TODO tests for splitReplace
+	  //TODO tests for splitReplace
 
 ];