# -*- coding: utf-8 -*-
"""
attribute parser tests
"""

import unittest
import attrparse        

class ScanTestCase(unittest.TestCase):
    
    def setUp(self):
        self.parser = attrparse.Parser()
    
    def testPlain(self):
        """ parse text with no attribute """
        self.parser.scan("plain")
        expected = [('plain', [])]
        self.assertEqual(expected, self.parser.output)

    def testSingle(self):
        """ parse text with single attribute """
        self.parser.scan("'''strong'''")
        expected = [('strong', ['strong'])]
        self.assertEqual(expected, self.parser.output)

    def testPlainAndSingle(self):
        """ parse plain text then attributed """
        self.parser.scan("plain '''strong'''")
        expected = [
            ('plain ', []),
            ('strong', ['strong']),
            ]
        self.assertEqual(expected, self.parser.output)

    def testSingleAndPlain(self):
        """ parse attributed then plain text """
        self.parser.scan("'''strong''' plain")
        expected = [
            ('strong', ['strong']),
            (' plain', []),
            ]
        self.assertEqual(expected, self.parser.output)

    def testAllSingles(self):
        """ parse all single attributes """
        text = "plain '''strong''' ''em'' __u__ plain"
        self.parser.scan(text)
        expected = [
            ('plain ', []),
            ('strong', ['strong']),
            (' ', []),
            ('em', ['em']),
            (' ', []),
            ('u', ['u']),
            (' plain', []),
            ]
        self.assertEqual(expected, self.parser.output)

    def testMultipleAttribute(self):
        """ parse multiple attribute per run of text """
        text = "'''''__strong em u__'''''"
        self.parser.scan(text)
        expected = [
            ('strong em u', ['em', 'strong', 'u']),
            ]
        self.assertEqual(expected, self.parser.output)

    def testNestedAttribtues(self):
        """ parse nested attributes <a><b></b></a> """
        text = "'''strong ''strong em'''''"
        self.parser.scan(text)
        expected = [
            ('strong ', ['strong']),
            ('strong em', ['em', 'strong']),
            ]
        self.assertEqual(expected, self.parser.output)

    def testCrossedAttribtues(self):
        """ parse mixed attributes <a><b></a></b> """
        text = "'''strong ''strong em''' em''"
        self.parser.scan(text)
        expected = [
            ('strong ', ['strong']),
            ('strong em', ['em', 'strong']),
            (' em', ['em']),
            ]
        self.assertEqual(expected, self.parser.output)

    def testOpenAttribtue(self):
        """ parse open attribute <a> """
        text = "plain '''strong"
        self.parser.scan(text)
        expected = [
            ('plain ', []),
            ('strong', ['strong']),
            ]
        self.assertEqual(expected, self.parser.output)


def suite():
    test_cases = [unittest.makeSuite(obj, 'test') 
        for name, obj in globals().items()
        if name.endswith('TestCase')]
    return unittest.TestSuite(test_cases)
    
if __name__ == '__main__':
    unittest.TextTestRunner(verbosity=2).run(suite())
