reportlab/test/test_platypus_breaking.py
author rgbecker
Fri, 14 Jul 2006 09:25:10 +0000
changeset 2660 c147aff8edae
parent 2659 08af1b8a5d05
child 2664 c9faa3a99e93
permissions -rw-r--r--
reportlab: minor fixes and add strokeDashArray
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2268
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2004
1424
5a0ceec0c421 KeepWithNext experiments
andy_robinson
parents:
diff changeset
     2
#see license.txt for license details
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2268
diff changeset
     3
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/test/test_platypus_breaking.py
1424
5a0ceec0c421 KeepWithNext experiments
andy_robinson
parents:
diff changeset
     4
"""Tests pageBreakBefore, frameBreakBefore, keepWithNext...
5a0ceec0c421 KeepWithNext experiments
andy_robinson
parents:
diff changeset
     5
"""
5a0ceec0c421 KeepWithNext experiments
andy_robinson
parents:
diff changeset
     6
5a0ceec0c421 KeepWithNext experiments
andy_robinson
parents:
diff changeset
     7
import sys, os, time
5a0ceec0c421 KeepWithNext experiments
andy_robinson
parents:
diff changeset
     8
from string import split, strip, join, whitespace
5a0ceec0c421 KeepWithNext experiments
andy_robinson
parents:
diff changeset
     9
from operator import truth
5a0ceec0c421 KeepWithNext experiments
andy_robinson
parents:
diff changeset
    10
from types import StringType, ListType
5a0ceec0c421 KeepWithNext experiments
andy_robinson
parents:
diff changeset
    11
5a0ceec0c421 KeepWithNext experiments
andy_robinson
parents:
diff changeset
    12
from reportlab.test import unittest
2505
0859d48c2558 reportlab/test: fix so individual tests print output folder location
rgbecker
parents: 2332
diff changeset
    13
from reportlab.test.utils import makeSuiteForClasses, outputfile, printLocation
1667
dfe2f821f4e2 Slightly refactored test suite to do the same thing with less code.
dinu_gherman
parents: 1440
diff changeset
    14
1424
5a0ceec0c421 KeepWithNext experiments
andy_robinson
parents:
diff changeset
    15
from reportlab.platypus.flowables import Flowable
5a0ceec0c421 KeepWithNext experiments
andy_robinson
parents:
diff changeset
    16
from reportlab.lib import colors
5a0ceec0c421 KeepWithNext experiments
andy_robinson
parents:
diff changeset
    17
from reportlab.lib.units import cm
5a0ceec0c421 KeepWithNext experiments
andy_robinson
parents:
diff changeset
    18
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
5a0ceec0c421 KeepWithNext experiments
andy_robinson
parents:
diff changeset
    19
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
5a0ceec0c421 KeepWithNext experiments
andy_robinson
parents:
diff changeset
    20
from reportlab.platypus.paragraph import Paragraph
5a0ceec0c421 KeepWithNext experiments
andy_robinson
parents:
diff changeset
    21
from reportlab.platypus.frames import Frame
5a0ceec0c421 KeepWithNext experiments
andy_robinson
parents:
diff changeset
    22
from reportlab.lib.randomtext import randomText, PYTHON
2659
08af1b8a5d05 add Indenter vs keepWithNext breakage found by Ilpo Nyyssönen
rgbecker
parents: 2505
diff changeset
    23
from reportlab.platypus.doctemplate import PageTemplate, BaseDocTemplate, Indenter, SimpleDocTemplate
1424
5a0ceec0c421 KeepWithNext experiments
andy_robinson
parents:
diff changeset
    24
from reportlab.platypus.paragraph import *
5a0ceec0c421 KeepWithNext experiments
andy_robinson
parents:
diff changeset
    25
5a0ceec0c421 KeepWithNext experiments
andy_robinson
parents:
diff changeset
    26
5a0ceec0c421 KeepWithNext experiments
andy_robinson
parents:
diff changeset
    27
def myMainPageFrame(canvas, doc):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    28
    "The page frame used for all PDF documents."
1427
5cdd8fa1458a Allowed for dubgging runs
rgbecker
parents: 1424
diff changeset
    29
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    30
    canvas.saveState()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    31
    canvas.setFont('Times-Roman', 12)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    32
    pageNumber = canvas.getPageNumber()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    33
    canvas.drawString(10*cm, cm, str(pageNumber))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    34
    canvas.restoreState()
1427
5cdd8fa1458a Allowed for dubgging runs
rgbecker
parents: 1424
diff changeset
    35
1424
5a0ceec0c421 KeepWithNext experiments
andy_robinson
parents:
diff changeset
    36
