import unittest
from xhtml2rest import main
from StringIO import StringIO

class XHTML2ReSTTestCase(unittest.TestCase):
    """ test the (3rd party for us!) xhtml2rst library
    
        we're not too eager on testing the whole thing here... just want to
        know if it supports the subset of xhtml that we use/produce in the
        Silva CMS
    """
    def test_no_body(self):
        s = StringIO()
        self.assertRaises(Exception, main, 'foo', s)

    def test_plain_text(self):
        html = '<body>foo</body>'
        s = StringIO()
        main(html, s)
        self.assertEquals('foo', s.getvalue().strip())

    def test_paragraph(self):
        html = '<body><p class="normal">foo</p></body>'
        s = StringIO()
        main(html, s)
        self.assertEquals('foo', s.getvalue().strip())

    def test_header(self):
        html = '<body><h3>foo</h3></body>'
        s = StringIO()
        main(html, s)
        # XXX not sure if I like ` as default header underline, as it's not
        # so nice to read... but let's not complain right now ;)
        self.assertEquals('foo\n```', s.getvalue().strip())

    def test_link(self):
        html = '<body><a href="http://www.foo.com/bar">foo.com/bar</a></body>'
        s = StringIO()
        main(html, s)
        self.assert_('http://www.foo.com/bar' in s.getvalue())

    def test_multiple_paragraphs(self):
        html = '<body>\n\n<p>foo</p>\n\n<p>bar</p>\n\n</body>'
        s = StringIO()
        main(html, s)
        self.assert_('foo' in s.getvalue())
        self.assert_('bar' in s.getvalue())

def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(XHTML2ReSTTestCase))
    return suite

if __name__ == '__main__':
    unittest.main()

