Skip to content

Commit

Permalink
add throwing of AssertionError for test frameworks etc
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Oct 5, 2012
1 parent 184aa00 commit 9c3cfa3
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 65 deletions.
34 changes: 22 additions & 12 deletions Readme.md
@@ -1,38 +1,48 @@


# better-assert # better-assert


Better in-application assertions for node, reporting the expr, filename, lineno etc using [callsite](https://github.com/visionmedia/callsite). Better c-style assertions using [callsite](https://github.com/visionmedia/callsite) for
self-documenting failure messages.


## Installation ## Installation


$ npm install better-assert $ npm install better-assert


## Example ## Example


By default assertions are disabled, so the calls to `assert()` will simply be ignored. Use the __ASSERT__ env var to enable: By default assertions are enabled, however the __NO_ASSERT__ environment variable
will deactivate them when truthy.


```js ```js
var assert = require('better-assert'); var assert = require('better-assert');


var user = { authenticated: false };
assert(user.authenticated);


user.authenticated = true; var assert = require('./');
assert(user.authenticated);


user.authenticated = 0; test();
assert(user.authenticated);
```


outputting: function test() {
var user = { name: 'tobi' };
assert('tobi' == user.name);
assert('number' == typeof user.age);
}


![assertions](http://f.cl.ly/items/1F1W0H0h2T0L233L352o/Screenshot.png) AssertionError: 'number' == typeof user.age
at test (/Users/tj/projects/better-assert/example.js:9:3)
at Object.<anonymous> (/Users/tj/projects/better-assert/example.js:4:1)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
```


## License ## License


(The MIT License) (The MIT License)


Copyright (c) 2011 TJ Holowaychuk &lt;tj@vision-media.ca&gt; Copyright (c) 2012 TJ Holowaychuk &lt;tj@vision-media.ca&gt;


Permission is hereby granted, free of charge, to any person obtaining Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the a copy of this software and associated documentation files (the
Expand Down
15 changes: 6 additions & 9 deletions example.js
@@ -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);
39 changes: 38 additions & 1 deletion index.js
@@ -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;
}
43 changes: 0 additions & 43 deletions lib/better-assert.js

This file was deleted.

0 comments on commit 9c3cfa3

Please sign in to comment.