matahari 0.3 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,73 +1,75 @@
1
- class InvocationMatcher
1
+ module Matahari
2
+ class InvocationMatcher
2
3
 
3
- #TODO I can't work out how to disentangle the 2 responsibilities of this class:
4
- #1. Inspecting and acting on spy invocations
5
- #2. Presenting the results of those inspections.
6
- #
7
- #One to revisit later when my head is less befuddled.
4
+ #TODO I can't work out how to disentangle the 2 responsibilities of this class:
5
+ #1. Inspecting and acting on spy invocations
6
+ #2. Presenting the results of those inspections.
7
+ #
8
+ #One to revisit later when my head is less befuddled.
8
9
 
9
- def initialize(iterator = nil)
10
- @expected_call_count = iterator ? iterator.count : nil
11
- end
10
+ def initialize(iterator = nil)
11
+ @expected_call_count = iterator ? iterator.count : nil
12
+ end
12
13
 
13
- def matches?(subject)
14
- @subject = subject
15
- @invocations_of_method = subject.invocations.select {|i| i[:method] == @call_to_verify}
16
- verifying_args = @args_to_verify.size != 0
14
+ def matches?(subject)
15
+ @subject = subject
16
+ @invocations_of_method = subject.invocations.select {|i| i[:method] == @call_to_verify}
17
+ verifying_args = @args_to_verify.size != 0
17
18
 
18
- match_passes?(verifying_args)
19
- end
19
+ match_passes?(verifying_args)
20
+ end
20
21
 
21
- def failure_message_for_should_not
22
- "Spy(:#{@subject.name}) expected not to receive :#{@call_to_verify} but received it #{prettify_times(@matching_calls)}"
23
- end
22
+ def failure_message_for_should_not
23
+ "Spy(:#{@subject.name}) expected not to receive :#{@call_to_verify} but received it #{prettify_times(@matching_calls)}"
24
+ end
24
25
 
25
- def failure_message_for_should
26
- if @args_to_verify.size > 0
27
- "Spy(:#{@subject.name}) expected to receive :#{@call_to_verify}(#{@args_to_verify.map(&:inspect).join(", ")}) #{prettify_times(@expected_call_count)}, received #{prettify_times(@matching_calls)}"
28
- else
29
- "Spy(:#{@subject.name}) expected to receive :#{@call_to_verify} #{prettify_times(@expected_call_count)}, received #{prettify_times(@matching_calls)}"
26
+ def failure_message_for_should
27
+ if @args_to_verify.size > 0
28
+ "Spy(:#{@subject.name}) expected to receive :#{@call_to_verify}(#{@args_to_verify.map(&:inspect).join(", ")}) #{prettify_times(@expected_call_count)}, received #{prettify_times(@matching_calls)}"
29
+ else
30
+ "Spy(:#{@subject.name}) expected to receive :#{@call_to_verify} #{prettify_times(@expected_call_count)}, received #{prettify_times(@matching_calls)}"
31
+ end
30
32
  end
31
- end
32
33
 
33
- #Allows chaining of method calls following has_received?/should have_received,
34
- #e.g. spy.should_have received.some_method, where #some_method is handled by
35
- #method_missing, its arguments and name being stored until #matches? is called.
36
- def method_missing(sym, *args, &block)
37
- @call_to_verify = sym
38
- @args_to_verify = args
39
- self
40
- end
34
+ #Allows chaining of method calls following has_received?/should have_received,
35
+ #e.g. spy.should_have received.some_method, where #some_method is handled by
36
+ #method_missing, its arguments and name being stored until #matches? is called.
37
+ def method_missing(sym, *args, &block)
38
+ @call_to_verify = sym
39
+ @args_to_verify = args
40
+ self
41
+ end
41
42
 
42
- def prettify_times(times)
43
- case times
44
- when 1
45
- "once"
46
- when nil
47
- "once"
48
- when 2
49
- "twice"
50
- else
51
- "#{times} times"
43
+ def prettify_times(times)
44
+ case times
45
+ when 1
46
+ "once"
47
+ when nil
48
+ "once"
49
+ when 2
50
+ "twice"
51
+ else
52
+ "#{times} times"
53
+ end
52
54
  end
53
- end
54
- private :prettify_times
55
+ private :prettify_times
55
56
 
56
- def matching_calls(verifying_args=true)
57
- @matching_calls ||= if verifying_args
58
- @invocations_of_method.select {|i| i[:args].flatten === @args_to_verify}.size
59
- else
60
- @invocations_of_method.size
61
- end
62
- end
63
- private :matching_calls
57
+ def matching_calls(verifying_args=true)
58
+ @matching_calls ||= if verifying_args
59
+ @invocations_of_method.select {|i| i[:args].flatten === @args_to_verify}.size
60
+ else
61
+ @invocations_of_method.size
62
+ end
63
+ end
64
+ private :matching_calls
64
65
 
