tests/test_platypus_paraparser.py
author robin <robin@reportlab.com>
Tue, 07 Mar 2017 10:00:34 +0000
changeset 4330 617ffa6bbdc8
parent 4252 fe660f227cac
child 4523 38996c0a8955
permissions -rw-r--r--
changes for release 3.4.0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     1
#!/bin/env python
4330
617ffa6bbdc8 changes for release 3.4.0
robin <robin@reportlab.com>
parents: 4252
diff changeset
     2
#Copyright ReportLab Europe Ltd. 2000-2017
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     3
#see license.txt for license details
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     4
#history TBC
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     5
#$Header$
4252
fe660f227cac changes for release 3.3.0
robin
parents: 4099
diff changeset
     6
__version__='3.3.0'
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     7
__doc__="""Tests of intra-paragraph parsing behaviour in Platypus."""
3913
0a2c63c9f8bf test_platypus_paraparser.py: fix for python2
robin
parents: 3898
diff changeset
     8
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, equalStrings
2984
c63f149d55aa tests.utils --> reportlab.lib.testutils
rgbecker
parents: 2966
diff changeset
     9
setOutDir(__name__)
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    10
from pprint import pprint as pp
2966
c9df63ccabdf reportlab-2.2: major changes to make tests run
rgbecker
parents: 2963
diff changeset
    11
import unittest
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    12
from reportlab.platypus import cleanBlockQuotedText
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    13
from reportlab.platypus.paraparser import ParaParser, ParaFrag
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    14
from reportlab.lib.colors import black
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    15
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    16
class ParaParserTestCase(unittest.TestCase):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    17
    """Tests of data structures created by paragraph parser.  Esp. ability
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    18
    to accept unicode and preserve it"""
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    19
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    20
    def setUp(self):
3307
688ff0cafa4d tests: fix test_platypus_paraparser.py
rgbecker
parents: 3306
diff changeset
    21
        style=ParaFrag()
688ff0cafa4d tests: fix test_platypus_paraparser.py
rgbecker
parents: 3306
diff changeset
    22
        style.fontName ='Times-Roman'
688ff0cafa4d tests: fix test_platypus_paraparser.py
rgbecker
parents: 3306
diff changeset
    23
        style.fontSize = 12
688ff0cafa4d tests: fix test_platypus_paraparser.py
rgbecker
parents: 3306
diff changeset
    24
        style.textColor = black
688ff0cafa4d tests: fix test_platypus_paraparser.py
rgbecker
parents: 3306
diff changeset
    25
        style.bulletFontName = black
688ff0cafa4d tests: fix test_platypus_paraparser.py
rgbecker
parents: 3306
diff changeset
    26
        style.bulletFontName = 'Times-Roman'
688ff0cafa4d tests: fix test_platypus_paraparser.py
rgbecker
parents: 3306
diff changeset
    27
        style.bulletFontSize = 12
688ff0cafa4d tests: fix test_platypus_paraparser.py
rgbecker
parents: 3306
diff changeset
    28
        style.bulletOffsetY = 3
688ff0cafa4d tests: fix test_platypus_paraparser.py
rgbecker
parents: 3306
diff changeset
    29
        style.textTransform = None
688ff0cafa4d tests: fix test_platypus_paraparser.py
rgbecker
parents: 3306
diff changeset
    30
        self.style = style
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    31
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    32
    def testPlain(self):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    33
        txt = "Hello World"
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    34
        stuff = ParaParser().parse(txt, self.style)
3800
e8547b00eb59 attempt to remove old type testing
robin
parents: 3721
diff changeset
    35
        assert isinstance(stuff,tuple)
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    36
        assert len(stuff) == 3
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    37
        assert  stuff[1][0].text == 'Hello World'
3445
a28144236f38 test_platypus_paraparser.py: white space changes
rgbecker
parents: 3307
diff changeset
    38
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    39
    def testBold(self):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    40
        txt = "Hello <b>Bold</b> World"
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    41
        fragList = ParaParser().parse(txt, self.style)[1]
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
    42
        self.assertEquals([x.text for x in fragList], ['Hello ','Bold',' World'])
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    43
        self.assertEquals(fragList[1].fontName, 'Times-Bold')
3445
a28144236f38 test_platypus_paraparser.py: white space changes
rgbecker
parents: 3307
diff changeset
    44
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    45
    def testStrong(self):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    46
        txt = "Hello <strong>Strong</strong> World"
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    47
        fragList = ParaParser().parse(txt, self.style)[1]
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
    48
        self.assertEquals([x.text for x in fragList], ['Hello ','Strong',' World'])
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    49
        self.assertEquals(fragList[1].fontName, 'Times-Bold')
