Permalink
Browse files

Move responsibility of error reporting to Reception from Reception::A…

…rguments
  • Loading branch information...
1 parent 2febc9d commit bb4850cbee01e535eedfa046b142deefe21a5e97 Nikolai Weibull committed Aug 1, 2012
@@ -85,18 +85,16 @@ def to_lookout_expected
# verify that any arguments are what they’re expected to be and then invoke
# _block_
def block(&block)
- args, calls = Lookout::Reception::Arguments.new(*@args), 0
+ args, calls = Arguments.for(*@args), 0
reception, object, method, range, body = self, @object, @method, @range, @body || Nil
proc{
stub(object, method => proc{ |*mock_args, &mock_block|
calls += 1
- raise Lookout::Reception::Error.
- from(reception, calls, range) if calls > range.end
- begin
- args.verify(*mock_args)
- rescue Lookout::Reception::Arguments::Error => e
- raise e, '%s: %s' % [reception, e]
- end
+ raise Error.from(reception, calls, range) if calls > range.end
+ raise Arguments::Error,
+ '%s: unexpected arguments: [%s]≠[%s]' %
+ [reception, Arguments::List.new(*mock_args), args] unless
+ args =~ mock_args
body.call(*mock_args, &mock_block)
}, &block) if block
calls
@@ -1,36 +1,20 @@
# -*- coding: utf-8 -*-
-# Arguments that should be passed to the mock method. Sets things up based on
-# what kind of arguments were passed to it and may then be used to {#verify}
-# that arguments that don’t {Lookout::Difference differ} from them are
-# subsequently passed to the mock method.
-class Lookout::Reception::Arguments
- # Sets up the expected _args_ to a mock method. If _args_ is #empty?, {Any}
- # will be used. If _args_ contains an {Any} or a {None}, they’ll be used.
- # Otherwise, _args_ will be wrapped in a {List}.
- # @param [Object, …] args
- Value(:'*args')
- def initialize(*args)
- @args = if args.empty? then Any.new
- elsif any = args.find{ |e| Any === e } then any
- elsif none = args.find{ |e| None === e } then none
- else List.new(*args)
- end
+# Arguments that should be passed to a method reception expectation. Sets
+# things up based on what kind of arguments were passed to it and may then be
+# used to {List#=~ verify} that arguments that don’t {Lookout::Difference
+# differ} from them are subsequently passed to the method.
+module Lookout::Reception::Arguments
+ class << self
+ # @param [Object, …] args
+ # @return [Any, List, None] {Any} if _args_ is #empty?, {Any} or {None} if
+ # _args_ contain either, or a {List} wrapping _args_
+ def for(*args)
+ if args.empty? then Any.new
+ elsif any = args.find{ |e| Any === e } then any
+ elsif none = args.find{ |e| None === e } then none
+ else List.new(*args)
+ end
+ end
end
-
- # Checks for {Lookout::Difference differences} between _args_ and the
- # expected ones.
- # @param [Object, …] args
- # @raise [Error] If the passed _args_ differ from the expected ones.
- # @return [true]
- def verify(*args)
- self.args =~ args or
- raise Error, 'unexpected arguments ([%s]≠[%s])' % [List.new(*args), self.args]
- end
-
- # @return The arguments as a String argument list
- def to_s; (result = args.to_s).empty? ? result : '(%s)' % result end
-
- # @return [Object, Array<Object>] The expected arguments as an array
- def to_a; args.to_a end
end
@@ -13,7 +13,4 @@ def hash; self.class.hash end
# @return The receiver as a String argument list
def to_s; '*args, &block' end
-
- # @return [Object, Array<Object>] The receiver as an array
- def to_a; [self] end
end
@@ -14,7 +14,4 @@ def =~(other) not args.to_lookout_expected.difference(other) end
# @return The receiver as a String argument list
def to_s; Lookout::Inspect::Argument.list(*args) end
-
- # @return [Object, Array<Object>] The expected arguments
- def to_a; args.to_a end
end
@@ -3,7 +3,4 @@
# Arguments matcher matching an empty list of arguments.
class Lookout::Reception::Arguments::None < Lookout::Reception::Arguments::List
def initialize; super end
-
- # @return [Object, Array<Object>] The receiver as an array
- def to_a; [self] end
end
@@ -72,7 +72,15 @@
stub.to.receive.call(1).to_lookout_expected.expect('test', 1){ |o| o.call }.call
end
- expect 'stub#call: unexpected arguments ([]≠[1])' do
+ expect 'stub#call: unexpected arguments: []≠[1]' do
stub.to.receive.call(1).to_lookout_expected.expect('test', 1){ |o| o.call }.call.exception.message
end
+
+ expect Lookout::Results::Error do
+ stub.to.receive.call(1).to_lookout_expected.expect('test', 1){ |o| o.call(1, 2) }.call
+ end
+
+ expect 'stub#call: unexpected arguments: [1, 2]≠[1]' do
+ stub.to.receive.call(1).to_lookout_expected.expect('test', 1){ |o| o.call(1, 2) }.call.exception.message
+ end
end
@@ -1,80 +1,23 @@
# -*- coding: utf-8 -*-
Expectations do
- expect true do
- Lookout::Reception::Arguments.new.verify
+ expect Lookout::Reception::Arguments::Any do
+ Lookout::Reception::Arguments.for
end
- expect true do
- Lookout::Reception::Arguments.new.verify(1)
+ expect Lookout::Reception::Arguments::Any do
+ Lookout::Reception::Arguments.for(1, Lookout::Reception::Arguments::Any.new)
end
- expect true do
- Lookout::Reception::Arguments.new.verify(1, 2)
+ expect Lookout::Reception::Arguments::None do
+ Lookout::Reception::Arguments.for(Lookout::Reception::Arguments::None.new)
end
- expect Lookout::Reception::Arguments::Error.new('unexpected arguments ([]≠[Object])') do
- Lookout::Reception::Arguments.new(Object).verify
+ expect Lookout::Reception::Arguments::None do
+ Lookout::Reception::Arguments.for(1, Lookout::Reception::Arguments::None.new)
end
- expect true do
- Lookout::Reception::Arguments.new(Object).verify(1)
- end
-
- expect Lookout::Reception::Arguments::Error.new('unexpected arguments ([1, 2]≠[Object])') do
- Lookout::Reception::Arguments.new(Object).verify(1, 2)
- end
-
- expect Lookout::Reception::Arguments::Error.new('unexpected arguments ([1]≠[Object, Object])') do
- Lookout::Reception::Arguments.new(Object, Object).verify(1)
- end
-
- expect Lookout::Reception::Arguments::Error.
- new('unexpected arguments ([(cannot inspect argument: error)]≠[Object, Object])') do
- Lookout::Reception::Arguments.new(Object, Object).verify(stub(:inspect => proc{ raise 'error' }))
- end
-
- expect true do
- Lookout::Reception::Arguments.new(Object, Object).verify(1, 2)
- end
-
- expect true do
- Lookout::Reception::Arguments.new(1, 2, 3).verify(1, 2, 3)
- end
-
- expect Lookout::Reception::Arguments::Error.new('unexpected arguments ([4, 5, 6]≠[1, 2, 3])') do
- Lookout::Reception::Arguments.new(1, 2, 3).verify(4, 5, 6)
- end
-
- expect true do
- Lookout::Reception::Arguments.new(/bcd/).verify('abcde')
- end
-
- expect true do
- Lookout::Reception::Arguments.new(Lookout::Reception::Arguments::None.new).verify
- end
-
- expect Lookout::Reception::Arguments::Error.new('unexpected arguments ([4]≠[])') do
- Lookout::Reception::Arguments.new(Lookout::Reception::Arguments::None.new).verify(4)
- end
-
- expect '' do
- Lookout::Reception::Arguments.new(Lookout::Reception::Arguments::None.new).to_s
- end
-
- expect '(1)' do
- Lookout::Reception::Arguments.new(1).to_s
- end
-
- expect [Lookout::Reception::Arguments::Any] do
- Lookout::Reception::Arguments.new.to_a
- end
-
- expect [1, 2, 3] do
- Lookout::Reception::Arguments.new(1, 2, 3).to_a
- end
-
- expect [Lookout::Reception::Arguments::None] do
- Lookout::Reception::Arguments.new(Lookout::Reception::Arguments::None.new).to_a
+ expect Lookout::Reception::Arguments::List.new(1, 2, 3) do
+ Lookout::Reception::Arguments.for(1, 2, 3)
end
end
@@ -12,8 +12,4 @@
expect true do
Lookout::Reception::Arguments::Any.new =~ [1, 2, 3]
end
-
- expect [Lookout::Reception::Arguments::Any] do
- Lookout::Reception::Arguments::Any.new.to_a
- end
end
@@ -16,8 +16,4 @@
expect '1, (cannot inspect argument: error), 3' do
Lookout::Reception::Arguments::List.new(1, stub(:inspect => proc{ raise 'error' } ), 3).to_s
end
-
- expect [1, 2, 3] do
- Lookout::Reception::Arguments::List.new(1, 2, 3).to_a
- end
end
@@ -12,8 +12,4 @@
expect '' do
Lookout::Reception::Arguments::None.new.to_s
end
-
- expect [Lookout::Reception::Arguments::None] do
- Lookout::Reception::Arguments::None.new.to_a
- end
end

0 comments on commit bb4850c

Please sign in to comment.