PageRenderTime 103ms CodeModel.GetById 26ms RepoModel.GetById 3ms app.codeStats 0ms

/test/shoulda/context_test.rb

https://github.com/rmm5t/shoulda-context
Ruby | 368 lines | 296 code | 66 blank | 6 comment | 2 complexity | b251069c9974487e7b81a699ae8b7c0d MD5 | raw file
  1. require 'test_helper'
  2. class ContextTest < Test::Unit::TestCase # :nodoc:
  3. def self.context_macro(&blk)
  4. context "with a subcontext made by a macro" do
  5. setup { @context_macro = :foo }
  6. merge_block &blk
  7. end
  8. end
  9. # def self.context_macro(&blk)
  10. # context "with a subcontext made by a macro" do
  11. # setup { @context_macro = :foo }
  12. # yield # <- this doesn't work.
  13. # end
  14. # end
  15. context "context with setup block" do
  16. setup do
  17. @blah = "blah"
  18. end
  19. should "run the setup block" do
  20. assert_equal "blah", @blah
  21. end
  22. should "have name set right" do
  23. assert_match(/^test: context with setup block/, self.to_s)
  24. end
  25. context "and a subcontext" do
  26. setup do
  27. @blah = "#{@blah} twice"
  28. end
  29. should "be named correctly" do
  30. assert_match(/^test: context with setup block and a subcontext should be named correctly/, self.to_s)
  31. end
  32. should "run the setup blocks in order" do
  33. assert_equal @blah, "blah twice"
  34. end
  35. end
  36. context_macro do
  37. should "have name set right" do
  38. assert_match(/^test: context with setup block with a subcontext made by a macro should have name set right/, self.to_s)
  39. end
  40. should "run the setup block of that context macro" do
  41. assert_equal :foo, @context_macro
  42. end
  43. should "run the setup block of the main context" do
  44. assert_equal "blah", @blah
  45. end
  46. end
  47. end
  48. context "another context with setup block" do
  49. setup do
  50. @blah = "foo"
  51. end
  52. should "have @blah == 'foo'" do
  53. assert_equal "foo", @blah
  54. end
  55. should "have name set right" do
  56. assert_match(/^test: another context with setup block/, self.to_s)
  57. end
  58. end
  59. context "context with method definition" do
  60. setup do
  61. def hello; "hi"; end
  62. end
  63. should "be able to read that method" do
  64. assert_equal "hi", hello
  65. end
  66. should "have name set right" do
  67. assert_match(/^test: context with method definition/, self.to_s)
  68. end
  69. end
  70. context "another context" do
  71. should "not define @blah" do
  72. assert_nil @blah
  73. end
  74. end
  75. context "context with multiple setups and/or teardowns" do
  76. cleanup_count = 0
  77. 2.times do |i|
  78. setup { cleanup_count += 1 }
  79. teardown { cleanup_count -= 1 }
  80. end
  81. 2.times do |i|
  82. should "call all setups and all teardowns (check ##{i + 1})" do
  83. assert_equal 2, cleanup_count
  84. end
  85. end
  86. context "subcontexts" do
  87. 2.times do |i|
  88. setup { cleanup_count += 1 }
  89. teardown { cleanup_count -= 1 }
  90. end
  91. 2.times do |i|
  92. should "also call all setups and all teardowns in parent and subcontext (check ##{i + 1})" do
  93. assert_equal 4, cleanup_count
  94. end
  95. end
  96. end
  97. end
  98. should_eventually "pass, since it's unimplemented" do
  99. flunk "what?"
  100. end
  101. should_eventually "not require a block when using should_eventually"
  102. should "pass without a block, as that causes it to piggyback to should_eventually"
  103. context "context for testing should piggybacking" do
  104. should "call should_eventually as we are not passing a block"
  105. end
  106. context "context" do
  107. context "with nested subcontexts" do
  108. should_eventually "only print this statement once for a should_eventually"
  109. end
  110. end
  111. class ::SomeModel; end
  112. context "given a test named after a class" do
  113. setup do
  114. self.class.stubs(:name).returns("SomeModelTest")
  115. end
  116. should "determine the described type" do
  117. assert_equal SomeModel, self.class.described_type
  118. end
  119. should "return a new instance of the described type as the subject if none exists" do
  120. assert_kind_of SomeModel, subject
  121. end
  122. context "with an explicit subject block" do
  123. setup { @expected = SomeModel.new }
  124. subject { @expected }
  125. should "return the result of the block as the subject" do
  126. assert_equal @expected, subject
  127. end
  128. context "nested context block without a subject block" do
  129. should "return the result of the parent context's subject block" do
  130. assert_equal @expected, subject
  131. end
  132. end
  133. end
  134. end
  135. end
  136. class ::Some
  137. class NestedModel; end
  138. end
  139. class Some::NestedModelTest < Test::Unit::TestCase
  140. should "determine the described type for a nested model" do
  141. assert_equal Some::NestedModel, self.class.described_type
  142. end
  143. end
  144. class ShouldMatcherTest < Test::Unit::TestCase
  145. class FakeMatcher
  146. attr_reader :subject
  147. attr_accessor :fail
  148. def description
  149. "do something"
  150. end
  151. def matches?(subject)
  152. @subject = subject
  153. !@fail
  154. end
  155. def failure_message
  156. "a failure message"
  157. end
  158. def negative_failure_message
  159. "not a failure message"
  160. end
  161. end
  162. def run_test
  163. @test_suite.run(@test_result) { |event, name |}
  164. end
  165. def setup
  166. @matcher = FakeMatcher.new
  167. @test_result = Test::Unit::TestResult.new
  168. class << @test_result
  169. def failure_messages
  170. @failures.map { |failure| failure.message }
  171. end
  172. end
  173. end
  174. def create_test_suite(&definition)
  175. test_class = Class.new(Test::Unit::TestCase, &definition)
  176. test_class.suite
  177. end
  178. def assert_failed_with(message, test_result)
  179. assert_equal 1, test_result.failure_count
  180. assert_equal [message], test_result.failure_messages
  181. end
  182. def assert_passed(test_result)
  183. assert_equal 0, test_result.failure_count
  184. end
  185. def assert_test_named(expected_name, test_suite)
  186. name = test_suite.tests.map { |test| test.method_name }.first
  187. assert name.include?(expected_name), "Expected #{name} to include #{expected_name}"
  188. end
  189. def self.should_use_positive_matcher
  190. should "generate a test using the matcher's description" do
  191. assert_test_named "should #{@matcher.description}", @test_suite
  192. end
  193. should "pass with a passing matcher" do
  194. @matcher.fail = false
  195. run_test
  196. assert_passed @test_result
  197. end
  198. should "fail with a failing matcher" do
  199. @matcher.fail = true
  200. run_test
  201. assert_failed_with @matcher.failure_message, @test_result
  202. end
  203. should "provide the subject" do
  204. @matcher.fail = false
  205. run_test
  206. assert_equal 'a subject', @matcher.subject
  207. end
  208. end
  209. def self.should_use_negative_matcher
  210. should "generate a test using the matcher's description" do
  211. assert_test_named "should not #{@matcher.description}", @test_suite
  212. end
  213. should "pass with a failing matcher" do
  214. @matcher.fail = true
  215. run_test
  216. assert_passed @test_result
  217. end
  218. should "fail with a passing matcher" do
  219. @matcher.fail = false
  220. run_test
  221. assert_failed_with @matcher.negative_failure_message, @test_result
  222. end
  223. should "provide the subject" do
  224. @matcher.fail = false
  225. run_test
  226. assert_equal 'a subject', @matcher.subject
  227. end
  228. end
  229. context "a should block with a matcher" do
  230. setup do
  231. matcher = @matcher
  232. @test_suite = create_test_suite do
  233. subject { 'a subject' }
  234. should matcher
  235. end
  236. end
  237. should_use_positive_matcher
  238. end
  239. context "a should block with a matcher within a context" do
  240. setup do
  241. matcher = @matcher
  242. @test_suite = create_test_suite do
  243. context "in context" do
  244. subject { 'a subject' }
  245. should matcher
  246. end
  247. end
  248. end
  249. should_use_positive_matcher
  250. end
  251. context "a should_not block with a matcher" do
  252. setup do
  253. matcher = @matcher
  254. @test_suite = create_test_suite do
  255. subject { 'a subject' }
  256. should_not matcher
  257. end
  258. end
  259. should_use_negative_matcher
  260. end
  261. context "a should_not block with a matcher within a context" do
  262. setup do
  263. matcher = @matcher
  264. @test_suite = create_test_suite do
  265. context "in context" do
  266. subject { 'a subject' }
  267. should_not matcher
  268. end
  269. end
  270. end
  271. should_use_negative_matcher
  272. end
  273. end
  274. class Subject; end
  275. class SubjectTest < Test::Unit::TestCase
  276. def setup
  277. @expected = Subject.new
  278. end
  279. subject { @expected }
  280. should "return a specified subject" do
  281. assert_equal @expected, subject
  282. end
  283. end
  284. class SubjectLazinessTest < Test::Unit::TestCase
  285. subject { Subject.new }
  286. should "only build the subject once" do
  287. assert_equal subject, subject
  288. end
  289. end