Skip to content

Commit

Permalink
Showing 14 changed files with 434 additions and 0 deletions.
25 changes: 25 additions & 0 deletions spec/truffle/specs/truffle/interop/boxed_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This
# code is released under a tri EPL/GPL/LGPL license. You can use it,
# redistribute it and/or modify it under the terms of the:
#
# Eclipse Public License version 1.0
# GNU General Public License version 2
# GNU Lesser General Public License version 2.1

require_relative '../../../../ruby/spec_helper'

describe "Truffle::Interop.boxed?" do

it "returns false for empty strings" do
Truffle::Interop.boxed?('').should be_false
end

it "returns true for strings with one byte" do
Truffle::Interop.boxed?('1').should be_true
end

it "returns false for strings with two bytes" do
Truffle::Interop.boxed?('12').should be_false
end

end
36 changes: 36 additions & 0 deletions spec/truffle/specs/truffle/interop/executable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This
# code is released under a tri EPL/GPL/LGPL license. You can use it,
# redistribute it and/or modify it under the terms of the:
#
# Eclipse Public License version 1.0
# GNU General Public License version 2
# GNU Lesser General Public License version 2.1

require_relative '../../../../ruby/spec_helper'

describe "Truffle::Interop.executable?" do

def test_method
end

it "returns true for methods" do
Truffle::Interop.executable?(method(:test_method)).should be_true
end

it "returns true for procs" do
Truffle::Interop.executable?(proc { }).should be_true
end

it "returns true for lambdas" do
Truffle::Interop.executable?(lambda { }).should be_true
end

it "returns false for nil" do
Truffle::Interop.executable?(nil).should be_false
end

it "returns false for strings" do
Truffle::Interop.executable?('hello').should be_false
end

end
37 changes: 37 additions & 0 deletions spec/truffle/specs/truffle/interop/execute_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This
# code is released under a tri EPL/GPL/LGPL license. You can use it,
# redistribute it and/or modify it under the terms of the:
#
# Eclipse Public License version 1.0
# GNU General Public License version 2
# GNU Lesser General Public License version 2.1

require_relative '../../../../ruby/spec_helper'

describe "Truffle::Interop.execute" do

def add(a, b)
a + b
end

it "calls methods" do
Truffle::Interop.execute(method(:add), 14, 2).should == 16
end

it "calls procs" do
Truffle::Interop.execute(proc { |a, b| a + b }, 14, 2).should == 16
end

it "calls lambdas" do
Truffle::Interop.execute(lambda { |a, b| a + b }, 14, 2).should == 16
end

it "doesn't call nil" do
lambda { Truffle::Interop.execute(nil) }.should raise_error(RubyTruffleError)
end

it "doesn't call strings" do
lambda { Truffle::Interop.execute('hello') }.should raise_error(RubyTruffleError)
end

end
29 changes: 29 additions & 0 deletions spec/truffle/specs/truffle/interop/invoke_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This
# code is released under a tri EPL/GPL/LGPL license. You can use it,
# redistribute it and/or modify it under the terms of the:
#
# Eclipse Public License version 1.0
# GNU General Public License version 2
# GNU Lesser General Public License version 2.1

require_relative '../../../../ruby/spec_helper'

describe "Truffle::Interop.invoke" do

class InvokeTestClass

def add(a, b)
a + b
end

end

it "invokes methods using symbols" do
Truffle::Interop.invoke(InvokeTestClass.new, :add, 14, 2).should == 16
end

it "invokes methods using strings" do
Truffle::Interop.invoke(InvokeTestClass.new, 'add', 14, 2).should == 16
end

end
29 changes: 29 additions & 0 deletions spec/truffle/specs/truffle/interop/null_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This
# code is released under a tri EPL/GPL/LGPL license. You can use it,
# redistribute it and/or modify it under the terms of the:
#
# Eclipse Public License version 1.0
# GNU General Public License version 2
# GNU Lesser General Public License version 2.1

require_relative '../../../../ruby/spec_helper'

describe "Truffle::Interop.null?" do

