Beispiel #1
0
    def testConstructionTypeError(self):
        """
        Test type error for reader/renderer arguments.
        """

        # Content
        with self.assertRaises(exceptions.MooseDocsException) as e:
            Translator(None, 'foo', HTMLRenderer(), [])
        self.assertIn("The argument 'content' must be", e.exception.message)

        # Reader
        content = page.PageNodeBase(None)
        with self.assertRaises(exceptions.MooseDocsException) as e:
            Translator(content, 'foo', HTMLRenderer(), [])
        self.assertIn("The argument 'reader' must be", e.exception.message)

        # Renderer
        with self.assertRaises(exceptions.MooseDocsException) as e:
            Translator(content, MarkdownReader(), 'foo', [])
        self.assertIn("The argument 'renderer' must be", e.exception.message)

        # Extensions
        with self.assertRaises(exceptions.MooseDocsException) as e:
            Translator(content, MarkdownReader(), HTMLRenderer(), ['foo'])
        self.assertIn("The argument 'extensions' must be", e.exception.message)
Beispiel #2
0
 def testTokenizeException(self):
     root = tokens.Token(None)
     content = page.PageNodeBase(None)
     reader = readers.Reader(lexers.RecursiveLexer('foo'))
     translator = Translator(content, reader, HTMLRenderer(), [])
     translator.init('')
     reader.add('foo', WordComponent())
     reader.parse(root, u'throw bar')
     self.assertIsInstance(root(0), tokens.ExceptionToken)
     self.assertIsInstance(root(1), tokens.Word)
     self.assertEqual(root(1).content, u'bar')
Beispiel #3
0
 def testReinit(self):
     ast = tokens.Paragraph(None)
     comp = ParComponent()
     content = page.PageNodeBase(None)
     renderer = renderers.HTMLRenderer()
     translator = Translator(content, MarkdownReader(), renderer, [])
     translator.init('')
     renderer.add(tokens.Paragraph, comp)
     renderer.render(ast)
     renderer.render(ast)
     self.assertEqual(comp.count, 2)
Beispiel #4
0
    def testBasic(self):
        root = tokens.Token(None)
        content = page.PageNodeBase(None)
        reader = readers.Reader(lexers.RecursiveLexer('block', 'inline'))
        translator = Translator(content, reader, HTMLRenderer(), [])
        translator.init('')
        reader.add('block', BlockComponent())
        reader.add('inline', WordComponent())
        reader.parse(root, u'foo bar')

        self.assertIsInstance(root(0), tokens.Token)
        self.assertIsInstance(root(0)(0), tokens.Word)
        self.assertEqual(root(0)(0).content, u'foo')
        self.assertIsInstance(root(0)(1), tokens.Word)
        self.assertEqual(root(0)(1).content, u'bar')
 def test(self):
     comp = RenderComponent()
     renderer = Renderer()
     content = page.PageNodeBase(None)
     t = Translator(content, Reader(RecursiveLexer('foo')), renderer, [])
     comp.init(t)
     self.assertEqual(comp.renderer, renderer)
Beispiel #6
0
 def testConstruction(self):
     """
     Test most basic construction.
     """
     content = page.PageNodeBase(None)
     translator = Translator(content, MarkdownReader(), HTMLRenderer(), [])
     self.assertIsInstance(translator.reader, MarkdownReader)
     self.assertIsInstance(translator.renderer, HTMLRenderer)
Beispiel #7
0
    def testExtend(self):
        """
        Test the extend method.
        """
        class ExtTester(Extension):
            """Dummy extension for testing."""
            def __init__(self, *args, **kwargs):
                Extension.__init__(self, *args, **kwargs)
                self.called = False
            def extend(self, reader, renderer):
                self.called = True

        ext = ExtTester()
        self.assertFalse(ext.called)
        content = pages.Page('foo', source='foo')
        t = Translator([content], Reader(RecursiveLexer('foo')), Renderer(), [ext])
        t.init()
        self.assertTrue(ext.called)