5a0ceec0c421 KeepWithNext experiments
andy_robinson
parents:
diff changeset
    37
class MyDocTemplate(BaseDocTemplate):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    38
    _invalidInitArgs = ('pageTemplates',)
1427
5cdd8fa1458a Allowed for dubgging runs
rgbecker
parents: 1424
diff changeset
    39
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    40
    def __init__(self, filename, **kw):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    41
        frame1 = Frame(2.5*cm, 15.5*cm, 6*cm, 10*cm, id='F1')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    42
        frame2 = Frame(11.5*cm, 15.5*cm, 6*cm, 10*cm, id='F2')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    43
        frame3 = Frame(2.5*cm, 2.5*cm, 6*cm, 10*cm, id='F3')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    44
        frame4 = Frame(11.5*cm, 2.5*cm, 6*cm, 10*cm, id='F4')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    45
        self.allowSplitting = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    46
        self.showBoundary = 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    47
        apply(BaseDocTemplate.__init__, (self, filename), kw)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    48
        template = PageTemplate('normal', [frame1, frame2, frame3, frame4], myMainPageFrame)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    49
        self.addPageTemplates(template)
1427
5cdd8fa1458a Allowed for dubgging runs
rgbecker
parents: 1424
diff changeset
    50
5cdd8fa1458a Allowed for dubgging runs
rgbecker
parents: 1424
diff changeset
    51
1667
dfe2f821f4e2 Slightly refactored test suite to do the same thing with less code.
dinu_gherman
parents: 1440
diff changeset
    52
def _test0(self):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    53
    "This makes one long multi-page paragraph."
1667
dfe2f821f4e2 Slightly refactored test suite to do the same thing with less code.
dinu_gherman
parents: 1440
diff changeset
    54
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    55
    # Build story.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    56
    story = []
1427
5cdd8fa1458a Allowed for dubgging runs
rgbecker
parents: 1424
diff changeset
    57
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    58
    styleSheet = getSampleStyleSheet()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    59
    h1 = styleSheet['Heading1']
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    60
    h1.pageBreakBefore = 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    61
    h1.keepWithNext = 1
1427
5cdd8fa1458a Allowed for dubgging runs
rgbecker
parents: 1424
diff changeset
    62
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    63
    h2 = styleSheet['Heading2']
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    64
    h2.frameBreakBefore = 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    65
    h2.keepWithNext = 1
1424
5a0ceec0c421 KeepWithNext experiments
andy_robinson
parents:
diff changeset
    66
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    67
    h3 = styleSheet['Heading3']
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    68
    h3.backColor = colors.cyan
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    69
    h3.keepWithNext = 1
1427
5cdd8fa1458a Allowed for dubgging runs
rgbecker
parents: 1424
diff changeset
    70
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    71
    bt = styleSheet['BodyText']
1427
5cdd8fa1458a Allowed for dubgging runs
rgbecker
parents: 1424
diff changeset
    72
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    73
    story.append(Paragraph("""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    74
        Subsequent pages test pageBreakBefore, frameBreakBefore and
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    75
        keepTogether attributes.  Generated at %s.  The number in brackets
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    76
        at the end of each paragraph is its position in the story. (%d)""" % (
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    77
            time.ctime(time.time()), len(story)), bt))
1427
5cdd8fa1458a Allowed for dubgging runs
rgbecker
parents: 1424
diff changeset
    78
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    79
    for i in range(10):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    80
        story.append(Paragraph('Heading 1 always starts a new page (%d)' % len(story), h1))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    81
        for j in range(3):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    82
            story.append(Paragraph('Heading1 paragraphs should always'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    83
                            'have a page break before.  Heading 2 on the other hand'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    84
                            'should always have a FRAME break before (%d)' % len(story), bt))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    85
            story.append(Paragraph('Heading 2 always starts a new frame (%d)' % len(story), h2))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    86
            story.append(Paragraph('Heading1 paragraphs should always'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    87
                            'have a page break before.  Heading 2 on the other hand'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    88
                            'should always have a FRAME break before (%d)' % len(story), bt))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    89
            for j in range(3):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    90
                story.append(Paragraph(randomText(theme=PYTHON, sentences=2)+' (%d)' % len(story), bt))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    91
                story.append(Paragraph('I should never be at the bottom of a frame (%d)' % len(story), h3))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    92
                story.append(Paragraph(randomText(theme=PYTHON, sentences=1)+' (%d)' % len(story), bt))
1427
5cdd8fa1458a Allowed for dubgging runs
rgbecker
parents: 1424
diff changeset
    93
2268
a51a4b6873c8 Move to having all test output in a temp folder
rgbecker
parents: 1683
diff changeset
    94
    doc = MyDocTemplate(outputfile('test_platypus_breaking.pdf'))
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    95
    doc.multiBuild(story)