it "returns true for nil" do
Truffle::Interop.null?(nil).should be_true
end

it "returns false for strings" do
Truffle::Interop.null?('').should be_false
end

it "returns false for zero" do
Truffle::Interop.null?(0).should be_false
end

it "returns false for false" do
Truffle::Interop.null?(false).should be_false
end

end
79 changes: 79 additions & 0 deletions spec/truffle/specs/truffle/interop/read_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This
# code is released under a tri EPL/GPL/LGPL license. You can use it,
# redistribute it and/or modify it under the terms of the:
#
# Eclipse Public License version 1.0
# GNU General Public License version 2
# GNU Lesser General Public License version 2.1

require_relative '../../../../ruby/spec_helper'

describe "Truffle::Interop.read" do

class ReadInstanceVariable

def initialize
@foo = 14
end

end

class HasMethod

def foo
14
end

end

class HasIndex

def [](n)
14
end

end

it "reads a byte from a string" do
Truffle::Interop.read('123', 1).should == '2'.ord
end

describe "reads an instance variable if given an @name" do
it "as a symbol" do
Truffle::Interop.read(ReadInstanceVariable.new, :@foo).should == 14
end

it "as a string" do
Truffle::Interop.read(ReadInstanceVariable.new, '@foo').should == 14
end
end

describe "calls #[] if there isn't a method with the same name" do
it "as a symbol" do
Truffle::Interop.read(HasIndex.new, :foo).should == 14
end

it "as a string" do
Truffle::Interop.read(HasIndex.new, 'foo').should == 14
end
end

describe "calls a method if there is a method with the same name" do
it "as a symbol" do
Truffle::Interop.read(HasMethod.new, :foo).should == 14
end

it "as a string" do
Truffle::Interop.read(HasMethod.new, 'foo').should == 14
end
end

it "can be used to index an array" do
Truffle::Interop.read([1, 2, 3], 1).should == 2
end

it "can be used to index a hash" do
Truffle::Interop.read({1 => 2, 3 => 4, 5 => 6}, 3).should == 4
end

end
37 changes: 37 additions & 0 deletions spec/truffle/specs/truffle/interop/size_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This
# code is released under a tri EPL/GPL/LGPL license. You can use it,
# redistribute it and/or modify it under the terms of the:
#
# Eclipse Public License version 1.0
# GNU General Public License version 2
# GNU Lesser General Public License version 2.1

require_relative '../../../../ruby/spec_helper'

describe "Truffle::Interop.size" do

class InteropSizeClass

def size
14
end

end

it "returns the size of an array" do
Truffle::Interop.size([1, 2, 3]).should == 3
end

it "returns the size of an array" do
Truffle::Interop.size({a: 1, b: 2, c: 3}).should == 3
end

it "returns the size of an string" do
Truffle::Interop.size('123').should == 3
end

it "returns the size of any object with a size method" do
Truffle::Interop.size(InteropSizeClass.new).should == 14
end

end
29 changes: 29 additions & 0 deletions spec/truffle/specs/truffle/interop/sizep_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This
# code is released under a tri EPL/GPL/LGPL license. You can use it,
# redistribute it and/or modify it under the terms of the:
#
# Eclipse Public License version 1.0
# GNU General Public License version 2
# GNU Lesser General Public License version 2.1

require_relative '../../../../ruby/spec_helper'

describe "Truffle::Interop.size?" do

it "returns true for arrays" do
Truffle::Interop.size?([]).should be_true
end

it "returns true for hashes" do
Truffle::Interop.size?({}).should be_true
end

it "returns true for strings" do
Truffle::Interop.size?('').should be_true
end

it "returns false for nil" do
Truffle::Interop.size?(nil).should be_false
end

end
29 changes: 29 additions & 0 deletions spec/truffle/specs/truffle/interop/unbox_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This
# code is released under a tri EPL/GPL/LGPL license. You can use it,
# redistribute it and/or modify it under the terms of the:
#
# Eclipse Public License version 1.0
# GNU General Public License version 2
# GNU Lesser General Public License version 2.1

