PageRenderTime 51ms CodeModel.GetById 13ms 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_should_receive.rb

http://github.com/IronLanguages/main
Ruby | 1100 lines | 944 code | 140 blank | 16 comment | 2 complexity | 094c91ce1c304d5f2314b34013b56303 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. require 'test/asserts'
  12. def mock_top_level_function
  13. :mtlf
  14. end
  15. module Kernel
  16. def mock_kernel_function
  17. :mkf
  18. end
  19. end
  20. # Used for testing
  21. class Cat
  22. def purr
  23. end
  24. def meow
  25. end
  26. end
  27. class TestFlexMockShoulds < Test::Unit::TestCase
  28. include FlexMock::TestCase
  29. include FlexMock::FailureAssertion
  30. # Expected error messages on failures
  31. COUNT_ERROR_MESSAGE = /\bcalled\s+incorrect\s+number\s+of\s+times\b/
  32. NO_MATCH_ERROR_MESSAGE = /\bno\s+matching\s+handler\b/
  33. AT_LEAST_ERROR_MESSAGE = /\bshould\s+be\s+called\s+at\s+least\b/
  34. AT_MOST_ERROR_MESSAGE = /\bshould\s+be\s+called\s+at\s+most\b/
  35. OUT_OF_ORDER_ERROR_MESSAGE = /\bcalled\s+out\s+of\s+order\b/
  36. NON_CONTAINER_MESSAGE = /\bis\s+not\s+in\s+a\s+container\b/
  37. def test_defaults
  38. FlexMock.use do |m|
  39. m.should_receive(:hi)
  40. assert_nil m.hi
  41. assert_nil m.hi(1)
  42. assert_nil m.hi("hello", 2)
  43. end
  44. end
  45. def test_returns_with_value
  46. FlexMock.use do |m|
  47. m.should_receive(:hi).returns(1)
  48. assert_equal 1, m.hi
  49. assert_equal 1, m.hi(123)
  50. end
  51. end
  52. def test_returns_with_multiple_values
  53. FlexMock.use do |m|
  54. m.should_receive(:hi).and_return(1,2,3)
  55. assert_equal 1, m.hi
  56. assert_equal 2, m.hi
  57. assert_equal 3, m.hi
  58. assert_equal 3, m.hi
  59. assert_equal 3, m.hi
  60. end
  61. end
  62. def test_multiple_returns
  63. FlexMock.use do |m|
  64. m.should_receive(:hi).and_return(1).and_return(2,3)
  65. assert_equal 1, m.hi
  66. assert_equal 2, m.hi
  67. assert_equal 3, m.hi
  68. assert_equal 3, m.hi
  69. assert_equal 3, m.hi
  70. end
  71. end
  72. def test_returns_with_block
  73. FlexMock.use do |m|
  74. result = nil
  75. m.should_receive(:hi).with(Object).returns { |obj| result = obj }
  76. m.hi(3)
  77. assert_equal 3, result
  78. end
  79. end
  80. def test_block_example_from_readme
  81. FlexMock.use do |m|
  82. m.should_receive(:foo).with(Integer,Proc).and_return(:got_block)
  83. m.should_receive(:foo).with(Integer).and_return(:no_block)
  84. assert_equal :no_block, m.foo(1)
  85. assert_equal :got_block, m.foo(1) { }
  86. end
  87. end
  88. def test_return_with_and_without_block_interleaved
  89. FlexMock.use do |m|
  90. m.should_receive(:hi).and_return(:a).and_return { :b }.and_return(:c)
  91. assert_equal :a, m.hi
  92. assert_equal :b, m.hi
  93. assert_equal :c, m.hi
  94. assert_equal :c, m.hi
  95. end
  96. end
  97. def test_and_returns_alias
  98. FlexMock.use do |m|
  99. m.should_receive(:hi).and_return(4)
  100. assert_equal 4, m.hi
  101. end
  102. end
  103. def test_and_return_undefined
  104. FlexMock.use do |m|
  105. m.should_receive(:foo).and_return_undefined
  106. m.should_receive(:phoo).returns_undefined
  107. assert_equal FlexMock.undefined, m.foo
  108. assert_equal FlexMock.undefined, m.foo.bar.baz.bing.ka_ching
  109. assert_equal FlexMock.undefined, m.phoo.bar.baz.bing.ka_ching
  110. end
  111. end
  112. def test_and_yield_will_continue_to_yield_the_same_value
  113. FlexMock.use do |m|
  114. m.should_receive(:hi).and_yield(:yield_value)
  115. assert_equal :yield_value, m.hi { |v| v }
  116. assert_equal :yield_value, m.hi { |v| v }
  117. end
  118. end
  119. def test_and_yield_with_multiple_values_yields_the_values
  120. FlexMock.use do |m|
  121. m.should_receive(:hi).and_yield(:one, :two).once
  122. assert_equal [:one, :two], m.hi { |a, b| [a, b] }
  123. end
  124. end
  125. def test_multiple_yields_are_done_sequentially
  126. FlexMock.use do |m|
  127. m.should_receive(:msg).and_yield(:one).and_yield(:two)
  128. assert_equal :one, m.msg { |a| a }
  129. assert_equal :two, m.msg { |a| a }
  130. assert_equal :two, m.msg { |a| a }
  131. end
  132. end
  133. def test_failure_if_no_block_given
  134. FlexMock.use do |m|
  135. m.should_receive(:hi).and_yield(:one, :two).once
  136. assert_raise(FlexMock::MockError) do m.hi end
  137. end
  138. end
  139. def test_failure_different_return_value_than_yield_return
  140. FlexMock.use do |m|
  141. m.should_receive(:hi).and_yield(:yld).once.and_return(:ret)
  142. yielded_value = nil
  143. assert_equal :ret, m.hi { |v| yielded_value = v }
  144. assert_equal :yld, yielded_value
  145. end
  146. end
  147. def test_multiple_yields
  148. FlexMock.use do |m|
  149. m.should_receive(:hi).and_yield(:one, :two).and_yield(1, 2)
  150. assert_equal [:one, :two], m.hi { |a, b| [a, b] }
  151. assert_equal [1, 2], m.hi { |a, b| [a, b] }
  152. end
  153. end
  154. def test_multiple_yields_will_yield_the_last_value_set
  155. FlexMock.use do |m|
  156. m.should_receive(:hi).and_yield(:a).and_yield(:b)
  157. assert_equal [:a], m.hi { |a, b| [a] }
  158. assert_equal [:b], m.hi { |a, b| [a] }
  159. assert_equal [:b], m.hi { |a, b| [a] }
  160. assert_equal [:b], m.hi { |a, b| [a] }
  161. assert_equal [:b], m.hi { |a, b| [a] }
  162. end
  163. end
  164. def test_yielding_then_not_yielding_and_then_yielding_again
  165. FlexMock.use do |m|
  166. m.should_receive(:hi).and_yield(:a).once
  167. m.should_receive(:hi).and_return(:b).once
  168. m.should_receive(:hi).and_yield(:c).once
  169. assert_equal :a, m.hi { |v| v }
  170. assert_equal :b, m.hi
  171. assert_equal :c, m.hi { |v| v }
  172. end
  173. end
  174. def test_yields_syntax
  175. FlexMock.use do |m|
  176. m.should_receive(:hi).yields(:one)
  177. assert_equal :one, m.hi { |a| a }
  178. end
  179. end
  180. class MyError < RuntimeError
  181. end
  182. def test_and_raises_with_exception_class_throws_exception
  183. FlexMock.use do |m|
  184. m.should_receive(:failure).and_raise(MyError)
  185. assert_raise MyError do
  186. m.failure
  187. end
  188. end
  189. end
  190. def test_and_raises_with_arguments_throws_exception_made_with_args
  191. FlexMock.use do |m|
  192. m.should_receive(:failure).and_raise(MyError, "my message")
  193. ex = assert_raise MyError do
  194. m.failure
  195. end
  196. assert_equal "my message", ex.message
  197. end
  198. end
  199. def test_and_raises_with_a_specific_exception_throws_the_exception
  200. FlexMock.use do |m|
  201. err = MyError.new
  202. m.should_receive(:failure).and_raise(err)
  203. ex = assert_raise MyError do
  204. m.failure
  205. end
  206. assert_equal err, ex
  207. end
  208. end
  209. def test_raises_is_an_alias_for_and_raise
  210. FlexMock.use do |m|
  211. m.should_receive(:failure).raises(RuntimeError)
  212. ex = assert_raise RuntimeError do
  213. m.failure
  214. end
  215. end
  216. end
  217. def test_multiple_and_raise_clauses_will_be_done_sequentially
  218. FlexMock.use do |m|
  219. m.should_receive(:failure).
  220. and_raise(RuntimeError, "ONE").
  221. and_raise(RuntimeError, "TWO")
  222. ex = assert_raise RuntimeError do m.failure end
  223. assert_equal "ONE", ex.message
  224. ex = assert_raise RuntimeError do m.failure end
  225. assert_equal "TWO", ex.message
  226. end
  227. end
  228. def test_and_throw_will_throw_a_symbol
  229. FlexMock.use do |m|
  230. m.should_receive(:msg).and_throw(:sym)
  231. value = catch(:sym) do
  232. m.msg
  233. fail "Should not reach this line"
  234. end
  235. assert_nil value
  236. end
  237. end
  238. def test_and_throw_with_expression_will_throw
  239. FlexMock.use do |m|
  240. m.should_receive(:msg).and_throw(:sym, :return_value)
  241. value = catch(:sym) do
  242. m.msg
  243. fail "Should not reach this line"
  244. end
  245. assert_equal :return_value, value
  246. end
  247. end
  248. def test_throws_is_an_alias_for_and_throw
  249. FlexMock.use do |m|
  250. m.should_receive(:msg).throws(:sym, :return_value)
  251. value = catch(:sym) do
  252. m.msg
  253. fail "Should not reach this line"
  254. end
  255. assert_equal :return_value, value
  256. end
  257. end
  258. def test_multiple_throws_will_be_done_sequentially
  259. FlexMock.use do |m|
  260. m.should_receive(:toss).
  261. and_throw(:sym, "ONE").
  262. and_throw(:sym, "TWO")
  263. value = catch(:sym) do m.toss end
  264. assert_equal "ONE", value
  265. value = catch(:sym) do m.toss end
  266. assert_equal "TWO", value
  267. end
  268. end
  269. def test_multiple_expectations
  270. FlexMock.use do |m|
  271. m.should_receive(:hi).with(1).returns(10)
  272. m.should_receive(:hi).with(2).returns(20)
  273. assert_equal 10, m.hi(1)
  274. assert_equal 20, m.hi(2)
  275. end
  276. end
  277. def test_with_no_args_with_no_args
  278. FlexMock.use do |m|
  279. m.should_receive(:hi).with_no_args
  280. m.hi
  281. end
  282. end
  283. def test_with_no_args_but_with_args
  284. ex = assert_failure(NO_MATCH_ERROR_MESSAGE) do
  285. FlexMock.use do |m|
  286. m.should_receive(:hi).with_no_args
  287. m.hi(1)
  288. end
  289. end
  290. end
  291. def test_with_any_args
  292. FlexMock.use do |m|
  293. m.should_receive(:hi).with_any_args
  294. m.hi
  295. m.hi(1)
  296. m.hi(1,2,3)
  297. m.hi("this is a test")
  298. end
  299. end
  300. def test_with_any_single_arg_matching
  301. FlexMock.use('greeter') do |m|
  302. m.should_receive(:hi).with(1,FlexMock.any).twice
  303. m.hi(1,2)
  304. m.hi(1, "this is a test")
  305. end
  306. end
  307. def test_with_any_single_arg_nonmatching
  308. FlexMock.use('greeter') do |m|
  309. m.should_receive(:hi).times(3)
  310. m.should_receive(:hi).with(1,FlexMock.any).never
  311. m.hi
  312. m.hi(1)
  313. m.hi(1, "hi", nil)
  314. end
  315. end
  316. def test_with_equal_arg_matching
  317. FlexMock.use('greeter') do |m|
  318. m.should_receive(:hi).with(FlexMock.eq(Object)).once
  319. m.hi(Object)
  320. end
  321. end
  322. def test_with_ducktype_arg_matching
  323. FlexMock.use('greeter') do |m|
  324. m.should_receive(:hi).with(FlexMock.ducktype(:purr, :meow)).once
  325. m.hi(Cat.new)
  326. end
  327. end
  328. def test_with_ducktype_arg_matching_no_match
  329. FlexMock.use('greeter') do |m|
  330. m.should_receive(:hi).with(FlexMock.ducktype(:purr, :meow, :growl))
  331. assert_failure {
  332. m.hi(Cat.new)
  333. }
  334. end
  335. end
  336. def test_with_hash_matching
  337. FlexMock.use('greeter') do |m|
  338. m.should_receive(:hi).with(FlexMock.hsh(:a => 1, :b => 2)).once
  339. m.hi(:a => 1, :b => 2, :c => 3)
  340. end
  341. end
  342. def test_with_hash_non_matching
  343. FlexMock.use('greeter') do |m|
  344. m.should_receive(:hi).with(FlexMock.hsh(:a => 1, :b => 2))
  345. assert_failure {
  346. m.hi(:a => 1, :b => 4, :c => 3)
  347. }
  348. end
  349. end
  350. def test_with_equal_arg_nonmatching
  351. FlexMock.use('greeter') do |m|
  352. m.should_receive(:hi).with(FlexMock.eq(Object)).never
  353. m.should_receive(:hi).never
  354. m.should_receive(:hi).with(1).once
  355. m.hi(1)
  356. end
  357. end
  358. def test_with_arbitrary_arg_matching
  359. FlexMock.use('greeter') do |m|
  360. m.should_receive(:hi).with(FlexMock.on { |arg| arg % 2 == 0 }).twice
  361. m.should_receive(:hi).never
  362. m.should_receive(:hi).with(1).once
  363. m.should_receive(:hi).with(2).never
  364. m.should_receive(:hi).with(3).once
  365. m.should_receive(:hi).with(4).never
  366. m.hi(1)
  367. m.hi(2)
  368. m.hi(3)
  369. m.hi(4)
  370. end
  371. end
  372. def test_args_matching_with_regex
  373. FlexMock.use do |m|
  374. m.should_receive(:hi).with(/one/).returns(10)
  375. m.should_receive(:hi).with(/t/).returns(20)
  376. assert_equal 10, m.hi("one")
  377. assert_equal 10, m.hi("done")
  378. assert_equal 20, m.hi("two")
  379. assert_equal 20, m.hi("three")
  380. end
  381. end
  382. def test_arg_matching_with_regex_matching_non_string
  383. FlexMock.use do |m|
  384. m.should_receive(:hi).with(/1/).returns(10)
  385. assert_equal 10, m.hi(319)
  386. end
  387. end
  388. def test_arg_matching_with_class
  389. FlexMock.use do |m|
  390. m.should_receive(:hi).with(Fixnum).returns(10)
  391. m.should_receive(:hi).with(Object).returns(20)
  392. assert_equal 10, m.hi(319)
  393. assert_equal 10, m.hi(Fixnum)
  394. assert_equal 20, m.hi("hi")
  395. end
  396. end
  397. def test_arg_matching_with_no_match
  398. FlexMock.use do |m|
  399. m.should_receive(:hi).with(1).returns(10)
  400. assert_failure(NO_MATCH_ERROR_MESSAGE) {
  401. assert_equal 20, m.hi(2)
  402. }
  403. end
  404. end
  405. def test_arg_matching_with_string_doesnt_over_match
  406. FlexMock.use do |m|
  407. m.should_receive(:hi).with(String).returns(20)
  408. assert_failure(NO_MATCH_ERROR_MESSAGE) {
  409. m.hi(1.0)
  410. }
  411. end
  412. end
  413. def test_block_arg_given_to_no_args
  414. FlexMock.use do |m|
  415. m.should_receive(:hi).with_no_args.returns(20)
  416. assert_failure(NO_MATCH_ERROR_MESSAGE) {
  417. m.hi { 1 }
  418. }
  419. end
  420. end
  421. def test_block_arg_given_to_matching_proc
  422. FlexMock.use do |m|
  423. arg = nil
  424. m.should_receive(:hi).with(Proc).once.
  425. and_return { |block| arg = block; block.call }
  426. result = m.hi { 1 }
  427. assert_equal 1, arg.call
  428. assert_equal 1, result
  429. end
  430. end
  431. def test_arg_matching_precedence_when_best_first
  432. FlexMock.use("greeter") do |m|
  433. m.should_receive(:hi).with(1).once
  434. m.should_receive(:hi).with(FlexMock.any).never
  435. m.hi(1)
  436. end
  437. end
  438. def test_arg_matching_precedence_when_best_last_but_still_matches_first
  439. FlexMock.use("greeter") do |m|
  440. m.should_receive(:hi).with(FlexMock.any).once
  441. m.should_receive(:hi).with(1).never
  442. m.hi(1)
  443. end
  444. end
  445. def test_never_and_never_called
  446. FlexMock.use do |m|
  447. m.should_receive(:hi).with(1).never
  448. end
  449. end
  450. def test_never_and_called_once
  451. ex = assert_failure(COUNT_ERROR_MESSAGE) do
  452. FlexMock.use do |m|
  453. m.should_receive(:hi).with(1).never
  454. m.hi(1)
  455. end
  456. end
  457. end
  458. def test_once_called_once
  459. FlexMock.use do |m|
  460. m.should_receive(:hi).with(1).returns(10).once
  461. m.hi(1)
  462. end
  463. end
  464. def test_once_but_never_called
  465. ex = assert_failure(COUNT_ERROR_MESSAGE) do
  466. FlexMock.use do |m|
  467. m.should_receive(:hi).with(1).returns(10).once
  468. end
  469. end
  470. end
  471. def test_once_but_called_twice
  472. ex = assert_failure(COUNT_ERROR_MESSAGE) do
  473. FlexMock.use do |m|
  474. m.should_receive(:hi).with(1).returns(10).once
  475. m.hi(1)
  476. m.hi(1)
  477. end
  478. end
  479. end
  480. def test_twice_and_called_twice
  481. FlexMock.use do |m|
  482. m.should_receive(:hi).with(1).returns(10).twice
  483. m.hi(1)
  484. m.hi(1)
  485. end
  486. end
  487. def test_zero_or_more_called_zero
  488. FlexMock.use do |m|
  489. m.should_receive(:hi).zero_or_more_times
  490. end
  491. end
  492. def test_zero_or_more_called_once
  493. FlexMock.use do |m|
  494. m.should_receive(:hi).zero_or_more_times
  495. m.hi
  496. end
  497. end
  498. def test_zero_or_more_called_100
  499. FlexMock.use do |m|
  500. m.should_receive(:hi).zero_or_more_times
  501. 100.times { m.hi }
  502. end
  503. end
  504. def test_times
  505. FlexMock.use do |m|
  506. m.should_receive(:hi).with(1).returns(10).times(10)
  507. 10.times { m.hi(1) }
  508. end
  509. end
  510. def test_at_least_called_once
  511. FlexMock.use do |m|
  512. m.should_receive(:hi).with(1).returns(10).at_least.once
  513. m.hi(1)
  514. end
  515. end
  516. def test_at_least_but_never_called
  517. ex = assert_failure(AT_LEAST_ERROR_MESSAGE) do
  518. FlexMock.use do |m|
  519. m.should_receive(:hi).with(1).returns(10).at_least.once
  520. end
  521. end
  522. end
  523. def test_at_least_once_but_called_twice
  524. FlexMock.use do |m|
  525. m.should_receive(:hi).with(1).returns(10).at_least.once
  526. m.hi(1)
  527. m.hi(1)
  528. end
  529. end
  530. def test_at_least_and_exact
  531. ex = assert_failure(COUNT_ERROR_MESSAGE) do
  532. FlexMock.use do |m|
  533. m.should_receive(:hi).with(1).returns(10).at_least.once.once
  534. m.hi(1)
  535. m.hi(1)
  536. end
  537. end
  538. end
  539. def test_at_most_but_never_called
  540. FlexMock.use do |m|
  541. m.should_receive(:hi).with(1).returns(10).at_most.once
  542. end
  543. end
  544. def test_at_most_called_once
  545. FlexMock.use do |m|
  546. m.should_receive(:hi).with(1).returns(10).at_most.once
  547. m.hi(1)
  548. end
  549. end
  550. def test_at_most_called_twice
  551. ex = assert_failure(AT_MOST_ERROR_MESSAGE) do
  552. FlexMock.use do |m|
  553. m.should_receive(:hi).with(1).returns(10).at_most.once
  554. m.hi(1)
  555. m.hi(1)
  556. end
  557. end
  558. end
  559. def test_at_most_and_at_least_called_never
  560. ex = assert_failure(AT_LEAST_ERROR_MESSAGE) do
  561. FlexMock.use do |m|
  562. m.should_receive(:hi).with(1).returns(10).at_least.once.at_most.twice
  563. end
  564. end
  565. end
  566. def test_at_most_and_at_least_called_once
  567. FlexMock.use do |m|
  568. m.should_receive(:hi).with(1).returns(10).at_least.once.at_most.twice
  569. m.hi(1)
  570. end
  571. end
  572. def test_at_most_and_at_least_called_twice
  573. FlexMock.use do |m|
  574. m.should_receive(:hi).with(1).returns(10).at_least.once.at_most.twice
  575. m.hi(1)
  576. m.hi(1)
  577. end
  578. end
  579. def test_at_most_and_at_least_called_three_times
  580. ex = assert_failure(AT_MOST_ERROR_MESSAGE) do
  581. FlexMock.use do |m|
  582. m.should_receive(:hi).with(1).returns(10).at_least.once.at_most.twice
  583. m.hi(1)
  584. m.hi(1)
  585. m.hi(1)
  586. end
  587. end
  588. end
  589. def test_call_counts_only_apply_to_matching_args
  590. FlexMock.use do |m|
  591. m.should_receive(:hi).with(1).once
  592. m.should_receive(:hi).with(2).twice
  593. m.should_receive(:hi).with(3)
  594. m.hi(1)
  595. m.hi(2)
  596. m.hi(2)
  597. 20.times { m.hi(3) }
  598. end
  599. end
  600. def test_call_counts_only_apply_to_matching_args_with_mismatch
  601. ex = assert_failure(COUNT_ERROR_MESSAGE) do
  602. FlexMock.use do |m|
  603. m.should_receive(:hi).with(1).once
  604. m.should_receive(:hi).with(2).twice
  605. m.should_receive(:hi).with(3)
  606. m.should_receive(:lo)
  607. m.hi(1)
  608. m.hi(2)
  609. m.lo
  610. 20.times { m.hi(3) }
  611. end
  612. end
  613. assert_match(/hi\(2\)/, ex.message)
  614. end
  615. def test_ordered_calls_in_order_will_pass
  616. FlexMock.use 'm' do |m|
  617. m.should_receive(:hi).ordered
  618. m.should_receive(:lo).ordered
  619. m.hi
  620. m.lo
  621. end
  622. end
  623. def test_ordered_calls_out_of_order_will_fail
  624. ex = assert_failure(OUT_OF_ORDER_ERROR_MESSAGE) do
  625. FlexMock.use 'm' do |m|
  626. m.should_receive(:hi).ordered
  627. m.should_receive(:lo).ordered
  628. m.lo
  629. m.hi
  630. end
  631. end
  632. end
  633. def test_order_calls_with_different_arg_lists_and_in_order_will_pass
  634. FlexMock.use 'm' do |m|
  635. m.should_receive(:hi).with("one").ordered
  636. m.should_receive(:hi).with("two").ordered
  637. m.hi("one")
  638. m.hi("two")
  639. end
  640. end
  641. def test_order_calls_with_different_arg_lists_and_out_of_order_will_fail
  642. ex = assert_failure(OUT_OF_ORDER_ERROR_MESSAGE) do
  643. FlexMock.use 'm' do |m|
  644. m.should_receive(:hi).with("one").ordered
  645. m.should_receive(:hi).with("two").ordered
  646. m.hi("two")
  647. m.hi("one")
  648. end
  649. end
  650. end
  651. def test_unordered_calls_do_not_effect_ordered_testing
  652. FlexMock.use 'm' do |m|
  653. m.should_receive(:blah)
  654. m.should_receive(:hi).ordered
  655. m.should_receive(:lo).ordered
  656. m.blah
  657. m.hi
  658. m.blah
  659. m.lo
  660. m.blah
  661. end
  662. end
  663. def test_ordered_with_multiple_calls_will_pass
  664. FlexMock.use 'm' do |m|
  665. m.should_receive(:hi).ordered
  666. m.should_receive(:lo).ordered
  667. m.hi
  668. m.hi
  669. m.lo
  670. m.lo
  671. end
  672. end
  673. def test_grouped_ordering_with_numbers
  674. FlexMock.use 'm' do |m|
  675. m.should_receive(:start).ordered(1)
  676. m.should_receive(:flip).ordered(2)
  677. m.should_receive(:flop).ordered(2)
  678. m.should_receive(:final).ordered
  679. m.start
  680. m.flop
  681. m.flip
  682. m.flop
  683. m.final
  684. end
  685. end
  686. def test_grouped_ordering_with_symbols
  687. FlexMock.use 'm' do |m|
  688. m.should_receive(:start).ordered(:start_group)
  689. m.should_receive(:flip).ordered(:flip_flop_group)
  690. m.should_receive(:flop).ordered(:flip_flop_group)
  691. m.should_receive(:final).ordered
  692. m.start
  693. m.flop
  694. m.flip
  695. m.flop
  696. m.final
  697. end
  698. end
  699. def test_explicit_ordering_mixed_with_implicit_ordering_should_not_overlap
  700. FlexMock.use 'm' do |m|
  701. xstart = m.should_receive(:start).ordered
  702. xmid = m.should_receive(:mid).ordered(:group_name)
  703. xend = m.should_receive(:end).ordered
  704. assert xstart.order_number < xmid.order_number
  705. assert xmid.order_number < xend.order_number
  706. end
  707. end
  708. def test_explicit_ordering_with_explicit_misorders
  709. ex = assert_failure(OUT_OF_ORDER_ERROR_MESSAGE) do
  710. FlexMock.use 'm' do |m|
  711. m.should_receive(:hi).ordered(:first_group)
  712. m.should_receive(:lo).ordered(:second_group)
  713. m.lo
  714. m.hi
  715. end
  716. end
  717. # TODO: It would be nice to get the group names in the error message.
  718. # assert_match /first_group/, ex.message
  719. # assert_match /second_group/, ex.message
  720. end
  721. # Test submitted by Mikael Pahmp to correct expectation matching.
  722. def test_ordering_with_explicit_no_args_matches_correctly
  723. FlexMock.use("m") do |m|
  724. m.should_receive(:foo).with_no_args.once.ordered
  725. m.should_receive(:bar).with_no_args.once.ordered
  726. m.should_receive(:foo).with_no_args.once.ordered
  727. m.foo
  728. m.bar
  729. m.foo
  730. end
  731. end
  732. # Test submitted by Mikael Pahmp to correct expectation matching.
  733. def test_ordering_with_any_arg_matching_correctly_matches
  734. FlexMock.use("m") do |m|
  735. m.should_receive(:foo).with_any_args.once.ordered
  736. m.should_receive(:bar).with_any_args.once.ordered
  737. m.should_receive(:foo).with_any_args.once.ordered
  738. m.foo
  739. m.bar
  740. m.foo
  741. end
  742. end
  743. def test_ordering_between_mocks_is_not_normally_defined
  744. FlexMock.use("x", "y") do |x, y|
  745. x.should_receive(:one).ordered
  746. y.should_receive(:two).ordered
  747. assert_nothing_raised do
  748. y.two
  749. x.one
  750. end
  751. end
  752. end
  753. def test_ordering_between_mocks_is_honored_for_global_ordering
  754. ex = assert_failure(OUT_OF_ORDER_ERROR_MESSAGE) do
  755. FlexMock.use("x", "y") do |x, y|
  756. x.should_receive(:one).globally.ordered
  757. y.should_receive(:two).globally.ordered
  758. y.two
  759. x.one
  760. end
  761. end
  762. end
  763. def test_expectation_formating
  764. mock = flexmock("m")
  765. exp = mock.should_receive(:f).with(1,"two", /^3$/).
  766. and_return(0).at_least.once
  767. mock.f(1, "two", 3)
  768. assert_equal 'f(1, "two", /^3$/)', exp.to_s
  769. end
  770. def test_multi_expectation_formatting
  771. mock = flexmock("mock")
  772. exp = mock.should_receive(:f, :g).with(1)
  773. assert_equal "[f(1), g(1)]", exp.to_s
  774. end
  775. def test_explicit_ordering_with_limits_allow_multiple_return_values
  776. FlexMock.use('mock') do |m|
  777. m.should_receive(:f).with(2).once.and_return { :first_time }
  778. m.should_receive(:f).with(2).twice.and_return { :second_or_third_time }
  779. m.should_receive(:f).with(2).and_return { :forever }
  780. assert_equal :first_time, m.f(2)
  781. assert_equal :second_or_third_time, m.f(2)
  782. assert_equal :second_or_third_time, m.f(2)
  783. assert_equal :forever, m.f(2)
  784. assert_equal :forever, m.f(2)
  785. assert_equal :forever, m.f(2)
  786. assert_equal :forever, m.f(2)
  787. assert_equal :forever, m.f(2)
  788. assert_equal :forever, m.f(2)
  789. assert_equal :forever, m.f(2)
  790. end
  791. end
  792. def test_global_methods_can_be_mocked
  793. m = flexmock("m")
  794. m.should_receive(:mock_top_level_function).and_return(:mock)
  795. assert_equal :mock, m.mock_top_level_function
  796. end
  797. def test_kernel_methods_can_be_mocked
  798. m = flexmock("m")
  799. m.should_receive(:mock_kernel_function).and_return(:mock)
  800. assert_equal :mock, m.mock_kernel_function
  801. end
  802. def test_undefing_kernel_methods_dont_effect_other_mocks
  803. m = flexmock("m")
  804. m2 = flexmock("m2")
  805. m.should_receive(:mock_kernel_function).and_return(:mock)
  806. assert_equal :mock, m.mock_kernel_function
  807. assert_equal :mkf, m2.mock_kernel_function
  808. end
  809. def test_expectations_can_by_marked_as_default
  810. m = flexmock("m")
  811. m.should_receive(:foo).and_return(:bar).by_default
  812. assert_equal :bar, m.foo
  813. end
  814. def test_default_expectations_are_search_in_the_proper_order
  815. m = flexmock("m")
  816. m.should_receive(:foo).with(Integer).once.and_return(:first).by_default
  817. m.should_receive(:foo).with(1).and_return(:second).by_default
  818. assert_equal :first, m.foo(1)
  819. assert_equal :second, m.foo(1)
  820. end
  821. def test_expectations_with_count_constraints_can_by_marked_as_default
  822. m = flexmock("m")
  823. m.should_receive(:foo).and_return(:bar).once.by_default
  824. assert_raise Test::Unit::AssertionFailedError do
  825. flexmock_teardown
  826. end
  827. end
  828. def test_default_expectations_are_overridden_by_later_expectations
  829. m = flexmock("m")
  830. m.should_receive(:foo).and_return(:bar).once.by_default
  831. m.should_receive(:foo).and_return(:bar).twice
  832. m.foo
  833. m.foo
  834. end
  835. def test_default_expectations_can_be_changed_by_later_expectations
  836. m = flexmock("m")
  837. m.should_receive(:foo).with(1).and_return(:bar).once.by_default
  838. m.should_receive(:foo).with(2).and_return(:baz).once
  839. assert_raise Test::Unit::AssertionFailedError do
  840. # This expectation should be hidded by the non-result
  841. m.foo(1)
  842. end
  843. m.foo(2)
  844. end
  845. def test_ordered_default_expectations_can_be_specified
  846. m = flexmock("m")
  847. m.should_receive(:foo).ordered.by_default
  848. m.should_receive(:bar).ordered.by_default
  849. m.bar
  850. assert_raise Test::Unit::AssertionFailedError do m.foo end
  851. end
  852. def test_ordered_default_expectations_can_be_overridden
  853. m = flexmock("m")
  854. m.should_receive(:foo).ordered.by_default
  855. m.should_receive(:bar).ordered.by_default
  856. m.should_receive(:bar).ordered
  857. m.should_receive(:foo).ordered
  858. m.bar
  859. m.foo
  860. end
  861. def test_by_default_works_at_mock_level
  862. m = flexmock("m",
  863. :foo => :bar,
  864. :pooh => :bear,
  865. :who => :dey).by_default
  866. m.should_receive(:pooh => :winnie)
  867. assert_equal :bar, m.foo
  868. assert_equal :dey, m.who
  869. assert_equal :winnie, m.pooh
  870. end
  871. def test_by_default_at_mock_level_does_nothing_with_no_expectations
  872. assert_nothing_raised do
  873. flexmock("m").by_default
  874. end
  875. end
  876. def test_partial_mocks_can_have_default_expectations
  877. obj = Object.new
  878. flexmock(obj).should_receive(:foo).and_return(:bar).by_default
  879. assert_equal :bar, obj.foo
  880. end
  881. def test_partial_mocks_can_have_default_expectations_overridden
  882. obj = Object.new
  883. flexmock(obj).should_receive(:foo).and_return(:bar).by_default
  884. flexmock(obj).should_receive(:foo).and_return(:baz)
  885. assert_equal :baz, obj.foo
  886. end
  887. def test_wicked_and_evil_tricks_with_by_default_are_thwarted
  888. mock = flexmock("mock")
  889. exp = mock.should_receive(:foo).and_return(:first).once
  890. mock.should_receive(:foo).and_return(:second)
  891. ex = assert_raise(FlexMock::UsageError) do
  892. exp.by_default
  893. end
  894. assert_match %r(previously defined), ex.message
  895. assert_equal :first, mock.foo
  896. assert_equal :second, mock.foo
  897. end
  898. def test_mocks_can_handle_multi_parameter_respond_tos
  899. mock = flexmock("a mock", :foo => :bar)
  900. assert mock.respond_to?(:foo)
  901. assert mock.respond_to?(:foo, true)
  902. assert mock.respond_to?(:foo, false)
  903. assert ! mock.respond_to?(:phoo)
  904. assert ! mock.respond_to?(:phoo, false)
  905. assert ! mock.respond_to?(:phoo, true)
  906. end
  907. def test_can_mock_operators
  908. assert_operator(:[]) { |m| m[1] }
  909. assert_operator(:[]=) { |m| m[1] = :value }
  910. assert_operator(:**) { |m| m ** :x }
  911. assert_operator(:+@) { |m| +m }
  912. assert_operator(:-@) { |m| -m }
  913. assert_operator(:+) { |m| m + :x }
  914. assert_operator(:-) { |m| m - :x }
  915. assert_operator(:*) { |m| m * :x }
  916. assert_operator(:"/") { |m| m / :x }
  917. assert_operator(:%) { |m| m % :x }
  918. assert_operator(:~) { |m| ~m } # )
  919. assert_operator(:&) { |m| m & :x }
  920. assert_operator(:|) { |m| m | :x }
  921. assert_operator(:^) { |m| m ^ :x }
  922. assert_operator(:<) { |m| m < :x }
  923. assert_operator(:>) { |m| m > :x }
  924. assert_operator(:>=) { |m| m >= :x }
  925. assert_operator(:<=) { |m| m <= :x }
  926. assert_operator(:==) { |m| m == :x }
  927. assert_operator(:===) { |m| m === :x }
  928. assert_operator(:<<) { |m| m << :x }
  929. assert_operator(:>>) { |m| m >> :x }
  930. assert_operator(:<=>) { |m| m <=> :x }
  931. assert_operator(:=~) { |m| m =~ :x }
  932. assert_operator(:"`") { |m| m.`("command") } # `
  933. end
  934. private
  935. def assert_operator(op, &block)
  936. m = flexmock("mock")
  937. m.should_receive(op).and_return(:value)
  938. assert_equal :value, block.call(m)
  939. end
  940. end
  941. class TestFlexMockShouldsWithInclude < Test::Unit::TestCase
  942. include FlexMock::ArgumentTypes
  943. def test_include_enables_unqualified_arg_type_references
  944. FlexMock.use("x") do |m|
  945. m.should_receive(:hi).with(any).once
  946. m.hi(1)
  947. end
  948. end
  949. end
  950. class TestFlexMockArgTypesDontLeak < Test::Unit::TestCase
  951. def test_unqualified_arg_type_references_are_undefined_by_default
  952. ex = assert_raise(NameError) do
  953. FlexMock.use("x") do |m|
  954. m.should_receive(:hi).with(any).once
  955. m.hi(1)
  956. end
  957. end
  958. assert_match(/\bany\b/, ex.message, "Error message should mention 'any'")
  959. end
  960. end