diff options
Diffstat (limited to 'spec/ruby/core/kernel/inspect_spec.rb')
| -rw-r--r-- | spec/ruby/core/kernel/inspect_spec.rb | 79 |
1 files changed, 68 insertions, 11 deletions
diff --git a/spec/ruby/core/kernel/inspect_spec.rb b/spec/ruby/core/kernel/inspect_spec.rb index a946d032db..9808341139 100644 --- a/spec/ruby/core/kernel/inspect_spec.rb +++ b/spec/ruby/core/kernel/inspect_spec.rb @@ -3,17 +3,7 @@ require_relative 'fixtures/classes' describe "Kernel#inspect" do it "returns a String" do - Object.new.inspect.should be_an_instance_of(String) - end - - ruby_version_is ''...'2.7' do - it "returns a tainted string if self is tainted" do - Object.new.taint.inspect.tainted?.should be_true - end - - it "returns an untrusted string if self is untrusted" do - Object.new.untrust.inspect.untrusted?.should be_true - end + Object.new.inspect.should.instance_of?(String) end it "does not call #to_s if it is defined" do @@ -30,4 +20,71 @@ describe "Kernel#inspect" do obj = Object.new obj.inspect.should =~ /^#<Object:0x[0-9a-f]+>$/ end + + it "returns a String for an object without #class method" do + obj = Object.new + class << obj + undef_method :class + end + obj.inspect.should.is_a?(String) + end + + ruby_version_is "4.0" do + it "calls #instance_variables_to_inspect private method to know which variables to display" do + obj = Object.new + obj.instance_eval do + @host = "localhost" + @user = "root" + @password = "hunter2" + end + obj.singleton_class.class_eval do + private def instance_variables_to_inspect = %i[@host @user @does_not_exist] + end + + inspected = obj.inspect.sub(/^#<Object:0x[0-9a-f]+/, '#<Object:0x00') + inspected.should == '#<Object:0x00 @host="localhost", @user="root">' + + obj = Object.new + obj.instance_eval do + @host = "localhost" + @user = "root" + @password = "hunter2" + end + obj.singleton_class.class_eval do + private def instance_variables_to_inspect = [] + end + + inspected = obj.inspect.sub(/^#<Object:0x[0-9a-f]+/, '#<Object:0x00') + inspected.should == "#<Object:0x00>" + end + + it "displays all instance variables if #instance_variables_to_inspect returns nil" do + obj = Object.new + obj.instance_eval do + @host = "localhost" + @user = "root" + @password = "hunter2" + end + obj.singleton_class.class_eval do + private def instance_variables_to_inspect = nil + end + + inspected = obj.inspect.sub(/^#<Object:0x[0-9a-f]+/, '#<Object:0x00') + inspected.should == %{#<Object:0x00 @host="localhost", @user="root", @password="hunter2">} + end + + it "raises an error if #instance_variables_to_inspect returns an invalid value" do + obj = Object.new + obj.instance_eval do + @host = "localhost" + @user = "root" + @password = "hunter2" + end + obj.singleton_class.class_eval do + private def instance_variables_to_inspect = {} + end + + ->{ obj.inspect }.should.raise(TypeError, "Expected #instance_variables_to_inspect to return an Array or nil, but it returned Hash") + end + end end |