require_relative '../../../../ruby/spec_helper'

describe "Truffle::Interop.unbox" do

it "doesn't work on empty strings" do
lambda { Truffle::Interop.unbox('') }.should raise_error(RubyTruffleError)
end

it "returns the first byte on strings with one byte" do
Truffle::Interop.unbox('1').should == '1'.ord
end

it "returns the first byte on strings with two bytes" do
Truffle::Interop.unbox('1').should == '1'.ord
end

it "returns nil for nil" do
Truffle::Interop.unbox(nil).should be_nil
end

end
91 changes: 91 additions & 0 deletions spec/truffle/specs/truffle/interop/write_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This
# code is released under a tri EPL/GPL/LGPL license. You can use it,
# redistribute it and/or modify it under the terms of the:
#
# Eclipse Public License version 1.0
# GNU General Public License version 2
# GNU Lesser General Public License version 2.1

require_relative '../../../../ruby/spec_helper'

describe "Truffle::Interop.write" do

class HasMethod

def foo=(value)
@called = true
end

def called?
@called
end

end

class HasIndexSet

def []=(n, value)
@called = true
end

def called?
@called
end

end

describe "writes an instance variable if given an @name" do
it "as a symbol" do
object = Object.new
Truffle::Interop.write ReadInstanceVariable.new, :@foo, 14
object.instance_variable_get('foo').should == 14
end

it "as a string" do
object = Object.new
Truffle::Interop.write ReadInstanceVariable.new, '@foo', 14
object.instance_variable_get('foo').should == 14
end
end

describe "calls #[]= if there isn't a method with the same name" do
it "as a symbol" do
object = HasIndexSet.new
Truffle::Interop.write object, :foo, 14
object.called?.should be_true
end

it "as a string" do
object = HasIndexSet.new
Truffle::Interop.write object, 'foo', 14
object.called?.should be_true
end
end

describe "calls a method if there is a method with the same name plus =" do
it "as a symbol" do
object = HasMethod.new
Truffle::Interop.write object, :foo, 14
object.called?.should be_true
end

it "as a string" do
object = HasMethod.new
Truffle::Interop.write object, 'foo', 14
object.called?.should be_true
end
end

it "can be used to assign an array" do
array = [1, 2, 3]
Truffle::Interop.write array, 1, 14
array[1].should == 14
end

it "can be used to assign a hash" do
hash = {1 => 2, 3 => 4, 5 => 6}
Truffle::Interop.write hash, 3, 14
hash[3].should == 14
end

end
2 changes: 2 additions & 0 deletions spec/truffle/tags/truffle/interop/null_tags.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fails:Truffle::Interop.null? returns false for zero
fails:Truffle::Interop.null? returns false for false
2 changes: 2 additions & 0 deletions spec/truffle/tags/truffle/interop/read_tags.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fails:Truffle::Interop.read can be used to index an array
fails:Truffle::Interop.read can be used to index a hash
3 changes: 3 additions & 0 deletions spec/truffle/tags/truffle/interop/sizep_tags.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fails:Truffle::Interop.size? returns true for arrays
fails:Truffle::Interop.size? returns true for hashes
fails:Truffle::Interop.size? returns true for strings
6 changes: 6 additions & 0 deletions spec/truffle/tags/truffle/interop/write_tags.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fails:Truffle::Interop.write can be used to assign an array
fails:Truffle::Interop.write can be used to assign a hash
fails:Truffle::Interop.write writes an instance variable if given an @name as a symbol
fails:Truffle::Interop.write writes an instance variable if given an @name as a string
fails:Truffle::Interop.write calls #[]= if there isn't a method with the same name as a symbol
fails:Truffle::Interop.write calls #[]= if there isn't a method with the same name as a string

1 comment on commit cb15935

@chrisseaton
Copy link
Contributor Author

@chrisseaton chrisseaton commented on cb15935 May 11, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eregon @jtulach @grimmerm I wrote specs for our interop, and lots of them fail, such as the array ones, so that's why your interop tests aren't working

Sorry, something went wrong.

Please sign in to comment.