1 | # -*- coding: utf-8 -*- |
---|
2 | # |
---|
3 | # Copyright (C) 2005-2007 Christopher Lenz <cmlenz@gmx.de> |
---|
4 | # Copyright (C) 2007-2010 Edgewall Software |
---|
5 | # All rights reserved. |
---|
6 | # |
---|
7 | # This software is licensed as described in the file COPYING, which |
---|
8 | # you should have received as part of this distribution. The terms |
---|
9 | # are also available at http://bitten.edgewall.org/wiki/License. |
---|
10 | |
---|
11 | import os |
---|
12 | import shutil |
---|
13 | import tempfile |
---|
14 | import unittest |
---|
15 | |
---|
16 | from bitten.build import xmltools |
---|
17 | from bitten.recipe import Context |
---|
18 | from bitten.util import xmlio |
---|
19 | |
---|
20 | |
---|
21 | class TransformTestCase(unittest.TestCase): |
---|
22 | |
---|
23 | def setUp(self): |
---|
24 | self.basedir = os.path.realpath(tempfile.mkdtemp()) |
---|
25 | self.ctxt = Context(self.basedir) |
---|
26 | |
---|
27 | def tearDown(self): |
---|
28 | shutil.rmtree(self.basedir) |
---|
29 | |
---|
30 | def test_transform_no_src(self): |
---|
31 | self.assertRaises(AssertionError, xmltools.transform, self.ctxt) |
---|
32 | |
---|
33 | def test_transform_no_dest(self): |
---|
34 | self.assertRaises(AssertionError, xmltools.transform, self.ctxt, |
---|
35 | src='src.xml') |
---|
36 | |
---|
37 | def test_transform_no_stylesheet(self): |
---|
38 | self.assertRaises(AssertionError, xmltools.transform, self.ctxt, |
---|
39 | src='src.xml', dest='dest.xml') |
---|
40 | |
---|
41 | def test_transform(self): |
---|
42 | src_file = file(self.ctxt.resolve('src.xml'), 'w') |
---|
43 | try: |
---|
44 | src_file.write("""<doc> |
---|
45 | <title>Document Title</title> |
---|
46 | <section> |
---|
47 | <title>Section Title</title> |
---|
48 | <para>This is a test.</para> |
---|
49 | <note>This is a note.</note> |
---|
50 | </section> |
---|
51 | </doc> |
---|
52 | """) |
---|
53 | finally: |
---|
54 | src_file.close() |
---|
55 | |
---|
56 | style_file = file(self.ctxt.resolve('style.xsl'), 'w') |
---|
57 | try: |
---|
58 | style_file.write("""<xsl:stylesheet version="1.0" |
---|
59 | xmlns:xsl="http://www.w3.org/1999/XSL/Transform" |
---|
60 | xmlns="http://www.w3.org/TR/xhtml1/strict"> |
---|
61 | <xsl:template match="doc"> |
---|
62 | <html> |
---|
63 | <head> |
---|
64 | <title><xsl:value-of select="title"/></title> |
---|
65 | </head> |
---|
66 | <body> |
---|
67 | <xsl:apply-templates/> |
---|
68 | </body> |
---|
69 | </html> |
---|
70 | </xsl:template> |
---|
71 | <xsl:template match="doc/title"> |
---|
72 | <h1><xsl:apply-templates/></h1> |
---|
73 | </xsl:template> |
---|
74 | <xsl:template match="section/title"> |
---|
75 | <h2><xsl:apply-templates/></h2> |
---|
76 | </xsl:template> |
---|
77 | <xsl:template match="para"> |
---|
78 | <p><xsl:apply-templates/></p> |
---|
79 | </xsl:template> |
---|
80 | <xsl:template match="note"> |
---|
81 | <p class="note"><b>NOTE: </b><xsl:apply-templates/></p> |
---|
82 | </xsl:template> |
---|
83 | </xsl:stylesheet> |
---|
84 | """) |
---|
85 | finally: |
---|
86 | style_file.close() |
---|
87 | |
---|
88 | xmltools.transform(self.ctxt, src='src.xml', dest='dest.xml', |
---|
89 | stylesheet='style.xsl') |
---|
90 | |
---|
91 | dest_file = file(self.ctxt.resolve('dest.xml')) |
---|
92 | try: |
---|
93 | dest = xmlio.parse(dest_file) |
---|
94 | finally: |
---|
95 | dest_file.close() |
---|
96 | |
---|
97 | self.assertEqual('html', dest.name) |
---|
98 | self.assertEqual('http://www.w3.org/TR/xhtml1/strict', dest.namespace) |
---|
99 | children = list(dest.children()) |
---|
100 | self.assertEqual(2, len(children)) |
---|
101 | self.assertEqual('head', children[0].name) |
---|
102 | head_children = list(children[0].children()) |
---|
103 | self.assertEqual(1, len(head_children)) |
---|
104 | self.assertEqual('title', head_children[0].name) |
---|
105 | self.assertEqual('Document Title', head_children[0].gettext()) |
---|
106 | self.assertEqual('body', children[1].name) |
---|
107 | body_children = list(children[1].children()) |
---|
108 | self.assertEqual(4, len(body_children)) |
---|
109 | self.assertEqual('h1', body_children[0].name) |
---|
110 | self.assertEqual('Document Title', body_children[0].gettext()) |
---|
111 | self.assertEqual('h2', body_children[1].name) |
---|
112 | self.assertEqual('Section Title', body_children[1].gettext()) |
---|
113 | self.assertEqual('p', body_children[2].name) |
---|
114 | self.assertEqual('This is a test.', body_children[2].gettext()) |
---|
115 | self.assertEqual('p', body_children[3].name) |
---|
116 | self.assertEqual('note', body_children[3].attr['class']) |
---|
117 | self.assertEqual('This is a note.', body_children[3].gettext()) |
---|
118 | |
---|
119 | |
---|
120 | def suite(): |
---|
121 | suite = unittest.TestSuite() |
---|
122 | if xmltools.have_libxslt or xmltools.have_msxml: |
---|
123 | suite.addTest(unittest.makeSuite(TransformTestCase, 'test')) |
---|
124 | return suite |
---|
125 | |
---|
126 | if __name__ == '__main__': |
---|
127 | unittest.main(defaultTest='suite') |
---|