diff --git a/index-v1.js b/index-v1.js new file mode 100644 index 0000000..fc5fe22 --- /dev/null +++ b/index-v1.js @@ -0,0 +1,78 @@ + +var _d = console.log.bind( console, 'dbg: ' ); +var fs = require('fs'); + +function DescribeBlock( parentBlock ){ + _d( 'DescribeBlock:init', parentBlock ); + this.parent = parentBlock; + if( parentBlock ){ + parentBlock.addChild( this ); + } + + this.children = []; + this.its = []; + this.beforeEach = null; + this.afterEach = null; +} + +DescribeBlock.prototype.addChild = function( child ){ + this.children.push( child ); +}; + + + + +function SimpleMocha(){ + + this.describe = this.describe.bind( this ); + this.it = this.it.bind( this ); + this.before = this.before.bind( this ); + this.after = this.after.bind( this ); + this.beforeEach = this.beforeEach.bind( this ); + this.afterEach = this.afterEach.bind( this ); + + this.rootDescribeBlock = new DescribeBlock(); + this.currentDescribeBlock = this.rootDescribeBlock; +} + +SimpleMocha.prototype.describe = function( description, fn ){ + var parentDescribeBlock = this.currentDescribeBlock; + var thisDescribeBlock = new DescribeBlock( parentDescribeBlock ); + + this.currentDescribeBlock = thisDescribeBlock; + fn(); + this.currentDescribeBlock = parentDescribeBlock; +}; + +SimpleMocha.prototype.it = function(){}; +SimpleMocha.prototype.before = function( fn ){ + this.currentDescribeBlock.beforeFn = fn; +}; + +SimpleMocha.prototype.after = function(){}; +SimpleMocha.prototype.beforeEach = function(){}; +SimpleMocha.prototype.afterEach = function(){}; + + +SimpleMocha.DescribeBlock = DescribeBlock; + + +SimpleMocha.load = function( fileName ){ + var sm = new SimpleMocha(); + var describe = sm.describe; + var it = sm.it; + var before = sm.before; + var after = sm.after; + var beforeEach = sm.after; + var afterEach = sm.after; + + var code = fs.readFileSync( fileName, 'utf-8' ); + + eval( code ); + + return sm; +}; + + + +module.exports = SimpleMocha; diff --git a/test/data/dummy.spec.js b/test/data/dummy.spec.js new file mode 100644 index 0000000..0a506a5 --- /dev/null +++ b/test/data/dummy.spec.js @@ -0,0 +1,52 @@ +/* globals describe, it, before */ + +var messageLog = []; +var pr = function( msg ){ + messageLog.push( msg ); + // console.log( '--', msg ); +} + +function asyncFn( args ){ + return function( cb ){ + setTimeout( function(){ + pr( args.msg ); + if( args.error ){ + if( args.isThrow ){ + throw args.error; + } else { + return cb( args.error ); + } + } else { + return cb( ); + } + }); + } +} + + +var beforeFn = asyncFn({ + msg: 'Before_block', + delay: 100, + error: null, +}); + +var it1 = asyncFn({ + msg: 'It_1', + delay: 10, + error: null, +}); + +var it2 = asyncFn({ + msg: 'It_2', + delay: 500, + error: null, +}); + + +describe( 'describe', function(){ + + before( beforeFn ); + it( 'it1', it1 ); + it( 'it2', it2 ); + +}); diff --git a/test/simple-mocha.spec.js b/test/simple-mocha.spec.js new file mode 100644 index 0000000..edbaabd --- /dev/null +++ b/test/simple-mocha.spec.js @@ -0,0 +1,30 @@ +/* globals describe, it, */ + +var SimpleMocha = require( __dirname + '/../index-v1' ); + +var assert = require('assert'); + + +describe( 'Simple before block + it s ', function(){ + + it( 'should parse simple test file' , function( done ){ + + var runner = SimpleMocha.load( __dirname + '/data/dummy.spec.js' ); + + assert( runner ); + + var describeBlock = runner.rootDescribeBlock; + + assert( describeBlock ); + assert( describeBlock instanceof SimpleMocha.DescribeBlock ); + assert.equal( describeBlock.children.length, 1 ); + + var firsDescribeBlock = describeBlock.children[0]; + + + assert( firsDescribeBlock instanceof SimpleMocha.DescribeBlock ); + assert( firsDescribeBlock.beforeFn ); + + done(); + }); +})