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
|
# coding: US-ASCII
require 'rdoc/test_case'
class TestRDocGeneratorJsonIndex < RDoc::TestCase
def setup
super
@tmpdir = File.join Dir.tmpdir, "test_rdoc_generator_darkfish_#{$$}"
FileUtils.mkdir_p @tmpdir
@options = RDoc::Options.new
@options.files = []
# JsonIndex is used in conjunction with another generator
@options.setup_generator 'darkfish'
@options.template_dir = ''
@options.op_dir = @tmpdir
@options.option_parser = OptionParser.new
@options.finish
@darkfish = RDoc::Generator::Darkfish.new @store, @options
@g = RDoc::Generator::JsonIndex.new @darkfish, @options
@rdoc.options = @options
@rdoc.generator = @g
@top_level = @store.add_file 'file.rb'
@top_level.parser = RDoc::Parser::Ruby
@klass = @top_level.add_class RDoc::NormalClass, 'C'
@meth = @klass.add_method RDoc::AnyMethod.new(nil, 'meth')
@meth.record_location @top_level
@nest_klass = @klass.add_class RDoc::NormalClass, 'D'
@nest_klass.record_location @top_level
@nest_meth = @nest_klass.add_method RDoc::AnyMethod.new(nil, 'meth')
@ignored = @top_level.add_class RDoc::NormalClass, 'Ignored'
@ignored.ignore
@page = @store.add_file 'page.rdoc'
@page.parser = RDoc::Parser::Simple
@top_levels = [@top_level, @page].sort
@klasses = [@klass, @nest_klass, @ignored]
Dir.chdir @tmpdir
end
def teardown
super
Dir.chdir @pwd
FileUtils.rm_rf @tmpdir
end
def test_build_index
index = @g.build_index
expected = {
:index => {
:searchIndex => %w[c d meth() meth() page],
:longSearchIndex => %w[c c::d c#meth() c::d#meth()],
:info => [
@klass.search_record[2..-1],
@nest_klass.search_record[2..-1],
@meth.search_record[2..-1],
@nest_meth.search_record[2..-1],
@page.search_record[2..-1],
],
},
}
expected[:index][:longSearchIndex] << ''
assert_equal expected, index
end
def test_class_dir
assert_equal @darkfish.class_dir, @g.class_dir
end
def test_file_dir
assert_equal @darkfish.file_dir, @g.file_dir
end
def test_generate
@g.generate
assert_file 'js/searcher.js'
assert_file 'js/navigation.js'
assert_file 'js/search_index.js'
json = File.read 'js/search_index.js'
json =~ /\Avar search_data = /
assignment = $&
index = $'
refute_empty assignment
index = JSON.parse index
info = [
@klass.search_record[2..-1],
@nest_klass.search_record[2..-1],
@meth.search_record[2..-1],
@nest_meth.search_record[2..-1],
@page.search_record[2..-1],
]
expected = {
'index' => {
'searchIndex' => [
'c',
'd',
'meth()',
'meth()',
'page',
],
'longSearchIndex' => [
'c',
'c::d',
'c#meth()',
'c::d#meth()',
'',
],
'info' => info,
},
}
assert_equal expected, index
end
def test_generate_gzipped
require 'zlib'
@g.generate
@g.generate_gzipped
assert_file 'js/searcher.js'
assert_file 'js/searcher.js.gz'
assert_file 'js/navigation.js'
assert_file 'js/navigation.js.gz'
assert_file 'js/search_index.js'
assert_file 'js/search_index.js.gz'
json = File.open('js/search_index.js.gz') {|gzip|
Zlib::GzipReader.new(gzip).read
}
json =~ /\Avar search_data = /
assignment = $&
index = $'
refute_empty assignment
index = JSON.parse index
info = [
@klass.search_record[2..-1],
@nest_klass.search_record[2..-1],
@meth.search_record[2..-1],
@nest_meth.search_record[2..-1],
@page.search_record[2..-1],
]
expected = {
'index' => {
'searchIndex' => [
'c',
'd',
'meth()',
'meth()',
'page',
],
'longSearchIndex' => [
'c',
'c::d',
'c#meth()',
'c::d#meth()',
'',
],
'info' => info,
},
}
assert_equal expected, index
end
def test_generate_utf_8
skip "Encoding not implemented" unless Object.const_defined? :Encoding
text = "5\xB0"
text.force_encoding Encoding::ISO_8859_1
@klass.add_comment comment(text), @top_level
@g.generate
json = File.read 'js/search_index.js'
json.force_encoding Encoding::UTF_8
json =~ /\Avar search_data = /
index = $'
index = JSON.parse index
klass_record = @klass.search_record[2..-1]
klass_record[-1] = "<p>5\xc2\xb0\n"
klass_record.last.force_encoding Encoding::UTF_8
info = [
klass_record,
@nest_klass.search_record[2..-1],
@meth.search_record[2..-1],
@nest_meth.search_record[2..-1],
@page.search_record[2..-1],
]
expected = {
'index' => {
'searchIndex' => [
'c',
'd',
'meth()',
'meth()',
'page',
],
'longSearchIndex' => [
'c',
'c::d',
'c#meth()',
'c::d#meth()',
'',
],
'info' => info,
},
}
assert_equal expected, index
end
def test_index_classes
@g.reset @top_levels, @klasses
@g.index_classes
expected = {
:searchIndex => %w[c d],
:longSearchIndex => %w[c c::d],
:info => [
@klass.search_record[2..-1],
@nest_klass.search_record[2..-1],
],
}
assert_equal expected, @g.index
end
def test_index_classes_nodoc
@klass.document_self = false
@nest_klass.document_self = false
@meth.document_self = false
@nest_meth.document_self = false
@g.reset @top_levels, @klasses
@g.index_classes
expected = {
:searchIndex => [],
:longSearchIndex => [],
:info => [],
}
assert_equal expected, @g.index
end
def test_index_methods
@g.reset @top_levels, @klasses
@g.index_methods
expected = {
:searchIndex => %w[meth() meth()],
:longSearchIndex => %w[c#meth() c::d#meth()],
:info => [
@meth.search_record[2..-1],
@nest_meth.search_record[2..-1],
],
}
assert_equal expected, @g.index
end
def test_index_pages
@g.reset @top_levels, @klasses
@g.index_pages
expected = {
:searchIndex => %w[page],
:longSearchIndex => [''],
:info => [@page.search_record[2..-1]],
}
assert_equal expected, @g.index
end
def test_search_string
assert_equal 'cd', @g.search_string('C d')
end
end
|