[Zope-Checkins] CVS: Zope/lib/python/TAL/tests - test_sourcepos.py:1.2 run.py:1.6

Shane Hathaway shane@cvs.zope.org
Wed, 3 Apr 2002 15:44:29 -0500


Update of /cvs-repository/Zope/lib/python/TAL/tests
In directory cvs.zope.org:/tmp/cvs-serv3170/lib/python/TAL/tests

Modified Files:
	run.py 
Added Files:
	test_sourcepos.py 
Log Message:
Merged shane-better-tracebacks-branch.  The changes are explained in http://dev.zope.org/Wikis/DevSite/Proposals/BetterTracebacks

=== Zope/lib/python/TAL/tests/test_sourcepos.py 1.1 => 1.2 ===
+"""Tests for TALInterpreter."""
+
+import sys
+import unittest
+
+from StringIO import StringIO
+
+from TAL.HTMLTALParser import HTMLTALParser
+from TAL.TALInterpreter import TALInterpreter
+from TAL.TALGenerator import TALGenerator
+from TAL.DummyEngine import DummyEngine
+
+
+page1 = '''<html metal:use-macro="main"><body>
+<div metal:fill-slot="body">
+page1=<span tal:replace="position:" />
+</div>
+</body></html>'''
+
+main_template = '''<html metal:define-macro="main"><body>
+main_template=<span tal:replace="position:" />
+<div metal:define-slot="body" />
+main_template=<span tal:replace="position:" />
+<div metal:use-macro="foot" />
+main_template=<span tal:replace="position:" />
+</body></html>'''
+
+footer = '''<div metal:define-macro="foot">
+footer=<span tal:replace="position:" />
+</div>'''
+
+expected = '''<html><body>
+main_template=main_template (2,14)
+<div>
+page1=page1 (3,6)
+</div>
+main_template=main_template (4,14)
+<div>
+footer=footer (2,7)
+</div>
+main_template=main_template (6,14)
+</body></html>'''
+
+
+
+class Tests(unittest.TestCase):
+
+    def parse(self, eng, s, fn):
+        gen = TALGenerator(expressionCompiler=eng, xml=0, source_file=fn)
+        parser = HTMLTALParser(gen)
+        parser.parseString(s)
+        program, macros = parser.getCode()
+        return program, macros
+
+    def testSourcePositions(self):
+        """Ensure source file and position are set correctly by TAL"""
+        macros = {}
+        eng = DummyEngine(macros)
+        page1_program, page1_macros = self.parse(eng, page1, 'page1')
+        main_template_program, main_template_macros = self.parse(
+            eng, main_template, 'main_template')
+        footer_program, footer_macros = self.parse(eng, footer, 'footer')
+
+        macros['main'] = main_template_macros['main']
+        macros['foot'] = footer_macros['foot']
+
+        stream = StringIO()
+        interp = TALInterpreter(page1_program, macros, eng, stream)
+        interp()
+        self.assertEqual(stream.getvalue().strip(), expected.strip(),
+                         stream.getvalue())
+
+
+def test_suite():
+    suite = unittest.TestSuite()
+    suite.addTest(unittest.makeSuite(Tests))
+    return suite
+
+if __name__ == "__main__":
+    unittest.main()


=== Zope/lib/python/TAL/tests/run.py 1.5 => 1.6 ===
 import test_talinterpreter
 import test_files
+import test_sourcepos
 
 def test_suite():
     suite = unittest.TestSuite()
@@ -18,6 +19,7 @@
         suite.addTest(test_xmlparser.test_suite())
     suite.addTest(test_talinterpreter.test_suite())
     suite.addTest(test_files.test_suite())
+    suite.addTest(test_sourcepos.test_suite())
     return suite
 
 def main():