blob: 16a4b1a0347289cc9a34b6da18ec171a84f72b34 [file] [log] [blame]
Brian Curtin6a4ffd72011-07-05 19:14:16 -05001from test.support import run_unittest
2import unittest
3import sys
4import subprocess
5import cgitb
6
7class TestCgitb(unittest.TestCase):
Brian Curtin692e26b2011-07-05 19:16:37 -05008
Brian Curtin6a4ffd72011-07-05 19:14:16 -05009 def test_fonts(self):
10 text = "Hello Robbie!"
11 self.assertEqual(cgitb.small(text), "<small>{}</small>".format(text))
12 self.assertEqual(cgitb.strong(text), "<strong>{}</strong>".format(text))
13 self.assertEqual(cgitb.grey(text),
14 '<font color="#909090">{}</font>'.format(text))
Brian Curtin692e26b2011-07-05 19:16:37 -050015
Brian Curtin6a4ffd72011-07-05 19:14:16 -050016 def test_blanks(self):
17 self.assertEqual(cgitb.small(""), "")
18 self.assertEqual(cgitb.strong(""), "")
19 self.assertEqual(cgitb.grey(""), "")
Brian Curtin692e26b2011-07-05 19:16:37 -050020
Brian Curtin6a4ffd72011-07-05 19:14:16 -050021 def test_html(self):
22 try:
23 raise ValueError("Hello World")
24 except ValueError as err:
25 # If the html was templated we could do a bit more here.
26 # At least check that we get details on what we just raised.
27 html = cgitb.html(sys.exc_info())
28 self.assertIn("ValueError", html)
29 self.assertIn(str(err), html)
30
31 def test_text(self):
32 try:
33 raise ValueError("Hello World")
34 except ValueError as err:
35 text = cgitb.text(sys.exc_info())
36 self.assertIn("ValueError", text)
37 self.assertIn("Hello World", text)
Brian Curtin692e26b2011-07-05 19:16:37 -050038
Brian Curtin6a4ffd72011-07-05 19:14:16 -050039 def test_hook(self):
40 proc = subprocess.Popen([sys.executable, '-c',
Brian Curtin692e26b2011-07-05 19:16:37 -050041 ('import cgitb;'
42 'cgitb.enable();'
Brian Curtin6a4ffd72011-07-05 19:14:16 -050043 'raise ValueError("Hello World")')],
44 stdout=subprocess.PIPE)
45 out = proc.stdout.read().decode(sys.getfilesystemencoding())
46 self.addCleanup(proc.stdout.close)
47 self.assertIn("ValueError", out)
48 self.assertIn("Hello World", out)
49
50
51def test_main():
52 run_unittest(TestCgitb)
Brian Curtin692e26b2011-07-05 19:16:37 -050053
Brian Curtin6a4ffd72011-07-05 19:14:16 -050054if __name__ == "__main__":
55 test_main()