/* 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();