summaryrefslogtreecommitdiff
path: root/spec/ruby/language
diff options
context:
space:
mode:
authoreregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-12-27 16:12:47 +0000
committereregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-12-27 16:12:47 +0000
commita34db218ade33b79e7404488db5a15bad2841c25 (patch)
tree2ed22ad149cd75e36d9aabbe29b32e96c27fa3a6 /spec/ruby/language
parent0f989b87a06563add3fdeb9cda983492e8a420af (diff)
Update to ruby/spec@0fe33ac
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61504 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/ruby/language')
-rw-r--r--spec/ruby/language/case_spec.rb252
-rw-r--r--spec/ruby/language/def_spec.rb8
-rw-r--r--spec/ruby/language/defined_spec.rb34
-rw-r--r--spec/ruby/language/predefined_spec.rb31
-rw-r--r--spec/ruby/language/regexp/back-references_spec.rb3
-rw-r--r--spec/ruby/language/send_spec.rb23
6 files changed, 213 insertions, 138 deletions
diff --git a/spec/ruby/language/case_spec.rb b/spec/ruby/language/case_spec.rb
index 25f5d0efc4..212964070d 100644
--- a/spec/ruby/language/case_spec.rb
+++ b/spec/ruby/language/case_spec.rb
@@ -3,15 +3,15 @@ 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
- when 2; false
- when 1; true
+ when 2; false
+ when 1; true
end.should == true
end
it "evaluates the body of the when clause whose array expression includes the case target expression" do
case 2
- when 3, 4; false
- when 1, 2; true
+ when 3, 4; false
+ when 1, 2; true
end.should == true
end
@@ -21,7 +21,7 @@ describe "The 'case'-construct" do
def bar; @calls << :bar; end
case true
- when foo, bar;
+ when foo, bar;
end
@calls.should == [:foo, :bar]
@@ -29,31 +29,31 @@ describe "The 'case'-construct" do
it "evaluates the body of the when clause whose range expression includes the case target expression" do
case 5
- when 21..30; false
- when 1..20; true
+ when 21..30; false
+ when 1..20; true
end.should == true
end
it "returns nil when no 'then'-bodies are given" do
case "a"
- when "a"
- when "b"
+ when "a"
+ when "b"
end.should == nil
end
it "evaluates the 'else'-body when no other expression matches" do
case "c"
- when "a"; 'foo'
- when "b"; 'bar'
- else 'zzz'
+ when "a"; 'foo'
+ when "b"; 'bar'
+ else 'zzz'
end.should == 'zzz'
end
it "returns nil when no expression matches and 'else'-body is empty" do
case "c"
- when "a"; "a"
- when "b"; "b"
- else
+ when "a"; "a"
+ when "b"; "b"
+ else
end.should == nil
end
@@ -70,105 +70,143 @@ describe "The 'case'-construct" do
it "returns the statement following 'then'" do
case "a"
- when "a" then 'foo'
- when "b" then 'bar'
+ when "a" then 'foo'
+ when "b" then 'bar'
end.should == 'foo'
end
it "tests classes with case equality" do
case "a"
- when String
- 'foo'
- when Symbol
- 'bar'
+ when String
+ 'foo'
+ when Symbol
+ 'bar'
end.should == 'foo'
end
it "tests with matching regexps" do
case "hello"
- when /abc/; false
- when /^hell/; true
+ when /abc/; false
+ when /^hell/; true
end.should == true
end
+ 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
+
it "does not test with equality when given classes" do
case :symbol.class
- when Symbol
- "bar"
- when String
- "bar"
- else
- "foo"
+ when Symbol
+ "bar"
+ when String
+ "bar"
+ else
+ "foo"
end.should == "foo"
end
it "takes lists of values" do
case 'z'
- when 'a', 'b', 'c', 'd'
- "foo"
- when 'x', 'y', 'z'
- "bar"
+ when 'a', 'b', 'c', 'd'
+ "foo"
+ when 'x', 'y', 'z'
+ "bar"
end.should == "bar"
case 'b'
- when 'a', 'b', 'c', 'd'
- "foo"
- when 'x', 'y', 'z'
- "bar"
+ when 'a', 'b', 'c', 'd'
+ "foo"
+ when 'x', 'y', 'z'
+ "bar"
end.should == "foo"
end
it "expands arrays to lists of values" do
case 'z'
- when *['a', 'b', 'c', 'd']
- "foo"
- when *['x', 'y', 'z']
- "bar"
+ when *['a', 'b', 'c', 'd']
+ "foo"
+ when *['x', 'y', 'z']
+ "bar"
end.should == "bar"
end
it "takes an expanded array in addition to a list of values" do
case 'f'
- when 'f', *['a', 'b', 'c', 'd']
- "foo"
- when *['x', 'y', 'z']
- "bar"
+ when 'f', *['a', 'b', 'c', 'd']
+ "foo"
+ when *['x', 'y', 'z']
+ "bar"
end.should == "foo"
case 'b'
- when 'f', *['a', 'b', 'c', 'd']
- "foo"
- when *['x', 'y', 'z']
- "bar"
+ when 'f', *['a', 'b', 'c', 'd']
+ "foo"
+ when *['x', 'y', 'z']
+ "bar"
end.should == "foo"
end
it "takes an expanded array before additional listed values" do
case 'f'
- when *['a', 'b', 'c', 'd'], 'f'
- "foo"
- when *['x', 'y', 'z']
- "bar"
+ when *['a', 'b', 'c', 'd'], 'f'
+ "foo"
+ when *['x', 'y', 'z']
+ "bar"
end.should == 'foo'
end
it "expands arrays from variables before additional listed values" do
a = ['a', 'b', 'c']
case 'a'
- when *a, 'd', 'e'
- "foo"
- when 'x'
- "bar"
+ when *a, 'd', 'e'
+ "foo"
+ when 'x'
+ "bar"
end.should == "foo"
end
it "expands arrays from variables before a single additional listed value" do
a = ['a', 'b', 'c']
case 'a'
- when *a, 'd'
- "foo"
- when 'x'
- "bar"
+ when *a, 'd'
+ "foo"
+ when 'x'
+ "bar"
end.should == "foo"
end
@@ -177,10 +215,10 @@ describe "The 'case'-construct" do
b = ['d', 'e', 'f']
case 'f'
- when *a, *b, 'g', 'h'
- "foo"
- when 'x'
- "bar"
+ when *a, *b, 'g', 'h'
+ "foo"
+ when 'x'
+ "bar"
end.should == "foo"
end
@@ -190,26 +228,26 @@ describe "The 'case'-construct" do
b = ['f']
case 'f'
- when 'f', *a|b
- "foo"
- when *['x', 'y', 'z']
- "bar"
+ when 'f', *a|b
+ "foo"
+ when *['x', 'y', 'z']
+ "bar"
end.should == "foo"
end
it "never matches when clauses with no values" do
case nil
- when *[]
- "foo"
+ when *[]
+ "foo"
end.should == nil
end
it "lets you define a method after the case statement" do
case (def foo; 'foo'; end; 'f')
- when 'a'
- 'foo'
- when 'f'
- 'bar'
+ when 'a'
+ 'foo'
+ when 'f'
+ 'bar'
end.should == 'bar'
end
@@ -217,8 +255,8 @@ describe "The 'case'-construct" do
lambda {
eval <<-CODE
case 4
- else
- true
+ else
+ true
end
CODE
}.should raise_error(SyntaxError)
@@ -228,9 +266,9 @@ describe "The 'case'-construct" do
lambda {
eval <<-CODE
case 4
- else
- true
- when 4; false
+ else
+ true
+ when 4; false
end
CODE
}.should raise_error(SyntaxError)
@@ -287,56 +325,56 @@ 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
- when true, false; 'foo'
+ when true, false; 'foo'
end.should == 'foo'
end
it "evaluates the body of the first when clause that is not false/nil" do
case
- when false; 'foo'
- when 2; 'bar'
- when 1 == 1; 'baz'
+ when false; 'foo'
+ when 2; 'bar'
+ when 1 == 1; 'baz'
end.should == 'bar'
case
- when false; 'foo'
- when nil; 'foo'
- when 1 == 1; 'bar'
+ when false; 'foo'
+ when nil; 'foo'
+ when 1 == 1; 'bar'
end.should == 'bar'
end
it "evaluates the body of the else clause if all when clauses are false/nil" do
case
- when false; 'foo'
- when nil; 'foo'
- when 1 == 2; 'bar'
- else 'baz'
+ when false; 'foo'
+ when nil; 'foo'
+ when 1 == 2; 'bar'
+ else 'baz'
end.should == 'baz'
end
it "evaluates multiple conditional expressions as a boolean disjunction" do
case
- when true, false; 'foo'
- else 'bar'
+ when true, false; 'foo'
+ else 'bar'
end.should == 'foo'
case
- when false, true; 'foo'
- else 'bar'
+ when false, true; 'foo'
+ else 'bar'
end.should == 'foo'
end
it "evaluates true as only 'true' when true is the first clause" do
case 1
- when true; "bad"
- when Integer; "good"
+ when true; "bad"
+ when Integer; "good"
end.should == "good"
end
it "evaluates false as only 'false' when false is the first clause" do
case nil
- when false; "bad"
- when nil; "good"
+ when false; "bad"
+ when nil; "good"
end.should == "good"
end
@@ -352,17 +390,17 @@ describe "The 'case'-construct with no target expression" do
a2 = ['b', 'a', 'r']
case 'f'
- when *a1, *['x', 'y', 'z']
- "foo"
- when *a2, *['x', 'y', 'z']
- "bar"
+ when *a1, *['x', 'y', 'z']
+ "foo"
+ when *a2, *['x', 'y', 'z']
+ "bar"
end.should == "foo"
case 'b'
- when *a1, *['x', 'y', 'z']
- "foo"
- when *a2, *['x', 'y', 'z']
- "bar"
+ when *a1, *['x', 'y', 'z']
+ "foo"
+ when *a2, *['x', 'y', 'z']
+ "bar"
end.should == "bar"
end
diff --git a/spec/ruby/language/def_spec.rb b/spec/ruby/language/def_spec.rb
index 55ee283b90..c31a2afb41 100644
--- a/spec/ruby/language/def_spec.rb
+++ b/spec/ruby/language/def_spec.rb
@@ -234,10 +234,10 @@ describe "A singleton method definition" do
(obj==2).should == 2
end
- it "raises RuntimeError if frozen" do
+ it "raises #{frozen_error_class} if frozen" do
obj = Object.new
obj.freeze
- lambda { def obj.foo; end }.should raise_error(RuntimeError)
+ lambda { def obj.foo; end }.should raise_error(frozen_error_class)
end
end
@@ -385,12 +385,12 @@ describe "A method definition inside a metaclass scope" do
lambda { Object.new.a_singleton_method }.should raise_error(NoMethodError)
end
- it "raises RuntimeError if frozen" do
+ it "raises #{frozen_error_class} if frozen" do
obj = Object.new
obj.freeze
class << obj
- lambda { def foo; end }.should raise_error(RuntimeError)
+ lambda { def foo; end }.should raise_error(frozen_error_class)
end
end
end
diff --git a/spec/ruby/language/defined_spec.rb b/spec/ruby/language/defined_spec.rb
index 0c8e657786..05f3075459 100644
--- a/spec/ruby/language/defined_spec.rb
+++ b/spec/ruby/language/defined_spec.rb
@@ -543,16 +543,12 @@ describe "The defined? keyword for variables" do
defined?($+).should be_nil
end
- it "returns nil for $1-$9" do
+ it "returns nil for any last match global" do
defined?($1).should be_nil
- defined?($2).should be_nil
- defined?($3).should be_nil
defined?($4).should be_nil
- defined?($5).should be_nil
- defined?($6).should be_nil
defined?($7).should be_nil
- defined?($8).should be_nil
- defined?($9).should be_nil
+ defined?($10).should be_nil
+ defined?($200).should be_nil
end
end
@@ -587,13 +583,10 @@ describe "The defined? keyword for variables" do
end
it "returns nil for non-captures" do
- defined?($3).should be_nil
defined?($4).should be_nil
- defined?($5).should be_nil
- defined?($6).should be_nil
defined?($7).should be_nil
- defined?($8).should be_nil
- defined?($9).should be_nil
+ defined?($10).should be_nil
+ defined?($200).should be_nil
end
end
@@ -622,16 +615,12 @@ describe "The defined? keyword for variables" do
defined?($+).should be_nil
end
- it "returns nil for $1-$9" do
+ it "returns nil for any last match global" do
defined?($1).should be_nil
- defined?($2).should be_nil
- defined?($3).should be_nil
defined?($4).should be_nil
- defined?($5).should be_nil
- defined?($6).should be_nil
defined?($7).should be_nil
- defined?($8).should be_nil
- defined?($9).should be_nil
+ defined?($10).should be_nil
+ defined?($200).should be_nil
end
end
@@ -666,13 +655,10 @@ describe "The defined? keyword for variables" do
end
it "returns nil for non-captures" do
- defined?($3).should be_nil
defined?($4).should be_nil
- defined?($5).should be_nil
- defined?($6).should be_nil
defined?($7).should be_nil
- defined?($8).should be_nil
- defined?($9).should be_nil
+ defined?($10).should be_nil
+ defined?($200).should be_nil
end
end
it "returns 'global-variable' for a global variable that has been assigned" do
diff --git a/spec/ruby/language/predefined_spec.rb b/spec/ruby/language/predefined_spec.rb
index f827fb2eb5..91cffce2e6 100644
--- a/spec/ruby/language/predefined_spec.rb
+++ b/spec/ruby/language/predefined_spec.rb
@@ -30,11 +30,11 @@ $` String The string preceding the match in a successful
is local to the current scope. [r/o, thread]
$' String The string following the match in a successful pattern match. This variable
is local to the current scope. [r/o, thread]
-$1 to $9 String The contents of successive groups matched in a successful pattern match. In
+$1 to $<N> String The contents of successive groups matched in a successful pattern match. In
"cat" =~/(c|a)(t|z)/, $1 will be set to “a” and $2 to “t”. This variable
is local to the current scope. [r/o, thread]
$~ MatchData An object that encapsulates the results of a successful pattern match. The
- variables $&, $`, $', and $1 to $9 are all derived from $~. Assigning to $~
+ variables $&, $`, $', and $1 to $<N> are all derived from $~. Assigning to $~
changes the values of these derived variables. This variable is local to the
current scope. [thread]
=end
@@ -645,6 +645,33 @@ describe "Predefined global $," do
end
end
+describe "Predefined global $." do
+ it "can be assigned an Integer" do
+ $. = 123
+ $..should == 123
+ end
+
+ it "can be assigned a Float" do
+ $. = 123.5
+ $..should == 123
+ end
+
+ it "should call #to_int to convert the object to an Integer" do
+ obj = mock("good-value")
+ obj.should_receive(:to_int).and_return(321)
+
+ $. = obj
+ $..should == 321
+ end
+
+ it "raises TypeError if object can't be converted to an Integer" do
+ obj = mock("bad-value")
+ obj.should_receive(:to_int).and_return('abc')
+
+ lambda { $. = obj }.should raise_error(TypeError)
+ end
+end
+
describe "Predefined global $_" do
it "is set to the last line read by e.g. StringIO#gets" do
stdin = StringIO.new("foo\nbar\n", "r")
diff --git a/spec/ruby/language/regexp/back-references_spec.rb b/spec/ruby/language/regexp/back-references_spec.rb
index 607f4463fd..b4fb3b66a5 100644
--- a/spec/ruby/language/regexp/back-references_spec.rb
+++ b/spec/ruby/language/regexp/back-references_spec.rb
@@ -7,7 +7,7 @@ describe "Regexps with back-references" do
$~.to_a.should == ["ll"]
end
- it "saves captures in numbered $[1-9] variables" do
+ it "saves captures in numbered $[1-N] variables" do
"1234567890" =~ /(1)(2)(3)(4)(5)(6)(7)(8)(9)(0)/
$~.to_a.should == ["1234567890", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]
$1.should == "1"
@@ -19,6 +19,7 @@ describe "Regexps with back-references" do
$7.should == "7"
$8.should == "8"
$9.should == "9"
+ $10.should == "0"
end
it "will not clobber capture variables across threads" do
diff --git a/spec/ruby/language/send_spec.rb b/spec/ruby/language/send_spec.rb
index 646a700785..15b73aac7f 100644
--- a/spec/ruby/language/send_spec.rb
+++ b/spec/ruby/language/send_spec.rb
@@ -403,6 +403,29 @@ describe "Invoking a method" do
specs.rest_len(0,*a,4,*5,6,7,*c,-1).should == 11
end
+ it "expands the Array elements from the splat after executing the arguments and block if no other arguments follow the splat" do
+ def self.m(*args, &block)
+ [args, block]
+ end
+
+ args = [1, nil]
+ m(*args, &args.pop).should == [[1], nil]
+
+ args = [1, nil]
+ order = []
+ m(*(order << :args; args), &(order << :block; args.pop)).should == [[1], nil]
+ order.should == [:args, :block]
+ end
+
+ it "evaluates the splatted arguments before the block if there are other arguments after the splat" do
+ def self.m(*args, &block)
+ [args, block]
+ end
+
+ args = [1, nil]
+ m(*args, 2, &args.pop).should == [[1, nil, 2], nil]
+ end
+
it "expands an array to arguments grouped in parentheses" do
specs.destructure2([40,2]).should == 42
end