Permalink
Browse files

Initial attempt at sinon-plugin for nodeunit

  • Loading branch information...
1 parent 6f654fa commit a945f3f0a1de59678cae94564d7c3d5db6c65b6d @cjohansen committed Dec 7, 2010
Showing with 404 additions and 0 deletions.
  1. +3 −0 .gitmodules
  2. +2 −0 AUTHORS
  3. +2 −0 Changelog.txt
  4. +27 −0 LICENSE
  5. +1 −0 deps/nodeunit
  6. +1 −0 deps/sinon
  7. +49 −0 examples/test-something.js
  8. +63 −0 lib/sinon-nodeunit.js
  9. +8 −0 run_examples
  10. +6 −0 run_tests
  11. +242 −0 test/sinon-nodeunit-test.js
View
@@ -1,3 +1,6 @@
[submodule "deps/sinon"]
path = deps/sinon
url = https://github.com/cjohansen/Sinon.JS.git
+[submodule "deps/nodeunit"]
+ path = deps/nodeunit
+ url = git@github.com:cjohansen/nodeunit.git
View
@@ -0,0 +1,2 @@
+Sinon was written by
+Christian Johansen, christian@cjohansen.no
View
@@ -0,0 +1,2 @@
+== 0.1.0 / 2010-12-07
+* Initial release
View
@@ -0,0 +1,27 @@
+(The BSD License)
+
+Copyright (c) 2010, Christian Johansen, christian@cjohansen.no
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification,
+are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+ * Neither the name of Christian Johansen nor the names of his contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Submodule nodeunit added at f705fb
Submodule sinon added at fa56a1
@@ -0,0 +1,49 @@
+require("sinon-nodeunit");
+
+exports.testSomething = function(test){
+ test.expect(1);
+ test.ok(true, "this assertion should pass");
+ test.done();
+};
+
+exports.testSomethingElse = function(test){
+ test.ok(false, "this assertion should fail");
+ test.done();
+};
+
+var someAPI = {
+ method: function () {
+ }
+};
+
+exports.testWithStub = function (test) {
+ test.stub(someAPI, "method").returns(42);
+ test.equal(42, someAPI.method());
+ test.done();
+};
+
+exports.testWithMock = function (test) {
+ test.mock(someAPI).expects("method").returns(42).once();
+ someAPI.method();
+ test.done();
+};
+
+exports.testWithFakeTime = function (test) {
+ var clock = test.useFakeTimers(0);
+ var now;
+
+ setTimeout(function () {
+ now = new Date().getTime();
+ }, 500);
+
+ clock.tick(500);
+ test.equal(500, now);
+ test.done();
+};
+
+exports.testSinonAssertions = function (test) {
+ var spy = test.spy();
+
+ test.called(spy);
+ test.done();
+};
View
@@ -0,0 +1,63 @@
+var sys = require("sys");
+
+var sinon = require("sinon");
+sinon.sandbox = require("sinon/sandbox");
+sinon.assert = require("sinon/assert");
+
+var nodeunit = {
+ core: require("core"),
+ types: require("types")
+};
+
+var typesTest = nodeunit.types.test;
+
+nodeunit.types.test = function () {
+ var obj = typesTest.apply(this, arguments);
+ var config = sinon.getConfig(sinon.config);
+ config.useFakeServer = false;
+ obj.sandbox = sinon.sandbox.create(config);
+ obj.sandbox.inject(obj);
+
+ obj.useFakeTimers = function () {
+ return obj.sandbox.useFakeTimers.apply(obj.sandbox, arguments);
+ };
+
+ var assertion, method;
+
+ for (var assertion in sinon.assert) {
+ method = sinon.assert[assertion];
+
+ if (typeof method == "function" && assertion != "fail" &&
+ assertion != "pass" && assertion != "expose") {
+ obj[assertion] = obj.wrapAssert(assertion, assertion, method.length + 1, sinon.assert);
+ }
+ }
+
+ return obj;
+};
+
+var nuRunTest = nodeunit.core.runTest;
+
+nodeunit.core.runTest = function () {
+ var testFn = arguments[1];
+
+ arguments[1] = function (test) {
+ var exception, retVal;
+
+ try {
+ retVal = testFn.apply(this, arguments);
+ } catch (e) {
+ exception = e;
+ }
+
+ test.sandbox.verifyAndRestore();
+
+ if (exception) {
+ throw exception;
+ }
+
+ return retVal;
+ };
+
+ return nuRunTest.apply(this, arguments);
+};
View
@@ -0,0 +1,8 @@
+#!/usr/bin/env node
+require.paths.unshift(__dirname + '/deps/sinon/lib');
+require.paths.unshift(__dirname + '/deps/nodeunit/lib');
+require.paths.unshift(__dirname + '/deps');
+require.paths.unshift(__dirname + '/lib');
+
+var reporter = require('nodeunit').reporters.default;
+reporter.run(['manual-test']);
View
@@ -0,0 +1,6 @@
+#!/usr/bin/env node
+require.paths.unshift("./deps/nodeunit/lib");
+require.paths.unshift("./deps/sinon/lib");
+require.paths.unshift("./lib");
+
+require("./test/sinon-nodeunit-test.js");
Oops, something went wrong. Retry.

0 comments on commit a945f3f

Please sign in to comment.