summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xext/-test-/scan_args/extconf.rb1
-rw-r--r--ext/-test-/scan_args/scan_args.c286
-rw-r--r--include/ruby/ruby.h21
-rw-r--r--test/-ext-/test_scan_args.rb219
4 files changed, 510 insertions, 17 deletions
diff --git a/ext/-test-/scan_args/extconf.rb b/ext/-test-/scan_args/extconf.rb
new file mode 100755
index 0000000..6cae9c2
--- /dev/null
+++ b/ext/-test-/scan_args/extconf.rb
@@ -0,0 +1 @@
+create_makefile("-test-/scan_args")
diff --git a/ext/-test-/scan_args/scan_args.c b/ext/-test-/scan_args/scan_args.c
new file mode 100644
index 0000000..dca353f
--- /dev/null
+++ b/ext/-test-/scan_args/scan_args.c
@@ -0,0 +1,286 @@
+#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);
+}
+
diff --git a/include/ruby/ruby.h b/include/ruby/ruby.h
index 4c72cfc..566e5f6 100644
--- a/include/ruby/ruby.h
+++ b/include/ruby/ruby.h
@@ -2279,29 +2279,16 @@ ALWAYS_INLINE(static int rb_scan_args_trail_idx(const char *fmt));
static inline int
rb_scan_args_trail_idx(const char *fmt)
{
- return (rb_scan_args_lead_p(fmt) ?
- (rb_scan_args_isdigit(fmt[1]) || fmt[1]=='*')+1 :
- (fmt[0]=='*'));
-}
-
-ALWAYS_INLINE(static int rb_scan_args_trail_p(const char *fmt));
-static inline int
-rb_scan_args_trail_p(const char *fmt)
-{
- return (rb_scan_args_lead_p(fmt) ?
- (rb_scan_args_isdigit(fmt[1]) || fmt[1]=='*') &&
- rb_scan_args_isdigit(fmt[2]) :
- fmt[0]=='*' && rb_scan_args_isdigit(fmt[1]));
+ const int idx = rb_scan_args_var_idx(fmt);
+ return idx+(fmt[idx]=='*');
}
ALWAYS_INLINE(static int rb_scan_args_n_trail(const char *fmt));
static inline int
rb_scan_args_n_trail(const char *fmt)
{
- return (rb_scan_args_lead_p(fmt) ?
- ((rb_scan_args_isdigit(fmt[1]) || fmt[1]=='*') &&
- rb_scan_args_isdigit(fmt[2]) ? fmt[2]-'0' : 0) :
- (fmt[0]=='*' && rb_scan_args_isdigit(fmt[1]) ? fmt[1]-'0' : 0));
+ const int idx = rb_scan_args_trail_idx(fmt);
+ return (rb_scan_args_isdigit(fmt[idx]) ? fmt[idx]-'0' : 0);
}
ALWAYS_INLINE(static int rb_scan_args_hash_idx(const char *fmt));
diff --git a/test/-ext-/test_scan_args.rb b/test/-ext-/test_scan_args.rb
new file mode 100644
index 0000000..7f929e4
--- /dev/null
+++ b/test/-ext-/test_scan_args.rb
@@ -0,0 +1,219 @@
+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")}
+ 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")}
+ 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))
+ 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"))
+ 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))
+ assert_equal([1, "a", [], {c: 1}], Bug::ScanArgs.opt_var_hash("a", c: 1))
+ assert_equal([0, nil, [], {c: 1}], Bug::ScanArgs.opt_var_hash(c: 1))
+ assert_equal([3, "a", ["b", "c"], nil], Bug::ScanArgs.opt_var_hash("a", "b", "c"))
+ end
+
+ def test_lead_opt_var_hash
+ assert_raise(ArgumentError) {Bug::ScanArgs.lead_opt_var_hash()}
+ assert_equal([1, "a", nil, [], nil], Bug::ScanArgs.lead_opt_var_hash("a"))
+ assert_equal([2, "a", "b", [], nil], Bug::ScanArgs.lead_opt_var_hash("a", "b"))
+ assert_equal([2, "a", "b", [], {c: 1}], Bug::ScanArgs.lead_opt_var_hash("a", "b", c: 1))
+ assert_equal([1, "a", nil, [], {c: 1}], Bug::ScanArgs.lead_opt_var_hash("a", c: 1))
+ assert_equal([1, {c: 1}, nil, [], nil], Bug::ScanArgs.lead_opt_var_hash(c: 1))
+ assert_equal([3, "a", "b", ["c"], nil], Bug::ScanArgs.lead_opt_var_hash("a", "b", "c"))
+ assert_equal([3, "a", "b", ["c"], {d: 1}], Bug::ScanArgs.lead_opt_var_hash("a", "b", "c", d: 1))
+ end
+
+ def test_opt_trail_hash
+ assert_raise(ArgumentError) {Bug::ScanArgs.opt_trail_hash()}
+ assert_equal([1, nil, "a", nil], Bug::ScanArgs.opt_trail_hash("a"))
+ assert_equal([2, "a", "b", nil], Bug::ScanArgs.opt_trail_hash("a", "b"))
+ assert_equal([1, nil, "a", {c: 1}], Bug::ScanArgs.opt_trail_hash("a", c: 1))
+ assert_equal([2, "a", "b", {c: 1}], Bug::ScanArgs.opt_trail_hash("a", "b", c: 1))
+ assert_equal([1, nil, {c: 1}, nil], Bug::ScanArgs.opt_trail_hash(c: 1))
+ assert_raise(ArgumentError) {Bug::ScanArgs.opt_trail_hash("a", "b", "c")}
+ end
+
+ def test_lead_opt_trail_hash
+ assert_raise(ArgumentError) {Bug::ScanArgs.lead_opt_trail_hash()}
+ assert_raise(ArgumentError) {Bug::ScanArgs.lead_opt_trail_hash("a")}
+ assert_raise(ArgumentError) {Bug::ScanArgs.lead_opt_trail_hash(c: 1)}
+ assert_equal([2, "a", nil, "b", nil], Bug::ScanArgs.lead_opt_trail_hash("a", "b"))
+ assert_equal([2, "a", nil, {c: 1}, nil], Bug::ScanArgs.lead_opt_trail_hash("a", c: 1))
+ assert_equal([2, "a", nil, "b", {c: 1}], Bug::ScanArgs.lead_opt_trail_hash("a", "b", c: 1))
+ assert_equal([3, "a", "b", "c", nil], Bug::ScanArgs.lead_opt_trail_hash("a", "b", "c"))
+ assert_equal([3, "a", "b", "c", {c: 1}], Bug::ScanArgs.lead_opt_trail_hash("a", "b", "c", c: 1))
+ assert_raise(ArgumentError) {Bug::ScanArgs.lead_opt_trail_hash("a", "b", "c", "d")}
+ end
+
+ def test_var_trail_hash
+ assert_raise(ArgumentError) {Bug::ScanArgs.var_trail_hash()}
+ assert_equal([1, [], "a", nil], Bug::ScanArgs.var_trail_hash("a"))
+ assert_equal([2, ["a"], "b", nil], Bug::ScanArgs.var_trail_hash("a", "b"))
+ assert_equal([1, [], "a", {c: 1}], Bug::ScanArgs.var_trail_hash("a", c: 1))
+ assert_equal([2, ["a"], "b", {c: 1}], Bug::ScanArgs.var_trail_hash("a", "b", c: 1))
+ assert_equal([1, [], {c: 1}, nil], Bug::ScanArgs.var_trail_hash(c: 1))
+ assert_equal([3, ["a", "b"], "c", nil], Bug::ScanArgs.var_trail_hash("a", "b", "c"))
+ assert_equal([3, ["a", "b"], "c", {c: 1}], Bug::ScanArgs.var_trail_hash("a", "b", "c", c: 1))
+ end
+
+ def test_lead_var_trail_hash
+ assert_raise(ArgumentError) {Bug::ScanArgs.lead_var_trail_hash()}
+ assert_raise(ArgumentError) {Bug::ScanArgs.lead_var_trail_hash("a")}
+ assert_raise(ArgumentError) {Bug::ScanArgs.lead_var_trail_hash(c: 1)}
+ assert_equal([2, "a", [], {c: 1}, nil], Bug::ScanArgs.lead_var_trail_hash("a", c: 1))
+ assert_equal([2, "a", [], "b", nil], Bug::ScanArgs.lead_var_trail_hash("a", "b"))
+ assert_equal([2, "a", [], "b", {c: 1}], Bug::ScanArgs.lead_var_trail_hash("a", "b", c: 1))
+ assert_equal([3, "a", ["b"], "c", nil], Bug::ScanArgs.lead_var_trail_hash("a", "b", "c"))
+ assert_equal([3, "a", ["b"], "c", {c: 1}], Bug::ScanArgs.lead_var_trail_hash("a", "b", "c", c: 1))
+ end
+
+ def test_opt_var_trail_hash
+ assert_raise(ArgumentError) {Bug::ScanArgs.opt_var_trail_hash()}
+ assert_equal([1, nil, [], "a", nil], Bug::ScanArgs.opt_var_trail_hash("a"))
+ assert_equal([1, nil, [], {c: 1}, nil], Bug::ScanArgs.opt_var_trail_hash(c: 1))
+ assert_equal([1, nil, [], "a", {c: 1}], Bug::ScanArgs.opt_var_trail_hash("a", c: 1))
+ assert_equal([2, "a", [], "b", nil], Bug::ScanArgs.opt_var_trail_hash("a", "b"))
+ assert_equal([2, "a", [], "b", {c: 1}], Bug::ScanArgs.opt_var_trail_hash("a", "b", c: 1))
+ assert_equal([3, "a", ["b"], "c", nil], Bug::ScanArgs.opt_var_trail_hash("a", "b", "c"))
+ assert_equal([3, "a", ["b"], "c", {c: 1}], Bug::ScanArgs.opt_var_trail_hash("a", "b", "c", c: 1))
+ end
+
+ def test_lead_opt_var_trail_hash
+ assert_raise(ArgumentError) {Bug::ScanArgs.lead_opt_var_trail_hash()}
+ assert_raise(ArgumentError) {Bug::ScanArgs.lead_opt_var_trail_hash("a")}
+ assert_equal([2, "a", nil, [], {b: 1}, nil], Bug::ScanArgs.lead_opt_var_trail_hash("a", b: 1))
+ assert_equal([2, "a", nil, [], "b", nil], Bug::ScanArgs.lead_opt_var_trail_hash("a", "b"))
+ assert_equal([2, "a", nil, [], "b", {c: 1}], Bug::ScanArgs.lead_opt_var_trail_hash("a", "b", c: 1))
+ assert_equal([3, "a", "b", [], "c", nil], Bug::ScanArgs.lead_opt_var_trail_hash("a", "b", "c"))
+ assert_equal([3, "a", "b", [], "c", {c: 1}], Bug::ScanArgs.lead_opt_var_trail_hash("a", "b", "c", c: 1))
+ assert_equal([4, "a", "b", ["c"], "d", nil], Bug::ScanArgs.lead_opt_var_trail_hash("a", "b", "c", "d"))
+ end
+end