diff options
Diffstat (limited to 'spec/rubyspec/core/kernel')
-rw-r--r-- | spec/rubyspec/core/kernel/autoload_spec.rb | 3 | ||||
-rw-r--r-- | spec/rubyspec/core/kernel/caller_spec.rb | 6 | ||||
-rw-r--r-- | spec/rubyspec/core/kernel/chomp_spec.rb | 2 | ||||
-rw-r--r-- | spec/rubyspec/core/kernel/chop_spec.rb | 2 | ||||
-rw-r--r-- | spec/rubyspec/core/kernel/clone_spec.rb | 10 | ||||
-rw-r--r-- | spec/rubyspec/core/kernel/fixtures/classes.rb | 34 | ||||
-rw-r--r-- | spec/rubyspec/core/kernel/public_method_spec.rb | 4 | ||||
-rw-r--r-- | spec/rubyspec/core/kernel/singleton_method_spec.rb | 41 |
8 files changed, 84 insertions, 18 deletions
diff --git a/spec/rubyspec/core/kernel/autoload_spec.rb b/spec/rubyspec/core/kernel/autoload_spec.rb index 082903e92d..e2741de74c 100644 --- a/spec/rubyspec/core/kernel/autoload_spec.rb +++ b/spec/rubyspec/core/kernel/autoload_spec.rb @@ -49,8 +49,7 @@ describe "Kernel#autoload" do end it "can autoload in instance_eval" do - instance_eval do - # this instance_eval is not needed because specs are run in instance_eval + Object.new.instance_eval do autoload :KSAutoloadD, fixture(__FILE__, "autoload_d.rb") KSAutoloadD.loaded.should == :ksautoload_d end diff --git a/spec/rubyspec/core/kernel/caller_spec.rb b/spec/rubyspec/core/kernel/caller_spec.rb index 94fbe3ab7e..d9be29a8db 100644 --- a/spec/rubyspec/core/kernel/caller_spec.rb +++ b/spec/rubyspec/core/kernel/caller_spec.rb @@ -22,6 +22,12 @@ describe 'Kernel#caller' do locations.length.should == 1 end + it 'returns an Array of caller locations using a range' do + locations = KernelSpecs::CallerTest.locations(1..1) + + locations.length.should == 1 + end + it 'returns the locations as String instances' do locations = KernelSpecs::CallerTest.locations line = __LINE__ - 1 diff --git a/spec/rubyspec/core/kernel/chomp_spec.rb b/spec/rubyspec/core/kernel/chomp_spec.rb index 4b34784169..524a4c8b1d 100644 --- a/spec/rubyspec/core/kernel/chomp_spec.rb +++ b/spec/rubyspec/core/kernel/chomp_spec.rb @@ -53,7 +53,7 @@ with_feature :encoding do it "removes the final carriage return, newline from a multi-byte $_" do script = fixture __FILE__, "#{@method}.rb" - KernelSpecs.encoded_chomp(script).should == "あれ" + KernelSpecs.run_with_dash_n(script).should == "あれ" end end diff --git a/spec/rubyspec/core/kernel/chop_spec.rb b/spec/rubyspec/core/kernel/chop_spec.rb index 53b6e47cd1..5106fefee8 100644 --- a/spec/rubyspec/core/kernel/chop_spec.rb +++ b/spec/rubyspec/core/kernel/chop_spec.rb @@ -41,7 +41,7 @@ with_feature :encoding do it "removes the final multi-byte character from $_" do script = fixture __FILE__, "#{@method}.rb" - KernelSpecs.encoded_chop(script).should == "あ" + KernelSpecs.run_with_dash_n(script).should == "あ" end end diff --git a/spec/rubyspec/core/kernel/clone_spec.rb b/spec/rubyspec/core/kernel/clone_spec.rb index 0e8216d434..48b3c24c7f 100644 --- a/spec/rubyspec/core/kernel/clone_spec.rb +++ b/spec/rubyspec/core/kernel/clone_spec.rb @@ -37,6 +37,16 @@ describe "Kernel#clone" do o3.frozen?.should == true end + ruby_version_is '2.4' do + it 'takes an option to copy freeze state or not' do + @obj.clone(freeze: true).frozen?.should == false + @obj.clone(freeze: false).frozen?.should == false + @obj.freeze + @obj.clone(freeze: true).frozen?.should == true + @obj.clone(freeze: false).frozen?.should == false + end + end + it "copies instance variables" do clone = @obj.clone clone.one.should == 1 diff --git a/spec/rubyspec/core/kernel/fixtures/classes.rb b/spec/rubyspec/core/kernel/fixtures/classes.rb index c4a4d00485..afa2bec12f 100644 --- a/spec/rubyspec/core/kernel/fixtures/classes.rb +++ b/spec/rubyspec/core/kernel/fixtures/classes.rb @@ -32,26 +32,36 @@ module KernelSpecs end def self.has_private_method(name) - cmd = %[| #{ruby_cmd(nil)} -n -e "print Kernel.private_method_defined?('#{name}')"] - ruby_exe("puts", args: cmd) == "true" + IO.popen([*ruby_exe, "-n", "-e", "print Kernel.private_method_defined?(#{name.inspect})"], "r+") do |io| + io.puts + io.close_write + io.read + end == "true" end def self.chop(str, method) - cmd = "| #{ruby_cmd(nil)} -n -e '$_ = #{str.inspect}; #{method}; print $_'" - ruby_exe "puts", args: cmd - end - - def self.encoded_chop(file) - ruby_exe "puts", args: "| #{ruby_cmd(nil)} -n #{file}" + IO.popen([*ruby_exe, "-n", "-e", "$_ = #{str.inspect}; #{method}; print $_"], "r+") do |io| + io.puts + io.close_write + io.read + end end def self.chomp(str, method, sep="\n") - cmd = "| #{ruby_cmd(nil)} -n -e '$_ = #{str.inspect}; $/ = #{sep.inspect}; #{method}; print $_'" - ruby_exe "puts", args: cmd + code = "$_ = #{str.inspect}; $/ = #{sep.inspect}; #{method}; print $_" + IO.popen([*ruby_exe, "-n", "-e", code], "r+") do |io| + io.puts + io.close_write + io.read + end end - def self.encoded_chomp(file) - ruby_exe "puts", args: "| #{ruby_cmd(nil)} -n #{file}" + def self.run_with_dash_n(file) + IO.popen([*ruby_exe, "-n", file], "r+") do |io| + io.puts + io.close_write + io.read + end end # kind_of?, is_a?, instance_of? diff --git a/spec/rubyspec/core/kernel/public_method_spec.rb b/spec/rubyspec/core/kernel/public_method_spec.rb index c4a29d9192..f1cc168420 100644 --- a/spec/rubyspec/core/kernel/public_method_spec.rb +++ b/spec/rubyspec/core/kernel/public_method_spec.rb @@ -18,9 +18,9 @@ describe "Kernel#public_method" do it "raises a NameError when called on a protected method" do @obj.send(:protected_method).should == :protected_method - lambda do + lambda { @obj.public_method(:protected_method) - end.should raise_error(NameError) + }.should raise_error(NameError) end it "raises a NameError if we only repond_to_missing? method, true" do diff --git a/spec/rubyspec/core/kernel/singleton_method_spec.rb b/spec/rubyspec/core/kernel/singleton_method_spec.rb new file mode 100644 index 0000000000..77022b40c2 --- /dev/null +++ b/spec/rubyspec/core/kernel/singleton_method_spec.rb @@ -0,0 +1,41 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Kernel#singleton_method" do + it "find a method defined on the singleton class" do + obj = Object.new + def obj.foo; end + obj.singleton_method(:foo).should be_an_instance_of(Method) + end + + it "returns a Method which can be called" do + obj = Object.new + def obj.foo; 42; end + obj.singleton_method(:foo).call.should == 42 + end + + it "only looks at singleton methods and not at methods in the class" do + klass = Class.new do + def foo + 42 + end + end + obj = klass.new + obj.foo.should == 42 + -> { + obj.singleton_method(:foo) + }.should raise_error(NameError) { |e| + # a NameError and not a NoMethodError + e.class.should == NameError + } + end + + it "raises a NameError if there is no such method" do + obj = Object.new + -> { + obj.singleton_method(:not_existing) + }.should raise_error(NameError) { |e| + # a NameError and not a NoMethodError + e.class.should == NameError + } + end +end |