|
| 1 | +# frozen_string_literal: true |
| 2 | +require File.expand_path("../helper", __FILE__) |
| 3 | + |
| 4 | +class TestRakeLazyTaskDefinition < Rake::TestCase # :nodoc: |
| 5 | + |
| 6 | + def setup |
| 7 | + super |
| 8 | + |
| 9 | + @tm = Rake::TestCase::TaskManager.new |
| 10 | + @tm.extend Rake::LazyTaskDefinition |
| 11 | + end |
| 12 | + |
| 13 | + def test_lazy_definition |
| 14 | + t1, t2, t3 = nil, nil, nil |
| 15 | + lazy_definition_call_count = 0 |
| 16 | + @tm.in_namespace("a1") do |
| 17 | + @tm.register_lazy_definition do |
| 18 | + t1 = @tm.define_task(Rake::Task, :t1) |
| 19 | + lazy_definition_call_count += 1 |
| 20 | + @tm.in_namespace("a2") do |
| 21 | + t2 = @tm.define_task(Rake::Task, :t2) |
| 22 | + end |
| 23 | + end |
| 24 | + end |
| 25 | + @tm.in_namespace("b") do |
| 26 | + t3 = @tm.define_task(Rake::Task, :t3) |
| 27 | + end |
| 28 | + # task t3 is not lazy. It can be found |
| 29 | + assert_equal t3, @tm[:t3, Rake::Scope.make("b")] |
| 30 | + # lazy definition is not called until we look for task in namespace a |
| 31 | + assert_equal lazy_definition_call_count, 0 |
| 32 | + |
| 33 | + # task t2 can be found |
| 34 | + found_task_t2 = @tm[:t2, Rake::Scope.make("a1:a2")] |
| 35 | + assert_equal t2, found_task_t2 |
| 36 | + # lazy definition is expected to be called |
| 37 | + assert_equal lazy_definition_call_count, 1 |
| 38 | + |
| 39 | + # task t1 can also be found |
| 40 | + found_task_t1 = @tm[:t1, Rake::Scope.make("a1")] |
| 41 | + assert_equal t1, found_task_t1 |
| 42 | + # lazy definition is called at most once |
| 43 | + assert_equal lazy_definition_call_count, 1 |
| 44 | + end |
| 45 | + |
| 46 | + def test_execute_all_lazy_definitions |
| 47 | + lazy_definition_call_count = 0 |
| 48 | + @tm.in_namespace("a") do |
| 49 | + @tm.register_lazy_definition do |
| 50 | + lazy_definition_call_count += 1 |
| 51 | + end |
| 52 | + end |
| 53 | + assert_equal lazy_definition_call_count, 0 |
| 54 | + @tm.execute_all_lazy_definitions |
| 55 | + assert_equal lazy_definition_call_count, 1 |
| 56 | + @tm.execute_all_lazy_definitions |
| 57 | + assert_equal lazy_definition_call_count, 1 |
| 58 | + end |
| 59 | + |
| 60 | +end |
0 commit comments