1424
5a0ceec0c421 KeepWithNext experiments
andy_robinson
parents:
diff changeset
    96
1667
dfe2f821f4e2 Slightly refactored test suite to do the same thing with less code.
dinu_gherman
parents: 1440
diff changeset
    97
1424
5a0ceec0c421 KeepWithNext experiments
andy_robinson
parents:
diff changeset
    98
class BreakingTestCase(unittest.TestCase):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
    99
    "Test multi-page splitting of paragraphs (eyeball-test)."
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
   100
    def test0(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
   101
        _test0(self)
1667
dfe2f821f4e2 Slightly refactored test suite to do the same thing with less code.
dinu_gherman
parents: 1440
diff changeset
   102
2659
08af1b8a5d05 add Indenter vs keepWithNext breakage found by Ilpo Nyyssönen
rgbecker
parents: 2505
diff changeset
   103
    def test1(self):
2660
c147aff8edae reportlab: minor fixes and add strokeDashArray
rgbecker
parents: 2659
diff changeset
   104
        '''Ilpo Nyyss\xf6nen posted this broken test'''
2659
08af1b8a5d05 add Indenter vs keepWithNext breakage found by Ilpo Nyyssönen
rgbecker
parents: 2505
diff changeset
   105
        normalStyle = ParagraphStyle(name = 'normal')
08af1b8a5d05 add Indenter vs keepWithNext breakage found by Ilpo Nyyssönen
rgbecker
parents: 2505
diff changeset
   106
        keepStyle = ParagraphStyle(name = 'keep', keepWithNext = True)
08af1b8a5d05 add Indenter vs keepWithNext breakage found by Ilpo Nyyssönen
rgbecker
parents: 2505
diff changeset
   107
        content = [
08af1b8a5d05 add Indenter vs keepWithNext breakage found by Ilpo Nyyssönen
rgbecker
parents: 2505
diff changeset
   108
            Paragraph("line 1", keepStyle),
08af1b8a5d05 add Indenter vs keepWithNext breakage found by Ilpo Nyyssönen
rgbecker
parents: 2505
diff changeset
   109
            Indenter(left = 1 * cm),
08af1b8a5d05 add Indenter vs keepWithNext breakage found by Ilpo Nyyssönen
rgbecker
parents: 2505
diff changeset
   110
            Paragraph("line 2", normalStyle),
08af1b8a5d05 add Indenter vs keepWithNext breakage found by Ilpo Nyyssönen
rgbecker
parents: 2505
diff changeset
   111
            ]
08af1b8a5d05 add Indenter vs keepWithNext breakage found by Ilpo Nyyssönen
rgbecker
parents: 2505
diff changeset
   112
        doc = SimpleDocTemplate(outputfile('test_platypus_breaking1.pdf'))
08af1b8a5d05 add Indenter vs keepWithNext breakage found by Ilpo Nyyssönen
rgbecker
parents: 2505
diff changeset
   113
        doc.build(content)
08af1b8a5d05 add Indenter vs keepWithNext breakage found by Ilpo Nyyssönen
rgbecker
parents: 2505
diff changeset
   114
1424
5a0ceec0c421 KeepWithNext experiments
andy_robinson
parents:
diff changeset
   115
5a0ceec0c421 KeepWithNext experiments
andy_robinson
parents:
diff changeset
   116
def makeSuite():
1667
dfe2f821f4e2 Slightly refactored test suite to do the same thing with less code.
dinu_gherman
parents: 1440
diff changeset
   117
    return makeSuiteForClasses(BreakingTestCase)
1424
5a0ceec0c421 KeepWithNext experiments
andy_robinson
parents:
diff changeset
   118
1667
dfe2f821f4e2 Slightly refactored test suite to do the same thing with less code.
dinu_gherman
parents: 1440
diff changeset
   119
dfe2f821f4e2 Slightly refactored test suite to do the same thing with less code.
dinu_gherman
parents: 1440
diff changeset
   120
#noruntests
1427
5cdd8fa1458a Allowed for dubgging runs
rgbecker
parents: 1424
diff changeset
   121
if __name__ == "__main__": #NORUNTESTS
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
   122
    if 'debug' in sys.argv:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
   123
        _test0(None)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1667
diff changeset
   124
    else:
2268
a51a4b6873c8 Move to having all test output in a temp folder
rgbecker
parents: 1683
diff changeset
   125
        unittest.TextTestRunner().run(makeSuite())
2505
0859d48c2558 reportlab/test: fix so individual tests print output folder location
rgbecker
parents: 2332
diff changeset
   126
        printLocation()