author  rptlab 
Tue, 30 Apr 2013 14:28:14 +0100  
branch  py33 
changeset 3723  99aa837b6703 
parent 3721  0c93dd8ff567 
child 3862  272e650eecf5 
permissions  rwxrxrx 
2963  1 
__version__=''' $Id''' 
2 
__doc__='''basic tests.''' 

2984  3 
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, printLocation 
4 
setOutDir(__name__) 

2963  5 

2966  6 
import unittest 
2963  7 

8 
def getrc(defns,depth=1): 

9 
from sys import getrefcount, _getframe 

10 
f = _getframe(depth) 

11 
G0 = f.f_globals 

12 
L = f.f_locals 

13 
if L is not G0: 

14 
LL = [L] 

15 
while 1: 

16 
f = f.f_back 

17 
G = f.f_globals 

18 
L = f.f_locals 

19 
if G is not G0 or G is L: break 

20 
LL.append(L) 

21 
L = {} 

22 
LL.reverse() 

23 
for l in LL: 

24 
L.update(l) 

25 
else: 

26 
L = L.copy() 

27 
G0 = G0.copy() 

28 
return ' '.join([str(getrefcount(eval(x,L,G0))1) for x in defns.split()]) 

29 

30 
def checkrc(defns,rcv0): 

31 
rcv1 = getrc(defns,2) 

32 
return ' '.join(["%s %s>%s" % (x,v,w) for x,v,w in zip(defns.split(),rcv0.split(),rcv1.split()) if v!=w]) 

33 

34 
class RlAccelTestCase(unittest.TestCase): 

35 

36 
def testFpStr(self): 

37 
# should give siz decimal places if less than 1. 

38 
# if more, give up to seven sig figs 

39 
from _rl_accel import fp_str 

40 
assert fp_str(1,2,3)=='1 2 3' 

41 
assert fp_str(1) == '1' 

42 

43 
assert fp_str(595.275574) == '595.2756' 

44 
assert fp_str(59.5275574) == '59.52756' 

45 
assert fp_str(5.95275574) == '5.952756' 

46 

47 
def test_AsciiBase85Encode(self): 

48 
from _rl_accel import _AsciiBase85Encode 

49 
assert _AsciiBase85Encode('Dragan Andric')=='6ul^K@;[2RDIdd%@f~>' 

50 

51 
def test_AsciiBase85Decode(self): 

52 
from _rl_accel import _AsciiBase85Decode 

53 
assert _AsciiBase85Decode('6ul^K@;[2RDIdd%@f~>')=='Dragan Andric' 

54 

55 
def testEscapePDF(self): 

56 
from _rl_accel import escapePDF 

57 
assert escapePDF('(test)')=='\\(test\\)' 

58 

59 
def test_instanceEscapePDF(self): 

60 
from _rl_accel import _instanceEscapePDF 

61 
assert _instanceEscapePDF('', '(test)')=='\\(test\\)' 

62 

63 
def testCalcChecksum(self): 

64 
from _rl_accel import calcChecksum 

65 
assert calcChecksum('test')==1952805748 

66 

67 
def test_instanceStringWidth(self): 

68 
from reportlab.pdfbase.pdfmetrics import registerFont, getFont, _fonts, unicode2T1 

69 
from reportlab.pdfbase.ttfonts import TTFont 

2977
beca8d75f400
reportlab: remove LetErrorRobotChrome, rina & luxi. Add DarkGardenMK and fix tests
rgbecker
parents:
2966
diff
changeset

70 
ttfn = 'Vera' 
2963  71 
t1fn = 'TimesRoman' 
2977
beca8d75f400
reportlab: remove LetErrorRobotChrome, rina & luxi. Add DarkGardenMK and fix tests
rgbecker
parents:
2966
diff
changeset

72 
registerFont(TTFont(ttfn, "Vera.ttf")) 
2963  73 
ttf = getFont(ttfn) 
74 
t1f = getFont(t1fn) 

75 
testCp1252 = 'copyright %s trademark %s registered %s ReportLab! Ol%s!' % (chr(169), chr(153),chr(174), chr(0xe9)) 

76 
enc='cp1252' 

77 
senc = 'utf8' 

78 
ts = 'ABCDEF\xce\x91\xce\xb2G' 

79 
utext = 'ABCDEF\xce\x91\xce\xb2G'.decode(senc) 

80 
fontSize = 12 

81 
defns="ttfn t1fn ttf t1f testCp1252 enc senc ts utext fontSize ttf.face ttf.face.charWidths ttf.face.defaultWidth t1f.widths t1f.encName t1f.substitutionFonts _fonts" 

82 
rcv = getrc(defns) 

83 
def tfunc(f,ts,fontSize,enc): 

84 
w1 = f.stringWidth(ts,fontSize,enc) 

