~ubuntu-branches/ubuntu/trusty/libmocha-ruby/trusty

« back to all changes in this revision

Viewing changes to lib/mocha/object.rb

  • Committer: Bazaar Package Importer
  • Author(s): Antonio Terceiro
  • Date: 2009-02-15 21:06:03 UTC
  • mfrom: (1.1.3 upstream) (2.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090215210603-vmhi06dceefkxala
Tags: 0.9.5-1
* New upstream release.
* debian/rules:
  - clarified version of GPL
  - added proper copyrigh signs (©)
* debian/control:
  - Added ${misc:Depends} to libmocha-ruby

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
require 'mocha/class_method'
4
4
require 'mocha/module_method'
5
5
require 'mocha/any_instance_method'
6
 
 
7
 
# Methods added all objects to allow mocking and stubbing on real objects.
8
 
#
9
 
# Methods return a Mocha::Expectation which can be further modified by methods on Mocha::Expectation.
10
 
class Object
11
 
  
12
 
  def mocha # :nodoc:
13
 
    @mocha ||= Mocha::Mockery.instance.mock_impersonating(self)
14
 
  end
15
 
  
16
 
  def reset_mocha # :nodoc:
17
 
    @mocha = nil
18
 
  end
19
 
  
20
 
  def stubba_method # :nodoc:
21
 
    Mocha::InstanceMethod
22
 
  end
23
 
  
24
 
  def stubba_object # :nodoc:
25
 
    self
26
 
  end
27
 
  
28
 
  # :call-seq: expects(symbol) -> expectation
29
 
  #
30
 
  # Adds an expectation that a method identified by +symbol+ must be called exactly once with any parameters.
31
 
  # Returns the new expectation which can be further modified by methods on Mocha::Expectation.
32
 
  #   product = Product.new
33
 
  #   product.expects(:save).returns(true)
34
 
  #   assert_equal false, product.save
35
 
  #
36
 
  # The original implementation of <tt>Product#save</tt> is replaced temporarily.
37
 
  #
38
 
  # The original implementation of <tt>Product#save</tt> is restored at the end of the test.
39
 
  def expects(symbol)
40
 
    mockery = Mocha::Mockery.instance
41
 
    mockery.on_stubbing(self, symbol)
42
 
    method = stubba_method.new(stubba_object, symbol)
43
 
    mockery.stubba.stub(method)
44
 
    mocha.expects(symbol, caller)
45
 
  end
46
 
  
47
 
  # :call-seq: stubs(symbol) -> expectation
48
 
  #
49
 
  # Adds an expectation that a method identified by +symbol+ may be called any number of times with any parameters.
50
 
  # Returns the new expectation which can be further modified by methods on Mocha::Expectation.
51
 
  #   product = Product.new
52
 
  #   product.stubs(:save).returns(true)
53
 
  #   assert_equal false, product.save
54
 
  #
55
 
  # The original implementation of <tt>Product#save</tt> is replaced temporarily.
56
 
  #
57
 
  # The original implementation of <tt>Product#save</tt> is restored at the end of the test.
58
 
  def stubs(symbol)
59
 
    mockery = Mocha::Mockery.instance
60
 
    mockery.on_stubbing(self, symbol)
61
 
    method = stubba_method.new(stubba_object, symbol)
62
 
    mockery.stubba.stub(method)
63
 
    mocha.stubs(symbol, caller)
64
 
  end
65
 
  
66
 
  def method_exists?(symbol, include_public_methods = true)
67
 
    existing_methods = private_methods(include_superclass_methods = true) + protected_methods(include_superclass_methods = true)
68
 
    existing_methods += public_methods(include_superclass_methods = true) if include_public_methods
69
 
    existing_methods.any? { |m| m.to_s == symbol.to_s } || (respond_to?(symbol) && include_public_methods)
70
 
  end
71
 
  
 
6
require 'mocha/argument_iterator'
 
7
 
 
8
module Mocha
 
9
  
 
10
  # Methods added all objects to allow mocking and stubbing on real objects.
 
11
  #
 
12
  # Methods return a Mocha::Expectation which can be further modified by methods on Mocha::Expectation.
 
13
  module ObjectMethods
 
14
  
 
15
    def mocha # :nodoc:
 
16
      @mocha ||= Mocha::Mockery.instance.mock_impersonating(self)
 
17
    end
 
18
  
 
19
    def reset_mocha # :nodoc:
 
20
      @mocha = nil
 
21
    end
 
22
  
 
23
    def stubba_method # :nodoc:
 
24
      Mocha::InstanceMethod
 
25
    end
 
26
  
 
27
    def stubba_object # :nodoc:
 
28
      self
 
29
    end
 
30
  
 
31
    # :call-seq: expects(method_name) -> expectation
 
32
    #            expects(method_names_vs_return_values) -> last expectation
 
33
    #
 
34
    # Adds an expectation that a method identified by +method_name+ Symbol must be called exactly once with any parameters.
 
35
    # Returns the new expectation which can be further modified by methods on Mocha::Expectation.
 
36
    #   product = Product.new
 
37
    #   product.expects(:save).returns(true)
 
38
    #   assert_equal true, product.save
 
39
    #
 
40
    # The original implementation of <tt>Product#save</tt> is replaced temporarily.
 
41
    #
 
42
    # The original implementation of <tt>Product#save</tt> is restored at the end of the test.
 
43
    #
 
44
    # If +method_names_vs_return_values+ is a +Hash+, an expectation will be set up for each entry using the key as +method_name+ and value as +return_value+.
 
45
    #   product = Product.new
 
46
    #   product.expects(:valid? => true, :save => true)
 
47
    #
 
48
    #   # exactly equivalent to
 
49
    #
 
50
    #   product = Product.new
 
51
    #   product.expects(:valid?).returns(true)
 
52
    #   product.expects(:save).returns(true)
 
53
    def expects(method_name_or_hash)
 
54
      expectation = nil
 
55
      mockery = Mocha::Mockery.instance
 
56
      iterator = ArgumentIterator.new(method_name_or_hash)
 
57
      iterator.each { |*args|
 
58
        method_name = args.shift
 
59
        mockery.on_stubbing(self, method_name)
 
60
        method = stubba_method.new(stubba_object, method_name)
 
61
        mockery.stubba.stub(method)
 
62
        expectation = mocha.expects(method_name, caller)
 
63
        expectation.returns(args.shift) if args.length > 0
 
64
      }
 
65
      expectation
 
66
    end
 
67
  
 
68
    # :call-seq: stubs(method_name) -> expectation
 
69
    #            stubs(method_names_vs_return_values) -> last expectation
 
70
    #
 
71
    # Adds an expectation that a method identified by +method_name+ Symbol may be called any number of times with any parameters.
 
72
    # Returns the new expectation which can be further modified by methods on Mocha::Expectation.
 
73
    #   product = Product.new
 
74
    #   product.stubs(:save).returns(true)
 
75
    #   assert_equal true, product.save
 
76
    #
 
77
    # The original implementation of <tt>Product#save</tt> is replaced temporarily.
 
78
    #
 
79
    # The original implementation of <tt>Product#save</tt> is restored at the end of the test.
 
80
    #
 
81
    # If +method_names_vs_return_values+ is a +Hash+, an expectation will be set up for each entry using the key as +method_name+ and value as +return_value+.
 
82
    #   product = Product.new
 
83
    #   product.stubs(:valid? => true, :save => true)
 
84
    #
 
85
    #   # exactly equivalent to
 
86
    #
 
87
    #   product = Product.new
 
88
    #   product.stubs(:valid?).returns(true)
 
89
    #   product.stubs(:save).returns(true)
 
90
    def stubs(method_name_or_hash)
 
91
      expectation = nil
 
92
      mockery = Mocha::Mockery.instance
 
93
      iterator = ArgumentIterator.new(method_name_or_hash)
 
94
      iterator.each { |*args|
 
95
        method_name = args.shift
 
96
        mockery.on_stubbing(self, method_name)
 
97
        method = stubba_method.new(stubba_object, method_name)
 
98
        mockery.stubba.stub(method)
 
99
        expectation = mocha.stubs(method_name, caller)
 
100
        expectation.returns(args.shift) if args.length > 0
 
101
      }
 
102
      expectation
 
103
    end
 
104
  
 
105
    def method_exists?(method, include_public_methods = true) # :nodoc:
 
106
      if include_public_methods
 
107
        return true if public_methods(include_superclass_methods = true).include?(method)
 
108
        return true if respond_to?(method.to_sym)
 
109
      end
 
110
      return true if protected_methods(include_superclass_methods = true).include?(method)
 
111
      return true if private_methods(include_superclass_methods = true).include?(method)
 
112
      return false
 
113
    end
 
114
  
 
115
  end
 
116
  
 
117
  module ModuleMethods # :nodoc:
 
118
    
 
119
    def stubba_method
 
120
      Mocha::ModuleMethod
 
121
    end
 
122
    
 
123
  end
 
124
  
 
125
  # Methods added all classes to allow mocking and stubbing on real objects.
 
126
  module ClassMethods
 
127
    
 
128
    def stubba_method # :nodoc:
 
129
      Mocha::ClassMethod
 
130
    end
 
131
 
 
132
    class AnyInstance # :nodoc:
 
133
    
 
134
      def initialize(klass)
 
135
        @stubba_object = klass
 
136
      end
 
137
    
 
138
      def mocha
 
139
        @mocha ||= Mocha::Mockery.instance.mock_impersonating_any_instance_of(@stubba_object)
 
140
      end
 
141
 
 
142
      def stubba_method
 
143
        Mocha::AnyInstanceMethod
 
144
      end
 
145
    
 
146
      def stubba_object
 
147
        @stubba_object
 
148
      end
 
149
    
 
150
      def method_exists?(method, include_public_methods = true)
 
151
        if include_public_methods
 
152
          return true if @stubba_object.public_instance_methods(include_superclass_methods = true).include?(method)
 
153
        end
 
154
        return true if @stubba_object.protected_instance_methods(include_superclass_methods = true).include?(method)
 
155
        return true if @stubba_object.private_instance_methods(include_superclass_methods = true).include?(method)
 
156
        return false
 
157
      end
 
158
    
 
159
    end
 
160
  
 
161
    # :call-seq: any_instance -> mock object
 
162
    #
 
163
    # Returns a mock object which will detect calls to any instance of this class.
 
164
    #   Product.any_instance.stubs(:save).returns(false)
 
165
    #   product_1 = Product.new
 
166
    #   assert_equal false, product_1.save
 
167
    #   product_2 = Product.new
 
168
    #   assert_equal false, product_2.save
 
169
    def any_instance
 
170
      @any_instance ||= AnyInstance.new(self)
 
171
    end
 
172
  
 
173
  end
 
174
  
 
175
end
 
176
 
 
177
class Object # :nodoc:
 
178
  include Mocha::ObjectMethods
72
179
end
73
180
 
74
181
class Module # :nodoc:
75
 
  
76
 
  def stubba_method
77
 
    Mocha::ModuleMethod
78
 
  end
79
 
    
80
 
end
81
 
  
82
 
class Class
83
 
  
84
 
  def stubba_method # :nodoc:
85
 
    Mocha::ClassMethod
86
 
  end
87
 
 
88
 
  class AnyInstance # :nodoc:
89
 
    
90
 
    def initialize(klass)
91
 
      @stubba_object = klass
92
 
    end
93
 
    
94
 
    def mocha
95
 
      @mocha ||= Mocha::Mockery.instance.mock_impersonating_any_instance_of(@stubba_object)
96
 
    end
97
 
 
98
 
    def stubba_method
99
 
      Mocha::AnyInstanceMethod
100
 
    end
101
 
    
102
 
    def stubba_object
103
 
      @stubba_object
104
 
    end
105
 
    
106
 
    def method_exists?(symbol, include_public_methods = true)
107
 
      existing_methods = @stubba_object.private_instance_methods(include_superclass_methods = true) + @stubba_object.protected_instance_methods(include_superclass_methods = true)
108
 
      existing_methods += @stubba_object.public_instance_methods(include_superclass_methods = true) if include_public_methods
109
 
      existing_methods.any? { |m| m.to_s == symbol.to_s }
110
 
    end
111
 
    
112
 
  end
113
 
  
114
 
  # :call-seq: any_instance -> mock object
115
 
  #
116
 
  # Returns a mock object which will detect calls to any instance of this class.
117
 
  #   Product.any_instance.stubs(:save).returns(false)
118
 
  #   product_1 = Product.new
119
 
  #   assert_equal false, product_1.save
120
 
  #   product_2 = Product.new
121
 
  #   assert_equal false, product_2.save
122
 
  def any_instance
123
 
    @any_instance ||= AnyInstance.new(self)
124
 
  end
125
 
  
126
 
end
127
 
 
 
182
  include Mocha::ModuleMethods
 
183
end
 
184
 
 
185
class Class # :nodoc:
 
186
  include Mocha::ClassMethods
 
187
end