Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 8cc3da55c7
Fetching contributors…

Cannot retrieve contributors at this time

file 99 lines (82 sloc) 1.811 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
require 'test_helper'

class InteropTest < MiniTest::Unit::TestCase
  module Mixin
    def mixin_method
      1234
    end
  end

  class Foo
    def bar
      34
    end
  end

  def test_toplevel_namespace
    assert_equal Fixnum, compile("Ruby.Fixnum")
  end

  def test_define_ruby_method
    compile(<<-CODE)
Ruby.Array.def('sum', ->
@ruby('reduce', '+'.ruby('to_sym'))
end)
CODE

    assert_respond_to [], :sum
    assert_equal 6, [1,2,3].sum
  end

  def test_include_ruby_module
    compile(<<-CODE)
Ruby.InteropTest.Foo.include(Ruby.InteropTest.Mixin)
CODE

    foo = Foo.new
    assert_respond_to foo, :mixin_method
    assert_equal 1234, foo.mixin_method
  end

  def test_extend_ruby_module
    foo = compile(<<-CODE)
foo = Ruby.InteropTest.Foo.new()
foo.extend(Ruby.InteropTest.Mixin)
CODE

    assert_respond_to foo, :mixin_method
    assert_equal 1234, foo.mixin_method
  end

  def test_create_ruby_module
    mod = compile(<<-CODE)
Ruby.Module.create(->
@def('answer', ->
42
end)
end)
CODE

    foo = Foo.new
    foo.extend(mod)

    assert_respond_to foo, :answer
    assert_equal 42, foo.answer
  end

  def test_create_ruby_class
    kls = compile(<<-CODE)
Ruby.Class.create(->
@def('answer', ->
42
end)
end)
CODE

    foo = kls.new

    assert_respond_to foo, :answer
    assert_equal 42, foo.answer
  end

  def test_call_noscript_from_ruby
    john = compile(<<-CODE)
Object.clone({
name: 'John',
age: ->
20
end,
money: -> day of month
30 - day of month
end
})
CODE

    assert_equal "John", john.name
    assert_equal 20, john.age
    assert_equal 10, john.money(20)
  end
end
Something went wrong with that request. Please try again.