Skip to content

Commit

Permalink
Add support for Before hooks (without tags)
Browse files Browse the repository at this point in the history
  • Loading branch information
josephwilk committed Apr 13, 2010
1 parent 6f410de commit b67e2b4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
15 changes: 11 additions & 4 deletions examples/javascript/features/step_definitions/fib_steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,21 @@ var fibonacciSeries = function(fibonacciLimit) {
return "[" + result.join(", ") + "]";
}

var fibResult;
function assert(expected, actual){
if(expected != actual){
throw 'Expected <' + expected + "> but got <" + actual + ">";
}
}

Before(function(n){
fibResult = 0;
});

When(/^I ask Javascript to calculate fibonacci up to (\d+)$/, function(n){
assert(0, fibResult)
fibResult = fibonacciSeries(parseInt(n));
});

Then(/^it should give me (\[.*\])$/, function(expectedResult){
if(fibResult != expectedResult){
throw 'Expected <' + expectedResult + "> but got <" + fibResult + ">";
}
assert(expectedResult, fibResult)
});
7 changes: 7 additions & 0 deletions lib/cucumber/js_support/js_dsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ var registerStepDefinition = function(regexp, func) {
jsLanguage.addStepDefinition(this, argumentsFrom, regexp, func);
};

var beforeHook = function(func){
jsLanguage.registerJsHook('before', func);
}

var Given = registerStepDefinition;
var When = registerStepDefinition;
var Then = registerStepDefinition;

var Before = beforeHook;

20 changes: 20 additions & 0 deletions lib/cucumber/js_support/js_language.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ def arguments_from(step_name)
end
end

class JsHook
def initialize(js_language, tag_names, proc)
@js_language, @tag_names, @proc = js_language, tag_names, proc
end

def tag_expressions
@tag_names
end

def invoke(location, scenario)
@js_language.current_world.eval("var block = #{@proc.ToString}; block();")
end
end

class JsArg
def initialize(arg)
@arg = arg
Expand Down Expand Up @@ -88,6 +102,12 @@ def addStepDefinition(this, argumentsFrom, regexp, func)
@step_definitions << JsStepDefinition.new(self, regexp, func)
end

#TODO support tag_names
def registerJsHook(phase, proc)
tag_names = []
add_hook(phase, JsHook.new(self, tag_names, proc))
end

def current_world
@world
end
Expand Down

0 comments on commit b67e2b4

Please sign in to comment.