Browse files

added assert example

  • Loading branch information...
1 parent aa98726 commit a83d33e2fe0c0beb6d82c849d56da0ba8f4db538 @tj committed Nov 29, 2011
Showing with 31 additions and 0 deletions.
  1. +31 −0 examples/assert.js
View
31 examples/assert.js
@@ -0,0 +1,31 @@
+
+require('../');
+
+var fs = require('fs');
+
+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 = parse(line);
+
+ var fmt = ' \033[91massert: \033[31m%s\033[0m'
+ + '\033[90m in %s:%d\033[0m';
+
+ console.error(fmt, src, file, lineno);
+}
+
+function parse(str) {
+ return str.match(/assert\((.*)\)/)[1];
+}
+
+assert('wahoo');
+
+var user = { authenticated: false };
+assert(user.authenticated);
+
+

0 comments on commit a83d33e

Please sign in to comment.