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