Guest User

Untitled

a guest
Feb 19th, 2018
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. require 'rubygems'
  2. require 'johnson'
  3. require 'spec'
  4.  
  5. RUNTIME = Johnson::Runtime.new
  6. RUNTIME.evaluate <<-JS
  7. var Person = function(name) {
  8. this.name = name;
  9. this.foods = [];
  10. }
  11.  
  12. Person.prototype.meet = function() {
  13. return "Hello there! My name is " + this.name + '.';
  14. }
  15.  
  16. Person.prototype.greet = function(friend) {
  17. return "Hello there " + friend + "! How are you?";
  18. }
  19.  
  20. Person.prototype.eats = function(foods) {
  21. this.foods = arguments;
  22. }
  23. JS
  24.  
  25. module JSClass
  26. def self.create(name)
  27. klass = RUNTIME.evaluate(name)
  28. klass._klass_name = name
  29. klass.extend(ClassMethods)
  30. end
  31.  
  32. module ClassMethods
  33. def new(*args)
  34. js_args = args.map { |a| a.inspect }.join(', ')
  35. RUNTIME.evaluate("new #{_klass_name}(#{js_args})")
  36. end
  37. end
  38. end
  39.  
  40. Person = JSClass.create('Person')
  41.  
  42. describe Person do
  43. before(:each) do
  44. @p = Person.new("Pat")
  45. end
  46.  
  47. it "should meet" do
  48. @p.meet.should == "Hello there! My name is Pat."
  49. end
  50.  
  51. it "should greet" do
  52. @p.greet("Buddy").should == "Hello there Buddy! How are you?"
  53. end
  54.  
  55. it "should work with multiple args" do
  56. @p.foods.length.should == 0
  57. @p.eats("Fish", "Cider", "Eggs")
  58. @p.foods.length.should == 3
  59. end
  60. end
Add Comment
Please, Sign In to add comment