PageRenderTime 93ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/External.LCA_RESTRICTED/Languages/Ruby/ruby19/lib/ruby/gems/1.9.1/gems/shoulda-2.11.3/test/other/context_test.rb

http://github.com/IronLanguages/main
Ruby | 372 lines | 301 code | 65 blank | 6 comment | 1 complexity | c1c7cb4adc0448f3e6cf5a0b80a6246c MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. require 'test_helper'
  2. class ContextTest < ActiveSupport::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 ShouldMatcherTest < Test::Unit::TestCase
  137. class FakeMatcher
  138. attr_reader :subject
  139. attr_accessor :fail
  140. def description
  141. "do something"
  142. end
  143. def matches?(subject)
  144. @subject = subject
  145. !@fail
  146. end
  147. def failure_message
  148. "a failure message"
  149. end
  150. def negative_failure_message
  151. "not a failure message"
  152. end
  153. end
  154. def run_test
  155. @test_suite.run(@test_result) { |event, name |}
  156. end
  157. def setup
  158. @matcher = FakeMatcher.new
  159. @test_result = Test::Unit::TestResult.new
  160. class << @test_result
  161. def failure_messages
  162. @failures.map { |failure| failure.message }
  163. end
  164. end
  165. end
  166. def create_test_suite(&definition)
  167. test_class = Class.new(Test::Unit::TestCase, &definition)
  168. test_class.suite
  169. end
  170. def assert_failed_with(message, test_result)
  171. assert_equal 1, test_result.failure_count
  172. assert_equal [message], test_result.failure_messages
  173. end
  174. def assert_passed(test_result)
  175. assert_equal 0, test_result.failure_count
  176. end
  177. def assert_test_named(expected_name, test_suite)
  178. name = test_suite.tests.map { |test| test.method_name }.first
  179. assert name.include?(expected_name), "Expected #{name} to include #{expected_name}"
  180. end
  181. def self.should_use_positive_matcher
  182. should "generate a test using the matcher's description" do
  183. assert_test_named "should #{@matcher.description}", @test_suite
  184. end
  185. should "pass with a passing matcher" do
  186. @matcher.fail = false
  187. run_test
  188. assert_passed @test_result
  189. end
  190. should "fail with a failing matcher" do
  191. @matcher.fail = true
  192. run_test
  193. assert_failed_with @matcher.failure_message, @test_result
  194. end
  195. should "provide the subject" do
  196. @matcher.fail = false
  197. run_test
  198. assert_equal 'a subject', @matcher.subject
  199. end
  200. end
  201. def self.should_use_negative_matcher
  202. should "generate a test using the matcher's description" do
  203. assert_test_named "should not #{@matcher.description}", @test_suite
  204. end
  205. should "pass with a failing matcher" do
  206. @matcher.fail = true
  207. run_test
  208. assert_passed @test_result
  209. end
  210. should "fail with a passing matcher" do
  211. @matcher.fail = false
  212. run_test
  213. assert_failed_with @matcher.negative_failure_message, @test_result
  214. end
  215. should "provide the subject" do
  216. @matcher.fail = false
  217. run_test
  218. assert_equal 'a subject', @matcher.subject
  219. end
  220. end
  221. context "a should block with a matcher" do
  222. setup do
  223. matcher = @matcher
  224. @test_suite = create_test_suite do
  225. subject { 'a subject' }
  226. should matcher
  227. end
  228. end
  229. should_use_positive_matcher
  230. end
  231. context "a should block with a matcher within a context" do
  232. setup do
  233. matcher = @matcher
  234. @test_suite = create_test_suite do
  235. context "in context" do
  236. subject { 'a subject' }
  237. should matcher
  238. end
  239. end
  240. end
  241. should_use_positive_matcher
  242. end
  243. context "a should_not block with a matcher" do
  244. setup do
  245. matcher = @matcher
  246. @test_suite = create_test_suite do
  247. subject { 'a subject' }
  248. should_not matcher
  249. end
  250. end
  251. should_use_negative_matcher
  252. end
  253. context "a should_not block with a matcher within a context" do
  254. setup do
  255. matcher = @matcher
  256. @test_suite = create_test_suite do
  257. context "in context" do
  258. subject { 'a subject' }
  259. should_not matcher
  260. end
  261. end
  262. end
  263. should_use_negative_matcher
  264. end
  265. end
  266. class Subject; end
  267. class SubjectTest < ActiveSupport::TestCase
  268. def setup
  269. @expected = Subject.new
  270. end
  271. subject { @expected }
  272. should "return a specified subject" do
  273. assert_equal @expected, subject
  274. end
  275. end
  276. class SubjectLazinessTest < ActiveSupport::TestCase
  277. subject { Subject.new }
  278. should "only build the subject once" do
  279. assert_equal subject, subject
  280. end
  281. end
  282. class SomeController < ActionController::Base
  283. end
  284. class ControllerSubjectTest < ActionController::TestCase
  285. tests SomeController
  286. should "use the controller as the subject outside a context" do
  287. assert_equal @controller, subject
  288. end
  289. context "in a context" do
  290. should "use the controller as the subject" do
  291. assert_equal @controller, subject
  292. end
  293. end
  294. end