File: test_platypus_paraparser.py

package info (click to toggle)
python-reportlab 3.3.0-2%2Bdeb9u1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 7,172 kB
  • sloc: python: 71,880; ansic: 19,093; xml: 1,494; makefile: 416; java: 193; sh: 100
file content (137 lines) | stat: -rw-r--r-- 5,108 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/env python
#Copyright ReportLab Europe Ltd. 2000-2016
#see license.txt for license details
#history TBC
#$Header$
__version__='3.3.0'
__doc__="""Tests of intra-paragraph parsing behaviour in Platypus."""
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, equalStrings
setOutDir(__name__)
from pprint import pprint as pp
import unittest
from reportlab.platypus import cleanBlockQuotedText
from reportlab.platypus.paraparser import ParaParser, ParaFrag
from reportlab.lib.colors import black

class ParaParserTestCase(unittest.TestCase):
    """Tests of data structures created by paragraph parser.  Esp. ability
    to accept unicode and preserve it"""

    def setUp(self):
        style=ParaFrag()
        style.fontName ='Times-Roman'
        style.fontSize = 12
        style.textColor = black
        style.bulletFontName = black
        style.bulletFontName = 'Times-Roman'
        style.bulletFontSize = 12
        style.bulletOffsetY = 3
        style.textTransform = None
        self.style = style

    def testPlain(self):
        txt = "Hello World"
        stuff = ParaParser().parse(txt, self.style)
        assert isinstance(stuff,tuple)
        assert len(stuff) == 3
        assert  stuff[1][0].text == 'Hello World'

    def testBold(self):
        txt = "Hello <b>Bold</b> World"
        fragList = ParaParser().parse(txt, self.style)[1]
        self.assertEquals([x.text for x in fragList], ['Hello ','Bold',' World'])
        self.assertEquals(fragList[1].fontName, 'Times-Bold')

    def testStrong(self):
        txt = "Hello <strong>Strong</strong> World"
        fragList = ParaParser().parse(txt, self.style)[1]
        self.assertEquals([x.text for x in fragList], ['Hello ','Strong',' World'])
        self.assertEquals(fragList[1].fontName, 'Times-Bold')

    def testItalic(self):
        txt = "Hello <i>Italic</i> World"
        fragList = ParaParser().parse(txt, self.style)[1]
        self.assertEquals([x.text for x in fragList], ['Hello ','Italic',' World'])
        self.assertEquals(fragList[1].fontName, 'Times-Italic')

    def testEm(self):
        txt = "Hello <em>Em</em> World"
        fragList = ParaParser().parse(txt, self.style)[1]
        self.assertEquals([x.text for x in fragList], ['Hello ','Em',' World'])
        self.assertEquals(fragList[1].fontName, 'Times-Italic')

    def testEntity(self):
        "Numeric entities should be unescaped by parser"
        txt = b"Hello &#169; copyright"
        fragList = ParaParser().parse(txt, self.style)[1]
        self.assertEquals([x.text for x in fragList], [u'Hello ', u'\xa9', u' copyright'])

    def testEscaped(self):
        "Escaped high-bit stuff should go straight through"
        txt = "Hello \xc2\xa9 copyright"
        fragList = ParaParser().parse(txt, self.style)[1]
        assert equalStrings(fragList[0].text,txt)

    def testPlainUnicode(self):
        "See if simple unicode goes through"
        txt = "Hello World"
        stuff = ParaParser().parse(txt, self.style)
        assert isinstance(stuff,tuple)
        assert len(stuff) == 3
        assert  stuff[1][0].text == 'Hello World'

    def testBoldUnicode(self):
        txt = "Hello <b>Bold</b> World"
        fragList = ParaParser().parse(txt, self.style)[1]
        self.assertEquals([x.text for x in fragList], ['Hello ','Bold',' World'])
        self.assertEquals(fragList[1].fontName, 'Times-Bold')

    def testEntityUnicode(self):
        "Numeric entities should be unescaped by parser"
        txt = "Hello &#169; copyright"
        fragList = ParaParser().parse(txt, self.style)[1]
        self.assertEquals([x.text for x in fragList], [u'Hello ', u'\xa9', u' copyright'])

    def testEscapedUnicode(self):
        "Escaped high-bit stuff should go straight through"
        txt = u"Hello \xa9 copyright"
        fragList = ParaParser().parse(txt, self.style)[1]
        assert fragList[0].text == txt

    def testBr(self):
        txt = "Hello <br/> World"
        fragList = ParaParser().parse(txt, self.style)[1]
        self.assertEquals([x.text for x in fragList], ['Hello ','',' World'])
        self.assertEquals(fragList[1].lineBreak, True)


    def testRejectsBadlyFormed(self):
        txt = "Hello <b>World"

        def parseIt(txt, style=self.style):
            fragList = ParaParser().parse(txt, self.style)[1]

        self.assertRaises(ValueError, parseIt, txt)
        

    #def testNakedAmpersands(self):
        #We no longer require this error to be raised when using html.parser
        # import pyRXPU
        # from reportlab.platypus.paragraph import Paragraph
        # def func():
        #     txt = "1 & 2"
        #     parser = ParaParser()
        #     parser.caseSensitive = True
        #     frags = ParaParser().parse(txt, self.style)[1]
        #     Paragraph(txt, self.style),
        # self.assertRaises(
        #         pyRXPU.error,
        #         func,
        #         )

def makeSuite():
    return makeSuiteForClasses(ParaParserTestCase)

#noruntests
if __name__ == "__main__":
    unittest.TextTestRunner().run(makeSuite())