bdd-lazy-var
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -5,4 +5,5 @@ var Mocha = require('mocha'); | ||
var currentlyDefinedVarField = Symbol.for('_definesVariable'); | ||
var parentDefinitionsField = Symbol.for('_parentContextForLazyVars'); | ||
var currentlyRetrievedVarField = Symbol.for('__definesVariable'); | ||
var parentDefinitionsField = Symbol.for('__parentContextForLazyVars'); | ||
var isExecutedField = Symbol.for('__isExecuted'); | ||
@@ -22,12 +23,12 @@ module.exports = Mocha.interfaces['bdd-lazy-var'] = function(rootSuite) { | ||
if (varName === currentTestContext[currentlyDefinedVarField]) { | ||
currentTestContext = getParentContextFor(varName, currentTestContext); | ||
if (varName === currentTestContext[currentlyRetrievedVarField]) { | ||
currentTestContext = getParentContextFor(varName, originalSuite); | ||
} | ||
try { | ||
currentTestContext[currentlyDefinedVarField] = varName; | ||
currentTestContext[currentlyRetrievedVarField] = varName; | ||
return lazyVar.getOrCreate(currentTestContext, varName); | ||
return currentTestContext[varName]; | ||
} finally { | ||
delete currentTestContext[currentlyDefinedVarField]; | ||
delete currentTestContext[currentlyRetrievedVarField]; | ||
currentTestContext = originalSuite; | ||
@@ -38,7 +39,9 @@ } | ||
context.def = function(varName, definition) { | ||
if (lazyVar.isDefined(currentlyDefinedSuite.ctx, varName)) { | ||
registerParentContextFor(varName, currentlyDefinedSuite); | ||
var suite = currentlyDefinedSuite; | ||
if (suite.parent && lazyVar.isDefined(suite.parent.ctx, varName)) { | ||
registerParentContextFor(varName, suite); | ||
} | ||
return lazyVar.register(currentlyDefinedSuite.ctx, varName, definition); | ||
return lazyVar.register(suite.ctx, varName, definition); | ||
}; | ||
@@ -60,4 +63,4 @@ | ||
runTests.apply(this, arguments); | ||
context.afterEach(lazyVar.cleanUp); | ||
context.after(lazyVar.cleanUp); | ||
context.afterEach(cleanUp); | ||
context.after(cleanUp); | ||
@@ -72,2 +75,6 @@ currentlyDefinedSuite = null; | ||
function cleanUp() { | ||
lazyVar.cleanUp(this); | ||
} | ||
function registerParentContextFor(varName, suite) { | ||
@@ -82,3 +89,7 @@ if (!suite.ctx.hasOwnProperty(parentDefinitionsField)) { | ||
function getParentContextFor(varName, testContext) { | ||
return testContext[parentDefinitionsField] ? testContext[parentDefinitionsField][varName] : null; | ||
if (!testContext[parentDefinitionsField]) { | ||
throw new Error('Unknown parent variable "' + varName + '".'); | ||
} | ||
return testContext[parentDefinitionsField][varName]; | ||
} | ||
@@ -85,0 +96,0 @@ |
@@ -1,68 +0,41 @@ | ||
var varsToClean = []; | ||
var prop = require('./symbol').for; | ||
var createdVarsPropName = prop('__createdVars'); | ||
function definitionNameFor(varName) { | ||
return prop('__' + varName + 'LazyDefinition'); | ||
} | ||
var lazyVarsPropName = prop('_lazyVars'); | ||
function getDefinition(context, definitionName) { | ||
return context[definitionName]; | ||
} | ||
function markVarAsCreated(context, definitionName) { | ||
if (!context.hasOwnProperty(createdVarsPropName)) { | ||
context[createdVarsPropName] = {}; | ||
} | ||
context[createdVarsPropName][definitionName] = getDefinition(context, definitionName); | ||
} | ||
function isCreated(context, definitionName) { | ||
var calledDefinitions = context[createdVarsPropName] || {}; | ||
return calledDefinitions[definitionName] === getDefinition(context, definitionName); | ||
} | ||
var lazyVar = { | ||
register: function(context, name, definition) { | ||
var propName = definitionNameFor(name); | ||
var hasOwnVariable = context.hasOwnProperty(name) && context.hasOwnProperty(lazyVarsPropName); | ||
if (context.hasOwnProperty(propName)) { | ||
if (hasOwnVariable && lazyVar.isDefined(context, name)) { | ||
throw new Error('Cannot define "' + name + '" variable twice in the same suite.'); | ||
} | ||
context[propName] = definition; | ||
}, | ||
if (!context.hasOwnProperty(lazyVarsPropName)) { | ||
context[lazyVarsPropName] = { defined: {}, created: {} }; | ||
} | ||
isDefined: function(context, name) { | ||
return context && definitionNameFor(name) in context; | ||
}, | ||
var metatadata = context[lazyVarsPropName]; | ||
getOrCreate: function(context, name) { | ||
if (!lazyVar.isDefined(context, name)) { | ||
throw new Error('Lazy variable "' + name + '" is not defined.'); | ||
} | ||
metatadata.defined[name] = true; | ||
var definitionName = definitionNameFor(name); | ||
var definition = getDefinition(context, definitionName); | ||
Object.defineProperty(context, name, { | ||
get: function() { | ||
if (!metatadata.created.hasOwnProperty(name)) { | ||
metatadata.created[name] = typeof definition === 'function' ? definition() : definition; | ||
} | ||
if (!(name in context) || !isCreated(context, definitionName)) { | ||
context[name] = typeof definition === 'function' ? definition() : definition; | ||
markVarAsCreated(context, definitionName); | ||
varsToClean.push({ context: context, name: name }); | ||
} | ||
return context[name]; | ||
return metatadata.created[name]; | ||
} | ||
}); | ||
}, | ||
cleanUp: function() { | ||
varsToClean.forEach(lazyVar.destroy); | ||
varsToClean.length = 0; | ||
isDefined: function(context, name) { | ||
var hasLazyVars = context && context[lazyVarsPropName]; | ||
return hasLazyVars && context[lazyVarsPropName].defined[name]; | ||
}, | ||
destroy: function(variable) { | ||
if (variable.context.hasOwnProperty(variable.name)) { | ||
delete variable.context[variable.name]; | ||
delete variable.context[createdVarsPropName]; | ||
cleanUp: function(context) { | ||
if (context.hasOwnProperty(lazyVarsPropName)) { | ||
context[lazyVarsPropName].created = {}; | ||
} | ||
@@ -69,0 +42,0 @@ } |
{ | ||
"name": "bdd-lazy-var", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"author": "Sergii Stotskyi", | ||
@@ -5,0 +5,0 @@ "description": "Provides \"ui\" for mocha.js which allows to define lazy variables and subjects", |
@@ -34,2 +34,6 @@ describe('Lazy variables interface', function() { | ||
}); | ||
it('allows to access variable using "this" keyword (i.e. "this.lazyVarName")', function() { | ||
expect(this.var).to.equal(get('var')); | ||
}); | ||
}); | ||
@@ -64,3 +68,3 @@ | ||
after('uses newly generated variable', function() { | ||
after('uses newly created variable', function() { | ||
expect(get('var')).to.equal(valueInAfterEach + 1); | ||
@@ -184,2 +188,22 @@ }); | ||
}); | ||
describe('when variable is used inside "afterEach" of parent and child suites', function() { | ||
var subjectInChild; | ||
subject(function() { | ||
return {}; | ||
}); | ||
describe('parent suite', function() { | ||
afterEach(function() { | ||
expect(subject()).to.equal(subjectInChild); | ||
}); | ||
describe('child suite', function() { | ||
it('uses the same variable instance', function() { | ||
subjectInChild = subject(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
303
22676
11