/tools/Ruby/lib/ruby/gems/1.8/gems/rake-0.9.2/test/test_rake_task_with_arguments.rb

http://github.com/agross/netopenspace · Ruby · 162 lines · 140 code · 22 blank · 0 comment · 0 complexity · b01513d54827cf14ac6e3f4672f9767a MD5 · raw file

  1. require File.expand_path('../helper', __FILE__)
  2. class TestRakeTaskWithArguments < Rake::TestCase
  3. include Rake
  4. def setup
  5. super
  6. Task.clear
  7. Rake::TaskManager.record_task_metadata = true
  8. end
  9. def teardown
  10. Rake::TaskManager.record_task_metadata = false
  11. super
  12. end
  13. def test_no_args_given
  14. t = task :t
  15. assert_equal [], t.arg_names
  16. end
  17. def test_args_given
  18. t = task :t, :a, :b
  19. assert_equal [:a, :b], t.arg_names
  20. end
  21. def test_name_and_needs
  22. t = task(:t => [:pre])
  23. assert_equal "t", t.name
  24. assert_equal [], t.arg_names
  25. assert_equal ["pre"], t.prerequisites
  26. end
  27. def test_name_args_and_explicit_needs
  28. ignore_deprecations do
  29. t = task(:t, :x, :y, :needs => [:pre])
  30. assert_equal "t", t.name
  31. assert_equal [:x, :y], t.arg_names
  32. assert_equal ["pre"], t.prerequisites
  33. end
  34. end
  35. def test_illegal_keys_in_task_name_hash
  36. ignore_deprecations do
  37. assert_raises RuntimeError do
  38. t = task(:t, :x, :y => 1, :needs => [:pre])
  39. end
  40. end
  41. end
  42. def test_arg_list_is_empty_if_no_args_given
  43. t = task(:t) { |tt, args| assert_equal({}, args.to_hash) }
  44. t.invoke(1, 2, 3)
  45. end
  46. def test_tasks_can_access_arguments_as_hash
  47. t = task :t, :a, :b, :c do |tt, args|
  48. assert_equal({:a => 1, :b => 2, :c => 3}, args.to_hash)
  49. assert_equal 1, args[:a]
  50. assert_equal 2, args[:b]
  51. assert_equal 3, args[:c]
  52. assert_equal 1, args.a
  53. assert_equal 2, args.b
  54. assert_equal 3, args.c
  55. end
  56. t.invoke(1, 2, 3)
  57. end
  58. def test_actions_of_various_arity_are_ok_with_args
  59. notes = []
  60. t = task(:t, :x) do
  61. notes << :a
  62. end
  63. t.enhance do | |
  64. notes << :b
  65. end
  66. t.enhance do |task|
  67. notes << :c
  68. assert_kind_of Task, task
  69. end
  70. t.enhance do |t2, args|
  71. notes << :d
  72. assert_equal t, t2
  73. assert_equal({:x => 1}, args.to_hash)
  74. end
  75. t.invoke(1)
  76. assert_equal [:a, :b, :c, :d], notes
  77. end
  78. def test_arguments_are_passed_to_block
  79. t = task(:t, :a, :b) { |tt, args|
  80. assert_equal( { :a => 1, :b => 2 }, args.to_hash )
  81. }
  82. t.invoke(1, 2)
  83. end
  84. def test_extra_parameters_are_ignored
  85. t = task(:t, :a) { |tt, args|
  86. assert_equal 1, args.a
  87. assert_nil args.b
  88. }
  89. t.invoke(1, 2)
  90. end
  91. def test_arguments_are_passed_to_all_blocks
  92. counter = 0
  93. t = task :t, :a
  94. task :t do |tt, args|
  95. assert_equal 1, args.a
  96. counter += 1
  97. end
  98. task :t do |tt, args|
  99. assert_equal 1, args.a
  100. counter += 1
  101. end
  102. t.invoke(1)
  103. assert_equal 2, counter
  104. end
  105. def test_block_with_no_parameters_is_ok
  106. t = task(:t) { }
  107. t.invoke(1, 2)
  108. end
  109. def test_name_with_args
  110. desc "T"
  111. t = task(:tt, :a, :b)
  112. assert_equal "tt", t.name
  113. assert_equal "T", t.comment
  114. assert_equal "[a,b]", t.arg_description
  115. assert_equal "tt[a,b]", t.name_with_args
  116. assert_equal [:a, :b],t.arg_names
  117. end
  118. def test_named_args_are_passed_to_prereqs
  119. value = nil
  120. pre = task(:pre, :rev) { |t, args| value = args.rev }
  121. t = task(:t, [:name, :rev] => [:pre])
  122. t.invoke("bill", "1.2")
  123. assert_equal "1.2", value
  124. end
  125. def test_args_not_passed_if_no_prereq_names
  126. pre = task(:pre) { |t, args|
  127. assert_equal({}, args.to_hash)
  128. assert_equal "bill", args.name
  129. }
  130. t = task(:t, [:name, :rev] => [:pre])
  131. t.invoke("bill", "1.2")
  132. end
  133. def test_args_not_passed_if_no_arg_names
  134. pre = task(:pre, :rev) { |t, args|
  135. assert_equal({}, args.to_hash)
  136. }
  137. t = task(:t => [:pre])
  138. t.invoke("bill", "1.2")
  139. end
  140. end