facon 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,12 @@
1
- === 0.1 / 2008-02-09
1
+ === 0.3 / 2008-02-18
2
2
 
3
- * First release!
3
+ * Fixed a bug with proxied methods not being cleared.
4
+ * Fixed a bug where the same mocks were repeatedly being added to $facon_mocks.
4
5
 
5
6
  === 0.2 / 2008-02-13
6
7
 
7
8
  * Use #after block to verify mocks so that the stack trace is more helpful (it now actually shows the line in the spec where the mock error occurred).
9
+
10
+ === 0.1 / 2008-02-09
11
+
12
+ * First release!
@@ -12,5 +12,5 @@ require 'facon/core_ext/object'
12
12
  # Facon is a mocking library in the spirit of the Bacon spec library. Small,
13
13
  # compact, and works with Bacon.
14
14
  module Facon
15
- VERSION = '0.2'
15
+ VERSION = '0.3'
16
16
  end
@@ -45,13 +45,16 @@ module Facon
45
45
  $facon_mocks.each { |mock| mock.spec_verify }
46
46
  end
47
47
 
48
+ def teardown_facon_mocks
49
+ $facon_mocks.each { |mock| mock.spec_reset }
50
+ $facon_mocks.clear
51
+ end
52
+
48
53
  def it_with_mock_verification(description, &block)
49
- before do
50
- setup_facon_mocks
51
- end
52
- setup_facon_mocks
54
+ @before.unshift(lambda { setup_facon_mocks })
53
55
  after do
54
56
  verify_facon_mocks
57
+ teardown_facon_mocks
55
58
  end
56
59
  it_without_mock_verification(description, &block)
57
60
  end
@@ -10,12 +10,12 @@ module Facon
10
10
  @target, @name = target, name
11
11
  @expectations = []
12
12
  @stubs = []
13
+ @proxied_methods = []
13
14
  @error_generator = ErrorGenerator.new(target, name)
14
15
  end
15
16
 
16
17
  def add_stub(expected_from, method)
17
- $facon_mocks << (@target) unless $facon_mocks.nil?
18
- define_expected_method(method)
18
+ add_method(method)
19
19
 
20
20
  # A stub is really an expectation that can be called any number of times.
21
21
  @stubs.unshift(Expectation.new(@error_generator, @expectation_ordering, expected_from, method, nil, :any))
@@ -23,16 +23,14 @@ module Facon
23
23
  end
24
24
 
25
25
  def add_expectation(expected_from, method, &block)
26
- $facon_mocks << (@target) unless $facon_mocks.nil?
27
- define_expected_method(method)
26
+ add_method(method)
28
27
 
29
28
  @expectations << Expectation.new(@error_generator, @expectation_ordering, expected_from, method, (block_given? ? block : nil), 1)
30
29
  @expectations.last
31
30
  end
32
31
 
33
32
  def add_negative_expectation(expected_from, method, &block)
34
- $facon_mocks << (@target) unless $facon_mocks.nil?
35
- define_expected_method(method)
33
+ add_method(method)
36
34
 
37
35
  @expectations << NegativeExpectation.new(@error_generator, @expectation_ordering, expected_from, method, (block_given? ? block : nil))
38
36
  @expectations.last
@@ -59,12 +57,27 @@ module Facon
59
57
  def reset
60
58
  @expectations.clear
61
59
  @stubs.clear
60
+ reset_proxied_methods
61
+ @proxied_methods.clear
62
62
  end
63
63
 
64
64
  private
65
+ def add_method(method)
66
+ $facon_mocks << @target unless $facon_mocks.nil? || $facon_mocks.detect { |m| m.equal?(@target) }
67
+ define_expected_method(method)
68
+ end
69
+
65
70
  # Defines an expected method that simply calls the
66
71
  # <code>message_received</code> method.
67
72
  def define_expected_method(method)
73
+ if @target.respond_to?(method) && !metaclass.method_defined?(munge(method))
74
+ munged_method = munge(method)
75
+ metaclass.instance_eval do
76
+ alias_method munged_method, method if method_defined?(method.to_s)
77
+ end
78
+ @proxied_methods << method
79
+ end
80
+
68
81
  metaclass_eval(<<-EOF, __FILE__, __LINE__)
69
82
  def #{method}(*args, &block)
70
83
  mock_proxy.message_received(:#{method}, *args, &block)
@@ -72,6 +85,24 @@ module Facon
72
85
  EOF
73
86
  end
74
87
 
88
+ def munge(method)
89
+ "proxied_by_facon__#{method.to_s}".to_sym
90
+ end
91
+
92
+ def reset_proxied_methods
93
+ @proxied_methods.each do |method|
94
+ munged_method = munge(method)
95
+ metaclass.instance_eval do
96
+ if method_defined?(munged_method.to_s)
97
+ alias_method method, munged_method
98
+ undef_method munged_method
99
+ else
100
+ undef_method method
101
+ end
102
+ end
103
+ end
104
+ end
105
+
75
106
  def metaclass
76
107
  (class << @target; self; end)
77
108
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: facon
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.2"
4
+ version: "0.3"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cheah Chu Yeow
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-02-13 00:00:00 +08:00
12
+ date: 2008-02-18 00:00:00 +08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency