diff options
Diffstat (limited to 'spec/ruby/core/binding')
| -rw-r--r-- | spec/ruby/core/binding/clone_spec.rb | 13 | ||||
| -rw-r--r-- | spec/ruby/core/binding/dup_spec.rb | 30 | ||||
| -rw-r--r-- | spec/ruby/core/binding/eval_spec.rb | 115 | ||||
| -rw-r--r-- | spec/ruby/core/binding/fixtures/classes.rb | 66 | ||||
| -rw-r--r-- | spec/ruby/core/binding/fixtures/location.rb | 6 | ||||
| -rw-r--r-- | spec/ruby/core/binding/local_variable_defined_spec.rb | 46 | ||||
| -rw-r--r-- | spec/ruby/core/binding/local_variable_get_spec.rb | 56 | ||||
| -rw-r--r-- | spec/ruby/core/binding/local_variable_set_spec.rb | 71 | ||||
| -rw-r--r-- | spec/ruby/core/binding/local_variables_spec.rb | 35 | ||||
| -rw-r--r-- | spec/ruby/core/binding/receiver_spec.rb | 11 | ||||
| -rw-r--r-- | spec/ruby/core/binding/shared/clone.rb | 56 | ||||
| -rw-r--r-- | spec/ruby/core/binding/source_location_spec.rb | 14 |
12 files changed, 519 insertions, 0 deletions
diff --git a/spec/ruby/core/binding/clone_spec.rb b/spec/ruby/core/binding/clone_spec.rb new file mode 100644 index 0000000000..f1769ac6de --- /dev/null +++ b/spec/ruby/core/binding/clone_spec.rb @@ -0,0 +1,13 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/classes' +require_relative 'shared/clone' + +describe "Binding#clone" do + it_behaves_like :binding_clone, :clone + + it "preserves frozen status" do + bind = binding.freeze + bind.frozen?.should == true + bind.clone.frozen?.should == true + end +end diff --git a/spec/ruby/core/binding/dup_spec.rb b/spec/ruby/core/binding/dup_spec.rb new file mode 100644 index 0000000000..4eff66bd9a --- /dev/null +++ b/spec/ruby/core/binding/dup_spec.rb @@ -0,0 +1,30 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/classes' +require_relative 'shared/clone' + +describe "Binding#dup" do + it_behaves_like :binding_clone, :dup + + it "resets frozen status" do + bind = binding.freeze + bind.frozen?.should == true + bind.dup.frozen?.should == false + end + + it "retains original binding variables but the list is distinct" do + bind1 = binding + eval "a = 1", bind1 + + bind2 = bind1.dup + eval("a = 2", bind2) + eval("a", bind1).should == 2 + eval("a", bind2).should == 2 + + eval("b = 2", bind2) + -> { eval("b", bind1) }.should raise_error(NameError) + eval("b", bind2).should == 2 + + bind1.local_variables.sort.should == [:a, :bind1, :bind2] + bind2.local_variables.sort.should == [:a, :b, :bind1, :bind2] + end +end diff --git a/spec/ruby/core/binding/eval_spec.rb b/spec/ruby/core/binding/eval_spec.rb new file mode 100644 index 0000000000..bb2036f739 --- /dev/null +++ b/spec/ruby/core/binding/eval_spec.rb @@ -0,0 +1,115 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/classes' + +describe "Binding#eval" do + it "behaves like Kernel.eval(..., self)" do + obj = BindingSpecs::Demo.new(1) + bind = obj.get_binding + + bind.eval("@secret += square(3)").should == 10 + bind.eval("a").should be_true + + bind.eval("class Inside; end") + bind.eval("Inside.name").should == "BindingSpecs::Demo::Inside" + end + + it "does not leak variables to cloned bindings" do + obj = BindingSpecs::Demo.new(1) + bind = obj.get_empty_binding + bind2 = bind.dup + + bind.eval("x = 72") + bind.local_variables.should == [:x] + bind2.local_variables.should == [] + end + + it "starts with line 1 if single argument is given" do + obj = BindingSpecs::Demo.new(1) + bind = obj.get_binding + bind.eval("__LINE__").should == 1 + end + + it "preserves __LINE__ across multiple calls to eval" do + obj = BindingSpecs::Demo.new(1) + bind = obj.get_binding + bind.eval("__LINE__").should == 1 + bind.eval("__LINE__").should == 1 + end + + it "increments __LINE__ on each line of a multiline eval" do + obj = BindingSpecs::Demo.new(1) + bind = obj.get_binding + bind.eval("#foo\n__LINE__").should == 2 + end + + it "starts with line 1 if the Binding is created with #send" do + obj = BindingSpecs::Demo.new(1) + bind, line = obj.get_binding_with_send_and_line + bind.eval("__LINE__").should == 1 + end + + it "starts with a __LINE__ of 1 if a filename is passed" do + bind = BindingSpecs::Demo.new(1).get_binding + bind.eval("__LINE__", "(test)").should == 1 + bind.eval("#foo\n__LINE__", "(test)").should == 2 + end + + it "starts with a __LINE__ from the third argument if passed" do + bind = BindingSpecs::Demo.new(1).get_binding + bind.eval("__LINE__", "(test)", 88).should == 88 + bind.eval("#foo\n__LINE__", "(test)", 88).should == 89 + end + + ruby_version_is ""..."3.3" do + it "uses (eval) as __FILE__ if single argument given" do + obj = BindingSpecs::Demo.new(1) + bind = obj.get_binding + bind.eval("__FILE__").should == '(eval)' + end + end + + it "uses 1 as __LINE__" do + obj = BindingSpecs::Demo.new(1) + bind = obj.get_binding + suppress_warning { bind.eval("__LINE__") }.should == 1 + end + + it "uses the __FILE__ that is passed in" do + bind = BindingSpecs::Demo.new(1).get_binding + bind.eval("__FILE__", "(test)").should == "(test)" + end + + describe "with a file given" do + it "does not store the filename permanently" do + obj = BindingSpecs::Demo.new(1) + bind = obj.get_binding + + bind.eval("__FILE__", "test.rb").should == "test.rb" + suppress_warning {bind.eval("__FILE__")}.should_not == "test.rb" + end + end + + it "with __method__ returns the method where the Binding was created" do + obj = BindingSpecs::Demo.new(1) + bind, meth = obj.get_binding_and_method + bind.eval("__method__").should == meth + end + + it "with __method__ returns the method where the Binding was created, ignoring #send" do + obj = BindingSpecs::Demo.new(1) + bind, meth = obj.get_binding_with_send_and_method + bind.eval("__method__").should == meth + end + + it "reflects refinements activated in the binding scope" do + bind = BindingSpecs::Refined.refined_binding + + bind.eval("'bar'.foo").should == "foo" + end + + ruby_version_is "3.3" do + it "uses the caller location as default filename" do + binding.eval("[__FILE__, __LINE__]").should == ["(eval at #{__FILE__}:#{__LINE__})", 1] + end + end +end diff --git a/spec/ruby/core/binding/fixtures/classes.rb b/spec/ruby/core/binding/fixtures/classes.rb new file mode 100644 index 0000000000..b5f3ce9008 --- /dev/null +++ b/spec/ruby/core/binding/fixtures/classes.rb @@ -0,0 +1,66 @@ +module BindingSpecs + class Demo + def initialize(n) + @secret = n + end + + def square(n) + n * n + end + + def get_binding_and_line + a = true + [binding, __LINE__] + end + + def get_binding + get_binding_and_line[0] + end + + def get_line_of_binding + get_binding_and_line[1] + end + + def get_file_of_binding + __FILE__ + end + + def get_binding_with_send_and_line + [send(:binding), __LINE__] + end + + def get_binding_and_method + [binding, :get_binding_and_method] + end + + def get_binding_with_send_and_method + [send(:binding), :get_binding_with_send_and_method] + end + + def get_empty_binding + binding + end + + def get_binding_in_block + a = true + 1.times do + b = false + return binding + end + end + end + + module AddFooToString + refine(String) do + def foo + "foo" + end + end + end + class Refined + using AddFooToString + def self.refined_binding + binding + end + end +end diff --git a/spec/ruby/core/binding/fixtures/location.rb b/spec/ruby/core/binding/fixtures/location.rb new file mode 100644 index 0000000000..a78ae75731 --- /dev/null +++ b/spec/ruby/core/binding/fixtures/location.rb @@ -0,0 +1,6 @@ +module BindingSpecs + module LocationMethod + FILE_PATH = __FILE__ + TEST_BINDING = binding + end +end diff --git a/spec/ruby/core/binding/local_variable_defined_spec.rb b/spec/ruby/core/binding/local_variable_defined_spec.rb new file mode 100644 index 0000000000..2fc6504ee5 --- /dev/null +++ b/spec/ruby/core/binding/local_variable_defined_spec.rb @@ -0,0 +1,46 @@ +require_relative '../../spec_helper' + +describe 'Binding#local_variable_defined?' do + it 'returns false when a variable is not defined' do + binding.local_variable_defined?(:foo).should == false + end + + it 'returns true when a regular local variable is defined' do + foo = 10 + binding.local_variable_defined?(:foo).should == true + end + + it 'returns true when a local variable is defined using eval()' do + bind = binding + bind.eval('foo = 10') + + bind.local_variable_defined?(:foo).should == true + end + + it 'returns true when a local variable is defined using Binding#local_variable_set' do + bind = binding + bind.local_variable_set(:foo, 10) + + bind.local_variable_defined?(:foo).should == true + end + + it 'returns true when a local variable is defined in a parent scope' do + foo = 10 + -> { + binding.local_variable_defined?(:foo) + }.call.should == true + end + + it 'allows usage of a String as the variable name' do + foo = 10 + binding.local_variable_defined?('foo').should == true + end + + it 'allows usage of an object responding to #to_str as the variable name' do + foo = 10 + name = mock(:obj) + name.stub!(:to_str).and_return('foo') + + binding.local_variable_defined?(name).should == true + end +end diff --git a/spec/ruby/core/binding/local_variable_get_spec.rb b/spec/ruby/core/binding/local_variable_get_spec.rb new file mode 100644 index 0000000000..005670becc --- /dev/null +++ b/spec/ruby/core/binding/local_variable_get_spec.rb @@ -0,0 +1,56 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/classes' + +describe "Binding#local_variable_get" do + it "reads local variables captured in the binding" do + a = 42 + bind = binding + bind.local_variable_get(:a).should == 42 + end + + it "raises a NameError for missing variables" do + bind = BindingSpecs::Demo.new(1).get_empty_binding + + -> { + bind.local_variable_get(:no_such_variable) + }.should raise_error(NameError) + end + + it "reads variables added later to the binding" do + bind = BindingSpecs::Demo.new(1).get_empty_binding + + -> { + bind.local_variable_get(:a) + }.should raise_error(NameError) + + bind.local_variable_set(:a, 42) + + bind.local_variable_get(:a).should == 42 + end + + it 'gets a local variable defined in a parent scope' do + number = 10 + + -> { + binding.local_variable_get(:number) + }.call.should == 10 + end + + it 'gets a local variable defined using eval()' do + bind = binding + bind.eval('number = 10') + + bind.local_variable_get(:number).should == 10 + end + + it "raises a NameError on global access" do + bind = binding + -> { bind.local_variable_get(:$0) }.should raise_error(NameError) + end + + it "raises a NameError on special variable access" do + bind = binding + -> { bind.local_variable_get(:$~) }.should raise_error(NameError) + -> { bind.local_variable_get(:$_) }.should raise_error(NameError) + end +end diff --git a/spec/ruby/core/binding/local_variable_set_spec.rb b/spec/ruby/core/binding/local_variable_set_spec.rb new file mode 100644 index 0000000000..1456c6dda1 --- /dev/null +++ b/spec/ruby/core/binding/local_variable_set_spec.rb @@ -0,0 +1,71 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/classes' + +describe "Binding#local_variable_set" do + it "adds nonexistent variables to the binding's eval scope" do + obj = BindingSpecs::Demo.new(1) + bind = obj.get_empty_binding + bind.eval('local_variables').should == [] + bind.local_variable_set :foo, 1 + bind.eval('local_variables').should == [:foo] + bind.eval('foo').should == 1 + end + + it 'sets a new local variable' do + bind = binding + + bind.local_variable_set(:number, 10) + bind.local_variable_get(:number).should == 10 + end + + it 'sets a local variable using a String as the variable name' do + bind = binding + + bind.local_variable_set('number', 10) + bind.local_variable_get('number').should == 10 + end + + it 'sets a local variable using an object responding to #to_str as the variable name' do + bind = binding + name = mock(:obj) + name.stub!(:to_str).and_return('number') + + bind.local_variable_set(name, 10) + bind.local_variable_get(name).should == 10 + end + + it 'scopes new local variables to the receiving Binding' do + bind = binding + bind.local_variable_set(:number, 10) + + -> { number }.should raise_error(NameError) + end + + it 'overwrites an existing local variable defined before a Binding' do + number = 10 + bind = binding + + bind.local_variable_set(:number, 20) + number.should == 20 + end + + it 'overwrites a local variable defined using eval()' do + bind = binding + bind.eval('number = 10') + + bind.local_variable_set(:number, 20) + bind.local_variable_get(:number).should == 20 + end + + it "raises a NameError on global access" do + bind = binding + -> { bind.local_variable_set(:$0, "") }.should raise_error(NameError) + end + + it "raises a NameError on special variable access" do + bind = binding + -> { bind.local_variable_set(:$~, "") }.should raise_error(NameError) + -> { bind.local_variable_set(:$_, "") }.should raise_error(NameError) + end + +end diff --git a/spec/ruby/core/binding/local_variables_spec.rb b/spec/ruby/core/binding/local_variables_spec.rb new file mode 100644 index 0000000000..92c817b9a8 --- /dev/null +++ b/spec/ruby/core/binding/local_variables_spec.rb @@ -0,0 +1,35 @@ +require_relative '../../spec_helper' + +describe "Binding#local_variables" do + it "returns an Array" do + binding.local_variables.should be_kind_of(Array) + end + + it "includes local variables in the current scope" do + a = 1 + b = nil + binding.local_variables.should == [:a, :b] + end + + it "includes local variables defined after calling binding.local_variables" do + binding.local_variables.should == [:a, :b] + a = 1 + b = 2 + end + + it "includes local variables of inherited scopes and eval'ed context" do + p = proc { |a| b = 1; eval("c = 2; binding.local_variables") } + p.call.should == [:c, :a, :b, :p] + end + + it "includes shadowed local variables only once" do + a = 1 + proc { |a| binding.local_variables }.call(2).should == [:a] + end + + it "includes new variables defined in the binding" do + b = binding + b.local_variable_set :a, 42 + b.local_variables.should == [:a, :b] + end +end diff --git a/spec/ruby/core/binding/receiver_spec.rb b/spec/ruby/core/binding/receiver_spec.rb new file mode 100644 index 0000000000..4bf5e7a7bd --- /dev/null +++ b/spec/ruby/core/binding/receiver_spec.rb @@ -0,0 +1,11 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/classes' + +describe "Binding#receiver" do + it "returns the object to which binding is bound" do + obj = BindingSpecs::Demo.new(1) + obj.get_binding.receiver.should == obj + + binding.receiver.should == self + end +end diff --git a/spec/ruby/core/binding/shared/clone.rb b/spec/ruby/core/binding/shared/clone.rb new file mode 100644 index 0000000000..2d854fce96 --- /dev/null +++ b/spec/ruby/core/binding/shared/clone.rb @@ -0,0 +1,56 @@ +describe :binding_clone, shared: true do + before :each do + @b1 = BindingSpecs::Demo.new(99).get_binding + @b2 = @b1.send(@method) + @b3 = BindingSpecs::Demo.new(99).get_binding_in_block + @b4 = @b3.send(@method) + end + + it "returns a copy of the Binding object" do + [[@b1, @b2, "a"], + [@b3, @b4, "a", "b"]].each do |b1, b2, *vars| + b1.should_not == b2 + + eval("@secret", b1).should == eval("@secret", b2) + eval("square(2)", b1).should == eval("square(2)", b2) + eval("self.square(2)", b1).should == eval("self.square(2)", b2) + vars.each do |v| + eval("#{v}", b1).should == eval("#{v}", b2) + end + end + end + + it "is a shallow copy of the Binding object" do + [[@b1, @b2, "a"], + [@b3, @b4, "a", "b"]].each do |b1, b2, *vars| + vars.each do |v| + eval("#{v} = false", b1) + eval("#{v}", b2).should == false + end + b1.local_variable_set(:x, 37) + b2.local_variable_defined?(:x).should == false + end + end + + ruby_version_is "3.4" do + it "copies instance variables" do + @b1.instance_variable_set(:@ivar, 1) + cl = @b1.send(@method) + cl.instance_variables.should == [:@ivar] + end + + it "copies the finalizer" do + code = <<-'RUBY' + obj = binding + + ObjectSpace.define_finalizer(obj, Proc.new { STDOUT.write "finalized\n" }) + + obj.clone + + exit 0 + RUBY + + ruby_exe(code).lines.sort.should == ["finalized\n", "finalized\n"] + end + end +end diff --git a/spec/ruby/core/binding/source_location_spec.rb b/spec/ruby/core/binding/source_location_spec.rb new file mode 100644 index 0000000000..d1c8191ea8 --- /dev/null +++ b/spec/ruby/core/binding/source_location_spec.rb @@ -0,0 +1,14 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/location' + +describe "Binding#source_location" do + it "returns an [file, line] pair" do + b = BindingSpecs::LocationMethod::TEST_BINDING + b.source_location.should == [BindingSpecs::LocationMethod::FILE_PATH, 4] + end + + it "works for eval with a given line" do + b = eval('binding', nil, "foo", 100) + b.source_location.should == ["foo", 100] + end +end |
