diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-09-20 20:18:52 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-09-20 20:18:52 +0000 |
commit | 1d15d5f08032acf1b7bceacbb450d617ff6e0931 (patch) | |
tree | a3785a79899302bc149e4a6e72f624ac27dc1f10 /spec/ruby/language/throw_spec.rb | |
parent | 75bfc6440d595bf339007f4fb280fd4d743e89c1 (diff) |
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
Diffstat (limited to 'spec/ruby/language/throw_spec.rb')
-rw-r--r-- | spec/ruby/language/throw_spec.rb | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/spec/ruby/language/throw_spec.rb b/spec/ruby/language/throw_spec.rb new file mode 100644 index 0000000000..d78c137708 --- /dev/null +++ b/spec/ruby/language/throw_spec.rb @@ -0,0 +1,80 @@ +require File.expand_path('../../spec_helper', __FILE__) + +describe "The throw keyword" do + it "abandons processing" do + i = 0 + catch(:done) do + loop do + i += 1 + throw :done if i > 4 + end + i += 1 + end + i.should == 5 + end + + it "supports a second parameter" do + msg = catch(:exit) do + throw :exit,:msg + end + msg.should == :msg + end + + it "uses nil as a default second parameter" do + msg = catch(:exit) do + throw :exit + end + msg.should == nil + end + + it "clears the current exception" do + catch :exit do + begin + raise "exception" + rescue + throw :exit + end + end + $!.should be_nil + end + + it "allows any object as its argument" do + catch(1) { throw 1, 2 }.should == 2 + o = Object.new + catch(o) { throw o, o }.should == o + end + + it "does not convert strings to a symbol" do + lambda { catch(:exit) { throw "exit" } }.should raise_error(ArgumentError) + end + + it "unwinds stack from within a method" do + def throw_method(handler, val) + throw handler, val + end + + catch(:exit) do + throw_method(:exit, 5) + end.should == 5 + end + + it "unwinds stack from within a lambda" do + c = lambda { throw :foo, :msg } + catch(:foo) { c.call }.should == :msg + end + + it "raises an ArgumentError if outside of scope of a matching catch" do + lambda { throw :test, 5 }.should raise_error(ArgumentError) + lambda { catch(:different) { throw :test, 5 } }.should raise_error(ArgumentError) + end + + it "raises an ArgumentError if used to exit a thread" do + lambda { + catch(:what) do + Thread.new { + throw :what + }.join + end + }.should raise_error(ArgumentError) + end +end |