[前][次][番号順一覧][スレッド一覧]

ruby-changes:59611

From: Jeremy <ko1@a...>
Date: Fri, 3 Jan 2020 11:41:08 +0900 (JST)
Subject: [ruby-changes:59611] e014e6bf66 (master): Update specs for keyword argument separation

https://git.ruby-lang.org/ruby.git/commit/?id=e014e6bf66

From e014e6bf6685f681998238ff005f6d161d43ce51 Mon Sep 17 00:00:00 2001
From: Jeremy Evans <code@j...>
Date: Sun, 6 Oct 2019 09:26:58 -0700
Subject: Update specs for keyword argument separation


diff --git a/spec/ruby/core/io/shared/new.rb b/spec/ruby/core/io/shared/new.rb
index a7b4fc1..27d32f9 100644
--- a/spec/ruby/core/io/shared/new.rb
+++ b/spec/ruby/core/io/shared/new.rb
@@ -197,11 +197,21 @@ describe :io_new, shared: true do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/io/shared/new.rb#L197
     @io.internal_encoding.to_s.should == 'IBM866'
   end
 
-  it "accepts nil options" do
-    @io = suppress_keyword_warning do
-      IO.send(@method, @fd, 'w', nil)
+  ruby_version_is ''...'2.8' do
+    it "accepts nil options" do
+      @io = suppress_keyword_warning do
+        IO.send(@method, @fd, 'w', nil)
+      end
+      @io.write("foo").should == 3
+    end
+  end
+
+  ruby_version_is '2.8' do
+    it "raises ArgumentError for nil options" do
+      -> {
+        IO.send(@method, @fd, 'w', nil)
+      }.should raise_error(ArgumentError)
     end
-    @io.write("foo").should == 3
   end
 
   it "coerces mode with #to_str" do
@@ -372,11 +382,21 @@ describe :io_new_errors, shared: true do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/io/shared/new.rb#L382
     }.should raise_error(ArgumentError)
   end
 
-  it "raises TypeError if passed a hash for mode and nil for options" do
-    -> {
-      suppress_keyword_warning do
+  ruby_version_is ''...'2.8' do
+    it "raises TypeError if passed a hash for mode and nil for options" do
+      -> {
+        suppress_keyword_warning do
+          @io = IO.send(@method, @fd, {mode: 'w'}, nil)
+        end
+      }.should raise_error(TypeError)
+    end
+  end
+
+  ruby_version_is '2.8' do
+    it "raises ArgumentError if passed a hash for mode and nil for options" do
+      -> {
         @io = IO.send(@method, @fd, {mode: 'w'}, nil)
-      end
-    }.should raise_error(TypeError)
+      }.should raise_error(ArgumentError)
+    end
   end
 end
diff --git a/spec/ruby/language/block_spec.rb b/spec/ruby/language/block_spec.rb
index 6f92383..1a8e790 100644
--- a/spec/ruby/language/block_spec.rb
+++ b/spec/ruby/language/block_spec.rb
@@ -44,27 +44,51 @@ describe "A block yielded a single" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/language/block_spec.rb#L44
       m([1, 2]) { |a, **k| [a, k] }.should == [1, {}]
     end
 
-    it "assigns elements to mixed argument types" do
-      suppress_keyword_warning do
-        result = m([1, 2, 3, {x: 9}]) { |a, b=5, *c, d, e: 2, **k| [a, b, c, d, e, k] }
-        result.should == [1, 2, [], 3, 2, {x: 9}]
+    ruby_version_is ''..."2.8" do
+      it "assigns elements to mixed argument types" do
+        suppress_keyword_warning do
+          result = m([1, 2, 3, {x: 9}]) { |a, b=5, *c, d, e: 2, **k| [a, b, c, d, e, k] }
+          result.should == [1, 2, [], 3, 2, {x: 9}]
+        end
+      end
+
+      it "assigns symbol keys from a Hash to keyword arguments" do
+        suppress_keyword_warning do
+          result = m(["a" => 1, a: 10]) { |a=nil, **b| [a, b] }
+          result.should == [{"a" => 1}, a: 10]
+        end
+      end
+
+      it "assigns symbol keys from a Hash returned by #to_hash to keyword arguments" do
+        suppress_keyword_warning do
+          obj = mock("coerce block keyword arguments")
+          obj.should_receive(:to_hash).and_return({"a" => 1, b: 2})
+
+          result = m([obj]) { |a=nil, **b| [a, b] }
+          result.should == [{"a" => 1}, b: 2]
+        end
       end
     end
 
-    it "assigns symbol keys from a Hash to keyword arguments" do
-      suppress_keyword_warning do
+    ruby_version_is "2.8" do
+      it "assigns elements to mixed argument types" do
+        result = m([1, 2, 3, {x: 9}]) { |a, b=5, *c, d, e: 2, **k| [a, b, c, d, e, k] }
+        result.should == [1, 2, [3], {x: 9}, 2, {}]
+      end
+
+      it "does not treat final Hash as keyword arguments" do
         result = m(["a" => 1, a: 10]) { |a=nil, **b| [a, b] }
-        result.should == [{"a" => 1}, a: 10]
+        result.should == [{"a" => 1, a: 10}, {}]
       end
-    end
 
-    it "assigns symbol keys from a Hash returned by #to_hash to keyword arguments" do
-      suppress_keyword_warning do
-        obj = mock("coerce block keyword arguments")
-        obj.should_receive(:to_hash).and_return({"a" => 1, b: 2})
+      it "does not call #to_hash on final argument to get keyword arguments" do
+        suppress_keyword_warning do
+          obj = mock("coerce block keyword arguments")
+          obj.should_not_receive(:to_hash)
 
