PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/test/test/unit/context_test.rb

https://github.com/kares/test-unit-context
Ruby | 195 lines | 181 code | 13 blank | 1 comment | 0 complexity | 5fca44751515a59fb0d9be6ec722ad8e MD5 | raw file
Possible License(s): Apache-2.0
  1. require File.expand_path('../../test_helper', File.dirname(__FILE__))
  2. module Test::Unit
  3. class TestContext < Test::Unit::TestCase
  4. def test_test_without_context
  5. assert true
  6. end
  7. test "another test without a context" do
  8. assert true
  9. end
  10. def test_context_aliases
  11. assert self.class.respond_to? :context
  12. assert self.class.respond_to? :contexts
  13. end
  14. def test_context_naming
  15. Class.new(Test::Unit::TestCase) do
  16. context("kiss = simple_name") {}
  17. context("a_(not-so)_simple; name") {}
  18. context("an '&\" name !?") {}
  19. end
  20. end
  21. class Default < Test::Unit::TestCase
  22. @_context_ = context "When testing" do
  23. def test_this_thing
  24. true
  25. end
  26. end
  27. context "More testing" do
  28. # @todo implement more tests here ...
  29. end
  30. end
  31. setup do
  32. @default_context = Default.instance_variable_get :@_context_
  33. end
  34. test "[default context] sets the context name" do
  35. assert_equal "When testing", @default_context.context_name
  36. end
  37. test "[default context] is a Test::Unit::TestCase" do
  38. assert @default_context.ancestors.include?(Test::Unit::TestCase)
  39. end
  40. test "[default context] is defived from the test class" do
  41. assert_equal Default, @default_context.superclass
  42. end
  43. test "[default context] reports among test case's context defs" do
  44. assert Default.respond_to?(:context_definitions)
  45. assert_include Default.context_definitions, @default_context
  46. assert_equal 2, Default.context_definitions.size
  47. end
  48. test "has a (context name derived) class name" do
  49. namespace = 'Test::Unit::TestContext::Default::'
  50. assert_equal "#{namespace}ContextWhenTesting", @default_context.name
  51. end
  52. class Anonymous < Test::Unit::TestCase
  53. @@_context_ = self.context do
  54. def test_some_thing
  55. true
  56. end
  57. end
  58. context do
  59. test 'another_thing' do
  60. end
  61. end
  62. context do
  63. #
  64. end
  65. end
  66. setup do
  67. @anonymous_context = Anonymous.send :class_variable_get, :@@_context_
  68. end
  69. test "[anonymous context] has a generated context name" do
  70. assert_not_nil @anonymous_context.name
  71. end
  72. test "[anonymous context] is defived from the test class" do
  73. assert_equal Anonymous, @anonymous_context.superclass
  74. end
  75. test "[anonymous context] reports among test case's context defs" do
  76. assert Anonymous.respond_to?(:context_definitions)
  77. assert_include Anonymous.context_definitions, @anonymous_context
  78. assert_equal 3, Anonymous.context_definitions.size
  79. assert_equal 3, Anonymous.context_definitions(true).size
  80. end
  81. test "[anonymous context] has a (context name derived) class name" do
  82. namespace = 'Test::Unit::TestContext::Anonymous::'
  83. context_name = @anonymous_context.context_name
  84. assert_equal "#{namespace}Context#{context_name}", @anonymous_context.name
  85. end
  86. class Nested < Test::Unit::TestCase
  87. @@_context_ = context "and we're testing" do
  88. @nested = context "should be nested" do
  89. def test_a_thing
  90. true
  91. end
  92. end
  93. def self.nested; @nested; end
  94. end
  95. end
  96. setup do
  97. @parent_context = Nested.send :class_variable_get, :@@_context_
  98. @nested_context = @parent_context.nested
  99. end
  100. test "[nested context] sets a nested context name" do
  101. assert_equal "and we're testing should be nested", @nested_context.context_name
  102. end
  103. test "[nested context] is also a Test::Unit::TestCase" do
  104. assert @nested_context.ancestors.include?(Test::Unit::TestCase)
  105. end
  106. test "[nested context] is defived from the prev context class" do
  107. assert_equal @parent_context, @nested_context.superclass
  108. end
  109. test "[nested context] reports context defs correctly" do
  110. assert Nested.respond_to?(:context_definitions)
  111. assert_equal 1, Nested.context_definitions.size
  112. assert_equal 1, @parent_context.context_definitions.size
  113. assert_equal 0, @nested_context.context_definitions.size
  114. assert_equal 2, Nested.context_definitions(true).size
  115. end
  116. test "[nested context] has a (context name derived) class name" do
  117. namespace = 'Test::Unit::TestContext::Nested::'
  118. assert_equal "#{namespace}ContextAndWeReTesting::ContextShouldBeNested",
  119. @nested_context.name
  120. end
  121. class Redefined < Test::Unit::TestCase
  122. CONTEXT = context 42 do
  123. def test_everything
  124. true
  125. end
  126. end
  127. @@warns = nil
  128. def self.warn(message)
  129. ( @@warns ||= [] ) << message
  130. end
  131. def self.warns; @@warns; end
  132. def self.reset_warns; @@warns = nil; end
  133. end
  134. setup do
  135. @redefined_context = Redefined::CONTEXT
  136. end
  137. test "[redefined context] sets the context name" do
  138. assert_equal 42, @redefined_context.context_name
  139. end
  140. test "[redefined context] warns when same context name used" do
  141. assert_nil Redefined.warns
  142. class Redefined
  143. context 42 do
  144. def test_something_else
  145. assert true
  146. end
  147. end
  148. end
  149. assert_not_nil Redefined.warns
  150. assert_equal 2, @redefined_context.instance_methods(false).grep(/test/).size
  151. Redefined.reset_warns
  152. assert_nil Redefined.warns
  153. class Redefined
  154. context '42' do # same class-name
  155. def test_a_little_thing
  156. end
  157. end
  158. end
  159. assert_not_nil Redefined.warns
  160. assert_equal 3, @redefined_context.instance_methods(false).grep(/test/).size
  161. end
  162. end
  163. end