File: test_pdfbase_pdfdoc.py

package info (click to toggle)
python-reportlab 3.3.0-2%2Bdeb9u1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 7,172 kB
  • sloc: python: 71,880; ansic: 19,093; xml: 1,494; makefile: 416; java: 193; sh: 100
file content (60 lines) | stat: -rw-r--r-- 2,726 bytes parent folder | download | duplicates (3)
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
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation, NearTestCase
setOutDir(__name__)
import unittest,re,codecs
from reportlab.pdfbase import pdfdoc
from reportlab import rl_config

class PdfdocTestCase(NearTestCase):
    """Tests of expected Unicode and encoding behaviour
    """
    def setUp(self):
        self.pdfMultiLine = rl_config.pdfMultiLine
        self.pdfComments = rl_config.pdfComments
        rl_config.pdfMultiLine = 0
        rl_config.pdfComments = 0

    def tearDown(self):
        rl_config.pdfMultiLine = self.pdfMultiLine
        rl_config.pdfComments = self.pdfComments

    def testPDFText(self):
        self.assertEquals(pdfdoc.PDFText(b'Hello World').format(self.doc),b'<48656c6c6f20576f726c64>')

    def testPDFString(self):
        self.assertEquals(pdfdoc.PDFString(b'Hello World').format(self.doc),b'(Hello World)')
        self.assertEquals(pdfdoc.PDFString(b'Hello\xc2\xa2World',0).format(self.doc),b'(Hello\xa2World)')
        self.assertEquals(pdfdoc.PDFString(b'Hello\xc2\xa0World',0).format(self.doc),b'(\xfe\xff\x00H\x00e\x00l\x00l\x00o\x00\xa0\x00W\x00o\x00r\x00l\x00d)')
        self.assertEquals(pdfdoc.PDFString(b'Hello\xc2\xa0World',1).format(self.doc),b'(\\376\\377\\000H\\000e\\000l\\000l\\000o\\000\\240\\000W\\000o\\000r\\000l\\000d)')
        self.assertEquals(pdfdoc.PDFString(u'Hello\xa0World'.encode('utf8'),1).format(self.doc),b'(\\376\\377\\000H\\000e\\000l\\000l\\000o\\000\\240\\000W\\000o\\000r\\000l\\000d)')
        self.assertEquals(pdfdoc.PDFString(u'Hello\xa0World'.encode('utf8'),0).format(self.doc),b'(\xfe\xff\x00H\x00e\x00l\x00l\x00o\x00\xa0\x00W\x00o\x00r\x00l\x00d)')

    def testPDFArray(self):
        self.assertEquals(pdfdoc.PDFArray([1,2,3,4]).format(self.doc),b'[ 1 2 3 4 ]')

    def testPDFIndirectObject(self):
        doc = self.doc
        doc.Reference(pdfdoc.PDFArray([0,1,2,3]),pdfdoc.PDFName('abracadabra')[1:])
        self.assertEquals(pdfdoc.PDFIndirectObject('abracadabra',pdfdoc.PDFArray([3,2,1,0])).format(doc),b'2 0 obj\r\n[ 3 2 1 0 ]\r\nendobj\r\n')

    def testPDFDictionary(self):
        self.assertEquals(pdfdoc.PDFDictionary(dict(A=pdfdoc.PDFArray([1,2,3,4]))).format(self.doc),b'<< /A [ 1 2 3 4 ] >>')

    def testPDFPageLabels(self):
        doc = self.doc
        PL=pdfdoc.PDFPageLabels()
        PL.addPageLabel(0,pdfdoc.PDFPageLabel('D',0,'AA'))
        self.assertEquals(PL.format(doc),b'<< /Nums [ 0 2 0 R ] >>')

    @property
    def doc(self):
        return pdfdoc.PDFDocument()

def makeSuite():
    return makeSuiteForClasses(
        PdfdocTestCase,
        )

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