summaryrefslogtreecommitdiff
path: root/test/rake/test_top_level_functions.rb
blob: 12a8cd1e36f393bf286946cdc4fafd9814adbc27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
require 'test/unit'
require_relative 'capture_stdout'
require 'rake'

class Rake::TestTopLevelFunctions < Test::Unit::TestCase
  include CaptureStdout

  def setup
    super
    @app = Rake.application
    Rake.application = @mock = Object.new
  end

  def teardown
    Rake.application = @app
    super
  end

  def defmock(sym, &block)
    class << @mock; self; end.class_eval do
      define_method(sym, block)
    end
  end

  def test_namespace
    args = []
    defmock(:in_namespace) {|a, *| args << a}
    namespace "xyz" do end
    assert_equal(["xyz"], args)
  end

  def test_import
    args = []
    defmock(:add_import) {|a| args << a}
    import('x', 'y', 'z')
    assert_equal(['x', 'y', 'z'], args)
  end

  def test_when_writing
    out = capture_stdout do
      when_writing("NOTWRITING") do
        puts "WRITING"
      end
    end
    assert_equal "WRITING\n", out
  end

  def test_when_not_writing
    RakeFileUtils.nowrite_flag = true
    out = capture_stdout do
      when_writing("NOTWRITING") do
        puts "WRITING"
      end
    end
    assert_equal "DRYRUN: NOTWRITING\n", out
  ensure
    RakeFileUtils.nowrite_flag = false
  end

  def test_missing_constants_task
    args = []
    defmock(:const_warning) {|a| args << a}
    Object.const_missing(:Task)
    assert_equal([:Task], args)
  end

  def test_missing_constants_file_task
    args = []
    defmock(:const_warning) {|a| args << a}
    Object.const_missing(:FileTask)
    assert_equal([:FileTask], args)
  end

  def test_missing_constants_file_creation_task
    args = []
    defmock(:const_warning) {|a| args << a}
    Object.const_missing(:FileCreationTask)
    assert_equal([:FileCreationTask], args)
  end

  def test_missing_constants_rake_app
    args = []
    defmock(:const_warning) {|a| args << a}
    Object.const_missing(:RakeApp)
    assert_equal([:RakeApp], args)
  end

  def test_missing_other_constant
    assert_raise(NameError) do Object.const_missing(:Xyz) end
  end
end