tests/test_platypus_programming.py
author robin <robin@reportlab.com>
Tue, 07 Mar 2017 10:00:34 +0000
changeset 4330 617ffa6bbdc8
parent 4252 fe660f227cac
child 4336 d39c63b164bc
permissions -rw-r--r--
changes for release 3.4.0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4330
617ffa6bbdc8 changes for release 3.4.0
robin <robin@reportlab.com>
parents: 4252
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2017
3002
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
     2
#see license.txt for license details
4252
fe660f227cac changes for release 3.3.0
robin
parents: 3898
diff changeset
     3
__version__='3.3.0'
3002
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
     4
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
     5
#tests and documents Page Layout API
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
     6
__doc__="""Tests low level programming of doc templates
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
     7
"""
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
     8
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
     9
setOutDir(__name__)
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    10
import sys
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    11
import unittest
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    12
class PlatypusProgrammingTestCase(unittest.TestCase):
3006
aa3921eacf40 test_platypus_programming.py: add DocAssert test
rgbecker
parents: 3002
diff changeset
    13
    "test platypus programming"
aa3921eacf40 test_platypus_programming.py: add DocAssert test
rgbecker
parents: 3002
diff changeset
    14
aa3921eacf40 test_platypus_programming.py: add DocAssert test
rgbecker
parents: 3002
diff changeset
    15
    def test0(self):
aa3921eacf40 test_platypus_programming.py: add DocAssert test
rgbecker
parents: 3002
diff changeset
    16
        from reportlab.lib.styles import ParagraphStyle
aa3921eacf40 test_platypus_programming.py: add DocAssert test
rgbecker
parents: 3002
diff changeset
    17
        from reportlab.platypus.flowables import DocPara, DocAssert
aa3921eacf40 test_platypus_programming.py: add DocAssert test
rgbecker
parents: 3002
diff changeset
    18
        from reportlab.platypus.doctemplate import SimpleDocTemplate
aa3921eacf40 test_platypus_programming.py: add DocAssert test
rgbecker
parents: 3002
diff changeset
    19
        def func(val):
aa3921eacf40 test_platypus_programming.py: add DocAssert test
rgbecker
parents: 3002
diff changeset
    20
            story = [
aa3921eacf40 test_platypus_programming.py: add DocAssert test
rgbecker
parents: 3002
diff changeset
    21
                    DocAssert(val,'this should fail'),
aa3921eacf40 test_platypus_programming.py: add DocAssert test
rgbecker
parents: 3002
diff changeset
    22
                    DocPara('repr(doc._nameSpace)',escape=True),
aa3921eacf40 test_platypus_programming.py: add DocAssert test
rgbecker
parents: 3002
diff changeset
    23
                    ]
aa3921eacf40 test_platypus_programming.py: add DocAssert test
rgbecker
parents: 3002
diff changeset
    24
            doc = SimpleDocTemplate(outputfile('test_doc_programming_asserts.pdf'))
aa3921eacf40 test_platypus_programming.py: add DocAssert test
rgbecker
parents: 3002
diff changeset
    25
            doc.build(story)
aa3921eacf40 test_platypus_programming.py: add DocAssert test
rgbecker
parents: 3002
diff changeset
    26
        self.assertRaises(AssertionError,func,False)
aa3921eacf40 test_platypus_programming.py: add DocAssert test
rgbecker
parents: 3002
diff changeset
    27
        func(True)
3002
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    28
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    29
    def test1(self):
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    30
        from reportlab.lib.styles import ParagraphStyle
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    31
        from reportlab.platypus import SimpleDocTemplate, Paragraph
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    32
        from reportlab.platypus.flowables import DocAssign, DocExec, DocPara, DocIf, DocWhile
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    33
        normal = ParagraphStyle(name='Normal', fontName='Helvetica', fontSize=8.5, leading=11)
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    34
        header = ParagraphStyle(name='Heading1', parent=normal, fontSize=14, leading=19,
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    35
                    spaceAfter=6, keepWithNext=1)
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    36
        story = [
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    37
                DocAssign('currentFrame','doc.frame.id'),
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    38
                DocAssign('currentPageTemplate','doc.pageTemplate.id'),
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    39
                DocAssign('aW','availableWidth'),
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    40
                DocAssign('aH','availableHeight'),
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    41
                DocAssign('aWH','availableWidth,availableHeight'),
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    42
                DocAssign('i',3),
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    43
                DocIf('i>3',Paragraph('The value of i is larger than 3',normal),Paragraph('The value of i is not larger than 3',normal)),
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    44
                DocIf('i==3',Paragraph('The value of i is equal to 3',normal),Paragraph('The value of i is not equal to 3',normal)),
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    45
                DocIf('i<3',Paragraph('The value of i is less than 3',normal),Paragraph('The value of i is not less than 3',normal)),
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    46
                DocWhile('i',[DocPara('i',format='The value of i is %(__expr__)d',style=normal),DocExec('i-=1')]),
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    47
                DocPara('repr(doc._nameSpace)',escape=True),
3642
4443366b724e test_platypus_programming.py: add usage of getPageCount
rptlab
parents: 3617
diff changeset
    48
                DocPara('doc.canv.getPageNumber()','The current page number is %(__expr__)d',style=normal) 
3002
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    49
                ]
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    50
        doc = SimpleDocTemplate(outputfile('test_doc_programming.pdf'))
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    51
        doc.build(story)
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    52
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    53
    def test2(self):
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    54
        "This makes one long multi-page paragraph in multi-pass for testing docWhile etc etc"
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    55
        from reportlab.platypus.flowables import DocAssign, DocExec, DocPara, DocIf, DocWhile
