Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add throwing of AssertionError for test frameworks etc
- Loading branch information
Showing
4 changed files
with
66 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,13 +1,10 @@ | |||
|
|
||
var assert = require('./'); | var assert = require('./'); | ||
|
|
||
assert('wahoo'); | test(); | ||
|
|
||
var user = { authenticated: false }; | function test() { | ||
assert(user.authenticated); | var user = { name: 'tobi' }; | ||
|
assert('tobi' == user.name); | ||
user.authenticated = true; | assert('number' == typeof user.age); | ||
assert(user.authenticated); | } | ||
|
|||
user.authenticated = 0; | |||
assert(user.authenticated); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,2 +1,39 @@ | |||
|
|
||
module.exports = require('./lib/better-assert'); | /** | ||
* Module dependencies. | |||
*/ | |||
|
|||
var AssertionError = require('assert').AssertionError | |||
, callsite = require('callsite') | |||
, fs = require('fs') | |||
|
|||
/** | |||
* Expose `assert`. | |||
*/ | |||
|
|||
module.exports = process.env.NO_ASSERT | |||
? function(){} | |||
: assert; | |||
|
|||
/** | |||
* Assert the given `expr`. | |||
*/ | |||
|
|||
function assert(expr) { | |||
if (expr) return; | |||
|
|||
var stack = __stack; | |||
var call = stack[1]; | |||
var file = call.getFileName(); | |||
var lineno = call.getLineNumber(); | |||
var src = fs.readFileSync(file, 'utf8'); | |||
var line = src.split('\n')[lineno-1]; | |||
var src = line.match(/assert\((.*)\)/)[1]; | |||
|
|||
var err = new AssertionError({ | |||
message: src, | |||
stackStartFunction: stack[0].fun | |||
}); | |||
|
|||
throw err; | |||
} |
This file was deleted.
Oops, something went wrong.