Wednesday, June 2, 2010

No Joy for Cucumber on V8

‹prev | My Chain | next›

Up tonight, I'd like to see if I can write some Cucumber scenarios in the v8 Javascript engine. Joseph Wilk recently wrote a blog post on how to do this, so hopefully it will prove straight forward.

I install therubyracer gem as normal:
gem install therubyracer
I also checkout the latest version of cucumber to be sure that I have the recent javascript bindings. The javascript example are located in the examples/javascript directory, along with a Rakefile to run them. Unfortunately, when I do run the examples, I find:
/home/cstrom/.rvm/rubies/ruby-1.8.7-p249/bin/ruby -I "/home/cstrom/repos/cuke.real/lib:lib" "/home/cstrom/repos/cuke.real/bin/cucumber" 
Uncaught Error: undefined method `ToString' for #: /home/cstrom/repos/cuke.real/bin/../lib/cucumber/js_support/js_dsl.js:12 (V8::JavascriptError)
/home/cstrom/.rvm/gems/ruby-1.8.7-p249/gems/therubyracer-0.7.0/lib/v8/context.rb:33:in `eval'
/home/cstrom/.rvm/gems/ruby-1.8.7-p249/gems/therubyracer-0.7.0/lib/v8/context.rb:38:in `evaluate'
/home/cstrom/.rvm/gems/ruby-1.8.7-p249/gems/therubyracer-0.7.0/lib/v8/context.rb:43:in `load'
/home/cstrom/.rvm/gems/ruby-1.8.7-p249/gems/therubyracer-0.7.0/lib/v8/context.rb:42:in `open'
/home/cstrom/.rvm/gems/ruby-1.8.7-p249/gems/therubyracer-0.7.0/lib/v8/context.rb:42:in `load'
/home/cstrom/repos/cuke.real/bin/../lib/cucumber/js_support/js_language.rb:31:in `send'
/home/cstrom/repos/cuke.real/bin/../lib/cucumber/js_support/js_language.rb:31:in `method_missing'
/home/cstrom/repos/cuke.real/bin/../lib/cucumber/js_support/js_language.rb:117:in `load_code_file'
/home/cstrom/repos/cuke.real/bin/../lib/cucumber/step_mother.rb:84:in `load_code_file'
/home/cstrom/repos/cuke.real/bin/../lib/cucumber/step_mother.rb:76:in `load_code_files'
/home/cstrom/repos/cuke.real/bin/../lib/cucumber/step_mother.rb:75:in `each'
/home/cstrom/repos/cuke.real/bin/../lib/cucumber/step_mother.rb:75:in `load_code_files'
/home/cstrom/repos/cuke.real/bin/../lib/cucumber/cli/main.rb:56:in `execute!'
/home/cstrom/repos/cuke.real/bin/../lib/cucumber/cli/main.rb:25:in `execute'
/home/cstrom/repos/cuke.real/bin/cucumber:8
rake aborted!
Command failed with status (1): [/home/cstrom/.rvm/rubies/ruby-1.8.7-p249/b...]

(See full trace by running task with --trace)
I am not sure what the deal is with the capital ToString. Converting it to toString has no effect. Converting it to to_s has some effect, but not enough. Eventually, I happen across an issue opened by Corey Haines on therubyracer's github issue tracker. Nice! That is the same backtrace I am getting and the problem looks to be solved by Charles Lowell himself.

After checking out Charles's fork of Cucumber, I am able to run the Cucumber feature, but the fibonnaci function seems to always return an empty array:
  @fibonacci
Scenario Outline: Series # features/fibonacci.feature:7
When I ask Javascript to calculate fibonacci up to <n> #
Then it should give me <series> #

Examples:
| n | series |
| 1 | [] |
Uncaught Expected <'[]'> but got <[]>: features/support/env.js:3 (V8::JavascriptError)
features/fibonacci.feature:9:in `Then it should give me <series>'
| 2 | [1, 1] |
Uncaught Expected <'[1, 1]'> but got <[]>: features/support/env.js:3 (V8::JavascriptError)
features/fibonacci.feature:9:in `Then it should give me <series>'
| 3 | [1, 1, 2] |
Uncaught Expected <'[1, 1, 2]'> but got <[]>: features/support/env.js:3 (V8::JavascriptError)
features/fibonacci.feature:9:in `Then it should give me <series>'
| 4 | [1, 1, 2, 3] |
Uncaught Expected <'[1, 1, 2, 3]'> but got <[]>: features/support/env.js:3 (V8::JavascriptError)
features/fibonacci.feature:9:in `Then it should give me <series>'
| 6 | [1, 1, 2, 3, 5] |
Uncaught Expected <'[1, 1, 2, 3, 5]'> but got <[]>: features/support/env.js:3 (V8::JavascriptError)
features/fibonacci.feature:9:in `Then it should give me <series>'
| 9 | [1, 1, 2, 3, 5, 8] |
Uncaught Expected <'[1, 1, 2, 3, 5, 8]'> but got <[]>: features/support/env.js:3 (V8::JavascriptError)
features/fibonacci.feature:9:in `Then it should give me <series>'
| 100 | [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] |
Uncaught Expected <'[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]'> but got <[]>: features/support/env.js:3 (V8::JavascriptError)
features/fibonacci.feature:9:in `Then it should give me <series>'
This one has me stumped, unfortunately. The problem seems to be with the fibonnaci number passed into the scenario. It ought to be a string and mostly behaves like a string, but, when I throw the parseInt value
var fibonacciSeries = function(fibonacciLimit) {
var result = Array();
var currentfibonacciValue = fibonacci(1);
var i = 2;
throw " " + currentfibonacciValue + " " + fibonacciLimit + " " + typeof(fibonacciLimit) + " " + parseInt("" + fibonacciLimit.toString() + "", 10);
while(currentfibonacciValue < parseInt(fibonacciLimit)) {
result.push(currentfibonacciValue);
throw result.join(",");

currentfibonacciValue = fibonacci(i);
i++;
}
// result = [1,1];
return "[" + result.join(", ") + "]";
}
I always end up with NaN:
 @fibonacci