3445
a28144236f38 test_platypus_paraparser.py: white space changes
rgbecker
parents: 3307
diff changeset
    50
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    51
    def testItalic(self):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    52
        txt = "Hello <i>Italic</i> World"
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    53
        fragList = ParaParser().parse(txt, self.style)[1]
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
    54
        self.assertEquals([x.text for x in fragList], ['Hello ','Italic',' World'])
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    55
        self.assertEquals(fragList[1].fontName, 'Times-Italic')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    56
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    57
    def testEm(self):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    58
        txt = "Hello <em>Em</em> World"
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    59
        fragList = ParaParser().parse(txt, self.style)[1]
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
    60
        self.assertEquals([x.text for x in fragList], ['Hello ','Em',' World'])
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    61
        self.assertEquals(fragList[1].fontName, 'Times-Italic')
3445
a28144236f38 test_platypus_paraparser.py: white space changes
rgbecker
parents: 3307
diff changeset
    62
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    63
    def testEntity(self):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    64
        "Numeric entities should be unescaped by parser"
3898
b7b0eea4946d test_fixes
robin
parents: 3800
diff changeset
    65
        txt = b"Hello &#169; copyright"
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    66
        fragList = ParaParser().parse(txt, self.style)[1]
3958
6a1c7f16aa74 test_platypus_paraparser.py: fix entity tests
robin
parents: 3954
diff changeset
    67
        self.assertEquals([x.text for x in fragList], [u'Hello ', u'\xa9', u' copyright'])
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    68
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    69
    def testEscaped(self):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    70
        "Escaped high-bit stuff should go straight through"
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    71
        txt = "Hello \xc2\xa9 copyright"
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    72
        fragList = ParaParser().parse(txt, self.style)[1]
3913
0a2c63c9f8bf test_platypus_paraparser.py: fix for python2
robin
parents: 3898
diff changeset
    73
        assert equalStrings(fragList[0].text,txt)
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    74
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    75
    def testPlainUnicode(self):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    76
        "See if simple unicode goes through"
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
    77
        txt = "Hello World"
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    78
        stuff = ParaParser().parse(txt, self.style)
3800
e8547b00eb59 attempt to remove old type testing
robin
parents: 3721
diff changeset
    79
        assert isinstance(stuff,tuple)
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    80
        assert len(stuff) == 3
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
    81
        assert  stuff[1][0].text == 'Hello World'
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    82
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    83
    def testBoldUnicode(self):
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
    84
        txt = "Hello <b>Bold</b> World"
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    85
        fragList = ParaParser().parse(txt, self.style)[1]
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
    86
        self.assertEquals([x.text for x in fragList], ['Hello ','Bold',' World'])
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    87
        self.assertEquals(fragList[1].fontName, 'Times-Bold')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    88
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    89
    def testEntityUnicode(self):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    90
        "Numeric entities should be unescaped by parser"
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
    91
        txt = "Hello &#169; copyright"
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    92
        fragList = ParaParser().parse(txt, self.style)[1]
3958
6a1c7f16aa74 test_platypus_paraparser.py: fix entity tests
robin
parents: 3954
diff changeset
    93
        self.assertEquals([x.text for x in fragList], [u'Hello ', u'\xa9', u' copyright'])
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    94
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    95
    def testEscapedUnicode(self):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    96
        "Escaped high-bit stuff should go straight through"
3898
b7b0eea4946d test_fixes
robin
parents: 3800
diff changeset
    97
        txt = u"Hello \xa9 copyright"
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    98
        fragList = ParaParser().parse(txt, self.style)[1]
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    99
        assert fragList[0].text == txt
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   100
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   101
    def testBr(self):
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   102
        txt = "Hello <br/> World"
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   103
        fragList = ParaParser().parse(txt, self.style)[1]
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   104
        self.assertEquals([x.text for x in fragList], ['Hello ','',' World'])
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   105
        self.assertEquals(fragList[1].lineBreak, True)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   106
4099
b711ede115e3 added test that badly formed markup raises errors
Andy Robinson <andy@reportlab.com>
parents: 3958
diff changeset
   107
b711ede115e3 added test that badly formed markup raises errors
Andy Robinson <andy@reportlab.com>
parents: 3958
diff changeset
   108
    def testRejectsBadlyFormed(self):