-        result = m([obj]) { |a=nil, **b| [a, b] }
-        result.should == [{"a" => 1}, b: 2]
+          result = m([obj]) { |a=nil, **b| [a, b] }
+          result.should == [obj, {}]
+        end
       end
     end
 
@@ -78,7 +102,7 @@ describe "A block yielded a single" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/language/block_spec.rb#L102
       end
     end
 
-    ruby_version_is "2.7" do
+    ruby_version_is "2.7"...'2.8' do
       it "calls #to_hash on the argument but ignores result when optional argument and keyword argument accepted" do
         obj = mock("coerce block keyword arguments")
         obj.should_receive(:to_hash).and_return({"a" => 1, "b" => 2})
@@ -88,11 +112,31 @@ describe "A block yielded a single" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/language/block_spec.rb#L112
       end
     end
 
+    ruby_version_is "2.8" do
+      it "does not call #to_hash on the argument when optional argument and keyword argument accepted" do
+        obj = mock("coerce block keyword arguments")
+        obj.should_not_receive(:to_hash)
+
+        result = m([obj]) { |a=nil, **b| [a, b] }
+        result.should == [obj, {}]
+      end
+    end
+
     describe "when non-symbol keys are in a keyword arguments Hash" do
-      it "separates non-symbol keys and symbol keys" do
-        suppress_keyword_warning do
-          result = m(["a" => 10, b: 2]) { |a=nil, **b| [a, b] }
-          result.should == [{"a" => 10}, {b: 2}]
+      ruby_version_is ""..."2.8" do
+        it "separates non-symbol keys and symbol keys" do
+          suppress_keyword_warning do
+            result = m(["a" => 10, b: 2]) { |a=nil, **b| [a, b] }
+            result.should == [{"a" => 10}, {b: 2}]
+          end
+        end
+      end
+      ruby_version_is "2.8" do
+        it "does not separates non-symbol keys and symbol keys" do
+          suppress_keyword_warning do
+            result = m(["a" => 10, b: 2]) { |a=nil, **b| [a, b] }
+            result.should == [{"a" => 10, b: 2}, {}]
+          end
         end
       end
     end
@@ -102,51 +146,71 @@ describe "A block yielded a single" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/language/block_spec.rb#L146
       result.should == [{"a" => 10}, {}]
     end
 
-    it "calls #to_hash on the last element if keyword arguments are present" do
-      suppress_keyword_warning do
+    ruby_version_is ''...'2.8' do
+      it "calls #to_hash on the last element if keyword arguments are present" do
+        suppress_keyword_warning do
+          obj = mock("destructure block keyword arguments")
+          obj.should_receive(:to_hash).and_return({x: 9})
+
+          result = m([1, 2, 3, obj]) { |a, *b, c, **k| [a, b, c, k] }
+          result.should == [1, [2], 3, {x: 9}]
+        end
+      end
+
+      it "assigns the last element to a non-keyword argument if #to_hash returns nil" do
+        suppress_keyword_warning do
+          obj = mock("destructure block keyword arguments")
+          obj.should_receive(:to_hash).and_return(nil)
+
+          result = m([1, 2, 3, obj]) { |a, *b, c, **k| [a, b, c, k] }
+          result.should == [1, [2, 3], obj, {}]
+        end
+      end
+
+      it "calls #to_hash on the last element when there are more arguments than parameters" do
+        suppress_keyword_warning do
+          x = mock("destructure matching block keyword argument")
+          x.should_receive(:to_hash).and_return({x: 9})
+
+          result = m([1, 2, 3, {y: 9}, 4, 5, x]) { |a, b=5, c, **k| [a, b, c, k] }
+          result.should == [1, 2, 3, {x: 9}]
+        end
+      end
+
+      it "raises a TypeError if #to_hash does not return a Hash" do
         obj = mock("destructure block keyword arguments")
-        obj.should_receive(:to_hash).and_return({x: 9})
+        obj.should_receive(:to_hash).and_return(1)
 
-        result = m([1, 2, 3, obj]) { |a, *b, c, **k| [a, b, c, k] }
-        result.should == [1, [2], 3, {x: 9}]
+        -> { m([1, 2, 3, obj]) { |a, *b, c, **k| } }.should raise_error(TypeError)
+      end
+
+      it "raises the error raised inside #to_hash" do
+        obj = mock("destructure block keyword arguments")
+        error = RuntimeError.new("error while converting to a hash")
+        obj.should_receive(:to_hash).and_raise(error)
+
+        -> { m([1, 2, 3, obj]) { |a, *b, c, **k| } }.should raise_error(error)
       end
     end
 
-    it "assigns the last element to a non-keyword argument if #to_hash returns nil" do
-      suppress_keyword_warning do
+    ruby_version_is '2.8' do
+      it "does not call #to_hash on the last element if keyword arguments are present" do
         obj = mock("destructure block keyword arguments")
-        obj.should_receive(:to_hash).and_return(nil)
+        obj.should_not_receive(:to_hash)
 
         result = m([1, 2, 3, obj]) { |a, *b, c, **k| [a, b, c, k] }
         result.should == [1, [2, 3], obj, {}]
       end
-    end
 
-    it "calls #to_hash on the last element when there are more arguments than parameters" do
-      suppress_keyword_warning do
+      it "does not call #to_hash on the last element when there are more arguments than parameters" do
         x = mock("destructure matching block keyword argument")
-        x.should_receive(:to_hash).and_return({x: 9})
+        x.should_not_receive(:to_hash)
 
         r (... truncated)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]