Created
October 29, 2010 17:53
-
-
Save jocafa/653995 to your computer and use it in GitHub Desktop.
A simple class with tests
This file contains hidden or 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
| require('./mootools'); | |
| var util = require('util'); | |
| var Base = new Class({ | |
| Implements: [Events, Options], | |
| options: { | |
| }, | |
| initialize: function (opts) { | |
| this.setOptions(opts || {}); | |
| return this; | |
| } | |
| }); | |
| exports.Base = Base; | |
| __filename == process.argv[1] && (function () { | |
| new (require('./Tester').Tester)({ | |
| tests: { | |
| instantiation: { | |
| 'no args': function () { | |
| var b = new Base(); | |
| this.assert(b instanceof Base, true); | |
| }, | |
| 'some options': function () { | |
| var b = new Base({foo: 'bar'}); | |
| this.assert(b.options.foo, 'bar'); | |
| }, | |
| 'event handler': { | |
| simple: function () { | |
| var fired = false; | |
| var b = new Base({ | |
| onSomething: function () { | |
| fired = true; | |
| } | |
| }); | |
| b.fireEvent('something'); | |
| this.assert(fired, true); | |
| }, | |
| 'with data': function () { | |
| var firedval = null; | |
| var b = new Base({ | |
| onSomething: function (val) { | |
| firedval = val; | |
| } | |
| }); | |
| b.fireEvent('something', 'foo'); | |
| this.assert(firedval, 'foo'); | |
| } | |
| } | |
| } | |
| } | |
| }); | |
| })(); |
This file contains hidden or 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
| var util = require('util'); | |
| require('./mootools'); | |
| var ansi = { | |
| r: '\033[0;31m', | |
| g: '\033[0;32m', | |
| y: '\033[0;33m', | |
| b: '\033[0;34m', | |
| m: '\033[0;35m', | |
| c: '\033[0;36m', | |
| w: '\033[0;37m', | |
| R: '\033[1;31m', | |
| G: '\033[1;32m', | |
| Y: '\033[1;33m', | |
| B: '\033[1;34m', | |
| M: '\033[1;35m', | |
| C: '\033[1;36m', | |
| W: '\033[1;37m', | |
| u: '\033[4;m', | |
| i: '\033[4;m', | |
| x: '\033[0;0m' | |
| }; | |
| var Tester = new Class({ | |
| Implements: [Events, Options], | |
| options: { | |
| tests: {} | |
| }, | |
| indent: 0, | |
| errors: 0, | |
| initialize: function (opts) { | |
| this.setOptions(opts || {}); | |
| this.runTests(); | |
| }, | |
| dump: function (thing) { | |
| util.puts(util.inspect(thing)); | |
| }, | |
| log: function (msg) { | |
| (this.indent).times( | |
| function () { | |
| util.print(' '); | |
| } | |
| ); | |
| util.puts(msg + ansi.x); | |
| }, | |
| warn: function (msg) { | |
| this.log(ansi.y + msg); | |
| }, | |
| error: function (msg) { | |
| this.log(ansi.r + msg); | |
| }, | |
| yay: function (msg) { | |
| this.log(ansi.g + msg); | |
| }, | |
| group: function (name) { | |
| this.log(name); | |
| this.indent++; | |
| }, | |
| groupEnd: function () { | |
| this.indent--; | |
| }, | |
| runTests: function () { | |
| (function (tests) { | |
| for (var k in tests) if (tests.hasOwnProperty(k)) { | |
| var test = tests[k]; | |
| switch (typeof test) { | |
| case 'object': | |
| this.group(ansi.u + k); | |
| arguments.callee.bind(this)(test); | |
| this.groupEnd(); | |
| break; | |
| case 'function': | |
| this.log(k); | |
| try { | |
| test.bind(this)(); | |
| } catch (e) { | |
| this.error(e); | |
| } | |
| break; | |
| } | |
| } | |
| }).bind(this)(this.options.tests); | |
| if (this.errors) { | |
| if (this.errors == 1) { | |
| this.error('There was an error!'); | |
| } else { | |
| this.error('There were ' + ansi.R + this.errors + ansi.r + ' errors!'); | |
| } | |
| } else { | |
| this.yay('Pass'); | |
| } | |
| }, | |
| assert: function (val, truth) { | |
| if (val != truth) { | |
| this.log(ansi.r + 'Expected ' + val + ' to be ' + truth); | |
| this.errors++; | |
| } | |
| } | |
| }); | |
| exports.Tester = Tester; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment