File: test_platypus_programming.py

package info (click to toggle)
python-reportlab 4.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,708 kB
  • sloc: python: 99,020; xml: 1,494; makefile: 143; sh: 12
file content (134 lines) | stat: -rw-r--r-- 7,499 bytes parent folder | download | duplicates (2)
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
#Copyright ReportLab Europe Ltd. 2000-2017
#see license.txt for license details
__version__='3.3.0'

#tests and documents Page Layout API
__doc__="""Tests low level programming of doc templates
"""
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation
setOutDir(__name__)
import sys, unittest
class PlatypusProgrammingTestCase(unittest.TestCase):
    "test platypus programming"

    def test0(self):
        from reportlab.lib.styles import ParagraphStyle
        from reportlab.platypus.flowables import DocPara, DocAssert
        from reportlab.platypus.doctemplate import SimpleDocTemplate
        def func(val):
            story = [
                    DocAssert(val,'this should fail'),
                    DocPara("'val=%r'" % val),
                    DocPara('"{"+", ".join(("%s=%s" % (_k,(repr(_v).replace("<","").replace(">","") if "<" in repr(_v) else repr(_v)[1:] if repr(_v) and repr(_v)[0] in "ub" else repr(_v))) for _k,_v in sorted(doc._nameSpace.items()) if _k not in ("_k","_v")))+"}"',escape=True),
                    ]
            doc = SimpleDocTemplate(outputfile('test_doc_programming_asserts.pdf'))
            doc.build(story)
        self.assertRaises(AssertionError,func,False)
        func(True)

    def test1(self):
        from reportlab.lib.styles import ParagraphStyle
        from reportlab.platypus import SimpleDocTemplate, Paragraph
        from reportlab.platypus.flowables import DocAssign, DocExec, DocPara, DocIf, DocWhile
        normal = ParagraphStyle(name='Normal', fontName='Helvetica', fontSize=8.5, leading=11)
        header = ParagraphStyle(name='Heading1', parent=normal, fontSize=14, leading=19,
                    spaceAfter=6, keepWithNext=1)
        story = [
                DocAssign('currentFrame','doc.frame.id'),
                DocAssign('currentPageTemplate','doc.pageTemplate.id'),
                DocAssign('aW','availableWidth'),
                DocAssign('aH','availableHeight'),
                DocAssign('aWH','availableWidth,availableHeight'),
                DocAssign('i',3),
                DocIf('i>3',Paragraph('The value of i is larger than 3',normal),Paragraph('The value of i is not larger than 3',normal)),
                DocIf('i==3',Paragraph('The value of i is equal to 3',normal),Paragraph('The value of i is not equal to 3',normal)),
                DocIf('i<3',Paragraph('The value of i is less than 3',normal),Paragraph('The value of i is not less than 3',normal)),
                DocWhile('i',[DocPara('i',format='The value of i is %(__expr__)d',style=normal),DocExec('i-=1')]),
                DocPara("'doc._nameSpace=' + repr(doc._nameSpace)"),
                DocPara('"{"+", ".join(("%s=%s" % (_k,(repr(_v).replace("<","").replace(">","") if "<" in repr(_v) else repr(_v)[1:] if repr(_v) and repr(_v)[0] in "ub" else repr(_v))) for _k,_v in sorted(doc._nameSpace.items()) if _k not in ("_k","_v")))+"}"',escape=True),
                DocPara('doc.canv.getPageNumber()','The current page number is %(__expr__)d',style=normal) 
                ]
        doc = SimpleDocTemplate(outputfile('test_doc_programming.pdf'))
        doc.build(story)

    def test2(self):
        "This makes one long multi-page paragraph in multi-pass for testing docWhile etc etc"
        from reportlab.platypus.flowables import DocAssign, DocExec, DocPara, DocIf, DocWhile
        from test_platypus_xref import MyDocTemplate
        from reportlab.platypus.tableofcontents import TableOfContents, SimpleIndex
        from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
        from reportlab.platypus import Paragraph
        from reportlab.lib import colors
        from reportlab.lib.randomtext import randomText, PYTHON
        import random
        from reportlab.rl_config import invariant as rl_invariant
        if rl_invariant: random.seed(2103696747)

        # Build story.
        story = []

        styleSheet = getSampleStyleSheet()
        h1 = styleSheet['Heading1']
        h1.pageBreakBefore = 1
        h1.keepWithNext = 1
        h1.outlineLevel = 0

        h2 = styleSheet['Heading2']
        h2.backColor = colors.cyan
        h2.keepWithNext = 1
        h2.outlineLevel = 1

        bt = styleSheet['BodyText']

        story.append(Paragraph("""Cross-Referencing Test""", styleSheet["Title"]))
        story.append(Paragraph("""
            Subsequent pages test cross-references: indexes, tables and individual
            cross references.  The number in brackets at the end of each paragraph
            is its position in the story. (%d)""" % len(story), bt))

        story.append(Paragraph("""Table of Contents:""", styleSheet["Title"]))
        toc = TableOfContents()
        story.append(toc)

        chapterNum = 1
        for i in range(10):
            story.append(Paragraph('Chapter %d: Chapters always starts a new page' % chapterNum, h1))
            chapterNum += chapterNum
            story.append(DocAssign('chapterNum',chapterNum))
            for j in range(3):
                story.append(Paragraph('Heading1 paragraphs should always'
                                'have a page break before.  Heading 2 on the other hand'
                                'should always have a FRAME break before (%d)' % len(story), bt))
                story.append(Paragraph('Heading 2 should always be kept with the next thing (%d)' % len(story), h2))
                for j in range(3):
                    story.append(Paragraph(randomText(theme=PYTHON, sentences=2)+' (%d)' % len(story), bt))
                    story.append(Paragraph('I should never be at the bottom of a frame (%d)' % len(story), h2))
                    story.append(Paragraph(randomText(theme=PYTHON, sentences=1)+' (%d)' % len(story), bt))

            story.extend([
                    DocAssign('currentFrame','doc.frame.id'),
                    DocAssign('currentPageTemplate','doc.pageTemplate.id'),
                    DocAssign('aW','availableWidth'),
                    DocAssign('aH','availableHeight'),
                    DocAssign('aWH','availableWidth,availableHeight'),
                    DocAssign('i',3,life='forever'),
                    DocIf('i>3',Paragraph('The value of i is larger than 3',bt),Paragraph('The value of i is not larger than 3',bt)),
                    DocIf('i==3',Paragraph('The value of i is equal to 3',bt),Paragraph('The value of i is not equal to 3',bt)),
                    DocIf('i<3',Paragraph('The value of i is less than 3',bt),Paragraph('The value of i is not less than 3',bt)),
                    DocWhile('i',[DocPara('i',format='The value of i is %(__expr__)d',style=bt),DocExec('i-=1')]),
                    DocPara('"{"+", ".join(("%s=%s" % (_k,(repr(_v).replace("<","").replace(">","") if "<" in repr(_v) else repr(_v)[1:] if repr(_v) and repr(_v)[0] in "ub" else repr(_v))) for _k,_v in sorted(doc._nameSpace.items()) if _k not in ("_k","_v")))+"}"',escape=True),
                    DocPara('doc.canv.getPageNumber()','The current page number is %(__expr__)d') 
                    ])
        story.append(Paragraph('The Index which goes at the back', h1))
        story.append(SimpleIndex())

        doc = MyDocTemplate(outputfile('test_platypus_programming_multipass.pdf'))
        doc.multiBuild(story)

def makeSuite():
    return makeSuiteForClasses(PlatypusProgrammingTestCase)

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