summaryrefslogtreecommitdiff
path: root/test/rdoc/test_rdoc_top_level.rb
blob: 6c1bc43e7a84f990cc9c2c519bfd0f5f5cf1192b (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
require 'rubygems'
require 'minitest/autorun'
require File.expand_path '../xref_test_case', __FILE__

class TestRDocTopLevel < XrefTestCase

  def setup
    super

    @top_level = RDoc::TopLevel.new 'path/top_level.rb'
  end

  def test_class_all_classes_and_modules
    expected = %w[
      C1 C2 C2::C3 C2::C3::H1 C3 C3::H1 C3::H2 C4 C4::C4 C5 C5::C1
      Child
      M1 M1::M2
      Parent
    ]

    assert_equal expected,
                 RDoc::TopLevel.all_classes_and_modules.map { |m| m.full_name }.sort
  end

  def test_class_classes
    expected = %w[
      C1 C2 C2::C3 C2::C3::H1 C3 C3::H1 C3::H2 C4 C4::C4 C5 C5::C1
      Child Parent
    ]

    assert_equal expected, RDoc::TopLevel.classes.map { |m| m.full_name }.sort
  end

  def test_class_complete
    @c2.add_module_alias @c2_c3, 'A1', @top_level

    RDoc::TopLevel.complete :public

    a1 = @xref_data.find_class_or_module 'C2::A1'

    assert_equal 'C2::A1', a1.full_name
    refute_empty a1.aliases
  end

  def test_class_files
    assert_equal %w[path/top_level.rb xref_data.rb],
                 RDoc::TopLevel.files.map { |m| m.full_name }.sort
  end

  def test_class_find_class_named
    assert_equal @c1, RDoc::TopLevel.find_class_named('C1')

    assert_equal @c2_c3, RDoc::TopLevel.find_class_named('C2::C3')
  end

  def test_class_find_class_named_from
    assert_equal @c5_c1, RDoc::TopLevel.find_class_named_from('C1', 'C5')

    assert_equal @c1,    RDoc::TopLevel.find_class_named_from('C1', 'C4')
  end

  def test_class_find_class_or_module
    assert_equal @m1, RDoc::TopLevel.find_class_or_module('M1')
    assert_equal @c1, RDoc::TopLevel.find_class_or_module('C1')

    assert_equal @m1, RDoc::TopLevel.find_class_or_module('::M1')
    assert_equal @c1, RDoc::TopLevel.find_class_or_module('::C1')
  end

  def test_class_find_file_named
    assert_equal @xref_data, RDoc::TopLevel.find_file_named(@file_name)
  end

  def test_class_find_module_named
    assert_equal @m1,    RDoc::TopLevel.find_module_named('M1')
    assert_equal @m1_m2, RDoc::TopLevel.find_module_named('M1::M2')
  end

  def test_class_modules
    assert_equal %w[M1 M1::M2],
                 RDoc::TopLevel.modules.map { |m| m.full_name }.sort
  end

  def test_class_new
    tl1 = RDoc::TopLevel.new 'file.rb'
    tl2 = RDoc::TopLevel.new 'file.rb'
    tl3 = RDoc::TopLevel.new 'other.rb'

    assert_same tl1, tl2
    refute_same tl1, tl3
  end

  def test_class_reset
    RDoc::TopLevel.reset

    assert_empty RDoc::TopLevel.classes
    assert_empty RDoc::TopLevel.modules
    assert_empty RDoc::TopLevel.files
  end

  def test_base_name
    assert_equal 'top_level.rb', @top_level.base_name
  end

  def test_eql_eh
    top_level2 = RDoc::TopLevel.new 'path/top_level.rb'
    other_level = RDoc::TopLevel.new 'path/other_level.rb'

    assert_operator @top_level, :eql?, top_level2

    refute_operator other_level, :eql?, @top_level
  end

  def test_equals2
    top_level2 = RDoc::TopLevel.new 'path/top_level.rb'
    other_level = RDoc::TopLevel.new 'path/other_level.rb'

    assert_equal @top_level, top_level2

    refute_equal other_level, @top_level
  end

  def test_find_class_or_module
    assert_equal @c1,    @xref_data.find_class_or_module('C1')
    assert_equal @c2_c3, @xref_data.find_class_or_module('C2::C3')
    assert_equal @c4,    @xref_data.find_class_or_module('C4')
    assert_equal @m1_m2, @xref_data.find_class_or_module('M1::M2')
  end

  def test_full_name
    assert_equal 'path/top_level.rb', @top_level.full_name
  end

  def test_hash
    tl2 = RDoc::TopLevel.new 'path/top_level.rb'
    tl3 = RDoc::TopLevel.new 'other/top_level.rb'

    assert_equal @top_level.hash, tl2.hash
    refute_equal @top_level.hash, tl3.hash
  end

  def test_http_url
    assert_equal 'prefix/path/top_level_rb.html', @top_level.http_url('prefix')
  end

  def test_last_modified
    assert_equal nil, @top_level.last_modified
    stat = Object.new
    def stat.mtime() 0 end
    @top_level.file_stat = stat
    assert_equal 0, @top_level.last_modified
  end

  def test_name
    assert_equal 'top_level.rb', @top_level.name
  end

end