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

/vendor/plugins/has_markup/vendor/gems/thoughtbot-shoulda-2.0.4/test/other/should_test.rb

https://github.com/technicalpickles/flockup
Ruby | 266 lines | 242 code | 24 blank | 0 comment | 2 complexity | 4c90cf5f4ef742003dc1bf24da185af8 MD5 | raw file
Possible License(s): GPL-2.0
  1. require File.join(File.dirname(__FILE__), '..', 'test_helper')
  2. class ShouldTest < Test::Unit::TestCase # :nodoc:
  3. should "be able to define a should statement outside of a context" do
  4. assert true
  5. end
  6. should "see the name of my class as ShouldTest" do
  7. assert_equal "ShouldTest", self.class.name
  8. end
  9. def self.should_see_class_methods
  10. should "be able to see class methods" do
  11. assert true
  12. end
  13. end
  14. def self.should_see_a_context_block_like_a_Test_Unit_class
  15. should "see a context block as a Test::Unit class" do
  16. assert_equal "ShouldTest", self.class.name
  17. end
  18. end
  19. def self.should_see_blah
  20. should "see @blah through a macro" do
  21. assert @blah
  22. end
  23. end
  24. def self.should_not_see_blah
  25. should "not see @blah through a macro" do
  26. assert_nil @blah
  27. end
  28. end
  29. def self.should_be_able_to_make_context_macros(prefix = nil)
  30. context "a macro" do
  31. should "have the tests named correctly" do
  32. assert_match(/^test: #{prefix}a macro should have the tests named correctly/, self.to_s)
  33. end
  34. end
  35. end
  36. context "Context" do
  37. should_see_class_methods
  38. should_see_a_context_block_like_a_Test_Unit_class
  39. should_be_able_to_make_context_macros("Context ")
  40. should "not define @blah" do
  41. assert ! self.instance_variables.include?("@blah")
  42. end
  43. should_not_see_blah
  44. should "be able to define a should statement" do
  45. assert true
  46. end
  47. should "see the name of my class as ShouldTest" do
  48. assert_equal "ShouldTest", self.class.name
  49. end
  50. context "with a subcontext" do
  51. should_be_able_to_make_context_macros("Context with a subcontext ")
  52. end
  53. end
  54. context "Context with setup block" do
  55. setup do
  56. @blah = "blah"
  57. end
  58. should "have @blah == 'blah'" do
  59. assert_equal "blah", @blah
  60. end
  61. should_see_blah
  62. should "have name set right" do
  63. assert_match(/^test: Context with setup block/, self.to_s)
  64. end
  65. context "and a subcontext" do
  66. setup do
  67. @blah = "#{@blah} twice"
  68. end
  69. should "be named correctly" do
  70. assert_match(/^test: Context with setup block and a subcontext should be named correctly/, self.to_s)
  71. end
  72. should "run the setup methods in order" do
  73. assert_equal @blah, "blah twice"
  74. end
  75. should_see_blah
  76. end
  77. end
  78. context "Another context with setup block" do
  79. setup do
  80. @blah = "foo"
  81. end
  82. should "have @blah == 'foo'" do
  83. assert_equal "foo", @blah
  84. end
  85. should "have name set right" do
  86. assert_match(/^test: Another context with setup block/, self.to_s)
  87. end
  88. should_see_blah
  89. end
  90. should_eventually "pass, since it's a should_eventually" do
  91. flunk "what?"
  92. end
  93. # Context creation and naming
  94. def test_should_create_a_new_context
  95. assert_nothing_raised do
  96. Thoughtbot::Shoulda::Context.new("context name", self) do; end
  97. end
  98. end
  99. def test_should_create_a_nested_context
  100. assert_nothing_raised do
  101. parent = Thoughtbot::Shoulda::Context.new("Parent", self) do; end
  102. child = Thoughtbot::Shoulda::Context.new("Child", parent) do; end
  103. end
  104. end
  105. def test_should_name_a_contexts_correctly
  106. parent = Thoughtbot::Shoulda::Context.new("Parent", self) do; end
  107. child = Thoughtbot::Shoulda::Context.new("Child", parent) do; end
  108. grandchild = Thoughtbot::Shoulda::Context.new("GrandChild", child) do; end
  109. assert_equal "Parent", parent.full_name
  110. assert_equal "Parent Child", child.full_name
  111. assert_equal "Parent Child GrandChild", grandchild.full_name
  112. end
  113. # Should statements
  114. def test_should_have_should_hashes_when_given_should_statements
  115. context = Thoughtbot::Shoulda::Context.new("name", self) do
  116. should "be good" do; end
  117. should "another" do; end
  118. end
  119. names = context.shoulds.map {|s| s[:name]}
  120. assert_equal ["another", "be good"], names.sort
  121. end
  122. # setup and teardown
  123. def test_should_capture_setup_and_teardown_blocks
  124. context = Thoughtbot::Shoulda::Context.new("name", self) do
  125. setup do; "setup"; end
  126. teardown do; "teardown"; end
  127. end
  128. assert_equal "setup", context.setup_blocks.first.call
  129. assert_equal "teardown", context.teardown_blocks.first.call
  130. end
  131. # building
  132. def test_should_create_shoulda_test_for_each_should_on_build
  133. context = Thoughtbot::Shoulda::Context.new("name", self) do
  134. should "one" do; end
  135. should "two" do; end
  136. end
  137. context.expects(:create_test_from_should_hash).with(has_entry(:name => "one"))
  138. context.expects(:create_test_from_should_hash).with(has_entry(:name => "two"))
  139. context.build
  140. end
  141. def test_should_create_test_methods_on_build
  142. tu_class = Test::Unit::TestCase
  143. context = Thoughtbot::Shoulda::Context.new("A Context", tu_class) do
  144. should "define the test" do; end
  145. end
  146. tu_class.expects(:define_method).with(:"test: A Context should define the test. ")
  147. context.build
  148. end
  149. def test_should_create_test_methods_on_build_when_subcontext
  150. tu_class = Test::Unit::TestCase
  151. context = Thoughtbot::Shoulda::Context.new("A Context", tu_class) do
  152. context "with a child" do
  153. should "define the test" do; end
  154. end
  155. end
  156. tu_class.expects(:define_method).with(:"test: A Context with a child should define the test. ")
  157. context.build
  158. end
  159. # Test::Unit integration
  160. def test_should_create_a_new_context_and_build_it_on_Test_Unit_context
  161. c = mock("context")
  162. c.expects(:build)
  163. Thoughtbot::Shoulda::Context.expects(:new).with("foo", kind_of(Class)).returns(c)
  164. self.class.context "foo" do; end
  165. end
  166. def test_should_create_a_one_off_context_and_build_it_on_Test_Unit_should
  167. s = mock("test")
  168. Thoughtbot::Shoulda::Context.any_instance.expects(:should).with("rock", {}).returns(s)
  169. Thoughtbot::Shoulda::Context.any_instance.expects(:build)
  170. self.class.should "rock" do; end
  171. end
  172. def test_should_create_a_one_off_context_and_build_it_on_Test_Unit_should_eventually
  173. s = mock("test")
  174. Thoughtbot::Shoulda::Context.any_instance.expects(:should_eventually).with("rock").returns(s)
  175. Thoughtbot::Shoulda::Context.any_instance.expects(:build)
  176. self.class.should_eventually "rock" do; end
  177. end
  178. should "run a :before proc", :before => lambda { @value = "before" } do
  179. assert_equal "before", @value
  180. end
  181. context "A :before proc" do
  182. setup do
  183. assert_equal "before", @value
  184. @value = "setup"
  185. end
  186. should "run before the current setup", :before => lambda { @value = "before" } do
  187. assert_equal "setup", @value
  188. end
  189. end
  190. context "a before statement" do
  191. setup do
  192. assert_equal "before", @value
  193. @value = "setup"
  194. end
  195. before_should "run before the current setup" do
  196. @value = "before"
  197. end
  198. end
  199. context "A context" do
  200. setup do
  201. @value = "outer"
  202. end
  203. context "with a subcontext and a :before proc" do
  204. before = lambda do
  205. assert "outer", @value
  206. @value = "before"
  207. end
  208. should "run after the parent setup", :before => before do
  209. assert_equal "before", @value
  210. end
  211. end
  212. end
  213. end