| OLD | NEW |
| (Empty) |
| 1 # IMPORTANT: | |
| 2 # When all the unit tests for Lore run, there's one more test to do: | |
| 3 # from a shell, | |
| 4 # cd Twisted/ | |
| 5 # admin/process-docs | |
| 6 # It takes a while to run (2:03 on a reasonable box) | |
| 7 # Make sure there are no errors! Warnings are OK. | |
| 8 | |
| 9 # ++ single anchor added to individual output file | |
| 10 # ++ two anchors added to individual output file | |
| 11 # ++ anchors added to individual output files | |
| 12 # ++ entry added to index | |
| 13 # ++ index entry pointing to correct file and anchor | |
| 14 # ++ multiple entries added to index | |
| 15 # ++ multiple index entries pointing to correct files and anchors | |
| 16 # __ all of above for files in deep directory structure | |
| 17 # | |
| 18 # ++ group index entries by indexed term | |
| 19 # ++ sort index entries by indexed term | |
| 20 # __ hierarchical index entries (e.g. language!programming) | |
| 21 # | |
| 22 # ++ add parameter for what the index filename should be | |
| 23 # ++ add (default) ability to NOT index (if index not specified) | |
| 24 # | |
| 25 # ++ put actual index filename into INDEX link (if any) in the template | |
| 26 # __ make index links RELATIVE! | |
| 27 # __ make index pay attention to the outputdir! | |
| 28 # | |
| 29 # __ make index look nice | |
| 30 # | |
| 31 # ++ add section numbers to headers in lore output | |
| 32 # ++ make text of index entry links be chapter numbers | |
| 33 # ++ make text of index entry links be section numbers | |
| 34 # | |
| 35 # __ put all of our test files someplace neat and tidy | |
| 36 # | |
| 37 | |
| 38 import os, shutil | |
| 39 from StringIO import StringIO | |
| 40 | |
| 41 from twisted.trial import unittest | |
| 42 | |
| 43 from twisted.lore import tree, process, indexer, numberer, htmlbook, default | |
| 44 from twisted.lore.default import factory | |
| 45 from twisted.lore.latex import LatexSpitter | |
| 46 | |
| 47 from twisted.python.util import sibpath | |
| 48 | |
| 49 from twisted.lore.scripts import lore | |
| 50 | |
| 51 from twisted.web import microdom, domhelpers | |
| 52 | |
| 53 def sp(originalFileName): | |
| 54 return sibpath(__file__, originalFileName) | |
| 55 | |
| 56 options = {"template" : sp("template.tpl"), 'baseurl': '%s', 'ext': '.xhtml' } | |
| 57 d = options | |
| 58 | |
| 59 def filenameGenerator(originalFileName, outputExtension): | |
| 60 return os.path.splitext(originalFileName)[0]+"1"+outputExtension | |
| 61 | |
| 62 def filenameGenerator2(originalFileName, outputExtension): | |
| 63 return os.path.splitext(originalFileName)[0]+"2"+outputExtension | |
| 64 | |
| 65 | |
| 66 class TestFactory(unittest.TestCase): | |
| 67 | |
| 68 file = sp('simple.html') | |
| 69 linkrel = "" | |
| 70 | |
| 71 def assertEqualFiles1(self, exp, act): | |
| 72 if (exp == act): return True | |
| 73 fact = open(act) | |
| 74 self.assertEqualsFile(exp, fact.read()) | |
| 75 | |
| 76 def assertEqualFiles(self, exp, act): | |
| 77 if (exp == act): return True | |
| 78 fact = open(sp(act)) | |
| 79 self.assertEqualsFile(exp, fact.read()) | |
| 80 | |
| 81 def assertEqualsFile(self, exp, act): | |
| 82 expected = open(sp(exp)).read() | |
| 83 self.assertEqualsString(expected, act) | |
| 84 | |
| 85 def assertEqualsString(self, expected, act): | |
| 86 if len(expected) != len(act): print "Actual: " + act ##d | |
| 87 self.assertEquals(len(expected), len(act)) | |
| 88 for i in range(len(expected)): | |
| 89 e = expected[i] | |
| 90 a = act[i] | |
| 91 self.assertEquals(e, a, "differ at %d: %s vs. %s" % (i, e, a)) | |
| 92 self.assertEquals(expected, act) | |
| 93 | |
| 94 def makeTemp(self, *filenames): | |
| 95 tmp = self.mktemp() | |
| 96 os.mkdir(tmp) | |
| 97 for filename in filenames: | |
| 98 tmpFile = os.path.join(tmp, filename) | |
| 99 shutil.copyfile(sp(filename), tmpFile) | |
| 100 return tmp | |
| 101 | |
| 102 ######################################## | |
| 103 | |
| 104 def setUp(self): | |
| 105 indexer.reset() | |
| 106 numberer.reset() | |
| 107 | |
| 108 def testProcessingFunctionFactory(self): | |
| 109 htmlGenerator = factory.generate_html(options) | |
| 110 htmlGenerator(self.file, self.linkrel) | |
| 111 self.assertEqualFiles('good_simple.xhtml', 'simple.xhtml') | |
| 112 | |
| 113 def testProcessingFunctionFactoryWithFilenameGenerator(self): | |
| 114 htmlGenerator = factory.generate_html(options, filenameGenerator2) | |
| 115 htmlGenerator(self.file, self.linkrel) | |
| 116 self.assertEqualFiles('good_simple.xhtml', 'simple2.xhtml') | |
| 117 | |
| 118 def test_doFile(self): | |
| 119 templ = microdom.parse(open(d['template'])) | |
| 120 | |
| 121 tree.doFile(self.file, self.linkrel, d['ext'], d['baseurl'], templ, d) | |
| 122 self.assertEqualFiles('good_simple.xhtml', 'simple.xhtml') | |
| 123 | |
| 124 def test_doFile_withFilenameGenerator(self): | |
| 125 templ = microdom.parse(open(d['template'])) | |
| 126 | |
| 127 tree.doFile(self.file, self.linkrel, d['ext'], d['baseurl'], templ, d, f
ilenameGenerator) | |
| 128 self.assertEqualFiles('good_simple.xhtml', 'simple1.xhtml') | |
| 129 | |
| 130 def test_munge(self): | |
| 131 indexer.setIndexFilename("lore_index_file.html") | |
| 132 doc = microdom.parse(open(self.file)) | |
| 133 templ = microdom.parse(open(d['template'])) | |
| 134 node = templ.cloneNode(1) | |
| 135 tree.munge(doc, node, self.linkrel, | |
| 136 os.path.dirname(self.file), | |
| 137 self.file, | |
| 138 d['ext'], d['baseurl'], d) | |
| 139 self.assertEqualsFile('good_internal.xhtml', node.toprettyxml()) | |
| 140 | |
| 141 def test_getProcessor(self): | |
| 142 options = { 'template': sp('template.tpl'), 'ext': '.xhtml', 'baseurl':
'burl', | |
| 143 'filenameMapping': None } | |
| 144 p = process.getProcessor(default, "html", options) | |
| 145 p(sp('simple3.html'), self.linkrel) | |
| 146 self.assertEqualFiles('good_simple.xhtml', 'simple3.xhtml') | |
| 147 | |
| 148 def test_getProcessorWithFilenameGenerator(self): | |
| 149 options = { 'template': sp('template.tpl'), | |
| 150 'ext': '.xhtml', | |
| 151 'baseurl': 'burl', | |
| 152 'filenameMapping': 'addFoo' } | |
| 153 p = process.getProcessor(default, "html", options) | |
| 154 p(sp('simple4.html'), self.linkrel) | |
| 155 self.assertEqualFiles('good_simple.xhtml', 'simple4foo.xhtml') | |
| 156 | |
| 157 def test_outputdirGenerator(self): | |
| 158 normp = os.path.normpath; join = os.path.join | |
| 159 inputdir = normp(join("/", 'home', 'joe')) | |
| 160 outputdir = normp(join("/", 'away', 'joseph')) | |
| 161 actual = process.outputdirGenerator(join("/", 'home', 'joe', "myfile.htm
l"), | |
| 162 '.xhtml', inputdir, outputdir) | |
| 163 expected = normp(join("/", 'away', 'joseph', 'myfile.xhtml')) | |
| 164 self.assertEquals(expected, actual) | |
| 165 | |
| 166 def test_outputdirGeneratorBadInput(self): | |
| 167 options = {'outputdir': '/away/joseph/', 'inputdir': '/home/joe/' } | |
| 168 self.assertRaises(ValueError, process.outputdirGenerator, '.html', '.xht
ml', **options) | |
| 169 | |
| 170 def test_makeSureDirectoryExists(self): | |
| 171 dirname = os.path.join("tmp", 'nonexistentdir') | |
| 172 if os.path.exists(dirname): | |
| 173 os.rmdir(dirname) | |
| 174 self.failIf(os.path.exists(dirname), "Hey: someone already created the d
ir") | |
| 175 filename = os.path.join(dirname, 'newfile') | |
| 176 tree.makeSureDirectoryExists(filename) | |
| 177 self.failUnless(os.path.exists(dirname), 'should have created dir') | |
| 178 os.rmdir(dirname) | |
| 179 | |
| 180 def test_indexAnchorsAdded(self): | |
| 181 indexer.setIndexFilename('theIndexFile.html') | |
| 182 # generate the output file | |
| 183 templ = microdom.parse(open(d['template'])) | |
| 184 tmp = self.makeTemp('lore_index_test.xhtml') | |
| 185 | |
| 186 tree.doFile(os.path.join(tmp, 'lore_index_test.xhtml'), | |
| 187 self.linkrel, '.html', d['baseurl'], templ, d) | |
| 188 self.assertEqualFiles1("lore_index_test_out.html", | |
| 189 os.path.join(tmp, "lore_index_test.html")) | |
| 190 | |
| 191 def test_indexEntriesAdded(self): | |
| 192 indexer.addEntry('lore_index_test.html', 'index02', 'language of program
ming', '1.3') | |
| 193 indexer.addEntry('lore_index_test.html', 'index01', 'programming languag
e', '1.2') | |
| 194 indexer.setIndexFilename("lore_index_file.html") | |
| 195 indexer.generateIndex() | |
| 196 self.assertEqualFiles1("lore_index_file_out.html", "lore_index_file.html
") | |
| 197 | |
| 198 def test_book(self): | |
| 199 tmp = self.makeTemp() | |
| 200 inputFilename = sp('lore_index_test.xhtml') | |
| 201 | |
| 202 bookFilename = os.path.join(tmp, 'lore_test_book.book') | |
| 203 bf = open(bookFilename, 'w') | |
| 204 bf.write('Chapter(r"%s", None)\r\n' % inputFilename) | |
| 205 bf.close() | |
| 206 | |
| 207 book = htmlbook.Book(bookFilename) | |
| 208 expected = {'indexFilename': None, | |
| 209 'chapters': [(inputFilename, None)], | |
| 210 } | |
| 211 dct = book.__dict__ | |
| 212 for k in dct: | |
| 213 self.assertEquals(dct[k], expected[k]) | |
| 214 | |
| 215 def test_runningLore(self): | |
| 216 options = lore.Options() | |
| 217 tmp = self.makeTemp('lore_index_test.xhtml') | |
| 218 | |
| 219 templateFilename = sp('template.tpl') | |
| 220 inputFilename = os.path.join(tmp, 'lore_index_test.xhtml') | |
| 221 indexFilename = 'theIndexFile' | |
| 222 | |
| 223 bookFilename = os.path.join(tmp, 'lore_test_book.book') | |
| 224 bf = open(bookFilename, 'w') | |
| 225 bf.write('Chapter(r"%s", None)\n' % inputFilename) | |
| 226 bf.close() | |
| 227 | |
| 228 options.parseOptions(['--null', '--book=%s' % bookFilename, | |
| 229 '--config', 'template=%s' % templateFilename, | |
| 230 '--index=%s' % indexFilename | |
| 231 ]) | |
| 232 result = lore.runGivenOptions(options) | |
| 233 self.assertEquals(None, result) | |
| 234 self.assertEqualFiles1("lore_index_file_unnumbered_out.html", indexFilen
ame + ".html") | |
| 235 | |
| 236 def test_runningLoreMultipleFiles(self): | |
| 237 tmp = self.makeTemp('lore_index_test.xhtml', 'lore_index_test2.xhtml') | |
| 238 templateFilename = sp('template.tpl') | |
| 239 inputFilename = os.path.join(tmp, 'lore_index_test.xhtml') | |
| 240 inputFilename2 = os.path.join(tmp, 'lore_index_test2.xhtml') | |
| 241 indexFilename = 'theIndexFile' | |
| 242 | |
| 243 bookFilename = os.path.join(tmp, 'lore_test_book.book') | |
| 244 bf = open(bookFilename, 'w') | |
| 245 bf.write('Chapter(r"%s", None)\n' % inputFilename) | |
| 246 bf.write('Chapter(r"%s", None)\n' % inputFilename2) | |
| 247 bf.close() | |
| 248 | |
| 249 options = lore.Options() | |
| 250 options.parseOptions(['--null', '--book=%s' % bookFilename, | |
| 251 '--config', 'template=%s' % templateFilename, | |
| 252 '--index=%s' % indexFilename | |
| 253 ]) | |
| 254 result = lore.runGivenOptions(options) | |
| 255 self.assertEquals(None, result) | |
| 256 self.assertEqualFiles1("lore_index_file_unnumbered_multiple_out.html", i
ndexFilename + ".html") | |
| 257 self.assertEqualFiles1("lore_index_test_out.html", | |
| 258 os.path.join(tmp, "lore_index_test.html")) | |
| 259 self.assertEqualFiles1("lore_index_test_out2.html", | |
| 260 os.path.join(tmp, "lore_index_test2.html")) | |
| 261 | |
| 262 def XXXtest_NumberedSections(self): | |
| 263 # run two files through lore, with numbering turned on | |
| 264 # every h2 should be numbered: | |
| 265 # first file's h2s should be 1.1, 1.2 | |
| 266 # second file's h2s should be 2.1, 2.2 | |
| 267 templateFilename = sp('template.tpl') | |
| 268 inputFilename = sp('lore_numbering_test.xhtml') | |
| 269 inputFilename2 = sp('lore_numbering_test2.xhtml') | |
| 270 indexFilename = 'theIndexFile' | |
| 271 | |
| 272 # you can number without a book: | |
| 273 options = lore.Options() | |
| 274 options.parseOptions(['--null', | |
| 275 '--index=%s' % indexFilename, | |
| 276 '--config', 'template=%s' % templateFilename, | |
| 277 '--config', 'ext=%s' % ".tns", | |
| 278 '--number', | |
| 279 inputFilename, inputFilename2]) | |
| 280 result = lore.runGivenOptions(options) | |
| 281 | |
| 282 self.assertEquals(None, result) | |
| 283 #self.assertEqualFiles1("lore_index_file_out_multiple.html", indexFilena
me + ".tns") | |
| 284 # VVV change to new, numbered files | |
| 285 self.assertEqualFiles("lore_numbering_test_out.html", "lore_numbering_te
st.tns") | |
| 286 self.assertEqualFiles("lore_numbering_test_out2.html", "lore_numbering_t
est2.tns") | |
| 287 | |
| 288 | |
| 289 def test_setIndexLink(self): | |
| 290 """ | |
| 291 Tests to make sure that index links are processed when an index page | |
| 292 exists and removed when there is not. | |
| 293 """ | |
| 294 templ = microdom.parse(open(d['template'])) | |
| 295 indexFilename = 'theIndexFile' | |
| 296 numLinks = len(domhelpers.findElementsWithAttribute(templ, | |
| 297 "class", | |
| 298 "index-link")) | |
| 299 | |
| 300 # if our testing template has no index-link nodes, complain about it | |
| 301 self.assertNotEquals( | |
| 302 [], | |
| 303 domhelpers.findElementsWithAttribute(templ, | |
| 304 "class", | |
| 305 "index-link")) | |
| 306 | |
| 307 tree.setIndexLink(templ, indexFilename) | |
| 308 | |
| 309 self.assertEquals( | |
| 310 [], | |
| 311 domhelpers.findElementsWithAttribute(templ, | |
| 312 "class", | |
| 313 "index-link")) | |
| 314 | |
| 315 indexLinks = domhelpers.findElementsWithAttribute(templ, | |
| 316 "href", | |
| 317 indexFilename) | |
| 318 self.assertTrue(len(indexLinks) >= numLinks) | |
| 319 | |
| 320 templ = microdom.parse(open(d['template'])) | |
| 321 self.assertNotEquals( | |
| 322 [], | |
| 323 domhelpers.findElementsWithAttribute(templ, | |
| 324 "class", | |
| 325 "index-link")) | |
| 326 indexFilename = None | |
| 327 | |
| 328 tree.setIndexLink(templ, indexFilename) | |
| 329 | |
| 330 self.assertEquals( | |
| 331 [], | |
| 332 domhelpers.findElementsWithAttribute(templ, | |
| 333 "class", | |
| 334 "index-link")) | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 class LatexSpitterTestCase(unittest.TestCase): | |
| 340 """ | |
| 341 Tests for the Latex output plugin. | |
| 342 """ | |
| 343 def test_indexedSpan(self): | |
| 344 """ | |
| 345 Test processing of a span tag with an index class results in a latex | |
| 346 \\index directive the correct value. | |
| 347 """ | |
| 348 dom = microdom.parseString('<span class="index" value="name" />').docume
ntElement | |
| 349 out = StringIO() | |
| 350 spitter = LatexSpitter(out.write) | |
| 351 spitter.visitNode(dom) | |
| 352 self.assertEqual(out.getvalue(), u'\\index{name}\n') | |
| OLD | NEW |