Skip to content

Commit

Permalink
Handle PyString with multiple lines. Support tables and pystrings whe…
Browse files Browse the repository at this point in the history
…n using Transform
  • Loading branch information
josephwilk committed Apr 18, 2010
1 parent 0027cfd commit 63a94cc
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
4 changes: 3 additions & 1 deletion examples/javascript/features/fibonacci.feature
Expand Up @@ -18,10 +18,12 @@ Feature: Fibonacci
| 100 | [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] |

Scenario: Single series tested via a PyString
When I ask Javascript to calculate fibonacci up to 2
When I ask Javascript to calculate fibonacci up to 2 with formatting
Then it should give me:
"""
[1, 1]
"""

Scenario: Single series tested via a Step Table
Expand Down
4 changes: 4 additions & 0 deletions examples/javascript/features/lib/fibonacci.js
Expand Up @@ -12,4 +12,8 @@ var fibonacciSeries = function(fibonacciLimit) {
i++;
}
return "[" + result.join(", ") + "]";
}

var fibonacciSeriesFormatted = function(fibonacciLimit){
return "\n" + fibonacciSeries(fibonacciLimit) + "\n"
}
5 changes: 5 additions & 0 deletions examples/javascript/features/step_definitions/fib_steps.js
Expand Up @@ -19,6 +19,11 @@ When(/^I ask Javascript to calculate fibonacci up to (\d+)$/, function(n){
fibResult = fibonacciSeries(n);
});

When(/^I ask Javascript to calculate fibonacci up to (\d+) with formatting$/, function(n){
assertEqual(0, fibResult)
fibResult = fibonacciSeriesFormatted(n);
});

Then(/^it should give me (\[.*\])$/, function(expectedResult){
assertEqual(expectedResult, fibResult)
});
Expand Down
15 changes: 10 additions & 5 deletions lib/cucumber/js_support/js_language.rb
Expand Up @@ -5,6 +5,10 @@
module Cucumber
module JsSupport

def self.argument_safe_string(string)
"'#{string.to_s.gsub("\n", '\n')}'"
end

class JsWorld
def initialize
@world = V8::Context.new
Expand All @@ -14,10 +18,13 @@ def execute(js_function, args=[])
js_args = args.map do |arg|
if arg.is_a?(Ast::Table)
"new CucumberJsDsl.Table(#{arg.raw.inspect})"
elsif arg.is_a?(Ast::PyString)
JsSupport.argument_safe_string(arg)
else
"'#{arg}'"
end
end

@world.eval("(#{js_function.ToString})(#{js_args.join(',')});")
end

Expand Down Expand Up @@ -67,14 +74,12 @@ def invoke(location, scenario)

class JsTransform
def initialize(js_language, regexp, js_function)
@js_language, @regexp, @js_function = js_language, regexp, js_function
@js_language, @regexp, @js_function = js_language, regexp.ToString, js_function
end

def match(arg)
#TODO: Tables get matched as multiline strings which makes the regexp
# match a syntax error. Ignoring table args for the moment
return if arg.is_a? Ast::Table
matches = eval_js "#{@regexp.ToString}.exec('#{arg}');"
arg = JsSupport.argument_safe_string(arg)
matches = eval_js "#{@regexp}.exec(#{arg});"
matches ? matches[1..-1] : nil
end

Expand Down

0 comments on commit 63a94cc

Please sign in to comment.