summaryrefslogtreecommitdiff
path: root/test/rdoc/test_rdoc_markup_pre_process.rb
blob: 587a680f28647f8bcd2d17df303b7de420b065f4 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# coding: utf-8

require 'tempfile'
require 'rubygems'
require 'minitest/autorun'
require 'rdoc/markup/pre_process'
require 'rdoc/code_objects'

class TestRDocMarkupPreProcess < MiniTest::Unit::TestCase

  def setup
    RDoc::Markup::PreProcess.registered.clear

    @tempfile = Tempfile.new 'test_rdoc_markup_pre_process'
    @file_name = File.basename @tempfile.path
    @dir  = File.dirname @tempfile.path

    @pp = RDoc::Markup::PreProcess.new __FILE__, [@dir]
  end

  def teardown
    @tempfile.close
  end

  def test_include_file
    @tempfile.write <<-INCLUDE
# -*- mode: rdoc; coding: utf-8; fill-column: 74; -*-

Regular expressions (<i>regexp</i>s) are patterns which describe the
contents of a string.
    INCLUDE

    @tempfile.flush
    @tempfile.rewind

    content = @pp.include_file @file_name, '', nil

    expected = <<-EXPECTED
Regular expressions (<i>regexp</i>s) are patterns which describe the
contents of a string.
    EXPECTED

    # FIXME 1.9 fix on windoze
    # preprocessor uses binread, so line endings are \r\n
    expected.gsub!("\n", "\r\n") if
      RUBY_VERSION < "1.9.3" && RUBY_PLATFORM =~ /mswin|mingw/

    assert_equal expected, content
  end

  def test_include_file_encoding_incompatible
    skip "Encoding not implemented" unless Object.const_defined? :Encoding

    @tempfile.write <<-INCLUDE
# -*- mode: rdoc; coding: utf-8; fill-column: 74; -*-

π
    INCLUDE

    @tempfile.flush
    @tempfile.rewind

    content = @pp.include_file @file_name, '', Encoding::US_ASCII

    expected = "?\n"

    # FIXME 1.9 fix on windoze
    # preprocessor uses binread, so line endings are \r\n
    expected.gsub!("\n", "\r\n") if
      RUBY_VERSION < "1.9.3" && RUBY_PLATFORM =~ /mswin|mingw/

    assert_equal expected, content
  end

  def test_handle
    text = "# :x: y\n"
    out = @pp.handle text

    assert_same out, text
    assert_equal "# :x: y\n", text
  end

  def test_handle_block
    text = "# :x: y\n"

    @pp.handle text do |directive, param|
      false
    end

    assert_equal "# :x: y\n", text

    @pp.handle text do |directive, param|
      ''
    end

    assert_equal "", text
  end

  def test_handle_category
    context = RDoc::Context.new
    original_section = context.current_section

    text = "# :category: other\n"

    @pp.handle text, context

    refute_equal original_section, context.current_section
  end

  def test_handle_code_object
    cd = RDoc::CodeObject.new
    text = "# :x: y\n"
    @pp.handle text, cd

    assert_equal "# :x: y\n", text
    assert_equal 'y', cd.metadata['x']

    cd.metadata.clear
    text = "# :x:\n"
    @pp.handle text, cd

    assert_equal "# :x: \n", text
    assert_includes cd.metadata, 'x'
  end

  def test_handle_code_object_block
    cd = RDoc::CodeObject.new
    text = "# :x: y\n"
    @pp.handle text, cd do
      false
    end

    assert_equal "# :x: y\n", text
    assert_empty cd.metadata

    @pp.handle text, cd do
      nil
    end

    assert_equal "# :x: y\n", text
    assert_equal 'y', cd.metadata['x']

    cd.metadata.clear

    @pp.handle text, cd do
      ''
    end

    assert_equal '', text
    assert_empty cd.metadata
  end

  def test_handle_registered
    RDoc::Markup::PreProcess.register 'x'
    text = "# :x: y\n"
    @pp.handle text

    assert_equal '', text

    text = "# :x: y\n"

    @pp.handle text do |directive, param|
      false
    end

    assert_equal "# :x: y\n", text

    text = "# :x: y\n"

    @pp.handle text do |directive, param|
      ''
    end

    assert_equal "", text
  end

  def test_handle_registered_block
    called = nil
    RDoc::Markup::PreProcess.register 'x' do |directive, param|
      called = [directive, param]
      'blah'
    end

    text = "# :x: y\n"
    @pp.handle text

    assert_equal 'blah', text
    assert_equal %w[x y], called
  end

  def test_handle_registered_code_object
    RDoc::Markup::PreProcess.register 'x'
    cd = RDoc::CodeObject.new

    text = "# :x: y\n"
    @pp.handle text, cd

    assert_equal '', text
    assert_equal 'y', cd.metadata['x']

    cd.metadata.clear
    text = "# :x: y\n"

    @pp.handle text do |directive, param|
      false
    end

    assert_equal "# :x: y\n", text
    assert_empty cd.metadata

    text = "# :x: y\n"

    @pp.handle text do |directive, param|
      ''
    end

    assert_equal "", text
    assert_empty cd.metadata
  end

end