aboutsummaryrefslogtreecommitdiffstats
path: root/spec/rubyspec/core/binding
diff options
context:
space:
mode:
Diffstat (limited to 'spec/rubyspec/core/binding')
-rw-r--r--spec/rubyspec/core/binding/clone_spec.rb7
-rw-r--r--spec/rubyspec/core/binding/dup_spec.rb7
-rw-r--r--spec/rubyspec/core/binding/eval_spec.rb27
-rw-r--r--spec/rubyspec/core/binding/fixtures/classes.rb32
-rw-r--r--spec/rubyspec/core/binding/local_variable_defined_spec.rb46
-rw-r--r--spec/rubyspec/core/binding/local_variable_get_spec.rb45
-rw-r--r--spec/rubyspec/core/binding/local_variable_set_spec.rb59
-rw-r--r--spec/rubyspec/core/binding/local_variables_spec.rb35
-rw-r--r--spec/rubyspec/core/binding/location_spec.rb46
-rw-r--r--spec/rubyspec/core/binding/receiver_spec.rb11
-rw-r--r--spec/rubyspec/core/binding/shared/clone.rb20
11 files changed, 335 insertions, 0 deletions
diff --git a/spec/rubyspec/core/binding/clone_spec.rb b/spec/rubyspec/core/binding/clone_spec.rb
new file mode 100644
index 0000000000..d607ae2a9e
--- /dev/null
+++ b/spec/rubyspec/core/binding/clone_spec.rb
@@ -0,0 +1,7 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/classes', __FILE__)
+require File.expand_path('../shared/clone', __FILE__)
+
+describe "Binding#clone" do
+ it_behaves_like(:binding_clone, :clone)
+end
diff --git a/spec/rubyspec/core/binding/dup_spec.rb b/spec/rubyspec/core/binding/dup_spec.rb
new file mode 100644
index 0000000000..7f242626d7
--- /dev/null
+++ b/spec/rubyspec/core/binding/dup_spec.rb
@@ -0,0 +1,7 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/classes', __FILE__)
+require File.expand_path('../shared/clone', __FILE__)
+
+describe "Binding#dup" do
+ it_behaves_like(:binding_clone, :dup)
+end
diff --git a/spec/rubyspec/core/binding/eval_spec.rb b/spec/rubyspec/core/binding/eval_spec.rb
new file mode 100644
index 0000000000..cedd5fcdf9
--- /dev/null
+++ b/spec/rubyspec/core/binding/eval_spec.rb
@@ -0,0 +1,27 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/classes', __FILE__)
+
+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
+
+ 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"
+ bind.eval("__FILE__").should_not == "test.rb"
+ end
+ end
+
+ it "needs to be reviewed for spec completeness"
+end
diff --git a/spec/rubyspec/core/binding/fixtures/classes.rb b/spec/rubyspec/core/binding/fixtures/classes.rb
new file mode 100644
index 0000000000..b7b7618411
--- /dev/null
+++ b/spec/rubyspec/core/binding/fixtures/classes.rb
@@ -0,0 +1,32 @@
+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_empty_binding
+ binding
+ end
+ end
+end
diff --git a/spec/rubyspec/core/binding/local_variable_defined_spec.rb b/spec/rubyspec/core/binding/local_variable_defined_spec.rb
new file mode 100644
index 0000000000..5fa79b19e2
--- /dev/null
+++ b/spec/rubyspec/core/binding/local_variable_defined_spec.rb
@@ -0,0 +1,46 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+
+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
+ lambda {
+ 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/rubyspec/core/binding/local_variable_get_spec.rb b/spec/rubyspec/core/binding/local_variable_get_spec.rb
new file mode 100644
index 0000000000..b7b0b14594
--- /dev/null
+++ b/spec/rubyspec/core/binding/local_variable_get_spec.rb
@@ -0,0 +1,45 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/classes', __FILE__)
+
+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
+
+ lambda {
+ 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
+
+ lambda {
+ 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
+
+ lambda {
+ 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
+end
diff --git a/spec/rubyspec/core/binding/local_variable_set_spec.rb b/spec/rubyspec/core/binding/local_variable_set_spec.rb
new file mode 100644
index 0000000000..4b14d891b3
--- /dev/null
+++ b/spec/rubyspec/core/binding/local_variable_set_spec.rb
@@ -0,0 +1,59 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/classes', __FILE__)
+
+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)
+
+ lambda { 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
+end
diff --git a/spec/rubyspec/core/binding/local_variables_spec.rb b/spec/rubyspec/core/binding/local_variables_spec.rb
new file mode 100644
index 0000000000..bc6ea71f10
--- /dev/null
+++ b/spec/rubyspec/core/binding/local_variables_spec.rb
@@ -0,0 +1,35 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+
+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/rubyspec/core/binding/location_spec.rb b/spec/rubyspec/core/binding/location_spec.rb
new file mode 100644
index 0000000000..b4a066038d
--- /dev/null
+++ b/spec/rubyspec/core/binding/location_spec.rb
@@ -0,0 +1,46 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/classes', __FILE__)
+
+describe "Binding#eval" do
+ it "inherits __LINE__ from the enclosing scope" do
+ obj = BindingSpecs::Demo.new(1)
+ bind = obj.get_binding
+ bind.eval("__LINE__").should == obj.get_line_of_binding
+ end
+
+ it "preserves __LINE__ across multiple calls to eval" do
+ obj = BindingSpecs::Demo.new(1)
+ bind = obj.get_binding
+ bind.eval("__LINE__").should == obj.get_line_of_binding
+ bind.eval("__LINE__").should == obj.get_line_of_binding
+ 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 == obj.get_line_of_binding + 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
+
+ it "inherits __FILE__ from the enclosing scope" do
+ obj = BindingSpecs::Demo.new(1)
+ bind = obj.get_binding
+ bind.eval("__FILE__").should == obj.get_file_of_binding
+ end
+
+ it "uses the __FILE__ that is passed in" do
+ bind = BindingSpecs::Demo.new(1).get_binding
+ bind.eval("__FILE__", "(test)").should == "(test)"
+ end
+end
diff --git a/spec/rubyspec/core/binding/receiver_spec.rb b/spec/rubyspec/core/binding/receiver_spec.rb
new file mode 100644
index 0000000000..8784ab0e38
--- /dev/null
+++ b/spec/rubyspec/core/binding/receiver_spec.rb
@@ -0,0 +1,11 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/classes', __FILE__)
+
+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/rubyspec/core/binding/shared/clone.rb b/spec/rubyspec/core/binding/shared/clone.rb
new file mode 100644
index 0000000000..723e0490c3
--- /dev/null
+++ b/spec/rubyspec/core/binding/shared/clone.rb
@@ -0,0 +1,20 @@
+describe :binding_clone, shared: true do
+ before :each do
+ @b1 = BindingSpecs::Demo.new(99).get_binding
+ @b2 = @b1.send(@method)
+ end
+
+ it "returns a copy of the Binding object" do
+ @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)
+ eval("a", @b1).should == eval("a", @b2)
+ end
+
+ it "is a shallow copy of the Binding object" do
+ eval("a = false", @b1)
+ eval("a", @b2).should == false
+ end
+end