# Copyright 2009-2010 ITA Software, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import os from lxml import etree from twisted.trial import unittest from nagcat import errors, filters class XPathTestCase(unittest.TestCase): example = """
Text #1
Text #2
""" def testBasic(self): f = filters.Filter(object(), "xpath://div/text()") self.assertEquals(f.filter(self.example), "This has been a test") def testMissing(self): f = filters.Filter(object(), "xpath://span/text()") self.assertIsInstance(f.filter(self.example), errors.Failure) def testDefault(self): f = filters.Filter(object(), "xpath[none]://span/text()") self.assertEquals(f.filter(self.example), "none") def testBad(self): f = filters.Filter(object(), "xpath://span/text()") self.assertIsInstance(f.filter("Text #1
\nText #2
") class XSLTTestCase(unittest.TestCase): # example swiped from wikipedia raw_xml = """Text #1
Text #2
""" expect = """Text #1
Text #2
""" def testBasic(self): f = filters.Filter(object(), "html") xml = f.filter(self.example) self.assertIsInstance(xml, str) self.assertEqualsXML(xml, self.expect) def assertEqualsXML(self, result, expect): # Parse the xml, strip white space, and convert back # this allows us to compare if they are logically equal parser = etree.XMLParser(remove_blank_text=True) result = etree.tostring(etree.XML(result, parser)) expect = etree.tostring(etree.XML(expect, parser)) self.assertEquals(result, expect)