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

/External.LCA_RESTRICTED/Languages/Ruby/ruby19/lib/ruby/gems/1.9.1/gems/flexmock-0.8.7/test/test_samples.rb

http://github.com/IronLanguages/main
Ruby | 285 lines | 205 code | 51 blank | 29 comment | 1 complexity | 20a570aa8334d19ee346b16054b60c29 MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. #!/usr/bin/env ruby
  2. #---
  3. # Copyright 2003, 2004, 2005, 2006, 2007 by Jim Weirich (jim@weirichhouse.org).
  4. # All rights reserved.
  5. # Permission is granted for use, copying, modification, distribution,
  6. # and distribution of modified versions of this work as long as the
  7. # above copyright notice is included.
  8. #+++
  9. require 'test/unit'
  10. require 'flexmock'
  11. # Sample FlexMock Usage.
  12. class TestSamples < Test::Unit::TestCase
  13. include FlexMock::TestCase
  14. # This is a basic example where we setup a mock object to mimic an
  15. # IO object. We know that the +count_lines+ method uses gets, so we
  16. # tell the mock object to handle +gets+ by returning successive
  17. # elements of an array (just as the real +gets+ returns successive
  18. # elements of a file.
  19. def test_file_io
  20. mock_file = flexmock("file")
  21. mock_file.should_receive(:gets).and_return("line 1", "line 2", nil)
  22. assert_equal 2, count_lines(mock_file)
  23. end
  24. # Count the number of lines in a file. Used in the test_file_io
  25. # test.
  26. def count_lines(file)
  27. n = 0
  28. while file.gets
  29. n += 1
  30. end
  31. n
  32. end
  33. end
  34. class TestUndefined < Test::Unit::TestCase
  35. include FlexMock::TestCase
  36. def test_undefined_values
  37. m = flexmock("mock")
  38. m.should_receive(:divide_by).with(0).
  39. and_return_undefined
  40. assert_equal FlexMock.undefined, m.divide_by(0)
  41. end
  42. end
  43. class TestSimple < Test::Unit::TestCase
  44. include FlexMock::TestCase
  45. def test_simple_mock
  46. m = flexmock(:pi => 3.1416, :e => 2.71)
  47. assert_equal 3.1416, m.pi
  48. assert_equal 2.71, m.e
  49. end
  50. end
  51. class TestDog < Test::Unit::TestCase
  52. include FlexMock::TestCase
  53. def test_dog_wags
  54. tail_mock = flexmock(:wag => :happy)
  55. assert_equal :happy, tail_mock.wag
  56. end
  57. end
  58. class Woofer
  59. end
  60. class Dog
  61. def initialize
  62. @woofer = Woofer.new
  63. end
  64. def bark
  65. @woofer.woof
  66. end
  67. def wag
  68. :happy
  69. end
  70. end
  71. class TestDogBarking < Test::Unit::TestCase
  72. include FlexMock::TestCase
  73. # Setup the tests by mocking the +new+ method of
  74. # Woofer and return a mock woofer.
  75. def setup
  76. @dog = Dog.new
  77. flexmock(@dog, :bark => :grrr)
  78. end
  79. def test_dog
  80. assert_equal :grrr, @dog.bark # Mocked Method
  81. assert_equal :happy, @dog.wag # Normal Method
  82. end
  83. end
  84. class TestDogBarkingWithNewInstances < Test::Unit::TestCase
  85. include FlexMock::TestCase
  86. # Setup the tests by mocking Woofer to always
  87. # return partial mocks.
  88. def setup
  89. flexmock(Woofer).new_instances.should_receive(:woof => :grrr)
  90. end
  91. def test_dog
  92. assert_equal :grrr, Dog.new.bark # All dog objects
  93. assert_equal :grrr, Dog.new.bark # are mocked.
  94. end
  95. end
  96. class TestDefaults < Test::Unit::TestCase
  97. include FlexMock::TestCase
  98. def setup
  99. @mock_dog = flexmock("Fido")
  100. @mock_dog.should_receive(:tail => :a_tail, :bark => "woof").by_default
  101. end
  102. def test_something_where_bark_must_be_called_once
  103. @mock_dog.should_receive(:bark => "bow wow").once
  104. assert_equal "bow wow", @mock_dog.bark
  105. assert_equal :a_tail, @mock_dog.tail
  106. end
  107. end
  108. class TestDemeter < Test::Unit::TestCase
  109. include FlexMock::TestCase
  110. def test_manual_mocking
  111. # Manually mocking a Law of Demeter violation
  112. cog = flexmock("cog")
  113. cog.should_receive(:turn).once.and_return(:ok)
  114. joint = flexmock("gear", :cog => cog)
  115. axle = flexmock("axle", :universal_joint => joint)
  116. chassis = flexmock("chassis", :axle => axle)
  117. car = flexmock("car", :chassis => chassis)
  118. # test code
  119. assert_equal :ok, car.chassis.axle.universal_joint.cog.turn
  120. end
  121. def test_demeter
  122. car = flexmock("car")
  123. car.should_receive( "chassis.axle.universal_joint.cog.turn" => :ok).once
  124. # Test code
  125. assert_equal :ok, car.chassis.axle.universal_joint.cog.turn
  126. end
  127. end
  128. class TestDb < Test::Unit::TestCase
  129. include FlexMock::TestCase
  130. def test_db
  131. db = flexmock('db')
  132. db.should_receive(:query).and_return([1,2,3])
  133. db.should_receive(:update).with(5).and_return(nil).once
  134. # test code
  135. assert_nil db.update(5)
  136. end
  137. end
  138. class TestDb < Test::Unit::TestCase
  139. include FlexMock::TestCase
  140. def test_query_and_update
  141. db = flexmock('db')
  142. db.should_receive(:query).and_return([1,2,3]).ordered
  143. db.should_receive(:update).and_return(nil).ordered
  144. # test code here
  145. assert_raises(Test::Unit::AssertionFailedError) do
  146. db.update
  147. db.query
  148. end
  149. end
  150. def test_ordered_queries
  151. db = flexmock('db')
  152. db.should_receive(:startup).once.ordered
  153. db.should_receive(:query).with("CPWR").and_return(12.3).
  154. once.ordered(:queries)
  155. db.should_receive(:query).with("MSFT").and_return(10.0).
  156. once.ordered(:queries)
  157. db.should_receive(:query).with(/^....$/).and_return(3.3).
  158. at_least.once.ordered(:queries)
  159. db.should_receive(:finish).once.ordered
  160. # test code here
  161. db.startup
  162. db.query("CPWR")
  163. db.query("MSFT")
  164. db.query("asdf")
  165. db.finish
  166. end
  167. def test_ordered_queries_in_record_mode
  168. db = flexmock('db')
  169. db.should_expect do |rec|
  170. rec.startup.once.ordered
  171. rec.query("CPWR") { 12.3 }.once.ordered(:queries)
  172. rec.query("MSFT") { 10.0 }.once.ordered(:queries)
  173. rec.query(/^....$/) { 3.3 }.at_least.once.ordered(:queries)
  174. rec.finish.once.ordered
  175. end
  176. # test code here using +db+.
  177. db.startup
  178. db.query("CPWR")
  179. db.query("MSFT")
  180. db.query("asdf")
  181. db.finish
  182. end
  183. def known_good_way_to_build_xml(builder)
  184. builder.html
  185. end
  186. def new_way_to_build_xml(builder)
  187. known_good_way_to_build_xml(builder)
  188. end
  189. def test_build_xml
  190. builder = flexmock('builder')
  191. builder.should_expect do |rec|
  192. rec.should_be_strict
  193. known_good_way_to_build_xml(rec) # record the messages
  194. end
  195. new_way_to_build_xml(builder) # compare to new way
  196. end
  197. end
  198. class TestMoreSamples < Test::Unit::TestCase
  199. include FlexMock::TestCase
  200. def test_multiple_gets
  201. file = flexmock('file')
  202. file.should_receive(:gets).with_no_args.
  203. and_return("line 1\n", "line 2\n")
  204. # test code here
  205. assert_equal "line 1\n", file.gets
  206. assert_equal "line 2\n", file.gets
  207. end
  208. def test_an_important_message
  209. m = flexmock('m')
  210. m.should_receive(:an_important_message).and_return(1).once
  211. m.should_ignore_missing
  212. # test code here
  213. assert_equal 1, m.an_important_message
  214. assert_equal FlexMock.undefined, m.other
  215. end
  216. class QuoteService
  217. end
  218. class Portfolio
  219. def initialize
  220. @quote_service = QuoteService.new
  221. end
  222. def value
  223. @quote_service.quote
  224. end
  225. end
  226. def test_portfolio_value
  227. flexmock(QuoteService).new_instances do |m|
  228. m.should_receive(:quote).and_return(100)
  229. end
  230. port = Portfolio.new
  231. value = port.value # Portfolio calls QuoteService.quote
  232. assert_equal 100, value
  233. end
  234. end