1# coding: US-ASCII 2 3require 'rdoc/test_case' 4 5class TestRDocGeneratorJsonIndex < RDoc::TestCase 6 7 def setup 8 super 9 10 @tmpdir = File.join Dir.tmpdir, "test_rdoc_generator_darkfish_#{$$}" 11 FileUtils.mkdir_p @tmpdir 12 13 @options = RDoc::Options.new 14 @options.files = [] 15 # JsonIndex is used in conjunction with another generator 16 @options.setup_generator 'darkfish' 17 @options.template_dir = '' 18 @options.op_dir = @tmpdir 19 @options.option_parser = OptionParser.new 20 @options.finish 21 22 @darkfish = RDoc::Generator::Darkfish.new @store, @options 23 @g = RDoc::Generator::JsonIndex.new @darkfish, @options 24 25 @rdoc.options = @options 26 @rdoc.generator = @g 27 28 @top_level = @store.add_file 'file.rb' 29 @top_level.parser = RDoc::Parser::Ruby 30 31 @klass = @top_level.add_class RDoc::NormalClass, 'C' 32 33 @meth = @klass.add_method RDoc::AnyMethod.new(nil, 'meth') 34 @meth.record_location @top_level 35 36 @nest_klass = @klass.add_class RDoc::NormalClass, 'D' 37 @nest_klass.record_location @top_level 38 39 @nest_meth = @nest_klass.add_method RDoc::AnyMethod.new(nil, 'meth') 40 41 @ignored = @top_level.add_class RDoc::NormalClass, 'Ignored' 42 @ignored.ignore 43 44 @page = @store.add_file 'page.rdoc' 45 @page.parser = RDoc::Parser::Simple 46 47 @top_levels = [@top_level, @page].sort 48 @klasses = [@klass, @nest_klass, @ignored] 49 50 Dir.chdir @tmpdir 51 end 52 53 def teardown 54 super 55 56 Dir.chdir @pwd 57 FileUtils.rm_rf @tmpdir 58 end 59 60 def assert_file path 61 assert File.file?(path), "#{path} is not a file" 62 end 63 64 def mu_pp obj 65 s = '' 66 s = PP.pp obj, s 67 s = s.force_encoding Encoding.default_external if defined? Encoding 68 s.chomp 69 end 70 71 def test_build_index 72 index = @g.build_index 73 74 expected = { 75 :index => { 76 :searchIndex => %w[c d meth() meth() page], 77 :longSearchIndex => %w[c c::d c#meth() c::d#meth()], 78 :info => [ 79 @klass.search_record[2..-1], 80 @nest_klass.search_record[2..-1], 81 @meth.search_record[2..-1], 82 @nest_meth.search_record[2..-1], 83 @page.search_record[2..-1], 84 ], 85 }, 86 } 87 88 expected[:index][:longSearchIndex] << '' 89 90 assert_equal expected, index 91 end 92 93 def test_class_dir 94 assert_equal @darkfish.class_dir, @g.class_dir 95 end 96 97 def test_file_dir 98 assert_equal @darkfish.file_dir, @g.file_dir 99 end 100 101 def test_generate 102 @g.generate 103 104 assert_file 'js/searcher.js' 105 assert_file 'js/navigation.js' 106 assert_file 'js/search_index.js' 107 108 json = File.read 'js/search_index.js' 109 110 json =~ /\Avar search_data = / 111 112 assignment = $& 113 index = $' 114 115 refute_empty assignment 116 117 index = JSON.parse index 118 119 info = [ 120 @klass.search_record[2..-1], 121 @nest_klass.search_record[2..-1], 122 @meth.search_record[2..-1], 123 @nest_meth.search_record[2..-1], 124 @page.search_record[2..-1], 125 ] 126 127 expected = { 128 'index' => { 129 'searchIndex' => [ 130 'c', 131 'd', 132 'meth()', 133 'meth()', 134 'page', 135 ], 136 'longSearchIndex' => [ 137 'c', 138 'c::d', 139 'c#meth()', 140 'c::d#meth()', 141 '', 142 ], 143 'info' => info, 144 }, 145 } 146 147 assert_equal expected, index 148 end 149 150 def test_generate_utf_8 151 skip "Encoding not implemented" unless Object.const_defined? :Encoding 152 153 text = "5\xB0" 154 text.force_encoding Encoding::ISO_8859_1 155 @klass.add_comment comment(text), @top_level 156 157 @g.generate 158 159 json = File.read 'js/search_index.js' 160 json.force_encoding Encoding::UTF_8 161 162 json =~ /\Avar search_data = / 163 164 index = $' 165 166 index = JSON.parse index 167 168 klass_record = @klass.search_record[2..-1] 169 klass_record[-1] = "<p>5\xc2\xb0\n" 170 klass_record.last.force_encoding Encoding::UTF_8 171 172 info = [ 173 klass_record, 174 @nest_klass.search_record[2..-1], 175 @meth.search_record[2..-1], 176 @nest_meth.search_record[2..-1], 177 @page.search_record[2..-1], 178 ] 179 180 expected = { 181 'index' => { 182 'searchIndex' => [ 183 'c', 184 'd', 185 'meth()', 186 'meth()', 187 'page', 188 ], 189 'longSearchIndex' => [ 190 'c', 191 'c::d', 192 'c#meth()', 193 'c::d#meth()', 194 '', 195 ], 196 'info' => info, 197 }, 198 } 199 200 assert_equal expected, index 201 end 202 203 def test_index_classes 204 @g.reset @top_levels, @klasses 205 206 @g.index_classes 207 208 expected = { 209 :searchIndex => %w[c d], 210 :longSearchIndex => %w[c c::d], 211 :info => [ 212 @klass.search_record[2..-1], 213 @nest_klass.search_record[2..-1], 214 ], 215 } 216 217 assert_equal expected, @g.index 218 end 219 220 def test_index_classes_nodoc 221 @klass.document_self = false 222 @nest_klass.document_self = false 223 @meth.document_self = false 224 @nest_meth.document_self = false 225 226 @g.reset @top_levels, @klasses 227 228 @g.index_classes 229 230 expected = { 231 :searchIndex => [], 232 :longSearchIndex => [], 233 :info => [], 234 } 235 236 assert_equal expected, @g.index 237 end 238 239 def test_index_methods 240 @g.reset @top_levels, @klasses 241 242 @g.index_methods 243 244 expected = { 245 :searchIndex => %w[meth() meth()], 246 :longSearchIndex => %w[c#meth() c::d#meth()], 247 :info => [ 248 @meth.search_record[2..-1], 249 @nest_meth.search_record[2..-1], 250 ], 251 } 252 253 assert_equal expected, @g.index 254 end 255 256 def test_index_pages 257 @g.reset @top_levels, @klasses 258 259 @g.index_pages 260 261 expected = { 262 :searchIndex => %w[page], 263 :longSearchIndex => [''], 264 :info => [@page.search_record[2..-1]], 265 } 266 267 assert_equal expected, @g.index 268 end 269 270 def test_search_string 271 assert_equal 'cd', @g.search_string('C d') 272 end 273 274end 275 276