Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'master' into truffle-head
  • Loading branch information
chrisseaton committed Mar 18, 2015
2 parents b6813b4 + bc11f01 commit 334ef87
Show file tree
Hide file tree
Showing 84 changed files with 451 additions and 1,288 deletions.
4 changes: 2 additions & 2 deletions spec/ruby/core/basicobject/__id__spec.rb
@@ -1,6 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../../../shared/object/object_id', __FILE__)
require File.expand_path('../../../shared/kernel/object_id', __FILE__)

describe "BasicObject#__id__" do
it_behaves_like :basic_object_id, :__id__, BasicObject
it_behaves_like :object_id, :__id__, BasicObject
end
7 changes: 0 additions & 7 deletions spec/ruby/core/basicobject/ancestors_spec.rb

This file was deleted.

7 changes: 0 additions & 7 deletions spec/ruby/core/basicobject/class_spec.rb

This file was deleted.

33 changes: 33 additions & 0 deletions spec/ruby/core/basicobject/fixtures/classes.rb
@@ -0,0 +1,33 @@
module BasicObjectSpecs
class IVars
def initialize
@secret = 99
end
end

module InstExec
def self.included(base)
base.instance_exec { @@count = 2 }
end
end

module InstExecIncluded
include InstExec
end

module InstEvalCVar
instance_eval { @@count = 2 }
end

class InstEvalConst
INST_EVAL_CONST_X = 2
end

module InstEvalOuter
module Inner
obj = InstEvalConst.new
X_BY_STR = obj.instance_eval("INST_EVAL_CONST_X") rescue nil
X_BY_BLOCK = obj.instance_eval { INST_EVAL_CONST_X } rescue nil
end
end
end
6 changes: 6 additions & 0 deletions spec/ruby/core/basicobject/initialize_spec.rb
Expand Up @@ -4,4 +4,10 @@
it "is a private instance method" do
BasicObject.should have_private_instance_method(:initialize)
end

it "does not accept arguments" do
lambda {
BasicObject.new("This", "makes it easier", "to call super", "from other constructors")
}.should raise_error(ArgumentError)
end
end
138 changes: 138 additions & 0 deletions spec/ruby/core/basicobject/instance_eval_spec.rb
@@ -1,6 +1,11 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)

describe "BasicObject#instance_eval" do
before :each do
ScratchPad.clear
end

it "is a public instance method" do
BasicObject.should have_public_instance_method(:instance_eval)
end
Expand All @@ -14,4 +19,137 @@
a = BasicObject.new
a.instance_eval('self').equal?(a).should be_true
end

it "expects a block with no arguments" do
lambda { "hola".instance_eval }.should raise_error(ArgumentError)
end

it "takes no arguments with a block" do
lambda { "hola".instance_eval(4, 5) {|a,b| a + b } }.should raise_error(ArgumentError)
end

it "yields the object to the block" do
"hola".instance_eval {|o| ScratchPad.record o }
ScratchPad.recorded.should == "hola"
end

it "returns the result of the block" do
"hola".instance_eval { :result }.should == :result
end

it "only binds the eval to the receiver" do
f = Object.new
f.instance_eval do
def foo
1
end
end
f.foo.should == 1
lambda { Object.new.foo }.should raise_error(NoMethodError)
end

it "preserves self in the original block when passed a block argument" do
prc = proc { self }

old_self = prc.call

new_self = Object.new
new_self.instance_eval(&prc).should == new_self

prc.call.should == old_self
end

# TODO: This should probably be replaced with a "should behave like" that uses
# the many scoping/binding specs from kernel/eval_spec, since most of those
# behaviors are the same for instance_eval. See also module_eval/class_eval.

it "binds self to the receiver" do
s = "hola"
(s == s.instance_eval { self }).should be_true
o = mock('o')
(o == o.instance_eval("self")).should be_true
end

it "executes in the context of the receiver" do
"Ruby-fu".instance_eval { size }.should == 7
"hola".instance_eval("size").should == 4
Object.class_eval { "hola".instance_eval("to_s") }.should == "hola"
Object.class_eval { "Ruby-fu".instance_eval{ to_s } }.should == "Ruby-fu"

end

it "has access to receiver's instance variables" do
BasicObjectSpecs::IVars.new.instance_eval { @secret }.should == 99
BasicObjectSpecs::IVars.new.instance_eval("@secret").should == 99
end

it "treats block-local variables as local to the block" do
prc = instance_eval <<-CODE
proc do |x, prc|
if x
n = 2
else
n = 1
prc.call(true, prc)
n
end
end
CODE

prc.call(false, prc).should == 1
end

# On 1.9 class variables aren't inherited so we have to modify the test
# from 1.8
it "sets class variables in the receiver" do
BasicObjectSpecs::InstEvalCVar.class_variables.should include(:@@count)
BasicObjectSpecs::InstEvalCVar.send(:class_variable_get, :@@count).should == 2
end

it "makes the receiver metaclass the scoped class when used with a string" do
obj = Object.new
klass = obj.instance_eval %{
class B; end
B
}
obj.singleton_class.const_get(:B).should be_an_instance_of(Class)
end

it "gets constants in the receiver if a string given" do
BasicObjectSpecs::InstEvalOuter::Inner::X_BY_STR.should == 2
end

it "doesn't get constants in the receiver if a block given" do
BasicObjectSpecs::InstEvalOuter::Inner::X_BY_BLOCK.should be_nil
end

it "raises a TypeError when defining methods on an immediate" do
lambda do
1.instance_eval { def foo; end }
end.should raise_error(TypeError)
lambda do
:foo.instance_eval { def foo; end }
end.should raise_error(TypeError)
end