3898
b7b0eea4946d test_fixes
robin
parents: 3721
diff changeset
    56
        from test_platypus_xref import MyDocTemplate
3002
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    57
        from reportlab.platypus.tableofcontents import TableOfContents, SimpleIndex
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    58
        from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    59
        from reportlab.platypus import Paragraph
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    60
        from reportlab.lib import colors
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    61
        from reportlab.lib.randomtext import randomText, PYTHON
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    62
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    63
        # Build story.
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    64
        story = []
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    65
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    66
        styleSheet = getSampleStyleSheet()
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    67
        h1 = styleSheet['Heading1']
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    68
        h1.pageBreakBefore = 1
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    69
        h1.keepWithNext = 1
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    70
        h1.outlineLevel = 0
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    71
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    72
        h2 = styleSheet['Heading2']
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    73
        h2.backColor = colors.cyan
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    74
        h2.keepWithNext = 1
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    75
        h2.outlineLevel = 1
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    76
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    77
        bt = styleSheet['BodyText']
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    78
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    79
        story.append(Paragraph("""Cross-Referencing Test""", styleSheet["Title"]))
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    80
        story.append(Paragraph("""
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    81
            Subsequent pages test cross-references: indexes, tables and individual
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    82
            cross references.  The number in brackets at the end of each paragraph
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    83
            is its position in the story. (%d)""" % len(story), bt))
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    84
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    85
        story.append(Paragraph("""Table of Contents:""", styleSheet["Title"]))
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    86
        toc = TableOfContents()
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    87
        story.append(toc)
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    88
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    89
        chapterNum = 1
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    90
        for i in range(10):
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    91
            story.append(Paragraph('Chapter %d: Chapters always starts a new page' % chapterNum, h1))
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    92
            chapterNum += chapterNum
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    93
            story.append(DocAssign('chapterNum',chapterNum))
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    94
            for j in range(3):
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    95
                story.append(Paragraph('Heading1 paragraphs should always'
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    96
                                'have a page break before.  Heading 2 on the other hand'
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    97
                                'should always have a FRAME break before (%d)' % len(story), bt))
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    98
                story.append(Paragraph('Heading 2 should always be kept with the next thing (%d)' % len(story), h2))
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
    99
                for j in range(3):
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
   100
                    story.append(Paragraph(randomText(theme=PYTHON, sentences=2)+' (%d)' % len(story), bt))
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
   101
                    story.append(Paragraph('I should never be at the bottom of a frame (%d)' % len(story), h2))
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
   102
                    story.append(Paragraph(randomText(theme=PYTHON, sentences=1)+' (%d)' % len(story), bt))
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
   103
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
   104
            story.extend([
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
   105
                    DocAssign('currentFrame','doc.frame.id'),
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
   106
                    DocAssign('currentPageTemplate','doc.pageTemplate.id'),
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
   107
                    DocAssign('aW','availableWidth'),
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
   108
                    DocAssign('aH','availableHeight'),
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
   109
                    DocAssign('aWH','availableWidth,availableHeight'),
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
   110
                    DocAssign('i',3,life='forever'),
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
   111
                    DocIf('i>3',Paragraph('The value of i is larger than 3',bt),Paragraph('The value of i is not larger than 3',bt)),
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
   112
                    DocIf('i==3',Paragraph('The value of i is equal to 3',bt),Paragraph('The value of i is not equal to 3',bt)),
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
   113
                    DocIf('i<3',Paragraph('The value of i is less than 3',bt),Paragraph('The value of i is not less than 3',bt)),
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
   114
                    DocWhile('i',[DocPara('i',format='The value of i is %(__expr__)d',style=bt),DocExec('i-=1')]),
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
   115
                    DocPara('repr(doc._nameSpace)',escape=True),
3642
4443366b724e test_platypus_programming.py: add usage of getPageCount
rptlab
parents: 3617
diff changeset
   116
                    DocPara('doc.canv.getPageNumber()','The current page number is %(__expr__)d') 
3002
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
   117
                    ])
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
   118
        story.append(Paragraph('The Index which goes at the back', h1))
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
   119
        story.append(SimpleIndex())
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
   120
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
   121
        doc = MyDocTemplate(outputfile('test_platypus_programming_multipass.pdf'))
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
   122
        doc.multiBuild(story)
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
   123
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
   124
def makeSuite():
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
   125
    return makeSuiteForClasses(PlatypusProgrammingTestCase)
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
   126
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
   127
#noruntests
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
   128
if __name__ == "__main__":
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
   129
    unittest.TextTestRunner().run(makeSuite())
6957f05aad20 tests: split off doc programming tests
rgbecker
parents:
diff changeset
   130
    printLocation()