Browse files

Using extended assert module.

  • Loading branch information...
1 parent 06db373 commit 64f60d9e677f768f2df0754882939af2ae754d1c @5long committed Oct 4, 2010
Showing with 69 additions and 20 deletions.
  1. +21 −0 LISENSE
  2. +18 −0 src/assert.js
  3. +19 −19 src/test_case.js
  4. +11 −1 test/test_case.js
View
21 LISENSE
@@ -0,0 +1,21 @@
+The MIT License
+
+Copyright (c) 2010 Whyme.Lyu
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
View
18 src/assert.js
@@ -0,0 +1,18 @@
+var assert = require('assert')
+ , fail = assert.fail
+ , extendedAssert = Object.create(assert)
+ , ext = extendedAssert
+
+ext.typeOf = function(value, expected, msg) {
+ if (typeof value != expected) {
+ fail(value, expected, msg, "typeof", ext.typeOf)
+ }
+}
+
+ext.instanceOf = function(instance, konstructor, msg) {
+ if (!(instance instanceof konstructor)) {
+ fail(instance, konstructor, msg, ext.instanceOf)
+ }
+}
+
+module.exports = extendedAssert
View
38 src/test_case.js
@@ -1,8 +1,8 @@
var util = require('./util')
- , assert = require('assert')
- , nativeAsserts = ("ok equal notEqual deepEqual notDeepEqual "
- + "strictEqual notStrictEqual throws doesNotThrow").split(" ")
-
+ , assert = require('./assert')
+ , supportedAsserts = ("ok equal notEqual deepEqual notDeepEqual"
+ + " strictEqual notStrictEqual throws doesNotThrow"
+ + " instanceOf typeOf").split(" ")
function TestCase(desc, action) {
if (arguments.length < 2) throw TypeError("Wrong number of arguments")
@@ -11,6 +11,21 @@ function TestCase(desc, action) {
this._results = []
}
+supportedAsserts.forEach(function(name) {
+ if (!(name in assert)) return
+ this[name] = function() {
+ var e, posForMsg = assert[name].length - 1
+ , msg = arguments[posForMsg]
+ try {
+ assert[name].apply(assert, arguments)
+ this._results.push({passed: true, desc: msg})
+ } catch (e) {
+ if (!(e instanceof assert.AssertionError)) this._doEnd(e)
+ this._results.push({passed: false, desc: msg, error: e})
+ }
+ }
+}, TestCase.prototype)
+
util.merge(TestCase.prototype, {
/*
* Supposed to be called by test runner.
@@ -40,19 +55,4 @@ util.merge(TestCase.prototype, {
}
})
-nativeAsserts.forEach(function(name) {
- if (!(name in assert)) return
- this[name] = function() {
- var e, posForMsg = assert[name].length - 1
- , msg = arguments[posForMsg]
- try {
- assert[name].apply(assert, arguments)
- this._results.push({passed: true, desc: msg})
- } catch (e) {
- if (!(e instanceof assert.AssertionError)) this._doEnd(e)
- this._results.push({passed: false, desc: msg, error: e})
- }
- }
-}, TestCase.prototype)
-
module.exports = TestCase
View
12 test/test_case.js
@@ -8,12 +8,22 @@ var assert = require("assert")
}
var theCase = new TestCase("a simple one", function(test) {
+ assertFunc(test.typeOf)
+ assertFunc(test.instanceOf)
+ assertFunc(test.ok)
+ assertFunc(test.strictEqual)
+ assertFunc(test.notDeepEqual)
+
test.ok(true, msg.passed)
- test.ok(0, msg.failed)
+ test.typeOf(0, "string", msg.failed)
test.equal("foo", "bar")
test.end()
})
+function assertFunc(obj) {
+ assert.equal(typeof obj, "function")
+}
+
theCase.run(function(err, report) {
h.remainingCallbacks--
var passed = report.passed[0]

0 comments on commit 64f60d9

Please sign in to comment.