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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
import unittest, sys
from unittest import TestCase
from plasTeX.Tokenizer import *
from plasTeX.TeX import *
class Numbers(TestCase):
def testReadDecimal(self):
s = TeX()
s.input(r'-1.0')
i = s.readDecimal()
assert i == -1, 'expected -1, but got %s' % i
s = TeX()
s.input(r'-11234.0')
i = s.readDecimal()
assert i == -11234, 'expected -11234, but got %s' % i
s = TeX()
s.input(r'0.0')
i = s.readDecimal()
assert i == 0, 'expected 0, but got %s' % i
def testReadDimen(self):
fuzz = 1e-3
s = TeX()
s.input(r'3 in')
i = s.readDimen()
assert i.inch - 3 < fuzz, i.inch
s = TeX()
s.input(r'29 pc')
i = s.readDimen()
assert i.pc - 29 < fuzz, i.pc
s = TeX()
s.input(r'-.013837in')
i = s.readDimen()
assert i.inch - -0.013837 < fuzz, i.inch
s = TeX()
s.input(r'+ 42,1 dd')
i = s.readDimen()
assert i.dd - 42.1 < fuzz, i.dd
s = TeX()
s.input(r'0.mm')
i = s.readDimen()
assert i.mm - 0 < fuzz, i.mm
s = TeX()
s.input(r'123456789sp')
i = s.readDimen()
assert i.sp - 123456789 < fuzz, i.sp
def testReadDimen2(self):
# This is illegal
# s = TeX()
# s.input(r"'.77pt")
# i = s.readDimen()
# s = TeX()
# s.input(r'"Ccc')
# i = s.readDimen()
s = TeX()
s.input(r'-,sp')
i = s.readDimen()
assert i.sp == 0, i.sp
def testUnitConversion(self):
fuzz = 1e-3
s = TeX()
s.input(r'1 pc')
i = s.readDimen()
assert i.pt - 12 < fuzz, i.pt
s = TeX()
s.input(r'1 in')
i = s.readDimen()
assert i.pt - 72.27 < fuzz, i.pt
s = TeX()
s.input(r'72 bp')
i = s.readDimen()
assert i.inch - 1 < fuzz, i.inch
s = TeX()
s.input(r'2.54 cm')
i = s.readDimen()
assert i.inch - 1 < fuzz, i.inch
s = TeX()
s.input(r'10 mm')
i = s.readDimen()
assert i.cm - 1 < fuzz, i.cm
s = TeX()
s.input(r'1157 dd')
i = s.readDimen()
assert i.pt - 1238 < fuzz, i.pt
s = TeX()
s.input(r'1 cc')
i = s.readDimen()
assert i.dd - 12 < fuzz, i.dd
s = TeX()
s.input(r'65536 sp')
i = s.readDimen()
assert i.pt - 1 < fuzz, i.pt
def testReadGlue(self):
s = TeX()
s.input(r'0pt plus 1fil')
i = s.readGlue()
assert i.pt == 0, i.pt
assert i.stretch.fil == 1, i.stretch.fil
assert i.shrink is None, i.shrink
s = TeX()
s.input(r'0pt plus 1fill')
i = s.readGlue()
assert i.pt == 0, i.pt
assert i.stretch.fil == 1, i.stretch.fil
assert i.shrink is None, i.shrink
s = TeX()
s.input(r'0pt plus 1fil minus 1 fil')
i = s.readGlue()
assert i.pt == 0, i.pt
assert i.stretch.fil == 1, i.stretch.fil
assert i.shrink.fil == 1, i.shrink.fil
s = TeX()
s.input(r'0pt plus -1fil')
i = s.readGlue()
assert i.pt == 0, i.pt
assert i.stretch.fil == -1, i.stretch.fil
assert i.shrink is None, i.shrink
def testReadGlue2(self):
s = TeX()
s.input(r'6pt plus 2pt minus 2pt')
i = s.readGlue()
assert i.pt == 6, i.pt
assert i.stretch.pt == 2, i.stretch.pt
assert i.shrink.pt == 2, i.shrink.pt
t = TeX()
t.input(r'6pt plus 2pt minus 2pt 1.2pt plus -1.fil-1.234pt\foo')
i = t.readGlue()
j = t.readGlue()
k = t.readGlue()
# print i.source
assert i.pt == 6, i.pt
assert i.stretch.pt == 2, i.stretch.pt
assert i.shrink.pt == 2, i.shrink.pt
# print j.source
assert j.pt == 1.2, i.pt
assert j.stretch.fil == -1, j.stretch.fil
assert j.shrink is None
# print k.source
assert k.pt == -1.234, k.pt
assert k.stretch is None
assert k.shrink is None
tokens = [x for x in t.itertokens()]
assert tokens == [EscapeSequence('foo')], tokens
class Parameters(TestCase):
def testParameters(self):
t = TeX()
t.input(r'\newcount\foo\foo=\tolerance')
t.parse()
foo = t.ownerDocument.context['foo'].value
tolerance = t.ownerDocument.context['tolerance'].value
assert foo == tolerance, '"%s" != "%s"' % (foo, tolerance)
t = TeX()
t.input(r'\newcount\foo\foo=7\tolerance')
t.parse()
foo = t.ownerDocument.context['foo'].value
tolerance = t.ownerDocument.context['tolerance'].value
assert foo == (7*tolerance), '"%s" != "%s"' % (foo, 7*tolerance)
t = TeX()
t.input(r'\newcount\foo\foo=-3\tolerance')
t.parse()
foo = t.ownerDocument.context['foo'].value
tolerance = t.ownerDocument.context['tolerance'].value
assert foo == (-3*tolerance), '"%s" != "%s"' % (foo, -3*tolerance)
def testDimenParameters(self):
t = TeX()
t.input(r'\newdimen\foo\foo=\hsize')
t.parse()
foo = t.ownerDocument.context['foo'].value
hsize = t.ownerDocument.context['hsize'].value
assert foo == hsize, '"%s" != "%s"' % (foo, hsize)
t = TeX()
t.input(r'\newdimen\foo\foo=7.6\hsize')
t.parse()
foo = t.ownerDocument.context['foo'].value
hsize = t.ownerDocument.context['hsize'].value
assert foo == (7.6*hsize), '"%s" != "%s"' % (foo, 7.6*hsize)
t = TeX()
t.input(r'\newdimen\foo\foo=-4\hsize')
t.parse()
foo = t.ownerDocument.context['foo'].value
hsize = t.ownerDocument.context['hsize'].value
assert foo == (-4*hsize), '"%s" != "%s"' % (foo, (-4*hsize))
def testGlueParameters(self):
t = TeX()
t.input(r'\newskip\foo\foo=\baselineskip')
t.parse()
foo = t.ownerDocument.context['foo'].value
baselineskip = t.ownerDocument.context['baselineskip'].value
assert foo == baselineskip, '"%s" != "%s"' % (foo, baselineskip)
t = TeX()
t.input(r'\newskip\foo\foo=7.6\baselineskip')
t.parse()
foo = t.ownerDocument.context['foo'].value
baselineskip = t.ownerDocument.context['baselineskip'].value
assert foo == (7.6*baselineskip), '"%s" != "%s"' % (foo, 7.6*baselineskip)
t = TeX()
t.input(r'\newskip\foo\foo=-4\baselineskip')
t.parse()
foo = t.ownerDocument.context['foo'].value
baselineskip = t.ownerDocument.context['baselineskip'].value
assert foo == (-4*baselineskip), '"%s" != "%s"' % (foo, (-4*baselineskip))
def testRomanNumeral(self):
t = TeX()
t.input(r'\romannumeral5')
p = t.parse()
assert ''.join(p) == 'v'
def testNumberSection(self):
t = TeX()
t.input(r'''
\documentclass{article}
\begin{document}
\section{}
\section{}
\number\thesection
\end{document}
''')
assert t.parse().textContent.strip() == '2'
def testRomanNumeralSection(self):
t = TeX()
t.input(r'''
\documentclass{article}
\begin{document}
\section{}
\section{}
\romannumeral\thesection
\end{document}
''')
assert t.parse().textContent.strip() == 'ii'
if __name__ == '__main__':
unittest.main()
|