b711ede115e3 added test that badly formed markup raises errors
Andy Robinson <andy@reportlab.com>
parents: 3958
diff changeset
   109
        txt = "Hello <b>World"
b711ede115e3 added test that badly formed markup raises errors
Andy Robinson <andy@reportlab.com>
parents: 3958
diff changeset
   110
b711ede115e3 added test that badly formed markup raises errors
Andy Robinson <andy@reportlab.com>
parents: 3958
diff changeset
   111
        def parseIt(txt, style=self.style):
b711ede115e3 added test that badly formed markup raises errors
Andy Robinson <andy@reportlab.com>
parents: 3958
diff changeset
   112
            fragList = ParaParser().parse(txt, self.style)[1]
b711ede115e3 added test that badly formed markup raises errors
Andy Robinson <andy@reportlab.com>
parents: 3958
diff changeset
   113
b711ede115e3 added test that badly formed markup raises errors
Andy Robinson <andy@reportlab.com>
parents: 3958
diff changeset
   114
        self.assertRaises(ValueError, parseIt, txt)
b711ede115e3 added test that badly formed markup raises errors
Andy Robinson <andy@reportlab.com>
parents: 3958
diff changeset
   115
        
b711ede115e3 added test that badly formed markup raises errors
Andy Robinson <andy@reportlab.com>
parents: 3958
diff changeset
   116
3954
44dbe56eb858 first attempt at HTMLParser-based paraparser
Andy Robinson <andy@reportlab.com>
parents: 3938
diff changeset
   117
    #def testNakedAmpersands(self):
44dbe56eb858 first attempt at HTMLParser-based paraparser
Andy Robinson <andy@reportlab.com>
parents: 3938
diff changeset
   118
        #We no longer require this error to be raised when using html.parser
44dbe56eb858 first attempt at HTMLParser-based paraparser
Andy Robinson <andy@reportlab.com>
parents: 3938
diff changeset
   119
        # import pyRXPU
44dbe56eb858 first attempt at HTMLParser-based paraparser
Andy Robinson <andy@reportlab.com>
parents: 3938
diff changeset
   120
        # from reportlab.platypus.paragraph import Paragraph
44dbe56eb858 first attempt at HTMLParser-based paraparser
Andy Robinson <andy@reportlab.com>
parents: 3938
diff changeset
   121
        # def func():
44dbe56eb858 first attempt at HTMLParser-based paraparser
Andy Robinson <andy@reportlab.com>
parents: 3938
diff changeset
   122
        #     txt = "1 & 2"
44dbe56eb858 first attempt at HTMLParser-based paraparser
Andy Robinson <andy@reportlab.com>
parents: 3938
diff changeset
   123
        #     parser = ParaParser()
44dbe56eb858 first attempt at HTMLParser-based paraparser
Andy Robinson <andy@reportlab.com>
parents: 3938
diff changeset
   124
        #     parser.caseSensitive = True
44dbe56eb858 first attempt at HTMLParser-based paraparser
Andy Robinson <andy@reportlab.com>
parents: 3938
diff changeset
   125
        #     frags = ParaParser().parse(txt, self.style)[1]
44dbe56eb858 first attempt at HTMLParser-based paraparser
Andy Robinson <andy@reportlab.com>
parents: 3938
diff changeset
   126
        #     Paragraph(txt, self.style),
44dbe56eb858 first attempt at HTMLParser-based paraparser
Andy Robinson <andy@reportlab.com>
parents: 3938
diff changeset
   127
        # self.assertRaises(
44dbe56eb858 first attempt at HTMLParser-based paraparser
Andy Robinson <andy@reportlab.com>
parents: 3938
diff changeset
   128
        #         pyRXPU.error,
44dbe56eb858 first attempt at HTMLParser-based paraparser
Andy Robinson <andy@reportlab.com>
parents: 3938
diff changeset
   129
        #         func,
44dbe56eb858 first attempt at HTMLParser-based paraparser
Andy Robinson <andy@reportlab.com>
parents: 3938
diff changeset
   130
        #         )
3305
2acdaed72491 fixed paragraph style bug in test
tim
parents: 2984
diff changeset
   131
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   132
def makeSuite():
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   133
    return makeSuiteForClasses(ParaParserTestCase)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   134
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   135
#noruntests
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   136
if __name__ == "__main__":
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   137
    unittest.TextTestRunner().run(makeSuite())