Added self-test attribute to validator.
[0publish.git] / tests / testvalidator.py
blob412a8cba70ce73644127db33833a55356a156e57
1 #!/usr/bin/env python2.4
2 import sys, logging
3 from StringIO import StringIO
4 from zeroinstall.injector.reader import InvalidInterface
5 import unittest
7 sys.path.insert(0, '..')
9 import validator
11 header = """<?xml version="1.0" ?>
12 <?xml-stylesheet type='text/xsl' href='http://0install.net/2006/stylesheets/interface.xsl'?>
13 <interface xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
14 <name>test</name>
15 <summary>for testing</summary>
16 <description>
17 This is for testing.
18 </description>
19 <homepage>http://0install.net</homepage>
20 <feed-for interface='http://0install.net/2007/tests/just-testing.xml'/>"""
21 footer = """
22 </interface>
23 """
25 root_logger = logging.getLogger()
27 def check(xml, expectWarnings = ""):
28 my_log_stream = StringIO()
29 my_handler = logging.StreamHandler(my_log_stream)
31 root_logger.handlers = []
33 root_logger.addHandler(my_handler)
34 old_stderr = sys.stderr
35 try:
36 sys.stderr = my_log_stream
37 validator.check(xml)
38 warnings = my_log_stream.getvalue()
39 finally:
40 sys.stderr = old_stderr
41 root_logger.removeHandler(my_handler)
42 if expectWarnings:
43 assert expectWarnings in warnings, "Expected warning '%s' not in '%s'" % (expectWarnings, warnings)
44 elif warnings:
45 raise Exception("Unexpected warnings '%s'" % warnings)
47 class TestValidator(unittest.TestCase):
48 def testSimple(self):
49 check(header + footer)
51 def testWarnings(self):
52 check(header + "<foo/>" + footer, expectWarnings = "Unknown Zero Install element <foo>")
53 check(header + "<foo:bar xmlns:foo='http://foo'/>" + footer)
54 check(header + "<group foo='bar'/>" + footer, expectWarnings = "Non Zero-Install attributes should be namespaced")
56 def testInvalid(self):
57 try:
58 check(header)
59 except InvalidInterface, ex:
60 assert "Invalid XML" in str(ex), ex
62 try:
63 check(header + "<implementation/>" + footer)
64 except InvalidInterface, ex:
65 assert "Missing 'id' attribute" in str(ex), ex
67 suite = unittest.makeSuite(TestValidator)
68 if __name__ == '__main__':
69 sys.argv.append('-v')
70 unittest.main()