PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/gems/mocha-0.9.3/lib/mocha/standalone.rb

http://github.com/stevenbristol/lovd-by-less
Ruby | 166 lines | 45 code | 13 blank | 108 comment | 6 complexity | 9cc5390c10de96ec393cf6c42c668b5a MD5 | raw file
Possible License(s): MIT
  1. require 'mocha/parameter_matchers'
  2. require 'mocha/mockery'
  3. require 'mocha/sequence'
  4. module Mocha # :nodoc:
  5. # Methods added to Test::Unit::TestCase or equivalent.
  6. module Standalone
  7. include ParameterMatchers
  8. # :call-seq: mock(name, &block) -> mock object
  9. # mock(expected_methods = {}, &block) -> mock object
  10. # mock(name, expected_methods = {}, &block) -> mock object
  11. #
  12. # Creates a mock object.
  13. #
  14. # +name+ is a +String+ identifier for the mock object.
  15. #
  16. # +expected_methods+ is a +Hash+ with expected method name symbols as keys and corresponding return values as values.
  17. #
  18. # Note that (contrary to expectations set up by #stub) these expectations <b>must</b> be fulfilled during the test.
  19. # def test_product
  20. # product = mock('ipod_product', :manufacturer => 'ipod', :price => 100)
  21. # assert_equal 'ipod', product.manufacturer
  22. # assert_equal 100, product.price
  23. # # an error will be raised unless both Product#manufacturer and Product#price have been called
  24. # end
  25. #
  26. # +block+ is an optional block to be evaluated against the mock object instance, giving an alernative way to set up expectations & stubs.
  27. # def test_product
  28. # product = mock('ipod_product') do
  29. # expects(:manufacturer).returns('ipod')
  30. # expects(:price).returns(100)
  31. # end
  32. # assert_equal 'ipod', product.manufacturer
  33. # assert_equal 100, product.price
  34. # # an error will be raised unless both Product#manufacturer and Product#price have been called
  35. # end
  36. def mock(*arguments, &block)
  37. name = arguments.shift if arguments.first.is_a?(String)
  38. expectations = arguments.shift || {}
  39. mock = name ? Mockery.instance.named_mock(name, &block) : Mockery.instance.unnamed_mock(&block)
  40. mock.expects(expectations)
  41. mock
  42. end
  43. # :call-seq: stub(name, &block) -> mock object
  44. # stub(stubbed_methods = {}, &block) -> mock object
  45. # stub(name, stubbed_methods = {}, &block) -> mock object
  46. #
  47. # Creates a mock object.
  48. #
  49. # +name+ is a +String+ identifier for the mock object.
  50. #
  51. # +stubbed_methods+ is a +Hash+ with stubbed method name symbols as keys and corresponding return values as values.
  52. # Note that (contrary to expectations set up by #mock) these expectations <b>need not</b> be fulfilled during the test.
  53. # def test_product
  54. # product = stub('ipod_product', :manufacturer => 'ipod', :price => 100)
  55. # assert_equal 'ipod', product.manufacturer
  56. # assert_equal 100, product.price
  57. # # an error will not be raised even if Product#manufacturer and Product#price have not been called
  58. # end
  59. #
  60. # +block+ is an optional block to be evaluated against the mock object instance, giving an alernative way to set up expectations & stubs.
  61. # def test_product
  62. # product = stub('ipod_product') do
  63. # stubs(:manufacturer).returns('ipod')
  64. # stubs(:price).returns(100)
  65. # end
  66. # assert_equal 'ipod', product.manufacturer
  67. # assert_equal 100, product.price
  68. # # an error will not be raised even if Product#manufacturer and Product#price have not been called
  69. # end
  70. def stub(*arguments, &block)
  71. name = arguments.shift if arguments.first.is_a?(String)
  72. expectations = arguments.shift || {}
  73. stub = name ? Mockery.instance.named_mock(name, &block) : Mockery.instance.unnamed_mock(&block)
  74. stub.stubs(expectations)
  75. stub
  76. end
  77. # :call-seq: stub_everything(name, &block) -> mock object
  78. # stub_everything(stubbed_methods = {}, &block) -> mock object
  79. # stub_everything(name, stubbed_methods = {}, &block) -> mock object
  80. #
  81. # Creates a mock object that accepts calls to any method.
  82. #
  83. # By default it will return +nil+ for any method call.
  84. #
  85. # +block+ is a block to be evaluated against the mock object instance, giving an alernative way to set up expectations & stubs.
  86. #
  87. # +name+ and +stubbed_methods+ work in the same way as for #stub.
  88. # def test_product
  89. # product = stub_everything('ipod_product', :price => 100)
  90. # assert_nil product.manufacturer
  91. # assert_nil product.any_old_method
  92. # assert_equal 100, product.price
  93. # end
  94. def stub_everything(*arguments, &block)
  95. name = arguments.shift if arguments.first.is_a?(String)
  96. expectations = arguments.shift || {}
  97. stub = name ? Mockery.instance.named_mock(name, &block) : Mockery.instance.unnamed_mock(&block)
  98. stub.stub_everything
  99. stub.stubs(expectations)
  100. stub
  101. end
  102. # :call-seq: sequence(name) -> sequence
  103. #
  104. # Returns a new sequence that is used to constrain the order in which expectations can occur.
  105. #
  106. # Specify that an expected invocation must occur in within a named +sequence+ by using Expectation#in_sequence.
  107. #
  108. # See also Expectation#in_sequence.
  109. # breakfast = sequence('breakfast')
  110. #
  111. # egg = mock('egg')
  112. # egg.expects(:crack).in_sequence(breakfast)
  113. # egg.expects(:fry).in_sequence(breakfast)
  114. # egg.expects(:eat).in_sequence(breakfast)
  115. def sequence(name)
  116. Sequence.new(name)
  117. end
  118. # :call-seq: states(name) -> state_machine
  119. #
  120. # Returns a new +state_machine+ that is used to constrain the order in which expectations can occur.
  121. #
  122. # Specify the initial +state+ of the +state_machine+ by using StateMachine#starts_as.
  123. #
  124. # Specify that an expected invocation should change the +state+ of the +state_machine+ by using Expectation#then.
  125. #
  126. # Specify that an expected invocation should be constrained to occur within a particular +state+ by using Expectation#when.
  127. #
  128. # A test can contain multiple +state_machines+.
  129. #
  130. # See also Expectation#then, Expectation#when and StateMachine.
  131. # power = states('power').starts_as('off')
  132. #
  133. # radio = mock('radio')
  134. # radio.expects(:switch_on).then(power.is('on'))
  135. # radio.expects(:select_channel).with('BBC Radio 4').when(power.is('on'))
  136. # radio.expects(:adjust_volume).with(+5).when(power.is('on'))
  137. # radio.expects(:select_channel).with('BBC World Service').when(power.is('on'))
  138. # radio.expects(:adjust_volume).with(-5).when(power.is('on'))
  139. # radio.expects(:switch_off).then(power.is('off'))
  140. def states(name)
  141. Mockery.instance.new_state_machine(name)
  142. end
  143. def mocha_setup # :nodoc:
  144. end
  145. def mocha_verify(assertion_counter = nil) # :nodoc:
  146. Mockery.instance.verify(assertion_counter)
  147. end
  148. def mocha_teardown # :nodoc:
  149. Mockery.instance.teardown
  150. Mockery.reset_instance
  151. end
  152. end
  153. end