Scenario Outline: Series # features/fibonacci.feature:7
When I ask Javascript to calculate fibonacci up to <n> #
Then it should give me <series> #

Examples:
| n | series |
| 1 | [] |
Uncaught 1 '1' string NaN: features/lib/fibonacci.js:9 (V8::JavascriptError)
features/fibonacci.feature:8:in `When I ask Javascript to calculate fibonacci up to <n>'
| 2 | [1, 1] |
Uncaught 1 '2' string NaN: features/lib/fibonacci.js:9 (V8::JavascriptError)
features/fibonacci.feature:8:in `When I ask Javascript to calculate fibonacci up to <n>'
...
I am a bit disappointed to have to call it a night here. Tomorrow, I will pick it back up with fresh eyes and possibly look to install the pre-release version of therubyracer in the hopes of resolving this.


Day #122

3 comments:

  1. Hi,

    The API for therubyracer has been undergoing some changes which have had some knock on effects for Cucumber. It looks to me like you are using the latest therubyracer but not my specific fork of cucumber which uses the newer therubyracer api. This will get merged into main Cucumber eventually but if you are impatient try it out: http://github.com/josephwilk/cucumber.

    Note that I'm now looking at some work with 0.7.1 of the therubyracer (with help from @cowboyd) so try and stick with 0.7.0.

    Please drop me an email if you have any further problems.

    ReplyDelete
  2. I've just pushed the fixed to my branch for therubyracer 0.7.1 so thats safe to try out now.

    ReplyDelete
  3. Joseph,

    Thanks for the pointers! I was not using your fork of Cucumber. I was only using the first patch in your fork from @cowboyd. I was eventually able to get Aslak's Cucumber working with just this:

    http://gist.github.com/424863

    I believe that works with both 0.7.0 and 0.7.1 of therubyracer.

    I did go back to try your fork with 0.7.1 and, yep, it works like a charm. Thanks!

    -Chris

    ReplyDelete