Permalink
Browse files

Stop Proxy's arity check from requiring args that are optional.

Signed-off-by: Jeremy McAnally <jeremymcanally@gmail.com>
  • Loading branch information...
1 parent 63c14db commit 1ed0ae4fc58fd59aa2bacf620c461634d126fb10 @eostrom eostrom committed with Nov 20, 2008
Showing with 35 additions and 5 deletions.
  1. +16 −4 lib/stump/proxy.rb
  2. +5 −1 test/test_helper.rb
  3. +14 −0 test/test_proxy.rb
View
@@ -50,10 +50,22 @@ def proxy_existing_method(method, options = {}, &block)
meta_eval do
module_eval("alias #{method_alias} #{method}")
end
-
+
+ check_arity = Proc.new do |args|
+ arity = method(method_alias.to_sym).arity
+ if ((arity >= 0) ?
+ (args.length != arity) :
+ (args.length < ~arity))
+ # Negative arity means some params are optional, so we check
+ # for the minimum required. Sadly, we can't tell what the
+ # maximum is.
+ raise ArgumentError
+ end
+ end
+
behavior = if options[:return]
lambda do |*args|
- raise ArgumentError if args.length != method(method_alias.to_sym).arity
+ check_arity.call(args)
Stump::Mocks.verify([self, method])
@@ -67,7 +79,7 @@ def proxy_existing_method(method, options = {}, &block)
end
else
lambda do |*args|
- raise ArgumentError if args.length != method(method_alias.to_sym).arity
+ check_arity.call(args)
Stump::Mocks.verify([self, method])
@@ -100,4 +112,4 @@ def proxy_missing_method(method, options = {}, &block)
meta_def method, &behavior
end
-end
+end
View
@@ -9,4 +9,8 @@ def tree
def trunk(name)
return name.upcase
end
-end
+
+ def branch(factor, initial = 1)
+ return factor * initial
+ end
+end
View
@@ -52,6 +52,20 @@ def test_proxy_args_are_required
end
end
+ def test_proxy_optional_args_are_not_required
+ proxied = MyStump.new
+ proxied.proxy!(:branch)
+
+ assert_raise ArgumentError do
+ proxied.branch
+ end
+
+ assert_nothing_raised do
+ assert_equal 3, proxied.branch(3)
+ assert_equal 6, proxied.branch(3, 2)
+ end
+ end
+
def test_proxy_fail
stumply = MyStump.new
stumply.proxy!(:tree, :return => "hi")

0 comments on commit 1ed0ae4

Please sign in to comment.