Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add throwing of AssertionError for test frameworks etc

  • Loading branch information...
commit 9c3cfa3eee00b74b4da1f12f418ebd39804bf166 1 parent 184aa00
@tj authored
Showing with 66 additions and 65 deletions.
  1. +22 −12 Readme.md
  2. +6 −9 example.js
  3. +38 −1 index.js
  4. +0 −43 lib/better-assert.js
View
34 Readme.md
@@ -1,7 +1,8 @@
# 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
@@ -9,30 +10,39 @@
## 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
var assert = require('better-assert');
-var user = { authenticated: false };
-assert(user.authenticated);
-user.authenticated = true;
-assert(user.authenticated);
+var assert = require('./');
-user.authenticated = 0;
-assert(user.authenticated);
-```
+test();
-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
(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
a copy of this software and associated documentation files (the
View
15 example.js
@@ -1,13 +1,10 @@
var assert = require('./');
-assert('wahoo');
+test();
-var user = { authenticated: false };
-assert(user.authenticated);
-
-user.authenticated = true;
-assert(user.authenticated);
-
-user.authenticated = 0;
-assert(user.authenticated);
+function test() {
+ var user = { name: 'tobi' };
+ assert('tobi' == user.name);
+ assert('number' == typeof user.age);
+}
View
39 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;
+}
View
43 lib/better-assert.js
@@ -1,43 +0,0 @@
-
-/*!
- * better-assert
- * Copyright(c) 2011 TJ Holowaychuk <tj@vision-media.ca>
- * MIT Licensed
- */
-
-/**
- * Module dependencies.
- */
-
-var fs = require('fs')
- , callsite = require('callsite');
-
-/**
- * Expose `assert`.
- */
-
-module.exports = process.env.ASSERT
- ? assert
- : function(){};
-
-/**
- * Assert the given `expr`.
- */
-
-function assert(expr) {
- if (expr) return;
-
- var call = __stack[1]
- , file = call.getFileName()
- , lineno = call.getLineNumber()
- , src = fs.readFileSync(file, 'utf8')
- , line = src.split('\n')[lineno-1]
- , src = line.match(/assert\((.*)\)/)[1];
-
- var fmt = ' \033[91massert: \033[31m%s\033[0m'
- + '\n \033[90min: %s:%d'
- + '\n value: %j\033[0m'
- + '\n';
-
- console.error(fmt, src, file, lineno, expr);
-}
Please sign in to comment.
Something went wrong with that request. Please try again.