2017-05-07 08:04:49 -04:00
|
|
|
require File.expand_path('../../spec_helper', __FILE__)
|
|
|
|
|
|
|
|
describe "The 'case'-construct" do
|
|
|
|
it "evaluates the body of the when clause matching the case target expression" do
|
|
|
|
case 1
|
2017-12-27 11:12:47 -05:00
|
|
|
when 2; false
|
|
|
|
when 1; true
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "evaluates the body of the when clause whose array expression includes the case target expression" do
|
|
|
|
case 2
|
2017-12-27 11:12:47 -05:00
|
|
|
when 3, 4; false
|
|
|
|
when 1, 2; true
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "evaluates the body of the when clause in left-to-right order if it's an array expression" do
|
|
|
|
@calls = []
|
|
|
|
def foo; @calls << :foo; end
|
|
|
|
def bar; @calls << :bar; end
|
|
|
|
|
|
|
|
case true
|
2017-12-27 11:12:47 -05:00
|
|
|
when foo, bar;
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@calls.should == [:foo, :bar]
|
|
|
|
end
|
|
|
|
|
|
|
|
it "evaluates the body of the when clause whose range expression includes the case target expression" do
|
|
|
|
case 5
|
2017-12-27 11:12:47 -05:00
|
|
|
when 21..30; false
|
|
|
|
when 1..20; true
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns nil when no 'then'-bodies are given" do
|
|
|
|
case "a"
|
2017-12-27 11:12:47 -05:00
|
|
|
when "a"
|
|
|
|
when "b"
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "evaluates the 'else'-body when no other expression matches" do
|
|
|
|
case "c"
|
2017-12-27 11:12:47 -05:00
|
|
|
when "a"; 'foo'
|
|
|
|
when "b"; 'bar'
|
|
|
|
else 'zzz'
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == 'zzz'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns nil when no expression matches and 'else'-body is empty" do
|
|
|
|
case "c"
|
2017-12-27 11:12:47 -05:00
|
|
|
when "a"; "a"
|
|
|
|
when "b"; "b"
|
|
|
|
else
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns 2 when a then body is empty" do
|
|
|
|
case Object.new
|
|
|
|
when Numeric then
|
|
|
|
1
|
|
|
|
when String then
|
|
|
|
# ok
|
|
|
|
else
|
|
|
|
2
|
|
|
|
end.should == 2
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the statement following 'then'" do
|
|
|
|
case "a"
|
2017-12-27 11:12:47 -05:00
|
|
|
when "a" then 'foo'
|
|
|
|
when "b" then 'bar'
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == 'foo'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "tests classes with case equality" do
|
|
|
|
case "a"
|
2017-12-27 11:12:47 -05:00
|
|
|
when String
|
|
|
|
'foo'
|
|
|
|
when Symbol
|
|
|
|
'bar'
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == 'foo'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "tests with matching regexps" do
|
|
|
|
case "hello"
|
2017-12-27 11:12:47 -05:00
|
|
|
when /abc/; false
|
|
|
|
when /^hell/; true
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == true
|
|
|
|
end
|
|
|
|
|
2017-12-27 11:12:47 -05:00
|
|
|
it "tests with matching regexps and sets $~ and captures" do
|
|
|
|
case "foo42"
|
|
|
|
when /oo(\d+)/
|
|
|
|
$~.should be_kind_of(MatchData)
|
|
|
|
$1.should == "42"
|
|
|
|
else
|
|
|
|
flunk
|
|
|
|
end
|
|
|
|
$~.should be_kind_of(MatchData)
|
|
|
|
$1.should == "42"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "tests with a regexp interpolated within another regexp" do
|
|
|
|
digits = '\d+'
|
|
|
|
case "foo44"
|
|
|
|
when /oo(#{digits})/
|
|
|
|
$~.should be_kind_of(MatchData)
|
|
|
|
$1.should == "44"
|
|
|
|
else
|
|
|
|
flunk
|
|
|
|
end
|
|
|
|
$~.should be_kind_of(MatchData)
|
|
|
|
$1.should == "44"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "tests with a string interpolated in a regexp" do
|
|
|
|
digits_regexp = /\d+/
|
|
|
|
case "foo43"
|
|
|
|
when /oo(#{digits_regexp})/
|
|
|
|
$~.should be_kind_of(MatchData)
|
|
|
|
$1.should == "43"
|
|
|
|
else
|
|
|
|
flunk
|
|
|
|
end
|
|
|
|
$~.should be_kind_of(MatchData)
|
|
|
|
$1.should == "43"
|
|
|
|
end
|
|
|
|
|
2017-05-07 08:04:49 -04:00
|
|
|
it "does not test with equality when given classes" do
|
|
|
|
case :symbol.class
|
2017-12-27 11:12:47 -05:00
|
|
|
when Symbol
|
|
|
|
"bar"
|
|
|
|
when String
|
|
|
|
"bar"
|
|
|
|
else
|
|
|
|
"foo"
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == "foo"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "takes lists of values" do
|
|
|
|
case 'z'
|
2017-12-27 11:12:47 -05:00
|
|
|
when 'a', 'b', 'c', 'd'
|
|
|
|
"foo"
|
|
|
|
when 'x', 'y', 'z'
|
|
|
|
"bar"
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == "bar"
|
|
|
|
|
|
|
|
case 'b'
|
2017-12-27 11:12:47 -05:00
|
|
|
when 'a', 'b', 'c', 'd'
|
|
|
|
"foo"
|
|
|
|
when 'x', 'y', 'z'
|
|
|
|
"bar"
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == "foo"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "expands arrays to lists of values" do
|
|
|
|
case 'z'
|
2017-12-27 11:12:47 -05:00
|
|
|
when *['a', 'b', 'c', 'd']
|
|
|
|
"foo"
|
|
|
|
when *['x', 'y', 'z']
|
|
|
|
"bar"
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == "bar"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "takes an expanded array in addition to a list of values" do
|
|
|
|
case 'f'
|
2017-12-27 11:12:47 -05:00
|
|
|
when 'f', *['a', 'b', 'c', 'd']
|
|
|
|
"foo"
|
|
|
|
when *['x', 'y', 'z']
|
|
|
|
"bar"
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == "foo"
|
|
|
|
|
|
|
|
case 'b'
|
2017-12-27 11:12:47 -05:00
|
|
|
when 'f', *['a', 'b', 'c', 'd']
|
|
|
|
"foo"
|
|
|
|
when *['x', 'y', 'z']
|
|
|
|
"bar"
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == "foo"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "takes an expanded array before additional listed values" do
|
|
|
|
case 'f'
|
2017-12-27 11:12:47 -05:00
|
|
|
when *['a', 'b', 'c', 'd'], 'f'
|
|
|
|
"foo"
|
|
|
|
when *['x', 'y', 'z']
|
|
|
|
"bar"
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == 'foo'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "expands arrays from variables before additional listed values" do
|
|
|
|
a = ['a', 'b', 'c']
|
|
|
|
case 'a'
|
2017-12-27 11:12:47 -05:00
|
|
|
when *a, 'd', 'e'
|
|
|
|
"foo"
|
|
|
|
when 'x'
|
|
|
|
"bar"
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == "foo"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "expands arrays from variables before a single additional listed value" do
|
|
|
|
a = ['a', 'b', 'c']
|
|
|
|
case 'a'
|
2017-12-27 11:12:47 -05:00
|
|
|
when *a, 'd'
|
|
|
|
"foo"
|
|
|
|
when 'x'
|
|
|
|
"bar"
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == "foo"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "expands multiple arrays from variables before additional listed values" do
|
|
|
|
a = ['a', 'b', 'c']
|
|
|
|
b = ['d', 'e', 'f']
|
|
|
|
|
|
|
|
case 'f'
|
2017-12-27 11:12:47 -05:00
|
|
|
when *a, *b, 'g', 'h'
|
|
|
|
"foo"
|
|
|
|
when 'x'
|
|
|
|
"bar"
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == "foo"
|
|
|
|
end
|
|
|
|
|
|
|
|
# MR: critical
|
|
|
|
it "concats arrays before expanding them" do
|
|
|
|
a = ['a', 'b', 'c', 'd']
|
|
|
|
b = ['f']
|
|
|
|
|
|
|
|
case 'f'
|
2017-12-27 11:12:47 -05:00
|
|
|
when 'f', *a|b
|
|
|
|
"foo"
|
|
|
|
when *['x', 'y', 'z']
|
|
|
|
"bar"
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == "foo"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "never matches when clauses with no values" do
|
|
|
|
case nil
|
2017-12-27 11:12:47 -05:00
|
|
|
when *[]
|
|
|
|
"foo"
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "lets you define a method after the case statement" do
|
|
|
|
case (def foo; 'foo'; end; 'f')
|
2017-12-27 11:12:47 -05:00
|
|
|
when 'a'
|
|
|
|
'foo'
|
|
|
|
when 'f'
|
|
|
|
'bar'
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == 'bar'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises a SyntaxError when 'else' is used when no 'when' is given" do
|
|
|
|
lambda {
|
|
|
|
eval <<-CODE
|
|
|
|
case 4
|
2017-12-27 11:12:47 -05:00
|
|
|
else
|
|
|
|
true
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
CODE
|
|
|
|
}.should raise_error(SyntaxError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises a SyntaxError when 'else' is used before a 'when' was given" do
|
|
|
|
lambda {
|
|
|
|
eval <<-CODE
|
|
|
|
case 4
|
2017-12-27 11:12:47 -05:00
|
|
|
else
|
|
|
|
true
|
|
|
|
when 4; false
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
CODE
|
|
|
|
}.should raise_error(SyntaxError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "supports nested case statements" do
|
|
|
|
result = false
|
|
|
|
case :x
|
|
|
|
when Symbol
|
|
|
|
case :y
|
|
|
|
when Symbol
|
|
|
|
result = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
result.should == true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "supports nested case statements followed by a when with a splatted array" do
|
|
|
|
result = false
|
|
|
|
case :x
|
|
|
|
when Symbol
|
|
|
|
case :y
|
|
|
|
when Symbol
|
|
|
|
result = true
|
|
|
|
end
|
|
|
|
when *[Symbol]
|
|
|
|
result = false
|
|
|
|
end
|
|
|
|
result.should == true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "supports nested case statements followed by a when with a splatted non-array" do
|
|
|
|
result = false
|
|
|
|
case :x
|
|
|
|
when Symbol
|
|
|
|
case :y
|
|
|
|
when Symbol
|
|
|
|
result = true
|
|
|
|
end
|
|
|
|
when *Symbol
|
|
|
|
result = false
|
|
|
|
end
|
|
|
|
result.should == true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works even if there's only one when statement" do
|
|
|
|
case 1
|
|
|
|
when 1
|
|
|
|
100
|
|
|
|
end.should == 100
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "The 'case'-construct with no target expression" do
|
|
|
|
it "evaluates the body of the first clause when at least one of its condition expressions is true" do
|
|
|
|
case
|
2017-12-27 11:12:47 -05:00
|
|
|
when true, false; 'foo'
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == 'foo'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "evaluates the body of the first when clause that is not false/nil" do
|
|
|
|
case
|
2017-12-27 11:12:47 -05:00
|
|
|
when false; 'foo'
|
|
|
|
when 2; 'bar'
|
|
|
|
when 1 == 1; 'baz'
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == 'bar'
|
|
|
|
|
|
|
|
case
|
2017-12-27 11:12:47 -05:00
|
|
|
when false; 'foo'
|
|
|
|
when nil; 'foo'
|
|
|
|
when 1 == 1; 'bar'
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == 'bar'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "evaluates the body of the else clause if all when clauses are false/nil" do
|
|
|
|
case
|
2017-12-27 11:12:47 -05:00
|
|
|
when false; 'foo'
|
|
|
|
when nil; 'foo'
|
|
|
|
when 1 == 2; 'bar'
|
|
|
|
else 'baz'
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == 'baz'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "evaluates multiple conditional expressions as a boolean disjunction" do
|
|
|
|
case
|
2017-12-27 11:12:47 -05:00
|
|
|
when true, false; 'foo'
|
|
|
|
else 'bar'
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == 'foo'
|
|
|
|
|
|
|
|
case
|
2017-12-27 11:12:47 -05:00
|
|
|
when false, true; 'foo'
|
|
|
|
else 'bar'
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == 'foo'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "evaluates true as only 'true' when true is the first clause" do
|
|
|
|
case 1
|
2017-12-27 11:12:47 -05:00
|
|
|
when true; "bad"
|
|
|
|
when Integer; "good"
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == "good"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "evaluates false as only 'false' when false is the first clause" do
|
|
|
|
case nil
|
2017-12-27 11:12:47 -05:00
|
|
|
when false; "bad"
|
|
|
|
when nil; "good"
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == "good"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "treats a literal array as its own when argument, rather than a list of arguments" do
|
|
|
|
case 'foo'
|
|
|
|
when ['foo', 'foo']; 'bad'
|
|
|
|
when 'foo'; 'good'
|
|
|
|
end.should == 'good'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "takes multiple expanded arrays" do
|
|
|
|
a1 = ['f', 'o', 'o']
|
|
|
|
a2 = ['b', 'a', 'r']
|
|
|
|
|
|
|
|
case 'f'
|
2017-12-27 11:12:47 -05:00
|
|
|
when *a1, *['x', 'y', 'z']
|
|
|
|
"foo"
|
|
|
|
when *a2, *['x', 'y', 'z']
|
|
|
|
"bar"
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == "foo"
|
|
|
|
|
|
|
|
case 'b'
|
2017-12-27 11:12:47 -05:00
|
|
|
when *a1, *['x', 'y', 'z']
|
|
|
|
"foo"
|
|
|
|
when *a2, *['x', 'y', 'z']
|
|
|
|
"bar"
|
2017-05-07 08:04:49 -04:00
|
|
|
end.should == "bar"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "calls === even when private" do
|
|
|
|
klass = Class.new do
|
|
|
|
def ===(o)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
private :===
|
|
|
|
end
|
|
|
|
|
|
|
|
case 1
|
|
|
|
when klass.new
|
|
|
|
:called
|
|
|
|
end.should == :called
|
|
|
|
end
|
2017-10-28 11:15:48 -04:00
|
|
|
|
|
|
|
it "accepts complex expressions within ()" do
|
|
|
|
case 'a'
|
|
|
|
when (raise if 2+2 == 3; /a/)
|
|
|
|
:called
|
|
|
|
end.should == :called
|
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|