PageRenderTime 54ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/bundle/ruby/1.9.1/gems/capistrano-2.8.0/test/configuration/namespace_dsl_test.rb

https://github.com/bublanina/masterlanguage
Ruby | 312 lines | 256 code | 55 blank | 1 comment | 2 complexity | ec76fa55947e7d311c05a4890aab5a93 MD5 | raw file
  1. # -*- encoding : utf-8 -*-
  2. require "utils"
  3. require 'capistrano/configuration/namespaces'
  4. class ConfigurationNamespacesDSLTest < Test::Unit::TestCase
  5. class MockConfig
  6. attr_reader :original_initialize_called, :options
  7. def initialize
  8. @original_initialize_called = true
  9. @options = {}
  10. end
  11. include Capistrano::Configuration::Namespaces
  12. end
  13. def setup
  14. @config = MockConfig.new
  15. end
  16. def test_initialize_should_initialize_collections
  17. assert @config.original_initialize_called
  18. assert @config.tasks.empty?
  19. assert @config.namespaces.empty?
  20. end
  21. def test_unqualified_task_should_define_task_at_top_namespace
  22. assert !@config.tasks.key?(:testing)
  23. @config.task(:testing) { puts "something" }
  24. assert @config.tasks.key?(:testing)
  25. end
  26. def test_qualification_should_define_task_within_namespace
  27. @config.namespace(:testing) do
  28. task(:nested) { puts "nested" }
  29. end
  30. assert !@config.tasks.key?(:nested)
  31. assert @config.namespaces.key?(:testing)
  32. assert @config.namespaces[:testing].tasks.key?(:nested)
  33. end
  34. def test_namespace_within_namespace_should_define_task_within_nested_namespace
  35. @config.namespace :outer do
  36. namespace :inner do
  37. task :nested do
  38. puts "nested"
  39. end
  40. end
  41. end
  42. assert !@config.tasks.key?(:nested)
  43. assert @config.namespaces.key?(:outer)
  44. assert @config.namespaces[:outer].namespaces.key?(:inner)
  45. assert @config.namespaces[:outer].namespaces[:inner].tasks.key?(:nested)
  46. end
  47. def test_pending_desc_should_apply_only_to_immediately_subsequent_task
  48. @config.desc "A description"
  49. @config.task(:testing) { puts "foo" }
  50. @config.task(:another) { puts "bar" }
  51. assert_equal "A description", @config.tasks[:testing].desc
  52. assert_nil @config.tasks[:another].desc
  53. end
  54. def test_pending_desc_should_apply_only_to_next_task_in_any_namespace
  55. @config.desc "A description"
  56. @config.namespace(:outer) { task(:testing) { puts "foo" } }
  57. assert_equal "A description", @config.namespaces[:outer].tasks[:testing].desc
  58. end
  59. def test_defining_task_without_block_should_raise_error
  60. assert_raises(ArgumentError) do
  61. @config.task(:testing)
  62. end
  63. end
  64. def test_defining_task_that_shadows_existing_method_should_raise_error
  65. assert_raises(ArgumentError) do
  66. @config.task(:sprintf) { puts "foo" }
  67. end
  68. end
  69. def test_defining_task_that_shadows_existing_namespace_should_raise_error
  70. @config.namespace(:outer) {}
  71. assert_raises(ArgumentError) do
  72. @config.task(:outer) { puts "foo" }
  73. end
  74. end
  75. def test_defining_namespace_that_shadows_existing_method_should_raise_error
  76. assert_raises(ArgumentError) do
  77. @config.namespace(:sprintf) {}
  78. end
  79. end
  80. def test_defining_namespace_that_shadows_existing_task_should_raise_error
  81. @config.task(:testing) { puts "foo" }
  82. assert_raises(ArgumentError) do
  83. @config.namespace(:testing) {}
  84. end
  85. end
  86. def test_defining_task_that_shadows_existing_task_should_not_raise_error
  87. @config.task(:original) { puts "foo" }
  88. assert_nothing_raised do
  89. @config.task(:original) { puts "bar" }
  90. end
  91. end
  92. def test_defining_ask_should_add_task_as_method
  93. assert !@config.methods.any? { |m| m.to_sym == :original }
  94. @config.task(:original) { puts "foo" }
  95. assert @config.methods.any? { |m| m.to_sym == :original }
  96. end
  97. def test_calling_defined_task_should_delegate_to_execute_task
  98. @config.task(:original) { puts "foo" }
  99. @config.expects(:execute_task).with(@config.tasks[:original])
  100. @config.original
  101. end
  102. def test_role_inside_namespace_should_raise_error
  103. assert_raises(NotImplementedError) do
  104. @config.namespace(:outer) do
  105. role :app, "hello"
  106. end
  107. end
  108. end
  109. def test_name_for_top_level_should_be_nil
  110. assert_nil @config.name
  111. end
  112. def test_parent_for_top_level_should_be_nil
  113. assert_nil @config.parent
  114. end
  115. def test_fqn_for_top_level_should_be_nil
  116. assert_nil @config.fully_qualified_name
  117. end
  118. def test_fqn_for_namespace_should_be_the_name_of_the_namespace
  119. @config.namespace(:outer) {}
  120. assert_equal "outer", @config.namespaces[:outer].fully_qualified_name
  121. end
  122. def test_parent_for_namespace_should_be_the_top_level
  123. @config.namespace(:outer) {}
  124. assert_equal @config, @config.namespaces[:outer].parent
  125. end
  126. def test_fqn_for_nested_namespace_should_be_color_delimited
  127. @config.namespace(:outer) { namespace(:inner) {} }
  128. assert_equal "outer:inner", @config.namespaces[:outer].namespaces[:inner].fully_qualified_name
  129. end
  130. def test_parent_for_nested_namespace_should_be_the_nesting_namespace
  131. @config.namespace(:outer) { namespace(:inner) {} }
  132. assert_equal @config.namespaces[:outer], @config.namespaces[:outer].namespaces[:inner].parent
  133. end
  134. def test_find_task_should_dereference_nested_tasks
  135. @config.namespace(:outer) do
  136. namespace(:inner) { task(:nested) { puts "nested" } }
  137. end
  138. task = @config.find_task("outer:inner:nested")
  139. assert_not_nil task
  140. assert_equal "outer:inner:nested", task.fully_qualified_name
  141. end
  142. def test_find_task_should_return_nil_if_no_task_matches
  143. assert_nil @config.find_task("outer:inner:nested")
  144. end
  145. def test_find_task_should_return_default_if_deferences_to_namespace_and_namespace_has_default
  146. @config.namespace(:outer) do
  147. namespace(:inner) { task(:default) { puts "nested" } }
  148. end
  149. task = @config.find_task("outer:inner")
  150. assert_not_nil task
  151. assert_equal :default, task.name
  152. assert_equal "outer:inner", task.namespace.fully_qualified_name
  153. end
  154. def test_find_task_should_return_nil_if_deferences_to_namespace_and_namespace_has_no_default
  155. @config.namespace(:outer) do
  156. namespace(:inner) { task(:nested) { puts "nested" } }
  157. end
  158. assert_nil @config.find_task("outer:inner")
  159. end
  160. def test_default_task_should_return_nil_for_top_level
  161. @config.task(:default) {}
  162. assert_nil @config.default_task
  163. end
  164. def test_default_task_should_return_nil_for_namespace_without_default
  165. @config.namespace(:outer) { task(:nested) { puts "nested" } }
  166. assert_nil @config.namespaces[:outer].default_task
  167. end
  168. def test_default_task_should_return_task_for_namespace_with_default
  169. @config.namespace(:outer) { task(:default) { puts "nested" } }
  170. task = @config.namespaces[:outer].default_task
  171. assert_not_nil task
  172. assert_equal :default, task.name
  173. end
  174. def test_task_list_should_return_only_tasks_immediately_within_namespace
  175. @config.task(:first) { puts "here" }
  176. @config.namespace(:outer) do
  177. task(:second) { puts "here" }
  178. namespace(:inner) do
  179. task(:third) { puts "here" }
  180. end
  181. end
  182. assert_equal %w(first), @config.task_list.map { |t| t.fully_qualified_name }
  183. end
  184. def test_task_list_with_all_should_return_all_tasks_under_this_namespace_recursively
  185. @config.task(:first) { puts "here" }
  186. @config.namespace(:outer) do
  187. task(:second) { puts "here" }
  188. namespace(:inner) do
  189. task(:third) { puts "here" }
  190. end
  191. end
  192. assert_equal %w(first outer:inner:third outer:second), @config.task_list(:all).map { |t| t.fully_qualified_name }.sort
  193. end
  194. def test_namespace_should_respond_to_its_parents_methods
  195. @config.namespace(:outer) {}
  196. ns = @config.namespaces[:outer]
  197. assert ns.respond_to?(:original_initialize_called)
  198. end
  199. def test_namespace_should_accept_respond_to_with_include_priv_parameter
  200. @config.namespace(:outer) {}
  201. ns = @config.namespaces[:outer]
  202. assert ns.respond_to?(:original_initialize_called, true)
  203. end
  204. def test_namespace_should_delegate_unknown_messages_to_its_parent
  205. @config.namespace(:outer) {}
  206. ns = @config.namespaces[:outer]
  207. assert ns.original_initialize_called
  208. end
  209. def test_namespace_should_not_understand_messages_that_neither_it_nor_its_parent_understands
  210. @config.namespace(:outer) {}
  211. ns = @config.namespaces[:outer]
  212. assert_raises(NoMethodError) { ns.alskdfjlsf }
  213. end
  214. def test_search_task_should_find_tasks_in_current_namespace
  215. @config.namespace(:outer) do
  216. namespace(:inner) do
  217. task(:third) { puts "here" }
  218. end
  219. end
  220. inner = @config.namespaces[:outer].namespaces[:inner]
  221. assert_equal inner.tasks[:third], inner.search_task(:third)
  222. end
  223. def test_search_task_should_find_tasks_in_parent_namespace
  224. @config.task(:first) { puts "here" }
  225. @config.namespace(:outer) do
  226. task(:second) { puts "here" }
  227. namespace(:inner) do
  228. task(:third) { puts "here" }
  229. end
  230. end
  231. inner = @config.namespaces[:outer].namespaces[:inner]
  232. assert_equal @config.tasks[:first], inner.search_task(:first)
  233. end
  234. def test_search_task_should_return_nil_if_no_tasks_are_found
  235. @config.namespace(:outer) { namespace(:inner) {} }
  236. inner = @config.namespaces[:outer].namespaces[:inner]
  237. assert_nil inner.search_task(:first)
  238. end
  239. def test_top_should_return_self_if_self_is_top
  240. assert_equal @config, @config.top
  241. end
  242. def test_top_should_return_parent_if_parent_is_top
  243. @config.namespace(:outer) {}
  244. assert_equal @config, @config.namespaces[:outer].top
  245. end
  246. def test_top_should_return_topmost_parent_if_self_is_deeply_nested
  247. @config.namespace(:outer) { namespace(:middle) { namespace(:inner) {} } }
  248. assert_equal @config, @config.namespaces[:outer].namespaces[:middle].namespaces[:inner].top
  249. end
  250. def test_find_task_should_return_nil_when_empty_inner_task
  251. @config.namespace :outer do
  252. namespace :inner do
  253. end
  254. end
  255. assert_nil @config.find_task("outer::inner")
  256. end
  257. end