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

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