Beispiel #8
0
    def testRender(self):
        ast = tokens.Paragraph(None)
        tokens.String(ast, content=u'foo')
        content = page.PageNodeBase(None)
        renderer = renderers.HTMLRenderer()
        translator = Translator(content, MarkdownReader(), renderer, [])
        translator.init('')

        renderer.add(tokens.Paragraph, ParComponent())
        renderer.add(tokens.String, StringComponent())

        root = renderer.render(ast)
        self.assertIsInstance(root, html.Tag)
        self.assertEqual(root.name, 'body')
        self.assertIsInstance(root(0), html.Tag)
        self.assertEqual(root(0).name, 'p')
        self.assertIsInstance(root(0)(0), html.String)
        self.assertEqual(root(0)(0).content, u'foo')
    def testExtend(self):
        """
        Test the extend method.
        """
        class ExtTester(Extension):
            """Dummy extension for testing."""
            def __init__(self, *args, **kwargs):
                Extension.__init__(self, *args, **kwargs)
                self.called = False
            def extend(self, reader, renderer):
                self.called = True

        ext = ExtTester()
        self.assertFalse(ext.called)
        content = page.PageNodeBase(None)
        t = Translator(content, Reader(RecursiveLexer('foo')), Renderer(), [ext])
        t.init('')
        self.assertTrue(ext.called)
        self.assertIs(ext.translator, t)
Beispiel #10
0
    def testExtend(self):
        """
        Test the extend method.
        """
        class ExtTester(Extension):
            """Dummy extension for testing."""
            def __init__(self, *args, **kwargs):
                Extension.__init__(self, *args, **kwargs)
                self.called = False
            def extend(self, reader, renderer):
                self.called = True

        ext = ExtTester()
        self.assertFalse(ext.called)
        content = page.PageNodeBase(None)
        t = Translator(content, Reader(RecursiveLexer('foo')), Renderer(), [ext])
        t.init('')
        self.assertTrue(ext.called)
        self.assertIs(ext.translator, t)
Beispiel #11
0
 def testBasic(self):
     """
     Test correct use.
     """
     t = Translator(page.PageNodeBase(None), MarkdownReader(),
                    HTMLRenderer(), [])
     obj = mixins.TranslatorObject()
     self.assertFalse(obj.initialized())
     obj.init(t)
     self.assertTrue(obj.initialized())
     self.assertIs(obj.translator, t)
Beispiel #12
0
    def testExceptions(self):
        """
        Test Exceptions.
        """
        t = Translator(page.PageNodeBase(None), MarkdownReader(),
                       HTMLRenderer(), [])
        obj = mixins.TranslatorObject()
        with self.assertRaises(exceptions.MooseDocsException) as e:
            obj.init('')
        self.assertIn("The argument 'translator'", e.exception.message)

        with self.assertRaises(exceptions.MooseDocsException) as e:
            obj.translator
        self.assertIn("The init() method of", e.exception.message)

        obj.init(t)
        with self.assertRaises(exceptions.MooseDocsException) as e:
            obj.init(t)
        self.assertIn("already been initialized", e.exception.message)
    def testDefault(self):
        """
        Test basic construction and errors.
        """
        comp = TokenComponent()
        reader = Reader(RecursiveLexer('foo'))
        content = page.PageNodeBase(None)
        t = Translator(content, reader, Renderer(), [])
        comp.init(t)

        self.assertIsNone(comp.settings)
        self.assertIs(comp.reader, reader)
        self.assertEqual(comp.attributes, dict())

        defaults = comp.defaultSettings()
        for key in ['id', 'class', 'style']:
            self.assertIn(key, defaults)
            self.assertIsInstance(defaults[key], tuple)
            self.assertEqual(len(defaults[key]), 2)
            self.assertEqual(defaults[key][0], u'')
            self.assertIsInstance(defaults[key][1], unicode)
    def testExceptions(self):
        """
        Test that exceptions are raised.
        """
        comp = TokenComponent()
        reader = Reader(RecursiveLexer('foo'))
        content = page.PageNodeBase(None)
        t = Translator(content, reader, Renderer(), [])
        comp.init(t)

        with self.assertRaises(NotImplementedError):
            comp.createToken([], [])

        # Test defaultSettings return type check
        class TestToken(TokenComponent):
            @staticmethod
            def defaultSettings():
                pass

        with self.assertRaises(exceptions.MooseDocsException) as e:
            TestToken()
        self.assertIn("must return a dict", e.exception.message)
    def test(self):
        """
        Test basic construction and errors.
        """
        comp = Component()

        with self.assertRaises(exceptions.MooseDocsException) as e:
            comp.translator
        self.assertIn("The init() method of", e.exception.message)

        with self.assertRaises(exceptions.MooseDocsException) as e:
            comp.init(42)
        self.assertIn("The argument 'translator' must be of type", e.exception.message)

        content = page.PageNodeBase(None)
        t = Translator(content, Reader(RecursiveLexer('foo')), Renderer(), [])
        comp.init(t)
        with self.assertRaises(exceptions.MooseDocsException) as e:
            comp.init(t)
        self.assertIn("already been", e.exception.message)

        self.assertIs(comp.translator, t)