65
- def match_passes?(verifying_args)
66
- if @expected_call_count
67
- matching_calls(verifying_args) == @expected_call_count
68
- else
69
- matching_calls(verifying_args) > 0
66
+ def match_passes?(verifying_args)
67
+ if @expected_call_count
68
+ matching_calls(verifying_args) == @expected_call_count
69
+ else
70
+ matching_calls(verifying_args) > 0
71
+ end
70
72
  end
73
+ private :match_passes?
71
74
  end
72
- private :match_passes?
73
75
  end
data/lib/matahari/spy.rb CHANGED
@@ -1,45 +1,47 @@
1
- class Spy
2
- attr_reader :name, :invocations
1
+ module Matahari
2
+ class Spy
3
+ attr_reader :name, :invocations
3
4
 
4
- def initialize(name = nil, opts={})
5
- @subject = opts[:subject]
6
- @name = name
7
- @invocations = []
8
- @stubbed_calls = {}
9
- class << self
10
- instance_methods.each do |meth|
11
- whitelist = [:name, :define_method, :stubs, :passes_on, :method_missing, :record_invocation, :invocations, :has_received?, :object_id, :respond_to?, :respond_to_missing?, :instance_eval, :instance_exec, :class_eval, :__send__, :send, :should, :should_not, :__id__, :__send__]
12
- next if whitelist.include?(meth) || whitelist.include?(meth.to_sym)
13
- undef_method(meth)
5
+ def initialize(name = nil, opts={})
6
+ @subject = opts[:subject]
7
+ @name = name
8
+ @invocations = []
9
+ @stubbed_calls = {}
10
+ class << self
11
+ instance_methods.each do |meth|
12
+ whitelist = [:name, :define_method, :stubs, :passes_on, :method_missing, :record_invocation, :invocations, :has_received?, :object_id, :respond_to?, :respond_to_missing?, :instance_eval, :instance_exec, :class_eval, :__send__, :send, :should, :should_not, :__id__, :__send__]
13
+ next if whitelist.include?(meth) || whitelist.include?(meth.to_sym)
14
+ undef_method(meth)
15
+ end
14
16
  end
15
17
  end
16
- end
17
18
 
18
- #When a given method call, sym, is invoked on self, call block and return its result
19
- def stubs(sym, &block)
20
- @stubbed_calls[sym] = block
21
- end
19
+ #When a given method call, sym, is invoked on self, call block and return its result
20
+ def stubs(sym, &block)
21
+ @stubbed_calls[sym] = block
22
+ end
22
23
 
23
- def passes_on(sym)
24
- @stubbed_calls[sym] = @subject.method(sym)
25
- end
24
+ def passes_on(sym)
25
+ @stubbed_calls[sym] = @subject.method(sym)
26
+ end
26
27
 
27
- #Captures the details of any method call and store for later inspection
28
- def method_missing(sym, *args, &block)
29
- record_invocation(sym, args)
30
- @stubbed_calls[sym].call if @stubbed_calls[sym]
31
- end
28
+ #Captures the details of any method call and store for later inspection
29
+ def method_missing(sym, *args, &block)
30
+ record_invocation(sym, args)
31
+ @stubbed_calls[sym].call if @stubbed_calls[sym]
32
+ end
32
33
 
33
- #Pass an iterator to this method to specify the number of times the method should
34
- #have been called. E.g. spy.has_received?(3.times). While other iterators might work,
35
- #the idea is to allow this nice DSL-ish way of asserting on the number of calls, hence
36
- #the odd method signature.
37
- def has_received?(times=nil)
38
- InvocationMatcher.new(times)
39
- end
34
+ #Pass an iterator to this method to specify the number of times the method should
35
+ #have been called. E.g. spy.has_received?(3.times). While other iterators might work,
36
+ #the idea is to allow this nice DSL-ish way of asserting on the number of calls, hence
37
+ #the odd method signature.
38
+ def has_received?(times=nil)
39
+ InvocationMatcher.new(times)
40
+ end
40
41
 
41
- private
42
- def record_invocation(sym, *args)
43
- @invocations << {:method => sym, :args => args}
42
+ private
43
+ def record_invocation(sym, *args)
44
+ @invocations << {:method => sym, :args => args}
45
+ end
44
46
  end
45
47
  end
@@ -1,3 +1,3 @@
1
1
  module Matahari
2
- VERSION = "0.3"
2
+ VERSION = "0.3.1"
3
3
  end
metadata CHANGED
@@ -5,7 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- version: "0.3"
8
+ - 1
9
+ version: 0.3.1
9
10
  platform: ruby
10
11
  authors:
11
12
  - Tom Stuart
@@ -13,7 +14,7 @@ autorequire:
13
14
  bindir: bin
14
15
  cert_chain: []
15
16
 
16
- date: 2011-06-28 00:00:00 +01:00
17
+ date: 2011-06-29 00:00:00 +01:00
17
18
  default_executable:
18
19
  dependencies:
19
20
  - !ruby/object:Gem::Dependency