https://github.com/cowboyd/redjs
Shared specs for a Ruby interface to JavaScript.
Useful for different JavaScript interpreters wanting to present a unified interface to Ruby.
Currently used by :
- The Ruby Rhino: http://github.com/cowboyd/therubyrhino
- The Ruby Racer: http://github.com/cowboyd/therubyracer
You'll need RSpec to use the specs RedJS provides.
add this your spec_helper.rb :
require'redjs'moduleRedJSContext=MyECMA::Context# e.g. V8::ContextError=MyECMA::JSError# e.g. Rhino::JSErrorendadd spec/redjs_spec.rb similar to this :
requireFile.expand_path('../spec_helper',File.dirname(__FILE__))require'redjs/load_specs'describeMyJS::Contextdoit_behaves_like'RedJS::Context'endIn case you need to conform with specs defined in a higher version number but can't pass specs from a previous version, you might (temporarily) exclude 'em :
# Gemfile :gem'redjs',:branch=>"0.6",:git=>'git://github.com/cowboyd/redjs.git'# spec_helper.rb :RSpec.configuredo |config|
config.filter_run_excluding:compat=>/(0.4.4)|(0.5.0)/end- should be instantiable using
newwithout arguments or with a hash of options - desired options passed to
Context.new::withemulates JavaScript's with statement (provides a shorthand for writing recurring accesses to the passed objects)
- if a block is provided should yield the context instance
- contexts provide an
evalmethod for evaluating JavaScript code - context instances implement [] for exposing Ruby instances to JavaScript
MyJS::Context.newdo |context|
context["math"]=MyMath.newcontext.eval("math.plus(20, 22)")#=> 42end- common error class for propagating errors that occur in JS into the Ruby side