1require 'rdoc/test_case'
2
3class TestRDocGeneratorDarkfish < RDoc::TestCase
4
5  def setup
6    super
7
8    @lib_dir = "#{@pwd}/lib"
9    $LOAD_PATH.unshift @lib_dir # ensure we load from this RDoc
10
11    @options = RDoc::Options.new
12    @options.option_parser = OptionParser.new
13
14    @tmpdir = File.join Dir.tmpdir, "test_rdoc_generator_darkfish_#{$$}"
15    FileUtils.mkdir_p @tmpdir
16    Dir.chdir @tmpdir
17    @options.op_dir = @tmpdir
18    @options.generator = RDoc::Generator::Darkfish
19
20    $LOAD_PATH.each do |path|
21      darkfish_dir = File.join path, 'rdoc/generator/template/darkfish/'
22      next unless File.directory? darkfish_dir
23      @options.template_dir = darkfish_dir
24      break
25    end
26
27    @rdoc.options = @options
28
29    @g = @options.generator.new @store, @options
30    @rdoc.generator = @g
31
32    @top_level = @store.add_file 'file.rb'
33    @top_level.parser = RDoc::Parser::Ruby
34    @klass = @top_level.add_class RDoc::NormalClass, 'Klass'
35
36    @alias_constant = RDoc::Constant.new 'A', nil, ''
37    @alias_constant.record_location @top_level
38
39    @top_level.add_constant @alias_constant
40
41    @klass.add_module_alias @klass, 'A', @top_level
42
43    @meth = RDoc::AnyMethod.new nil, 'method'
44    @meth_bang = RDoc::AnyMethod.new nil, 'method!'
45    @attr = RDoc::Attr.new nil, 'attr', 'RW', ''
46
47    @klass.add_method @meth
48    @klass.add_method @meth_bang
49    @klass.add_attribute @attr
50
51    @ignored = @top_level.add_class RDoc::NormalClass, 'Ignored'
52    @ignored.ignore
53
54    @store.complete :private
55
56    @object      = @store.find_class_or_module 'Object'
57    @klass_alias = @store.find_class_or_module 'Klass::A'
58  end
59
60  def teardown
61    super
62
63    $LOAD_PATH.shift
64    Dir.chdir @pwd
65    FileUtils.rm_rf @tmpdir
66  end
67
68  def assert_file path
69    assert File.file?(path), "#{path} is not a file"
70  end
71
72  def refute_file path
73    refute File.exist?(path), "#{path} exists"
74  end
75
76  def mu_pp obj
77    s = ''
78    s = PP.pp obj, s
79    s = s.force_encoding Encoding.default_external if defined? Encoding
80    s.chomp
81  end
82
83  def test_generate
84    top_level = @store.add_file 'file.rb'
85    top_level.add_class @klass.class, @klass.name
86
87    @g.generate
88
89    assert_file 'index.html'
90    assert_file 'Object.html'
91    assert_file 'table_of_contents.html'
92    assert_file 'js/search_index.js'
93
94    encoding = if Object.const_defined? :Encoding then
95                 Regexp.escape Encoding::UTF_8.name
96               else
97                 Regexp.escape 'UTF-8'
98               end
99
100    assert_match(/<meta content="text\/html; charset=#{encoding}"/,
101                 File.read('index.html'))
102    assert_match(/<meta content="text\/html; charset=#{encoding}"/,
103                 File.read('Object.html'))
104
105    refute_match(/Ignored/, File.read('index.html'))
106  end
107
108  def test_generate_dry_run
109    @g.dry_run = true
110    top_level = @store.add_file 'file.rb'
111    top_level.add_class @klass.class, @klass.name
112
113    @g.generate
114
115    refute_file 'index.html'
116    refute_file 'Object.html'
117  end
118
119  def test_generate_static
120    FileUtils.mkdir_p 'dir/images'
121    FileUtils.touch 'dir/images/image.png'
122    FileUtils.mkdir_p 'file'
123    FileUtils.touch 'file/file.txt'
124
125    @options.static_path = [
126      File.expand_path('dir'),
127      File.expand_path('file/file.txt'),
128    ]
129
130    @g.generate
131
132    assert_file 'images/image.png'
133    assert_file 'file.txt'
134  end
135
136  def test_generate_static_dry_run
137    FileUtils.mkdir 'static'
138    FileUtils.touch 'static/image.png'
139
140    @options.static_path = [File.expand_path('static')]
141    @g.dry_run = true
142
143    @g.generate
144
145    refute_file 'image.png'
146  end
147
148  def test_setup
149    @g.setup
150
151    assert_equal [@klass_alias, @ignored, @klass, @object],
152                 @g.classes.sort_by { |klass| klass.full_name }
153    assert_equal [@top_level],                           @g.files
154    assert_equal [@meth, @meth, @meth_bang, @meth_bang], @g.methods
155    assert_equal [@klass_alias, @klass, @object], @g.modsort
156  end
157
158  def test_template_for
159    classpage = Pathname.new @options.template_dir + 'class.rhtml'
160
161    template = @g.send(:template_for, classpage, true, RDoc::ERBIO)
162    assert_kind_of RDoc::ERBIO, template
163
164    assert_same template, @g.send(:template_for, classpage)
165  end
166
167  def test_template_for_dry_run
168    classpage = Pathname.new @options.template_dir + 'class.rhtml'
169
170    template = @g.send(:template_for, classpage, true, ERB)
171    assert_kind_of ERB, template
172
173    assert_same template, @g.send(:template_for, classpage)
174  end
175
176  def test_template_for_partial
177    partial = Pathname.new @options.template_dir + '_sidebar_classes.rhtml'
178
179    template = @g.send(:template_for, partial, false, RDoc::ERBPartial)
180
181    assert_kind_of RDoc::ERBPartial, template
182
183    assert_same template, @g.send(:template_for, partial)
184  end
185
186end
187
188