adding all of botlist, initial add
[botlist.git] / openbotlist / WEB-INF / lib / ruby / rspec / lib / spec / mocks / error_generator.rb
blob01d8f720d564aeb0cbb676413a078ca2bbb4f7ec
1 module Spec
2   module Mocks
3     class ErrorGenerator
4       attr_writer :opts
5       
6       def initialize(target, name)
7         @target = target
8         @name = name
9       end
10       
11       def opts
12         @opts ||= {}
13       end
15       def raise_unexpected_message_error(sym, *args)
16         __raise "#{intro} received unexpected message :#{sym}#{arg_message(*args)}"
17       end
18       
19       def raise_unexpected_message_args_error(expectation, *args)
20         expected_args = format_args(*expectation.expected_args)
21         actual_args = args.empty? ? "(no args)" : format_args(*args)
22         __raise "#{intro} expected #{expectation.sym.inspect} with #{expected_args} but received it with #{actual_args}"
23       end
24       
25       def raise_expectation_error(sym, expected_received_count, actual_received_count, *args)
26         __raise "#{intro} expected :#{sym}#{arg_message(*args)} #{count_message(expected_received_count)}, but received it #{count_message(actual_received_count)}"
27       end
28       
29       def raise_out_of_order_error(sym)
30         __raise "#{intro} received :#{sym} out of order"
31       end
32       
33       def raise_block_failed_error(sym, detail)
34         __raise "#{intro} received :#{sym} but passed block failed with: #{detail}"
35       end
36       
37       def raise_missing_block_error(args_to_yield)
38         __raise "#{intro} asked to yield |#{arg_list(*args_to_yield)}| but no block was passed"
39       end
40       
41       def raise_wrong_arity_error(args_to_yield, arity)
42         __raise "#{intro} yielded |#{arg_list(*args_to_yield)}| to block with arity of #{arity}"
43       end
44       
45       private
46       def intro
47         @name ? "Mock '#{@name}'" : @target.inspect
48       end
49       
50       def __raise(message)
51         message = opts[:message] unless opts[:message].nil?
52         Kernel::raise(Spec::Mocks::MockExpectationError, message)
53       end
54       
55       def arg_message(*args)
56         " with " + format_args(*args)
57       end
58       
59       def format_args(*args)
60         return "(no args)" if args.empty? || args == [:no_args]
61         return "(any args)" if args == [:any_args]
62         "(" + arg_list(*args) + ")"
63       end
65       def arg_list(*args)
66         args.collect do |arg|
67           arg.respond_to?(:description) ? arg.description : arg.inspect
68         end.join(", ")
69       end
70       
71       def count_message(count)
72         return "at least #{pretty_print(count.abs)}" if count < 0
73         return pretty_print(count)
74       end
76       def pretty_print(count)
77         return "once" if count == 1
78         return "twice" if count == 2
79         return "#{count} times"
80       end
82     end
83   end
84 end