UNPKG

cucumber

Version:

The official JavaScript implementation of Cucumber.

83 lines (70 loc) 2.84 kB
var Library = function(supportCodeDefinition) { var MISSING_WORLD_INSTANCE_ERROR = "World constructor called back without World instance."; var Cucumber = require('../../cucumber'); var stepDefinitions = Cucumber.Type.Collection(); var hooker = Cucumber.SupportCode.Library.Hooker(); var worldConstructor = Cucumber.SupportCode.WorldConstructor(); var self = { lookupStepDefinitionByName: function lookupStepDefinitionByName(name) { var matchingStepDefinition; stepDefinitions.syncForEach(function(stepDefinition) { if (stepDefinition.matchesStepName(name)) { matchingStepDefinition = stepDefinition; } }); return matchingStepDefinition; }, isStepDefinitionNameDefined: function isStepDefinitionNameDefined(name) { var stepDefinition = self.lookupStepDefinitionByName(name); return (stepDefinition != undefined); }, hookUpFunction: function hookUpFunction(userFunction, scenario, world) { var hookedUpFunction = hooker.hookUpFunction(userFunction, scenario, world); return hookedUpFunction; }, defineAroundHook: function defineAroundHook() { var tagGroupStrings = Cucumber.Util.Arguments(arguments); var code = tagGroupStrings.pop(); hooker.addAroundHookCode(code, {tags: tagGroupStrings}); }, defineBeforeHook: function defineBeforeHook() { var tagGroupStrings = Cucumber.Util.Arguments(arguments); var code = tagGroupStrings.pop(); hooker.addBeforeHookCode(code, {tags: tagGroupStrings}); }, defineAfterHook: function defineAfterHook() { var tagGroupStrings = Cucumber.Util.Arguments(arguments); var code = tagGroupStrings.pop(); hooker.addAfterHookCode(code, {tags: tagGroupStrings}); }, defineStep: function defineStep(name, code) { var stepDefinition = Cucumber.SupportCode.StepDefinition(name, code); stepDefinitions.add(stepDefinition); }, instantiateNewWorld: function instantiateNewWorld(callback) { new worldConstructor(function(world) { if (!world) { throw new Error(MISSING_WORLD_INSTANCE_ERROR); } process.nextTick(function() { // release the constructor callback(world); }); }); } }; var supportCodeHelper = { Around : self.defineAroundHook, Before : self.defineBeforeHook, After : self.defineAfterHook, Given : self.defineStep, When : self.defineStep, Then : self.defineStep, defineStep : self.defineStep, World : worldConstructor }; supportCodeDefinition.call(supportCodeHelper); worldConstructor = supportCodeHelper.World; return self; }; Library.Hooker = require('./library/hooker'); module.exports = Library;