From 1871514d378f7cafd2276d5092ed148346edf9aa Mon Sep 17 00:00:00 2001 From: "Harish.K" Date: Fri, 1 Apr 2016 01:16:54 +0530 Subject: [PATCH] Feat: parse it blocks Tests passed --- index-v1.js | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/index-v1.js b/index-v1.js index 89f26e9..934a7d2 100644 --- a/index-v1.js +++ b/index-v1.js @@ -1,10 +1,35 @@ + // var _d = console.log.bind( console, 'dbg: ' ); var _d = function(){}; var fs = require('fs'); -function DescribeBlock( parentBlock ){ + + +function mkAsyncFn( fn ){ + return function( done ){ + setTimeout( function(){ + fn(); + done(); + }, 1 ); + }; +} + + + +function ItBlock( description, fn ){ + var isAsync = ( fn.length > 0 ); + + this.description = description; + this.fn = isAsync ? fn : mkAsyncFn( fn ); +} + + + +function DescribeBlock( description, parentBlock ){ _d( 'DescribeBlock:init', parentBlock ); + + this.description = description || ''; this.parent = parentBlock; if( parentBlock ){ parentBlock.addChild( this ); @@ -16,11 +41,17 @@ function DescribeBlock( parentBlock ){ this.afterEach = null; } + DescribeBlock.prototype.addChild = function( child ){ this.children.push( child ); }; +DescribeBlock.prototype.addItBlock = function( description, fn ){ + this.its.push( new ItBlock( description, fn ) ); +}; + + function SimpleMocha(){ @@ -36,20 +67,27 @@ function SimpleMocha(){ this.currentDescribeBlock = this.rootDescribeBlock; } + SimpleMocha.prototype.describe = function( description, fn ){ var parentDescribeBlock = this.currentDescribeBlock; - var thisDescribeBlock = new DescribeBlock( parentDescribeBlock ); + var thisDescribeBlock = new DescribeBlock( description, parentDescribeBlock ); this.currentDescribeBlock = thisDescribeBlock; fn(); this.currentDescribeBlock = parentDescribeBlock; }; -SimpleMocha.prototype.it = function(){}; + +SimpleMocha.prototype.it = function( description, fn ){ + this.currentDescribeBlock.addItBlock( description, fn ); +}; + + SimpleMocha.prototype.before = function( fn ){ this.currentDescribeBlock.beforeFn = fn; }; + SimpleMocha.prototype.after = function(){}; SimpleMocha.prototype.beforeEach = function(){}; SimpleMocha.prototype.afterEach = function(){};