PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/ceedling/vendor/cmock/vendor/hardmock/test/functional/direct_mock_usage_test.rb

http://github.com/mikedlowis/data-structures
Ruby | 396 lines | 361 code | 29 blank | 6 comment | 1 complexity | c3b8fd81c9dc4f9bd2db5470d1d056dc MD5 | raw file
Possible License(s): BSD-3-Clause
  1. require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
  2. require 'hardmock'
  3. class DirectMockUsageTest < Test::Unit::TestCase
  4. def setup
  5. @bird = Mock.new('bird')
  6. end
  7. def teardown
  8. end
  9. #
  10. # TESTS
  11. #
  12. it "raises VerifyError if expected method not called" do
  13. @bird.expects.flap_flap
  14. err = assert_raise VerifyError do
  15. @bird._verify
  16. end
  17. assert_match(/unmet expectations/i, err.message)
  18. end
  19. should "not raise when expected calls are made in order" do
  20. @bird.expects.flap_flap
  21. @bird.expects.bang
  22. @bird.expects.plop
  23. @bird.flap_flap
  24. @bird.bang
  25. @bird.plop
  26. @bird._verify
  27. end
  28. it "raises ExpectationError when unexpected method are called" do
  29. @bird.expects.flap_flap
  30. err = assert_raise ExpectationError do
  31. @bird.shoot
  32. end
  33. assert_match(/wrong method/i, err.message)
  34. end
  35. it "raises ExpectationError on bad arguments" do
  36. @bird.expects.flap_flap(:swoosh)
  37. err = assert_raise ExpectationError do
  38. @bird.flap_flap(:rip)
  39. end
  40. assert_match(/wrong arguments/i, err.message)
  41. end
  42. it "raises VerifyError when not all expected methods are called" do
  43. @bird.expects.flap_flap
  44. @bird.expects.bang
  45. @bird.expects.plop
  46. @bird.flap_flap
  47. err = assert_raise VerifyError do
  48. @bird._verify
  49. end
  50. assert_match(/unmet expectations/i, err.message)
  51. end
  52. it "raises ExpectationError when calls are made out of order" do
  53. @bird.expects.flap_flap
  54. @bird.expects.bang
  55. @bird.expects.plop
  56. @bird.flap_flap
  57. err = assert_raise ExpectationError do
  58. @bird.plop
  59. end
  60. assert_match(/wrong method/i, err.message)
  61. end
  62. it "returns the configured value" do
  63. @bird.expects.plop.returns(':P')
  64. assert_equal ':P', @bird.plop
  65. @bird._verify
  66. @bird.expects.plop.returns(':x')
  67. assert_equal ':x', @bird.plop
  68. @bird._verify
  69. end
  70. it "returns nil when no return is specified" do
  71. @bird.expects.plop
  72. assert_nil @bird.plop
  73. @bird._verify
  74. end
  75. it "raises the configured exception" do
  76. err = RuntimeError.new('shaq')
  77. @bird.expects.plop.raises(err)
  78. actual_err = assert_raise RuntimeError do
  79. @bird.plop
  80. end
  81. assert_same err, actual_err, 'should be the same error'
  82. @bird._verify
  83. end
  84. it "raises a RuntimeError when told to 'raise' a string" do
  85. @bird.expects.plop.raises('shaq')
  86. err = assert_raise RuntimeError do
  87. @bird.plop
  88. end
  89. assert_match(/shaq/i, err.message)
  90. @bird._verify
  91. end
  92. it "raises a default RuntimeError" do
  93. @bird.expects.plop.raises
  94. err = assert_raise RuntimeError do
  95. @bird.plop
  96. end
  97. assert_match(/error/i, err.message)
  98. @bird._verify
  99. end
  100. it "is quiet when correct arguments given" do
  101. thing = Object.new
  102. @bird.expects.plop(:big,'one',thing)
  103. @bird.plop(:big,'one',thing)
  104. @bird._verify
  105. end
  106. it "raises ExpectationError when wrong number of arguments specified" do
  107. thing = Object.new
  108. @bird.expects.plop(:big,'one',thing)
  109. err = assert_raise ExpectationError do
  110. # more
  111. @bird.plop(:big,'one',thing,:other)
  112. end
  113. assert_match(/wrong arguments/i, err.message)
  114. @bird._verify
  115. @bird.expects.plop(:big,'one',thing)
  116. err = assert_raise ExpectationError do
  117. # less
  118. @bird.plop(:big,'one')
  119. end
  120. assert_match(/wrong arguments/i, err.message)
  121. @bird._verify
  122. @bird.expects.plop
  123. err = assert_raise ExpectationError do
  124. # less
  125. @bird.plop(:big)
  126. end
  127. assert_match(/wrong arguments/i, err.message)
  128. @bird._verify
  129. end
  130. it "raises ExpectationError when arguments don't match" do
  131. thing = Object.new
  132. @bird.expects.plop(:big,'one',thing)
  133. err = assert_raise ExpectationError do
  134. @bird.plop(:big,'two',thing,:other)
  135. end
  136. assert_match(/wrong arguments/i, err.message)
  137. @bird._verify
  138. end
  139. it "can use a block for custom reactions" do
  140. mitt = nil
  141. @bird.expects.plop { mitt = :ball }
  142. assert_nil mitt
  143. @bird.plop
  144. assert_equal :ball, mitt, 'didnt catch the ball'
  145. @bird._verify
  146. @bird.expects.plop { raise 'ball' }
  147. err = assert_raise RuntimeError do
  148. @bird.plop
  149. end
  150. assert_match(/ball/i, err.message)
  151. @bird._verify
  152. end
  153. it "passes mock-call arguments to the expectation block" do
  154. ball = nil
  155. mitt = nil
  156. @bird.expects.plop {|arg1,arg2|
  157. ball = arg1
  158. mitt = arg2
  159. }
  160. assert_nil ball
  161. assert_nil mitt
  162. @bird.plop(:ball,:mitt)
  163. assert_equal :ball, ball
  164. assert_equal :mitt, mitt
  165. @bird._verify
  166. end
  167. it "validates arguments if specified in addition to a block" do
  168. ball = nil
  169. mitt = nil
  170. @bird.expects.plop(:ball,:mitt) {|arg1,arg2|
  171. ball = arg1
  172. mitt = arg2
  173. }
  174. assert_nil ball
  175. assert_nil mitt
  176. @bird.plop(:ball,:mitt)
  177. assert_equal :ball, ball
  178. assert_equal :mitt, mitt
  179. @bird._verify
  180. ball = nil
  181. mitt = nil
  182. @bird.expects.plop(:bad,:stupid) {|arg1,arg2|
  183. ball = arg1
  184. mitt = arg2
  185. }
  186. assert_nil ball
  187. assert_nil mitt
  188. err = assert_raise ExpectationError do
  189. @bird.plop(:ball,:mitt)
  190. end
  191. assert_match(/wrong arguments/i, err.message)
  192. assert_nil ball
  193. assert_nil mitt
  194. @bird._verify
  195. ball = nil
  196. mitt = nil
  197. @bird.expects.plop(:ball,:mitt) {|arg1,arg2|
  198. ball = arg1
  199. mitt = arg2
  200. }
  201. assert_nil ball
  202. assert_nil mitt
  203. err = assert_raise ExpectationError do
  204. @bird.plop(:ball)
  205. end
  206. assert_match(/wrong arguments/i, err.message)
  207. assert_nil ball
  208. assert_nil mitt
  209. @bird._verify
  210. end
  211. it "passes runtime blocks to the expectation block as the final argument" do
  212. runtime_block_called = false
  213. got_arg = nil
  214. # Eg, bird expects someone to subscribe to :tweet using the 'when' method
  215. @bird.expects.when(:tweet) { |arg1, block|
  216. got_arg = arg1
  217. block.call
  218. }
  219. @bird.when(:tweet) do
  220. runtime_block_called = true
  221. end
  222. assert_equal :tweet, got_arg, "Wrong arg"
  223. assert runtime_block_called, "The runtime block should have been invoked by the user block"
  224. @bird.expects.when(:warnk) { |e,blk| }
  225. err = assert_raise ExpectationError do
  226. @bird.when(:honk) { }
  227. end
  228. assert_match(/wrong arguments/i, err.message)
  229. @bird._verify
  230. end
  231. it "passes the runtime block to the expectation block as sole argument if no other args come into play" do
  232. runtime_block_called = false
  233. @bird.expects.subscribe { |block| block.call }
  234. @bird.subscribe do
  235. runtime_block_called = true
  236. end
  237. assert runtime_block_called, "The runtime block should have been invoked by the user block"
  238. end
  239. it "provides nil as final argument if expectation block seems to want a block" do
  240. invoked = false
  241. @bird.expects.kablam(:scatter) { |shot,block|
  242. assert_equal :scatter, shot, "Wrong shot"
  243. assert_nil block, "The expectation block should get a nil block when user neglects to pass one"
  244. invoked = true
  245. }
  246. @bird.kablam :scatter
  247. assert invoked, "Expectation block not invoked"
  248. @bird._verify
  249. end
  250. it "can set explicit return after an expectation block" do
  251. got = nil
  252. @bird.expects.kablam(:scatter) { |shot|
  253. got = shot
  254. }.returns(:death)
  255. val = @bird.kablam :scatter
  256. assert_equal :death, val, "Wrong return value"
  257. assert_equal :scatter, got, "Wrong argument"
  258. @bird._verify
  259. end
  260. it "can raise after an expectation block" do
  261. got = nil
  262. @bird.expects.kablam(:scatter) do |shot|
  263. got = shot
  264. end.raises "hell"
  265. err = assert_raise RuntimeError do
  266. @bird.kablam :scatter
  267. end
  268. assert_match(/hell/i, err.message)
  269. @bird._verify
  270. end
  271. it "stores the semantic value of the expectation block after it executes" do
  272. expectation = @bird.expects.kablam(:slug) { |shot|
  273. "The shot was #{shot}"
  274. }
  275. assert_not_nil expectation, "Expectation nil"
  276. assert_nil expectation.block_value, "Block value should start out nil"
  277. ret_val = @bird.kablam :slug
  278. assert_equal "The shot was slug", expectation.block_value
  279. assert_equal "The shot was slug", ret_val, "Block value should also be used for return"
  280. @bird._verify
  281. end
  282. it "uses the value of the expectation block as the default return value" do
  283. @bird.expects.kablam(:scatter) { |shot|
  284. "The shot was #{shot}"
  285. }
  286. val = @bird.kablam :scatter
  287. assert_equal "The shot was scatter", val, "Wrong return value"
  288. @bird._verify
  289. end
  290. it "returns the Expectation even if 'returns' is used" do
  291. expectation = @bird.expects.kablam(:slug) { |shot|
  292. "The shot was #{shot}"
  293. }.returns :hosed
  294. assert_not_nil expectation, "Expectation nil"
  295. assert_nil expectation.block_value, "Block value should start out nil"
  296. ret_val = @bird.kablam :slug
  297. assert_equal "The shot was slug", expectation.block_value
  298. assert_equal :hosed, ret_val, "Block value should also be used for return"
  299. @bird._verify
  300. end
  301. it "returns the Expectation even if 'raises' is used" do
  302. expectation = @bird.expects.kablam(:slug) { |shot|
  303. "The shot was #{shot}"
  304. }.raises "aiee!"
  305. assert_not_nil expectation, "Expectation nil"
  306. assert_nil expectation.block_value, "Block value should start out nil"
  307. err = assert_raise RuntimeError do
  308. @bird.kablam :slug
  309. end
  310. assert_match(/aiee!/i, err.message)
  311. assert_equal "The shot was slug", expectation.block_value
  312. @bird._verify
  313. end
  314. it "supports assignment-style methods" do
  315. @bird.expects.size = "large"
  316. @bird.size = "large"
  317. @bird._verify
  318. end
  319. it "supports assignments and raising (using explicit-method syntax)" do
  320. @bird.expects('size=','large').raises "boom"
  321. err = assert_raise RuntimeError do
  322. @bird.size = "large"
  323. end
  324. assert_match(/boom/i, err.message)
  325. end
  326. end