logilab/doctools

view test/validation_tests.py @ 0:cc367abb080e

forget the past. forget the past.
author root
date Wed, 26 Apr 2006 10:48:09 +0000
parents
children 23f217b6e336
line source
1 # -*- coding: ISO-8859-1 -*-
2 """Validation tests for doctools
4 These tests are based on the specification document (see doc/spec_mkdoc.pdf)
5 """
6 MKDOC = '../bin/mkdoc'
7 DEBUG = 0
9 __revision__ = '$Id: validation_tests.py,v 1.26 2005-11-30 15:40:16 alf Exp $'
12 import unittest
13 import os, sys, shutil
15 SAMPLEDOC = '''<?xml version="1.0" encoding="iso-8859-15"?>
16 <!DOCTYPE article>
17 %s
18 <article id="test" lang="fr">
19 <articleinfo>
20 <title>Test</title>
21 <subtitle>Gros test</subtitle>
22 <author><firstname>Alexandre</firstname><surname>FAYOLLE</surname></author>
23 <copyright><year>2003</year><holder>Logilab</holder></copyright>
24 </articleinfo>
25 <section><title>Présentation</title>
26 <para>Test des utilitaires mkdoc de <orgname>Logilab</orgname>.</para>
27 </section>
28 </article>'''
30 SAMPLELETTRE = '''<?xml version="1.0" encoding="iso-8859-15"?>
31 <!DOCTYPE lettre>
32 %s
33 <lettre id="test" lang="fr">
34 <articleinfo>
35 <title>Test</title>
36 <subtitle>Gros test</subtitle>
37 <author><firstname>Alexandre</firstname><surname>FAYOLLE</surname></author>
38 <copyright><year>2003</year><holder>Logilab</holder></copyright>
39 </articleinfo>
40 <section><title>Présentation</title>
41 <para>Test des utilitaires mkdoc de <orgname>Logilab</orgname>.</para>
42 </section>
43 </lettre>'''
45 SAMPLECONTRAT = '''<?xml version="1.0" encoding="iso-8859-15"?>
46 <!DOCTYPE contrat>
47 %s
48 <contrat id="test" lang="fr">
49 <articleinfo>
50 <title>Test</title>
51 <subtitle>Gros test</subtitle>
52 <author><firstname>Alexandre</firstname><surname>FAYOLLE</surname></author>
53 <copyright><year>2003</year><holder>Logilab</holder></copyright>
54 </articleinfo>
55 <section><title>Présentation</title>
56 <para>Test des utilitaires mkdoc de <orgname>Logilab</orgname>.</para>
57 </section>
58 </contrat>'''
60 SAMPLESITE = '''<?xml version="1.0" encoding="iso-8859-15"?>
61 <!DOCTYPE site>
62 %s
63 <site id="test" lang="fr" target-dir="html">
64 <page filename="index">
65 <title>Test</title>
66 <para>Gros test</para>
67 </page>
68 </site>'''
70 SAMPLEREST = '''Test ReST
71 =========
73 :Author: Sylvain Thénault
74 :Organization: Logilab
75 :Version: $Revision: 1.26 $
76 :Date: $Date: 2005-11-30 15:40:16 $
77 :Abstract:
78 Test de la conversion avec du ReST
80 Test1
81 -----
83 Description
84 ```````````
85 Des tests et du text à convertir. Convertit des fichiers au format ReST_ (Restructured Text) ou Docbook_ dans divers formats tels que html ou pdf.
87 .. _ReST: http://docutils.sourceforge.net/rst.html
88 .. _Docbook: http://www.docbook.org
91 Les subsititutions avec des charactères non latin-1 sont également mises en |oe| uvre.
93 .. |oe| unicode:: &#x0153;
94 :rtrim:
96 '''
98 SAMPLEFO = '''<?xml version="1.0" encoding="UTF-8"?>
99 <root xmlns="http://www.w3.org/1999/XSL/Format" font-size="16pt">
100 <layout-master-set>
101 <simple-page-master
102 margin-right="15mm" margin-left="15mm"
103 margin-bottom="15mm" margin-top="15mm"
104 page-width="210mm" page-height="297mm"
105 master-name="bookpage">
106 <region-body region-name="bookpage-body"
107 margin-bottom="5mm" margin-top="5mm" />
108 </simple-page-master>
109 </layout-master-set>
110 <page-sequence master-reference="bookpage">
111 <title>Hello world example</title>
112 <flow flow-name="bookpage-body">
113 <block>Hello XSLFO!</block>
114 </flow>
115 </page-sequence>
116 </root>'''
118 EXTENSIONS = {
119 'pdf': 'pdf',
120 'html': 'html',
121 'docbook': 'xml',
122 }
124 class TestMkdoc(unittest.TestCase):
125 """Base class. Defines some helper functions and no tests"""
127 EXTENSION = 'xml'
128 TARGET = None
129 def setUp(self):
130 """initialize the name of the file to process and the name of the result"""
131 self.filename = mktemp('.' + self.EXTENSION)
132 self.targetname = os.path.basename(self.filename).replace(self.EXTENSION,
133 EXTENSIONS[self.TARGET])
135 def tearDown(self):
136 """remove generated files"""
137 os.remove(self.filename)
138 if os.path.exists(self.targetname):
139 os.remove(self.targetname)
142 def run_mkdoc(self, doc, style=None, **additional_options):
143 f = open(self.filename,'w')
144 f.write(doc)
145 f.close()
146 commandline = [MKDOC, '--target', self.TARGET, '--check', 'no']
147 if not DEBUG:
148 commandline.append('--quiet')
149 if style:
150 commandline.append('--stylesheet')
151 commandline.append(style)
152 for key, val in additional_options.items():
153 commandline.append('--%s' % key)
154 commandline.append(val)
156 commandline.append(self.filename)
157 if DEBUG:
158 print '*'*80
159 print ' '.join(commandline)
160 print 'target', self.targetname
161 sys.stdout.flush()
162 status = os.spawnv(os.P_WAIT,MKDOC, commandline)
163 return status
165 def make_doc(self, target, xslt, sample=SAMPLEDOC):
166 """default implementation for XML documents"""
167 PI = '<?logidoc-style target="%s" xslt="%s"?>'%(target,xslt)
168 doc = sample % PI
169 return doc
172 def default_test(self,target=None,xslt=None, **additional_options):
173 """The average default test.
174 Tests that mkdoc runs fine and that a file is generated
175 with some contents"""
176 doc = self.make_doc(target, xslt)
177 status = self.run_mkdoc(doc, xslt, **additional_options)
178 self.failUnlessEqual(status,0)
179 self.failUnless(os.path.isfile(self.targetname))
180 self.failUnless(os.path.getsize(self.targetname)>0)
182 class Xml2Pdf(TestMkdoc):
183 """Tests the XML to PDF conversions"""
184 TARGET = 'pdf'
186 def test_pdf_standard(self):
187 self.default_test('pdf','standard')
189 def test_pdf_standard(self):
190 self.default_test('pdf','standard')
192 def test_pdf_ao(self):
193 self.default_test('pdf','reponse-ao')
195 def test_pdf_admin(self):
196 self.default_test('pdf','rapport-admin')
198 def test_pdf_pubtech(self):
199 self.default_test('pdf','publi-technique')
201 def test_pdf_pubcom(self):
202 self.default_test('pdf','publi-commerciale')
204 def test_pdf_lettre(self):
205 doc = self.make_doc(self.TARGET, 'lettre', sample=SAMPLELETTRE)
206 status = self.run_mkdoc(doc)
207 self.failUnlessEqual(status,0)
208 self.failUnless(os.path.isfile(self.targetname))
210 def test_pdf_contrat(self):
211 doc = self.make_doc(self.TARGET, 'contrat', sample=SAMPLECONTRAT)
212 status = self.run_mkdoc(doc)
213 self.failUnlessEqual(status,0)
214 self.failUnless(os.path.isfile(self.targetname))
216 def test_pdf_formation(self):
217 self.default_test('pdf','catalogue-formation')
220 class Xml2Html(TestMkdoc):
221 """Tests the XML to HTML conversions"""
222 EXTENSION = 'dbk'
223 TARGET = 'html'
225 def tearDown(self):
226 """remove generated files"""
227 if os.path.exists(self.filename):
228 os.remove(self.filename)
229 if os.path.exists(self.targetname):
230 os.remove(self.targetname)
231 if os.path.exists('html'):
232 shutil.rmtree('html')
234 def multi_test(self, target=None, xslt=None, sample=SAMPLEDOC,
235 **additional_options):
236 """The average default test.
237 Tests that mkdoc runs fine and that a file is generated
238 with some contents"""
239 doc = self.make_doc(target, xslt, sample)
240 status = self.run_mkdoc(doc, xslt, **additional_options)
241 self.failUnlessEqual(status,0)
242 self.failUnless(os.path.isdir('html'))
243 self.failUnless(os.path.isfile('html/index.html'))
245 def test_html_standard(self):
246 self.default_test('html','standard')
248 def test_html_single(self):
249 self.default_test('html','single-file')
251 def test_html_multi(self):
252 self.multi_test('html','multi-files')
254 def test_html_website(self):
255 self.multi_test('html','web-site', sample=SAMPLESITE)
257 class Rest2Html(Xml2Html):
258 EXTENSION = 'rst'
260 def make_doc(self, target, xslt, sample=None):
261 return SAMPLEREST
263 def test_html_website(self):
264 pass
265 #self.multi_test('html','web-site', doctype='site')
267 class Rest2Pdf(Xml2Pdf):
268 EXTENSION = 'txt'
270 def test_pdf_admin(self):
271 self.default_test('pdf','rapport-admin', doctype='article')
273 def test_pdf_lettre(self):
274 self.default_test('pdf','lettre', doctype='lettre')
276 def test_pdf_contrat(self):
277 self.default_test('pdf','contrat', doctype='contrat')
279 def test_pdf_pubcom(self):
280 self.default_test('pdf','publi-commerciale', doctype='article')
282 def make_doc(self,target,xslt):
283 return SAMPLEREST
286 class Rest2Docbook(TestMkdoc):
287 EXTENSION = 'rest'
288 TARGET = 'docbook'
289 def make_doc(self,*args):
290 return SAMPLEREST
292 def test_rest_to_docbook(self):
293 self.default_test()
295 class Fo2Pdf(TestMkdoc):
296 EXTENSION = 'fo'
297 TARGET = 'pdf'
298 def make_doc(self,*args):
299 return SAMPLEFO
301 def test_fo_to_pdf(self):
302 self.default_test()
305 class Xml2PdfWithPreprocess(TestMkdoc):
306 EXTENSION = 'xml'
307 TARGET = 'pdf'
308 def make_doc(self,target,xslt):
309 """default implementation for XML documents"""
310 PI = '<?logidoc-style target="%s" xslt="%s"?>'%(target,xslt)
311 PI2 = '<?logidoc-preprocess xslt="session2prg-cours"?>'
312 doc = SAMPLEDOC % (PI + PI2)
313 return doc
315 def test_pdf_admin(self):
316 self.default_test('pdf','rapport-admin')
320 def ensure_mkdoc_executable():
321 import stat
322 mask = stat.S_IMODE(os.stat(MKDOC)[stat.ST_MODE])
323 if not mask & 0111:
324 try:
325 os.chmod(MKDOC, mask|0111)
326 except OSError:
327 sys.exit('%s is not executable')
329 def mktemp(extension=''):
330 import time, md5
331 while 1:
332 filename = '/tmp/temp_%s%s'%(md5.new(str(time.time())).hexdigest(),
333 extension)
334 if os.path.exists(filename):
335 time.sleep(.1)
336 else:
337 return filename
340 ensure_mkdoc_executable()
342 def suite():
343 loader = unittest.TestLoader()
344 testsuite = loader.loadTestsFromModule(sys.modules[__name__])
345 return testsuite
348 if __name__ == '__main__':
349 if os.environ.get('PYUNIT', 'text') == 'graphic':
350 try:
351 from unittestgui import main
352 except ImportError:
353 from unittest import main
354 else:
355 from unittest import main
356 main()