From 1d15d5f08032acf1b7bceacbb450d617ff6e0931 Mon Sep 17 00:00:00 2001 From: eregon Date: Wed, 20 Sep 2017 20:18:52 +0000 Subject: Move spec/rubyspec to spec/ruby for consistency * Other ruby implementations use the spec/ruby directory. [Misc #13792] [ruby-core:82287] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- spec/ruby/core/binding/clone_spec.rb | 7 +++ spec/ruby/core/binding/dup_spec.rb | 7 +++ spec/ruby/core/binding/eval_spec.rb | 37 +++++++++++ spec/ruby/core/binding/fixtures/classes.rb | 40 ++++++++++++ .../core/binding/local_variable_defined_spec.rb | 46 ++++++++++++++ spec/ruby/core/binding/local_variable_get_spec.rb | 56 +++++++++++++++++ spec/ruby/core/binding/local_variable_set_spec.rb | 71 ++++++++++++++++++++++ spec/ruby/core/binding/local_variables_spec.rb | 35 +++++++++++ spec/ruby/core/binding/location_spec.rb | 46 ++++++++++++++ spec/ruby/core/binding/receiver_spec.rb | 11 ++++ spec/ruby/core/binding/shared/clone.rb | 34 +++++++++++ 11 files changed, 390 insertions(+) create mode 100644 spec/ruby/core/binding/clone_spec.rb create mode 100644 spec/ruby/core/binding/dup_spec.rb create mode 100644 spec/ruby/core/binding/eval_spec.rb create mode 100644 spec/ruby/core/binding/fixtures/classes.rb create mode 100644 spec/ruby/core/binding/local_variable_defined_spec.rb create mode 100644 spec/ruby/core/binding/local_variable_get_spec.rb create mode 100644 spec/ruby/core/binding/local_variable_set_spec.rb create mode 100644 spec/ruby/core/binding/local_variables_spec.rb create mode 100644 spec/ruby/core/binding/location_spec.rb create mode 100644 spec/ruby/core/binding/receiver_spec.rb create mode 100644 spec/ruby/core/binding/shared/clone.rb (limited to 'spec/ruby/core/binding') diff --git a/spec/ruby/core/binding/clone_spec.rb b/spec/ruby/core/binding/clone_spec.rb new file mode 100644 index 0000000000..d607ae2a9e --- /dev/null +++ b/spec/ruby/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/ruby/core/binding/dup_spec.rb b/spec/ruby/core/binding/dup_spec.rb new file mode 100644 index 0000000000..7f242626d7 --- /dev/null +++ b/spec/ruby/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/ruby/core/binding/eval_spec.rb b/spec/ruby/core/binding/eval_spec.rb new file mode 100644 index 0000000000..4864b9f61f --- /dev/null +++ b/spec/ruby/core/binding/eval_spec.rb @@ -0,0 +1,37 @@ +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 + + 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 + + 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/ruby/core/binding/fixtures/classes.rb b/spec/ruby/core/binding/fixtures/classes.rb new file mode 100644 index 0000000000..05ca2479ba --- /dev/null +++ b/spec/ruby/core/binding/fixtures/classes.rb @@ -0,0 +1,40 @@ +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 + + def get_binding_in_block + a = true + 1.times do + b = false + return binding + end + end + 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..5fa79b19e2 --- /dev/null +++ b/spec/ruby/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/ruby/core/binding/local_variable_get_spec.rb b/spec/ruby/core/binding/local_variable_get_spec.rb new file mode 100644 index 0000000000..e65d08130e --- /dev/null +++ b/spec/ruby/core/binding/local_variable_get_spec.rb @@ -0,0 +1,56 @@ +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 + + it "raises a NameError on global access" do + bind = binding + lambda { bind.local_variable_get(:$0) }.should raise_error(NameError) + end + + it "raises a NameError on special variable access" do + bind = binding + lambda { bind.local_variable_get(:$~) }.should raise_error(NameError) + lambda { 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..067dcbd03e --- /dev/null +++ b/spec/ruby/core/binding/local_variable_set_spec.rb @@ -0,0 +1,71 @@ +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 + + it "raises a NameError on global access" do + bind = binding + lambda { bind.local_variable_set(:$0, "") }.should raise_error(NameError) + end + + it "raises a NameError on special variable access" do + bind = binding + lambda { bind.local_variable_set(:$~, "") }.should raise_error(NameError) + lambda { 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..bc6ea71f10 --- /dev/null +++ b/spec/ruby/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/ruby/core/binding/location_spec.rb b/spec/ruby/core/binding/location_spec.rb new file mode 100644 index 0000000000..b4a066038d --- /dev/null +++ b/spec/ruby/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/ruby/core/binding/receiver_spec.rb b/spec/ruby/core/binding/receiver_spec.rb new file mode 100644 index 0000000000..8784ab0e38 --- /dev/null +++ b/spec/ruby/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/ruby/core/binding/shared/clone.rb b/spec/ruby/core/binding/shared/clone.rb new file mode 100644 index 0000000000..0e934ac1b5 --- /dev/null +++ b/spec/ruby/core/binding/shared/clone.rb @@ -0,0 +1,34 @@ +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 +end -- cgit v1.2.3