diff options
Diffstat (limited to 'spec/ruby/core/kernel')
-rw-r--r-- | spec/ruby/core/kernel/Integer_spec.rb | 13 | ||||
-rw-r--r-- | spec/ruby/core/kernel/at_exit_spec.rb | 61 | ||||
-rw-r--r-- | spec/ruby/core/kernel/exit_spec.rb | 10 | ||||
-rw-r--r-- | spec/ruby/core/kernel/fixtures/at_exit.rb | 3 | ||||
-rw-r--r-- | spec/ruby/core/kernel/open_spec.rb | 25 | ||||
-rw-r--r-- | spec/ruby/core/kernel/singleton_class_spec.rb | 20 |
6 files changed, 61 insertions, 71 deletions
diff --git a/spec/ruby/core/kernel/Integer_spec.rb b/spec/ruby/core/kernel/Integer_spec.rb index 2c78e27428..c691cb4c41 100644 --- a/spec/ruby/core/kernel/Integer_spec.rb +++ b/spec/ruby/core/kernel/Integer_spec.rb @@ -573,10 +573,17 @@ describe "Integer() given a String and base", shared: true do -> { Integer("0#{d}1", base) }.should raise_error(ArgumentError) end end + end - it "raises an ArgumentError if a base is given for a non-String value" do - -> { Integer(98, 15) }.should raise_error(ArgumentError) - end + it "raises an ArgumentError if a base is given for a non-String value" do + -> { Integer(98, 15) }.should raise_error(ArgumentError) + end + + it "tries to convert the base to an integer using to_int" do + obj = mock('8') + obj.should_receive(:to_int).and_return(8) + + Integer("777", obj).should == 0777 end describe "when passed exception: false" do diff --git a/spec/ruby/core/kernel/at_exit_spec.rb b/spec/ruby/core/kernel/at_exit_spec.rb index a784c1ae17..ebd9a71d15 100644 --- a/spec/ruby/core/kernel/at_exit_spec.rb +++ b/spec/ruby/core/kernel/at_exit_spec.rb @@ -1,67 +1,16 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' +require_relative '../../shared/kernel/at_exit' describe "Kernel.at_exit" do + it_behaves_like :kernel_at_exit, :at_exit + it "is a private method" do Kernel.should have_private_instance_method(:at_exit) end - it "runs after all other code" do - ruby_exe("at_exit {print 5}; print 6").should == "65" - end - - it "runs in reverse order of registration" do - code = "at_exit {print 4};at_exit {print 5}; print 6; at_exit {print 7}" - ruby_exe(code).should == "6754" - end - - it "allows calling exit inside at_exit handler" do - code = "at_exit {print 3}; at_exit {print 4; exit; print 5}; at_exit {print 6}" - ruby_exe(code).should == "643" - end - - it "gives access to the last raised exception" do - code = <<-EOC - at_exit do - puts "The exception matches: \#{$! == $exception} (message=\#{$!.message})" - end - - begin - raise "foo" - rescue => $exception - raise - end - EOC - - result = ruby_exe(code, args: "2>&1", exit_status: 1) - result.lines.should.include?("The exception matches: true (message=foo)\n") - end - - it "both exceptions in at_exit and in the main script are printed" do - code = 'at_exit { raise "at_exit_error" }; raise "main_script_error"' - result = ruby_exe(code, args: "2>&1", exit_status: 1) - result.should.include?('at_exit_error (RuntimeError)') - result.should.include?('main_script_error (RuntimeError)') - end - - it "decides the exit status if both at_exit and the main script raise SystemExit" do - ruby_exe('at_exit { exit 43 }; exit 42', args: "2>&1", exit_status: 43) - $?.exitstatus.should == 43 - end - - it "runs all at_exit even if some raise exceptions" do - code = 'at_exit { STDERR.puts "last" }; at_exit { exit 43 }; at_exit { STDERR.puts "first" }; exit 42' - result = ruby_exe(code, args: "2>&1", exit_status: 43) - result.should == "first\nlast\n" - $?.exitstatus.should == 43 - end - - it "runs at_exit handlers even if the main script fails to parse" do - script = fixture(__FILE__, "at_exit.rb") - result = ruby_exe('{', options: "-r#{script}", args: "2>&1", exit_status: 1) - $?.should_not.success? - result.should.include?("at_exit ran\n") - result.should.include?("syntax error") + it "raises ArgumentError if called without a block" do + -> { at_exit }.should raise_error(ArgumentError, "called without a block") end end diff --git a/spec/ruby/core/kernel/exit_spec.rb b/spec/ruby/core/kernel/exit_spec.rb index f168cb375e..93cec3fee5 100644 --- a/spec/ruby/core/kernel/exit_spec.rb +++ b/spec/ruby/core/kernel/exit_spec.rb @@ -10,6 +10,10 @@ describe "Kernel#exit" do it_behaves_like :process_exit, :exit, KernelSpecs::Method.new end +describe "Kernel.exit" do + it_behaves_like :process_exit, :exit, Kernel +end + describe "Kernel#exit!" do it "is a private method" do Kernel.should have_private_instance_method(:exit!) @@ -18,10 +22,6 @@ describe "Kernel#exit!" do it_behaves_like :process_exit!, :exit!, "self" end -describe "Kernel.exit" do - it_behaves_like :process_exit, :exit, Kernel -end - describe "Kernel.exit!" do - it_behaves_like :process_exit!, :exit!, Kernel + it_behaves_like :process_exit!, :exit!, "Kernel" end diff --git a/spec/ruby/core/kernel/fixtures/at_exit.rb b/spec/ruby/core/kernel/fixtures/at_exit.rb deleted file mode 100644 index 9c11a7ad6c..0000000000 --- a/spec/ruby/core/kernel/fixtures/at_exit.rb +++ /dev/null @@ -1,3 +0,0 @@ -at_exit do - STDERR.puts "at_exit ran" -end diff --git a/spec/ruby/core/kernel/open_spec.rb b/spec/ruby/core/kernel/open_spec.rb index fa9299f9d4..bad2ae9d2c 100644 --- a/spec/ruby/core/kernel/open_spec.rb +++ b/spec/ruby/core/kernel/open_spec.rb @@ -72,6 +72,17 @@ describe "Kernel#open" do -> { open }.should raise_error(ArgumentError) end + ruby_version_is "3.0" do + it "accepts options as keyword arguments" do + @file = open(@name, "r", 0666, flags: File::CREAT) + @file.should be_kind_of(File) + + -> { + open(@name, "r", 0666, {flags: File::CREAT}) + }.should raise_error(ArgumentError, "wrong number of arguments (given 4, expected 1..3)") + end + end + describe "when given an object that responds to to_open" do before :each do ScratchPad.clear @@ -109,11 +120,21 @@ describe "Kernel#open" do it "passes its arguments onto #to_open" do obj = mock('to_open') - obj.should_receive(:to_open).with(1,2,3) - + obj.should_receive(:to_open).with(1, 2, 3) open(obj, 1, 2, 3) end + it "passes keyword arguments onto #to_open as keyword arguments if to_open accepts them" do + obj = Object.new + def obj.to_open(*args, **kw) + ScratchPad << {args: args, kw: kw} + end + + ScratchPad.record [] + open(obj, 1, 2, 3, a: "b") + ScratchPad.recorded.should == [args: [1, 2, 3], kw: {a: "b"}] + end + it "passes the return value from #to_open to a block" do obj = mock('to_open') obj.should_receive(:to_open).and_return(:value) diff --git a/spec/ruby/core/kernel/singleton_class_spec.rb b/spec/ruby/core/kernel/singleton_class_spec.rb index c56fa08cc1..4865e29c10 100644 --- a/spec/ruby/core/kernel/singleton_class_spec.rb +++ b/spec/ruby/core/kernel/singleton_class_spec.rb @@ -20,10 +20,26 @@ describe "Kernel#singleton_class" do end it "raises TypeError for Integer" do - -> { 123.singleton_class }.should raise_error(TypeError) + -> { 123.singleton_class }.should raise_error(TypeError, "can't define singleton") + end + + it "raises TypeError for Float" do + -> { 3.14.singleton_class }.should raise_error(TypeError, "can't define singleton") end it "raises TypeError for Symbol" do - -> { :foo.singleton_class }.should raise_error(TypeError) + -> { :foo.singleton_class }.should raise_error(TypeError, "can't define singleton") + end + + it "raises TypeError for a frozen deduplicated String" do + -> { (-"string").singleton_class }.should raise_error(TypeError, "can't define singleton") + -> { a = -"string"; a.singleton_class }.should raise_error(TypeError, "can't define singleton") + -> { a = "string"; (-a).singleton_class }.should raise_error(TypeError, "can't define singleton") + end + + it "returns a frozen singleton class if object is frozen" do + obj = Object.new + obj.freeze + obj.singleton_class.frozen?.should be_true end end |