85 
w2 = f._py_stringWidth(ts,fontSize,enc) 

86 
assert abs(w1w2)<1e10,"f(%r).stringWidthU(%r,%s,%r)>%r != f._py_stringWidth(...)>%r" % (f,ts,fontSize,enc,w1,w2) 

87 
tfunc(t1f,testCp1252,fontSize,enc) 

88 
tfunc(t1f,ts,fontSize,senc) 

89 
tfunc(t1f,utext,fontSize,senc) 

90 
tfunc(ttf,ts,fontSize,senc) 

91 
tfunc(ttf,testCp1252,fontSize,enc) 

92 
tfunc(ttf,utext,fontSize,senc) 

93 
rcc = checkrc(defns,rcv) 

94 
assert not rcc, "rc diffs (%s)" % rcc 

95 

96 
def test_unicode2T1(self): 

97 
from reportlab.pdfbase.pdfmetrics import _py_unicode2T1, getFont, _fonts 

98 
from _rl_accel import unicode2T1 

99 
t1fn = 'TimesRoman' 

100 
t1f = getFont(t1fn) 

101 
enc = 'cp1252' 

102 
senc = 'utf8' 

103 
testCp1252 = ('copyright %s trademark %s registered %s ReportLab! Ol%s!' % (chr(169), chr(153),chr(174), chr(0xe9))).decode(enc) 

104 
utext = 'This is the end of the \xce\x91\xce\xb2 world. This is the end of the \xce\x91\xce\xb2 world jap=\xe3\x83\x9b\xe3\x83\x86. This is the end of the \xce\x91\xce\xb2 world. This is the end of the \xce\x91\xce\xb2 world jap=\xe3\x83\x9b\xe3\x83\x86'.decode('utf8') 

105 
def tfunc(f,ts): 

106 
w1 = unicode2T1(ts,[f]+f.substitutionFonts) 

107 
w2 = _py_unicode2T1(ts,[f]+f.substitutionFonts) 

108 
assert w1==w2,"%r != %r" % (w1,w2) 

109 
defns="t1fn t1f testCp1252 enc senc utext t1f.widths t1f.encName t1f.substitutionFonts _fonts" 

110 
rcv = getrc(defns) 

111 
tfunc(t1f,testCp1252) 

112 
tfunc(t1f,utext) 

113 
rcc = checkrc(defns,rcv) 

114 
assert not rcc, "rc diffs (%s)" % rcc 

115 

116 
def test_sameFrag(self): 

117 
from _rl_accel import _sameFrag 

118 
class ABag: 

119 
def __init__(self,**kwd): 

120 
self.__dict__.update(kwd) 

121 
def __str__(self): 

3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

122 
V=['%s=%r' % v for v in self.__dict__.items()] 
2963  123 
V.sort() 
124 
return 'ABag(%s)' % ','.join(V) 

125 

126 
a=ABag(fontName='Helvetica',fontSize=12, textColor="red", rise=0, underline=0, strike=0, link="aaaa") 

127 
b=ABag(fontName='Helvetica',fontSize=12, textColor="red", rise=0, underline=0, strike=0, link="aaaa") 

128 
for name in ("fontName", "fontSize", "textColor", "rise", "underline", "strike", "link"): 

129 
old = getattr(a,name) 

130 
assert _sameFrag(a,b)==1, "_sameFrag(%s,%s)!=1" % (a,b) 

131 
assert _sameFrag(b,a)==1, "_sameFrag(%s,%s)!=1" % (b,a) 

132 
setattr(a,name,None) 

133 
assert _sameFrag(a,b)==0, "_sameFrag(%s,%s)!=0" % (a,b) 

134 
assert _sameFrag(b,a)==0, "_sameFrag(%s,%s)!=0" % (b,a) 

135 
delattr(a,name) 

136 
assert _sameFrag(a,b)==0, "_sameFrag(%s,%s)!=0" % (a,b) 

137 
assert _sameFrag(b,a)==0, "_sameFrag(%s,%s)!=0" % (b,a) 

138 
delattr(b,name) 

139 
assert _sameFrag(a,b)==1, "_sameFrag(%s,%s)!=1" % (a,b) 

140 
assert _sameFrag(b,a)==1, "_sameFrag(%s,%s)!=1" % (b,a) 

141 
setattr(a,name,old) 

142 
setattr(b,name,old) 

143 

144 
def makeSuite(): 

145 
# only run the tests if _rl_accel is present 

146 
try: 

147 
import _rl_accel 

148 
Klass = RlAccelTestCase 

149 
except: 

150 
class Klass(unittest.TestCase): 

151 
pass 

152 
return makeSuiteForClasses(Klass) 

153 

154 
#noruntests 

155 
if __name__ == "__main__": 

156 
unittest.TextTestRunner().run(makeSuite()) 

157 
printLocation() 