quarantine! do # Not clean, leaves cvars lying around to break other specs
it "scopes class var accesses in the caller when called on a Fixnum" do
# Fixnum can take instance vars
Fixnum.class_eval "@@__tmp_instance_eval_spec = 1"
(defined? @@__tmp_instance_eval_spec).should be_nil

@@__tmp_instance_eval_spec = 2
1.instance_eval { @@__tmp_instance_eval_spec }.should == 2
Fixnum.__send__(:remove_class_variable, :@@__tmp_instance_eval_spec)
end
end

it "raises a TypeError when defining methods on numerics" do
lambda do
(1.0).instance_eval { def foo; end }
end.should raise_error(TypeError)
lambda do
(1 << 64).instance_eval { def foo; end }
end.should raise_error(TypeError)
end
end
90 changes: 90 additions & 0 deletions spec/ruby/core/basicobject/instance_exec_spec.rb
@@ -1,4 +1,5 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)

describe "BasicObject#instance_exec" do
it "is a public instance method" do
Expand All @@ -14,4 +15,93 @@
a = BasicObject.new
a.instance_exec(1) { |b| b }.should equal(1)
end

it "raises a LocalJumpError unless given a block" do
lambda { "hola".instance_exec }.should raise_error(LocalJumpError)
end

it "has an arity of -1" do
Object.new.method(:instance_exec).arity.should == -1
end

it "accepts arguments with a block" do
lambda { "hola".instance_exec(4, 5) { |a,b| a + b } }.should_not raise_error
end

it "doesn't pass self to the block as an argument" do
"hola".instance_exec { |o| o }.should be_nil
end

it "passes any arguments to the block" do
Object.new.instance_exec(1,2) {|one, two| one + two}.should == 3
end

it "only binds the exec to the receiver" do
f = Object.new
f.instance_exec do
def foo
1
end
end
f.foo.should == 1
lambda { Object.new.foo }.should raise_error(NoMethodError)
end

# TODO: This should probably be replaced with a "should behave like" that uses
# the many scoping/binding specs from kernel/eval_spec, since most of those
# behaviors are the same for instance_exec. See also module_eval/class_eval.

it "binds self to the receiver" do
s = "hola"
(s == s.instance_exec { self }).should == true
end

it "binds the block's binding self to the receiver" do
s = "hola"
(s == s.instance_exec { eval "self", binding }).should == true
end

it "executes in the context of the receiver" do
"Ruby-fu".instance_exec { size }.should == 7
Object.class_eval { "Ruby-fu".instance_exec{ to_s } }.should == "Ruby-fu"
end

it "has access to receiver's instance variables" do
BasicObjectSpecs::IVars.new.instance_exec { @secret }.should == 99
end

it "sets class variables in the receiver" do
BasicObjectSpecs::InstExec.class_variables.should include(:@@count)
BasicObjectSpecs::InstExec.send(:class_variable_get, :@@count).should == 2
end

it "raises a TypeError when defining methods on an immediate" do
lambda do
1.instance_exec { def foo; end }
end.should raise_error(TypeError)
lambda do
:foo.instance_exec { def foo; end }
end.should raise_error(TypeError)
end

quarantine! do # Not clean, leaves cvars lying around to break other specs
it "scopes class var accesses in the caller when called on a Fixnum" do
# Fixnum can take instance vars
Fixnum.class_eval "@@__tmp_instance_exec_spec = 1"
(defined? @@__tmp_instance_exec_spec).should == nil

@@__tmp_instance_exec_spec = 2
1.instance_exec { @@__tmp_instance_exec_spec }.should == 2
Fixnum.__send__(:remove_class_variable, :@@__tmp_instance_exec_spec)
end
end

it "raises a TypeError when defining methods on numerics" do
lambda do
(1.0).instance_exec { def foo; end }
end.should raise_error(TypeError)
lambda do
(1 << 64).instance_exec { def foo; end }
end.should raise_error(TypeError)
end
end
10 changes: 0 additions & 10 deletions spec/ruby/core/basicobject/new_spec.rb

This file was deleted.

7 changes: 0 additions & 7 deletions spec/ruby/core/basicobject/superclass_spec.rb

This file was deleted.

6 changes: 4 additions & 2 deletions spec/ruby/core/integer/round_spec.rb
Expand Up @@ -28,8 +28,10 @@
(-25 * 10**70 + 1).round(-71).should eql(-20 * 10**70)
end

it "raises a RangeError when passed a big negative value" do
lambda { 42.round(fixnum_min()) }.should raise_error(RangeError)
platform_is_not :wordsize => 32 do
it "raises a RangeError when passed a big negative value" do
lambda { 42.round(fixnum_min()) }.should raise_error(RangeError)
end
end

it "raises a RangeError when passed Float::INFINITY" do
Expand Down
22 changes: 21 additions & 1 deletion spec/ruby/core/kernel/class_spec.rb
Expand Up @@ -2,5 +2,25 @@
require File.expand_path('../fixtures/classes', __FILE__)

describe "Kernel#class" do
it "needs to be reviewed for spec completeness"
it "returns the class of the object" do
Object.new.class.should equal(Object)

1.class.should equal(Fixnum)
3.14.class.should equal(Float)
:hello.class.should equal(Symbol)
"hello".class.should equal(String)
[1, 2].class.should equal(Array)
{ 1 => 2 }.class.should equal(Hash)
end

it "returns Class for a class" do
BasicObject.class.should equal(Class)
String.class.should equal(Class)
end

it "returns the first non-singleton class" do
a = "hello"
def a.my_singleton_method; end
a.class.should equal(String)
end
end

0 comments on commit 334ef87

Please sign in to comment.