summaryrefslogtreecommitdiff
path: root/jni/ruby/test/rdoc/test_rdoc_parser.rb
blob: 34d4486cb1ff432df01759e12ddf1e2b43b7836e (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# -*- coding: us-ascii -*-

require 'rdoc/test_case'

class TestRDocParser < RDoc::TestCase

  def setup
    super

    @RP = RDoc::Parser
    @binary_dat = File.expand_path '../binary.dat', __FILE__

    @fn = 'file.rb'
    @top_level = RDoc::TopLevel.new @fn
    @options = RDoc::Options.new
  end

  def test_class_binary_eh_ISO_2022_JP
    iso_2022_jp = File.join Dir.tmpdir, "test_rdoc_parser_#{$$}.rd"

    open iso_2022_jp, 'wb' do |io|
      io.write "# coding: ISO-2022-JP\n"
      io.write ":\e$B%3%^%s%I\e(B:\n"
    end

    refute @RP.binary? iso_2022_jp
  ensure
    File.unlink iso_2022_jp
  end

  def test_class_binary_eh_marshal
    marshal = File.join Dir.tmpdir, "test_rdoc_parser_#{$$}.marshal"
    open marshal, 'wb' do |io|
      io.write Marshal.dump('')
      io.write 'lots of text ' * 500
    end

    assert @RP.binary?(marshal)
  ensure
    File.unlink marshal
  end

  def test_class_binary_japanese_text
    file_name = File.expand_path '../test.ja.txt', __FILE__
    refute @RP.binary?(file_name)
  end

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

    capture_io do
      begin
        extenc, Encoding.default_external =
          Encoding.default_external, Encoding::US_ASCII
        file_name = File.expand_path '../test.ja.largedoc', __FILE__
        assert !@RP.binary?(file_name)
      ensure
        Encoding.default_external = extenc
      end
    end
  end

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

    file_name = File.expand_path '../test.ja.rdoc', __FILE__
    refute @RP.binary?(file_name)
  end

  def test_class_can_parse
    assert_equal @RP.can_parse(__FILE__), @RP::Ruby

    readme_file_name = File.expand_path '../test.txt', __FILE__

    assert_equal @RP::Simple, @RP.can_parse(readme_file_name)

    assert_equal @RP::Simple, @RP.can_parse(@binary_dat)

    jtest_file_name = File.expand_path '../test.ja.txt', __FILE__
    assert_equal @RP::Simple, @RP.can_parse(jtest_file_name)

    jtest_rdoc_file_name = File.expand_path '../test.ja.rdoc', __FILE__
    assert_equal @RP::Simple, @RP.can_parse(jtest_rdoc_file_name)

    readme_file_name = File.expand_path '../README', __FILE__
    assert_equal @RP::Simple, @RP.can_parse(readme_file_name)

    jtest_largerdoc_file_name = File.expand_path '../test.ja.largedoc', __FILE__
    assert_equal @RP::Simple, @RP.can_parse(jtest_largerdoc_file_name)

    @RP.alias_extension 'rdoc', 'largedoc'
    assert_equal @RP::Simple, @RP.can_parse(jtest_largerdoc_file_name)
  end

  def test_class_for_executable
    temp_dir do
      content = "#!/usr/bin/env ruby -w\n"
      open 'app', 'w' do |io| io.write content end
      app = @store.add_file 'app'

      parser = @RP.for app, 'app', content, @options, :stats

      assert_kind_of RDoc::Parser::Ruby, parser

      assert_equal 'app', parser.file_name
    end
  end

  def test_class_for_forbidden
    skip 'chmod not supported' if Gem.win_platform?

    tf = Tempfile.open 'forbidden' do |io|
      begin
        File.chmod 0000, io.path
        forbidden = @store.add_file io.path

        parser = @RP.for forbidden, 'forbidden', '', @options, :stats

        assert_nil parser
      ensure
        File.chmod 0400, io.path
      end
      io
    end
    tf.close! if tf.respond_to? :close!
  end

  def test_class_for_modeline
    temp_dir do
      content = "# -*- rdoc -*-\n= NEWS\n"

      open 'NEWS', 'w' do |io| io.write content end
      app = @store.add_file 'NEWS'

      parser = @RP.for app, 'NEWS', content, @options, :stats

      assert_kind_of RDoc::Parser::Simple, parser

      assert_equal "= NEWS\n", parser.content
    end
  end

  def test_can_parse_modeline
    readme_ext = File.join Dir.tmpdir, "README.EXT.#{$$}"

    open readme_ext, 'w' do |io|
      io.puts "# README.EXT -  -*- rdoc -*- created at: Mon Aug 7 16:45:54 JST 1995"
      io.puts
      io.puts "This document explains how to make extension libraries for Ruby."
    end

    assert_equal RDoc::Parser::Simple, @RP.can_parse(readme_ext)
  ensure
    File.unlink readme_ext
  end

  ##
  # Selenium hides a .jar file using a .txt extension.

  def test_class_can_parse_zip
    hidden_zip = File.expand_path '../hidden.zip.txt', __FILE__
    assert_nil @RP.can_parse(hidden_zip)
  end

  def test_check_modeline
    readme_ext = File.join Dir.tmpdir, "README.EXT.#{$$}"

    open readme_ext, 'w' do |io|
      io.puts "# README.EXT -  -*- RDoc -*- created at: Mon Aug 7 16:45:54 JST 1995"
      io.puts
      io.puts "This document explains how to make extension libraries for Ruby."
    end

    assert_equal 'rdoc', @RP.check_modeline(readme_ext)
  ensure
    File.unlink readme_ext
  end

  def test_check_modeline_coding
    readme_ext = File.join Dir.tmpdir, "README.EXT.#{$$}"

    open readme_ext, 'w' do |io|
      io.puts "# -*- coding: utf-8 -*-"
    end

    assert_nil @RP.check_modeline readme_ext
  ensure
    File.unlink readme_ext
  end

  def test_check_modeline_with_other
    readme_ext = File.join Dir.tmpdir, "README.EXT.#{$$}"

    open readme_ext, 'w' do |io|
      io.puts "# README.EXT -  -*- mode: RDoc; indent-tabs-mode: nil -*-"
      io.puts
      io.puts "This document explains how to make extension libraries for Ruby."
    end

    assert_equal 'rdoc', @RP.check_modeline(readme_ext)
  ensure
    File.unlink readme_ext
  end

  def test_check_modeline_no_modeline
    readme_ext = File.join Dir.tmpdir, "README.EXT.#{$$}"

    open readme_ext, 'w' do |io|
      io.puts "This document explains how to make extension libraries for Ruby."
    end

    assert_nil @RP.check_modeline(readme_ext)
  ensure
    File.unlink readme_ext
  end

  def test_class_for_binary
    rp = @RP.dup

    class << rp
      alias old_can_parse can_parse
    end

    def rp.can_parse(*args) nil end

    assert_nil @RP.for(nil, @binary_dat, nil, nil, nil)
  end

  def test_class_for_markup
    content = <<-CONTENT
# coding: utf-8 markup: rd
    CONTENT

    parser = @RP.for @top_level, __FILE__, content, @options, nil

    assert_kind_of @RP::RD, parser
  end

  def test_class_use_markup
    content = <<-CONTENT
# coding: utf-8 markup: rd
    CONTENT

    parser = @RP.use_markup content

    assert_equal @RP::RD, parser
  end

  def test_class_use_markup_markdown
    content = <<-CONTENT
# coding: utf-8 markup: markdown
    CONTENT

    parser = @RP.use_markup content

    assert_equal @RP::Ruby, parser
  end

  def test_class_use_markup_modeline
    content = <<-CONTENT
# -*- coding: utf-8 -*-
# markup: rd
    CONTENT

    parser = @RP.use_markup content

    assert_equal @RP::RD, parser
  end

  def test_class_use_markup_modeline_shebang
    content = <<-CONTENT
#!/bin/sh
/* -*- coding: utf-8 -*-
 * markup: rd
 */
    CONTENT

    parser = @RP.use_markup content

    assert_equal @RP::RD, parser
  end

  def test_class_use_markup_shebang
    content = <<-CONTENT
#!/usr/bin/env ruby
# coding: utf-8 markup: rd
    CONTENT

    parser = @RP.use_markup content

    assert_equal @RP::RD, parser
  end

  def test_class_use_markup_tomdoc
    content = <<-CONTENT
# coding: utf-8 markup: tomdoc
    CONTENT

    parser = @RP.use_markup content

    assert_equal @RP::Ruby, parser
  end

  def test_class_use_markup_none
    parser = @RP.use_markup ''

    assert_nil parser
  end

  def test_class_use_markup_unknown
    content = <<-CONTENT
# :markup: RDoc
    CONTENT

    parser = @RP.use_markup content

    assert_nil parser
  end

  def test_initialize
    @RP.new @top_level, @fn, '', @options, nil

    assert_equal @RP, @top_level.parser
  end

end