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

ruby-changes:47695

From: nagachika <ko1@a...>
Date: Sun, 10 Sep 2017 12:49:15 +0900 (JST)
Subject: [ruby-changes:47695] nagachika:r59811 (ruby_2_4): merge revision(s) 59624, 59626: [Backport #13830]

nagachika	2017-09-10 12:49:10 +0900 (Sun, 10 Sep 2017)

  New Revision: 59811

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59811

  Log:
    merge revision(s) 59624,59626: [Backport #13830]
    
    ruby.h: fix rb_scan_args_trail_idx
    
    * include/ruby/ruby.h (rb_scan_args_trail_idx): fix the case both
      of optional and rest arguments are defined.
      [ruby-core:82427] [Bug #13830]
    
    * include/ruby/ruby.h (rb_scan_args_n_trail): ditto.
    non-keywords hash
    
    * class.c (rb_scan_args), include/ruby/ruby.h (rb_scan_args_set):
      return non-keywords elements only in the last hash when keyword
      arguments are extracted from it, as well as methods defined in
      ruby level.  [ruby-core:82427] [Bug #13830]

  Added directories:
    branches/ruby_2_4/ext/-test-/scan_args/
  Added files:
    branches/ruby_2_4/test/-ext-/test_scan_args.rb
  Modified directories:
    branches/ruby_2_4/
  Modified files:
    branches/ruby_2_4/class.c
    branches/ruby_2_4/include/ruby/ruby.h
    branches/ruby_2_4/version.h
Index: ruby_2_4/class.c
===================================================================
--- ruby_2_4/class.c	(revision 59810)
+++ ruby_2_4/class.c	(revision 59811)
@@ -1924,8 +1924,8 @@ rb_scan_args(int argc, const VALUE *argv https://github.com/ruby/ruby/blob/trunk/ruby_2_4/class.c#L1924
     va_list vargs;
     int f_var = 0, f_hash = 0, f_block = 0;
     int n_lead = 0, n_opt = 0, n_trail = 0, n_mand;
-    int argi = 0;
-    VALUE hash = Qnil;
+    int argi = 0, last_idx = -1;
+    VALUE hash = Qnil, last_hash = 0;
 
     if (ISDIGIT(*p)) {
 	n_lead = *p - '0';
@@ -1976,7 +1976,8 @@ rb_scan_args(int argc, const VALUE *argv https://github.com/ruby/ruby/blob/trunk/ruby_2_4/class.c#L1976
 	    hash = rb_check_hash_type(last);
 	    if (!NIL_P(hash)) {
 		VALUE opts = rb_extract_keywords(&hash);
-		if (!hash) argc--;
+		if (!(last_hash = hash)) argc--;
+		else last_idx = argc - 1;
 		hash = opts ? opts : Qnil;
 	    }
 	}
@@ -1984,14 +1985,14 @@ rb_scan_args(int argc, const VALUE *argv https://github.com/ruby/ruby/blob/trunk/ruby_2_4/class.c#L1985
     /* capture leading mandatory arguments */
     for (i = n_lead; i-- > 0; ) {
 	var = va_arg(vargs, VALUE *);
-	if (var) *var = argv[argi];
+	if (var) *var = (argi == last_idx) ? last_hash : argv[argi];
 	argi++;
     }
     /* capture optional arguments */
     for (i = n_opt; i-- > 0; ) {
 	var = va_arg(vargs, VALUE *);
 	if (argi < argc - n_trail) {
-	    if (var) *var = argv[argi];
+	    if (var) *var = (argi == last_idx) ? last_hash : argv[argi];
 	    argi++;
 	}
 	else {
@@ -2004,7 +2005,11 @@ rb_scan_args(int argc, const VALUE *argv https://github.com/ruby/ruby/blob/trunk/ruby_2_4/class.c#L2005
 
 	var = va_arg(vargs, VALUE *);
 	if (0 < n_var) {
-	    if (var) *var = rb_ary_new4(n_var, &argv[argi]);
+	    if (var) {
+		int f_last = (last_idx + 1 == argc - n_trail);
+		*var = rb_ary_new4(n_var-f_last, &argv[argi]);
+		if (f_last) rb_ary_push(*var, last_hash);
+	    }
 	    argi += n_var;
 	}
 	else {
@@ -2014,7 +2019,7 @@ rb_scan_args(int argc, const VALUE *argv https://github.com/ruby/ruby/blob/trunk/ruby_2_4/class.c#L2019
     /* capture trailing mandatory arguments */
     for (i = n_trail; i-- > 0; ) {
 	var = va_arg(vargs, VALUE *);
-	if (var) *var = argv[argi];
+	if (var) *var = (argi == last_idx) ? last_hash : argv[argi];
 	argi++;
     }
     /* capture an option hash - phase 2: assignment */
Index: ruby_2_4/ext/-test-/scan_args/extconf.rb
===================================================================
--- ruby_2_4/ext/-test-/scan_args/extconf.rb	(nonexistent)
+++ ruby_2_4/ext/-test-/scan_args/extconf.rb	(revision 59811)
@@ -0,0 +1 @@
+create_makefile("-test-/scan_args")

Property changes on: ruby_2_4/ext/-test-/scan_args/extconf.rb
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+LF
\ No newline at end of property
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Index: ruby_2_4/ext/-test-/scan_args/scan_args.c
===================================================================
--- ruby_2_4/ext/-test-/scan_args/scan_args.c	(nonexistent)
+++ ruby_2_4/ext/-test-/scan_args/scan_args.c	(revision 59811)
@@ -0,0 +1,286 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_4/ext/-test-/scan_args/scan_args.c#L1
+#include <ruby.h>
+
+#ifndef numberof
+#define numberof(array) (int)(sizeof(array) / sizeof((array)[0]))
+#endif
+
+static VALUE
+scan_args_lead(int argc, VALUE *argv, VALUE self)
+{
+    VALUE args[2];
+    int n = rb_scan_args(argc, argv, "1", args+1);
+    args[0] = INT2NUM(n);
+    return rb_ary_new_from_values(numberof(args), args);
+}
+
+static VALUE
+scan_args_opt(int argc, VALUE *argv, VALUE self)
+{
+    VALUE args[2];
+    int n = rb_scan_args(argc, argv, "01", args+1);
+    args[0] = INT2NUM(n);
+    return rb_ary_new_from_values(numberof(args), args);
+}
+
+static VALUE
+scan_args_lead_opt(int argc, VALUE *argv, VALUE self)
+{
+    VALUE args[3];
+    int n = rb_scan_args(argc, argv, "11", args+1, args+2);
+    args[0] = INT2NUM(n);
+    return rb_ary_new_from_values(numberof(args), args);
+}
+
+/* var */
+static VALUE
+scan_args_var(int argc, VALUE *argv, VALUE self)
+{
+    VALUE args[2];
+    int n = rb_scan_args(argc, argv, "*", args+1);
+    args[0] = INT2NUM(n);
+    return rb_ary_new_from_values(numberof(args), args);
+}
+
+static VALUE
+scan_args_lead_var(int argc, VALUE *argv, VALUE self)
+{
+    VALUE args[3];
+    int n = rb_scan_args(argc, argv, "1*", args+1, args+2);
+    args[0] = INT2NUM(n);
+    return rb_ary_new_from_values(numberof(args), args);
+}
+
+static VALUE
+scan_args_opt_var(int argc, VALUE *argv, VALUE self)
+{
+    VALUE args[3];
+    int n = rb_scan_args(argc, argv, "01*", args+1, args+2);
+    args[0] = INT2NUM(n);
+    return rb_ary_new_from_values(numberof(args), args);
+}
+
+static VALUE
+scan_args_lead_opt_var(int argc, VALUE *argv, VALUE self)
+{
+    VALUE args[4];
+    int n = rb_scan_args(argc, argv, "11*", args+1, args+2, args+3);
+    args[0] = INT2NUM(n);
+    return rb_ary_new_from_values(numberof(args), args);
+}
+
+/* trail */
+static VALUE
+scan_args_opt_trail(int argc, VALUE *argv, VALUE self)
+{
+    VALUE args[3];
+    int n = rb_scan_args(argc, argv, "011", args+1, args+2);
+    args[0] = INT2NUM(n);
+    return rb_ary_new_from_values(numberof(args), args);
+}
+
+static VALUE
+scan_args_lead_opt_trail(int argc, VALUE *argv, VALUE self)
+{
+    VALUE args[4];
+    int n = rb_scan_args(argc, argv, "111", args+1, args+2, args+3);
+    args[0] = INT2NUM(n);
+    return rb_ary_new_from_values(numberof(args), args);
+}
+
+static VALUE
+scan_args_var_trail(int argc, VALUE *argv, VALUE self)
+{
+    VALUE args[3];
+    int n = rb_scan_args(argc, argv, "*1", args+1, args+2);
+    args[0] = INT2NUM(n);
+    return rb_ary_new_from_values(numberof(args), args);
+}
+
+static VALUE
+scan_args_lead_var_trail(int argc, VALUE *argv, VALUE self)
+{
+    VALUE args[4];
+    int n = rb_scan_args(argc, argv, "1*1", args+1, args+2, args+3);
+    args[0] = INT2NUM(n);
+    return rb_ary_new_from_values(numberof(args), args);
+}
+
+static VALUE
+scan_args_opt_var_trail(int argc, VALUE *argv, VALUE self)
+{
+    VALUE args[4];
+    int n = rb_scan_args(argc, argv, "01*1", args+1, args+2, args+3);
+    args[0] = INT2NUM(n);
+    return rb_ary_new_from_values(numberof(args), args);
+}
+
+static VALUE
+scan_args_lead_opt_var_trail(int argc, VALUE *argv, VALUE self)
+{
+    VALUE args[5];
+    int n = rb_scan_args(argc, argv, "11*1", args+1, args+2, args+3, args+4);
+    args[0] = INT2NUM(n);
+    return rb_ary_new_from_values(numberof(args), args);
+}
+
+/* hash */
+static VALUE
+scan_args_hash(int argc, VALUE *argv, VALUE self)
+{
+    VALUE args[2];
+    int n = rb_scan_args(argc, argv, ":", args+1);
+    args[0] = INT2NUM(n);
+    return rb_ary_new_from_values(numberof(args), args);
+}
+
+static VALUE
+scan_args_lead_hash(int argc, VALUE *argv, VALUE self)
+{
+    VALUE args[3];
+    int n = rb_scan_args(argc, argv, "1:", args+1, args+2);
+    args[0] = INT2NUM(n);
+    return rb_ary_new_from_values(numberof(args), args);
+}
+
+static VALUE
+scan_args_opt_hash(int argc, VALUE *argv, VALUE self)
+{
+    VALUE args[3];
+    int n = rb_scan_args(argc, argv, "01:", args+1, args+2);
+    args[0] = INT2NUM(n);
+    return rb_ary_new_from_values(numberof(args), args);
+}
+
+static VALUE
+scan_args_lead_opt_hash(int argc, VALUE *argv, VALUE self)
+{
+    VALUE args[4];
+    int n = rb_scan_args(argc, argv, "11:", args+1, args+2, args+3);
+    args[0] = INT2NUM(n);
+    return rb_ary_new_from_values(numberof(args), args);
+}
+
+static VALUE
+scan_args_var_hash(int argc, VALUE *argv, VALUE self)
+{
+    VALUE args[3];
+    int n = rb_scan_args(argc, argv, "*:", args+1, args+2);
+    args[0] = INT2NUM(n);
+    return rb_ary_new_from_values(numberof(args), args);
+}
+
+static VALUE
+scan_args_lead_var_hash(int argc, VALUE *argv, VALUE self)
+{
+    VALUE args[4];
+    int n = rb_scan_args(argc, argv, "1*:", args+1, args+2, args+3);
+    args[0] = INT2NUM(n);
+    return rb_ary_new_from_values(numberof(args), args);
+}
+
+static VALUE
+scan_args_opt_var_hash(int argc, VALUE *argv, VALUE self)
+{
+    VALUE args[4];
+    int n = rb_scan_args(argc, argv, "01*:", args+1, args+2, args+3);
+    args[0] = INT2NUM(n);
+    return rb_ary_new_from_values(numberof(args), args);
+}
+
+static VALUE
+scan_args_lead_opt_var_hash(int argc, VALUE *argv, VALUE self)
+{
+    VALUE args[5];
+    int n = rb_scan_args(argc, argv, "11*:", args+1, args+2, args+3, args+4);
+    args[0] = INT2NUM(n);
+    return rb_ary_new_from_values(numberof(args), args);
+}
+
+static VALUE
+scan_args_opt_trail_hash(int argc, VALUE *argv, VALUE self)
+{
+    VALUE args[4];
+    int n = rb_scan_args(argc, argv, "011:", args+1, args+2, args+3);
+    args[0] = INT2NUM(n);
+    return rb_ary_new_from_values(numberof(args), args);
+}
+
+static VALUE
+scan_args_lead_opt_trail_hash(int argc, VALUE *argv, VALUE self)
+{
+    VALUE args[5];
+    int n = rb_scan_args(argc, argv, "111:", args+1, args+2, args+3, args+4);
+    args[0] = INT2NUM(n);
+    return rb_ary_new_from_values(numberof(args), args);
+}
+
+static VALUE
+scan_args_var_trail_hash(int argc, VALUE *argv, VALUE self)
+{
+    VALUE args[4];
+    int n = rb_scan_args(argc, argv, "*1:", args+1, args+2, args+3);
+    args[0] = INT2NUM(n);
+    return rb_ary_new_from_values(numberof(args), args);
+}
+
+static VALUE
+scan_args_lead_var_trail_hash(int argc, VALUE *argv, VALUE self)
+{
+    VALUE args[5];
+    int n = rb_scan_args(argc, argv, "1*1:", args+1, args+2, args+3, args+4);
+    args[0] = INT2NUM(n);
+    return rb_ary_new_from_values(numberof(args), args);
+}
+
+static VALUE
+scan_args_opt_var_trail_hash(int argc, VALUE *argv, VALUE self)
+{
+    VALUE args[5];
+    int n = rb_scan_args(argc, argv, "01*1:", args+1, args+2, args+3, args+4);
+    args[0] = INT2NUM(n);
+    return rb_ary_new_from_values(numberof(args), args);
+}
+
+static VALUE
+scan_args_lead_opt_var_trail_hash(int argc, VALUE *argv, VALUE self)
+{
+    VALUE args[6];
+    int n = rb_scan_args(argc, argv, "11*1:", args+1, args+2, args+3, args+4, args+5);
+    args[0] = INT2NUM(n);
+    return rb_ary_new_from_values(numberof(args), args);
+}
+
+void
+Init_scan_args(void)
+{
+    VALUE module = rb_define_module("Bug");
+    module = rb_define_module_under(module, "ScanArgs");
+    rb_define_singleton_method(module, "lead", scan_args_lead, -1);
+    rb_define_singleton_method(module, "opt", scan_args_opt, -1);
+    rb_define_singleton_method(module, "lead_opt", scan_args_lead_opt, -1);
+    rb_define_singleton_method(module, "var", scan_args_var, -1);
+    rb_define_singleton_method(module, "lead_var", scan_args_lead_var, -1);
+    rb_define_singleton_method(module, "opt_var", scan_args_opt_var, -1);
+    rb_define_singleton_method(module, "lead_opt_var", scan_args_lead_opt_var, -1);
+    rb_define_singleton_method(module, "opt_trail", scan_args_opt_trail, -1);
+    rb_define_singleton_method(module, "lead_opt_trail", scan_args_lead_opt_trail, -1);
+    rb_define_singleton_method(module, "var_trail", scan_args_var_trail, -1);
+    rb_define_singleton_method(module, "lead_var_trail", scan_args_lead_var_trail, -1);
+    rb_define_singleton_method(module, "opt_var_trail", scan_args_opt_var_trail, -1);
+    rb_define_singleton_method(module, "lead_opt_var_trail", scan_args_lead_opt_var_trail, -1);
+    rb_define_singleton_method(module, "hash", scan_args_hash, -1);
+    rb_define_singleton_method(module, "lead_hash", scan_args_lead_hash, -1);
+    rb_define_singleton_method(module, "opt_hash", scan_args_opt_hash, -1);
+    rb_define_singleton_method(module, "lead_opt_hash", scan_args_lead_opt_hash, -1);
+    rb_define_singleton_method(module, "var_hash", scan_args_var_hash, -1);
+    rb_define_singleton_method(module, "lead_var_hash", scan_args_lead_var_hash, -1);
+    rb_define_singleton_method(module, "opt_var_hash", scan_args_opt_var_hash, -1);
+    rb_define_singleton_method(module, "lead_opt_var_hash", scan_args_lead_opt_var_hash, -1);
+    rb_define_singleton_method(module, "opt_trail_hash", scan_args_opt_trail_hash, -1);
+    rb_define_singleton_method(module, "lead_opt_trail_hash", scan_args_lead_opt_trail_hash, -1);
+    rb_define_singleton_method(module, "var_trail_hash", scan_args_var_trail_hash, -1);
+    rb_define_singleton_method(module, "lead_var_trail_hash", scan_args_lead_var_trail_hash, -1);
+    rb_define_singleton_method(module, "opt_var_trail_hash", scan_args_opt_var_trail_hash, -1);
+    rb_define_singleton_method(module, "lead_opt_var_trail_hash", scan_args_lead_opt_var_trail_hash, -1);
+}
+

Property changes on: ruby_2_4/ext/-test-/scan_args/scan_args.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+LF
\ No newline at end of property
Index: ruby_2_4/version.h
===================================================================
--- ruby_2_4/version.h	(revision 59810)
+++ ruby_2_4/version.h	(revision 59811)
@@ -1,6 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_4/version.h#L1
 #define RUBY_VERSION "2.4.2"
 #define RUBY_RELEASE_DATE "2017-09-10"
-#define RUBY_PATCHLEVEL 187
+#define RUBY_PATCHLEVEL 188
 
 #define RUBY_RELEASE_YEAR 2017
 #define RUBY_RELEASE_MONTH 9
Index: ruby_2_4/test/-ext-/test_scan_args.rb
===================================================================
--- ruby_2_4/test/-ext-/test_scan_args.rb	(nonexistent)
+++ ruby_2_4/test/-ext-/test_scan_args.rb	(revision 59811)
@@ -0,0 +1,231 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_4/test/-ext-/test_scan_args.rb#L1
+require 'test/unit'
+require '-test-/scan_args'
+
+class TestScanArgs < Test::Unit::TestCase
+  def test_lead
+    assert_raise(ArgumentError) {Bug::ScanArgs.lead()}
+    assert_equal([1, "a"], Bug::ScanArgs.lead("a"))
+    assert_raise(ArgumentError) {Bug::ScanArgs.lead("a", "b")}
+  end
+
+  def test_opt
+    assert_equal([0, nil], Bug::ScanArgs.opt())
+    assert_equal([1, "a"], Bug::ScanArgs.opt("a"))
+    assert_raise(ArgumentError) {Bug::ScanArgs.opt("a", "b")}
+  end
+
+  def test_lead_opt
+    assert_raise(ArgumentError) {Bug::ScanArgs.lead_opt()}
+    assert_equal([1, "a", nil], Bug::ScanArgs.lead_opt("a"))
+    assert_equal([2, "a", "b"], Bug::ScanArgs.lead_opt("a", "b"))
+    assert_raise(ArgumentError) {Bug::ScanArgs.lead_opt("a", "b", "c")}
+  end
+
+  def test_var
+    assert_equal([0, []], Bug::ScanArgs.var())
+    assert_equal([3, ["a", "b", "c"]], Bug::ScanArgs.var("a", "b", "c"))
+  end
+
+  def test_lead_var
+    assert_raise(ArgumentError) {Bug::ScanArgs.lead_var()}
+    assert_equal([3, "a", ["b", "c"]], Bug::ScanArgs.lead_var("a", "b", "c"))
+  end
+
+  def test_opt_var
+    assert_equal([0, nil, []], Bug::ScanArgs.opt_var())
+    assert_equal([3, "a", ["b", "c"]], Bug::ScanArgs.opt_var("a", "b", "c"))
+  end
+
+  def test_lead_opt_var
+    assert_raise(ArgumentError) {Bug::ScanArgs.lead_opt_var()}
+    assert_equal([3, "a", "b", ["c"]], Bug::ScanArgs.lead_opt_var("a", "b", "c"))
+  end
+
+  def test_opt_trail
+    assert_raise(ArgumentError) {Bug::ScanArgs.opt_trail()}
+    assert_equal([2, "a", "b"], Bug::ScanArgs.opt_trail("a", "b"))
+    assert_equal([1, nil, "a"], Bug::ScanArgs.opt_trail("a"))
+    assert_raise(ArgumentError) {Bug::ScanArgs.opt_trail("a", "b", "c")}
+  end
+
+  def test_lead_opt_trail
+    assert_raise(ArgumentError) {Bug::ScanArgs.lead_opt_trail()}
+    assert_raise(ArgumentError) {Bug::ScanArgs.lead_opt_trail("a")}
+    assert_equal([2, "a", nil, "b"], Bug::ScanArgs.lead_opt_trail("a", "b"))
+    assert_equal([3, "a", "b", "c"], Bug::ScanArgs.lead_opt_trail("a", "b", "c"))
+    assert_raise(ArgumentError) {Bug::ScanArgs.lead_opt_trail("a", "b", "c", "d")}
+  end
+
+  def test_var_trail
+    assert_raise(ArgumentError) {Bug::ScanArgs.var_trail()}
+    assert_equal([1, [], "a"], Bug::ScanArgs.var_trail("a"))
+    assert_equal([2, ["a"], "b"], Bug::ScanArgs.var_trail("a", "b"))
+  end
+
+  def test_lead_var_trail
+    assert_raise(ArgumentError) {Bug::ScanArgs.lead_var_trail("a")}
+    assert_equal([2, "a", [], "b"], Bug::ScanArgs.lead_var_trail("a", "b"))
+    assert_equal([3, "a", ["b"], "c"], Bug::ScanArgs.lead_var_trail("a", "b", "c"))
+  end
+
+  def test_opt_var_trail
+    assert_raise(ArgumentError) {Bug::ScanArgs.opt_var_trail()}
+    assert_equal([1, nil, [], "a"], Bug::ScanArgs.opt_var_trail("a"))
+    assert_equal([2, "a", [], "b"], Bug::ScanArgs.opt_var_trail("a", "b"))
+    assert_equal([3, "a", ["b"], "c"], Bug::ScanArgs.opt_var_trail("a", "b", "c"))
+  end
+
+  def test_lead_opt_var_trail
+    assert_raise(ArgumentError) {Bug::ScanArgs.lead_opt_var_trail("a")}
+    assert_equal([2, "a", nil, [], "b"], Bug::ScanArgs.lead_opt_var_trail("a", "b"))
+    assert_equal([3, "a", "b", [], "c"], Bug::ScanArgs.lead_opt_var_trail("a", "b", "c"))
+    assert_equal([4, "a", "b", ["c"], "d"], Bug::ScanArgs.lead_opt_var_trail("a", "b", "c", "d"))
+  end
+
+  def test_hash
+    assert_equal([0, nil], Bug::ScanArgs.hash())
+    assert_raise(ArgumentError) {Bug::ScanArgs.hash("a")}
+    assert_equal([0, {a: 0}], Bug::ScanArgs.hash(a: 0))
+  end
+
+  def test_lead_hash
+    assert_raise(ArgumentError) {Bug::ScanArgs.lead_hash()}
+    assert_equal([1, "a", nil], Bug::ScanArgs.lead_hash("a"))
+    assert_raise(ArgumentError) {Bug::ScanArgs.lead_hash("a", "b")}
+    assert_equal([1, "a", {b: 1}], Bug::ScanArgs.lead_hash("a", b: 1))
+    assert_equal([1, {b: 1}, nil], Bug::ScanArgs.lead_hash(b: 1))
+  end
+
+  def test_opt_hash
+    assert_equal([0, nil, nil], Bug::ScanArgs.opt_hash())
+    assert_equal([1, "a", nil], Bug::ScanArgs.opt_hash("a"))
+    assert_equal([0, nil, {b: 1}], Bug::ScanArgs.opt_hash(b: 1))
+    assert_equal([1, "a", {b: 1}], Bug::ScanArgs.opt_hash("a", b: 1))
+    assert_raise(ArgumentError) {Bug::ScanArgs.opt_hash("a", "b")}
+    assert_equal([1, {"a"=>0}, {b: 1}], Bug::ScanArgs.opt_hash("a"=>0, b: 1))
+  end
+
+  def test_lead_opt_hash
+    assert_equal([1, "a", nil, nil], Bug::ScanArgs.lead_opt_hash("a"))
+    assert_equal([2, "a", "b", nil], Bug::ScanArgs.lead_opt_hash("a", "b"))
+    assert_equal([1, "a", nil, {c: 1}], Bug::ScanArgs.lead_opt_hash("a", c: 1))
+    assert_equal([2, "a", "b", {c: 1}], Bug::ScanArgs.lead_opt_hash("a", "b", c: 1))
+    assert_equal([1, {c: 1}, nil, nil], Bug::ScanArgs.lead_opt_hash(c: 1))
+    assert_raise(ArgumentError) {Bug::ScanArgs.lead_opt_hash("a", "b", "c")}
+    assert_equal([2, "a", {"b"=>0}, {c: 1}], Bug::ScanArgs.lead_opt_hash("a", "b"=>0, c: 1))
+  end
+
+  def test_var_hash
+    assert_equal([0, [], nil], Bug::ScanArgs.var_hash())
+    assert_equal([1, ["a"], nil], Bug::ScanArgs.var_hash("a"))
+    assert_equal([1, ["a"], {b: 1}], Bug::ScanArgs.var_hash("a", b: 1))
+    assert_equal([0, [], {b: 1}], Bug::ScanArgs.var_hash(b: 1))
+    assert_equal([1, [{"a"=>0}], {b: 1}], Bug::ScanArgs.var_hash("a"=>0, b: 1))
+  end
+
+  def test_lead_var_hash
+    assert_raise(ArgumentError) {Bug::ScanArgs.lead_var_hash()}
+    assert_equal([1, "a", [], nil], Bug::ScanArgs.lead_var_hash("a"))
+    assert_equal([2, "a", ["b"], nil], Bug::ScanArgs.lead_var_hash("a", "b"))
+    assert_equal([2, "a", ["b"], {c: 1}], Bug::ScanArgs.lead_var_hash("a", "b", c: 1))
+    assert_equal([1, "a", [], {c: 1}], Bug::ScanArgs.lead_var_hash("a", c: 1))
+    assert_equal([1, {c: 1}, [], nil], Bug::ScanArgs.lead_var_hash(c: 1))
+    assert_equal([3, "a", ["b", "c"], nil], Bug::ScanArgs.lead_var_hash("a", "b", "c"))
+    assert_equal([2, "a", [{"b"=>0}], {c: 1}], Bug::ScanArgs.lead_var_hash("a", "b"=>0, c: 1))
+  end
+
+  def test_opt_var_hash
+    assert_equal([0, nil, [], nil], Bug::ScanArgs.opt_var_hash())
+    assert_equal([1, "a", [], nil], Bug::ScanArgs.opt_var_hash("a"))
+    assert_equal([2, "a", ["b"], nil], Bug::ScanArgs.opt_var_hash("a", "b"))
+    assert_equal([2, "a", ["b"], {c: 1}], Bug::ScanArgs.opt_var_hash("a", "b", c: 1))
+     (... truncated)

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

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