| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
""" |
| 4 |
These are very basic tests, because fribidi has its own tests. It may be |
| 5 |
better to reuse fribidi own tests, but its not clear what is the value |
| 6 |
of base_dir for those tests. |
| 7 |
""" |
| 8 |
|
| 9 |
import sys |
| 10 |
import unittest |
| 11 |
import pyfribidi |
| 12 |
from pyfribidi import RTL, LTR, ON |
| 13 |
|
| 14 |
|
| 15 |
class TestSkipped(Exception): |
| 16 |
""" Raised when test can not run """ |
| 17 |
|
| 18 |
|
| 19 |
class InputTests(unittest.TestCase): |
| 20 |
|
| 21 |
def testRequireInputString(self): |
| 22 |
""" input: require input string or unicode """ |
| 23 |
self.assertRaises(TypeError, pyfribidi.log2vis) |
| 24 |
|
| 25 |
def testInvalidInputString(self): |
| 26 |
""" input: raise TypeError for non string or unicode input """ |
| 27 |
self.assertRaises(TypeError, pyfribidi.log2vis, 1) |
| 28 |
|
| 29 |
def testInvalidDirection(self): |
| 30 |
""" input: raise ValueError for invalid direction """ |
| 31 |
self.assertRaises(ValueError, pyfribidi.log2vis, u"שלום", |
| 32 |
base_direction=1) |
| 33 |
|
| 34 |
def testUnknownEncoding(self): |
| 35 |
""" input: raise LookupError for invalid encoding """ |
| 36 |
encoded = u"שלום".encode('utf-8') |
| 37 |
self.assertRaises(LookupError, pyfribidi.log2vis, encoded, |
| 38 |
encoding='foo') |
| 39 |
|
| 40 |
def testInvalidEncodedString(self): |
| 41 |
""" input: raise UnicodeError for invalid encoded string """ |
| 42 |
encoded = u"שלום".encode('utf-8') |
| 43 |
self.assertRaises(UnicodeError, pyfribidi.log2vis, encoded, |
| 44 |
encoding='iso8859-8') |
| 45 |
|
| 46 |
|
| 47 |
class UnicodeTests(unittest.TestCase): |
| 48 |
|
| 49 |
def testEmpty(self): |
| 50 |
""" unicode: empty string """ |
| 51 |
self.assertEqual(pyfribidi.log2vis(u''), u'') |
| 52 |
|
| 53 |
def testBigString(self): |
| 54 |
""" unicode: big string |
| 55 |
|
| 56 |
It does not make sense to order such big strings, this just |
| 57 |
checks that there are no size limits in pyfribidi. |
| 58 |
""" |
| 59 |
# About 2MB string for default python build (ucs2) |
| 60 |
big = (u'א' * 1024) * 1024 |
| 61 |
self.assertEqual(pyfribidi.log2vis(big), big) |
| 62 |
|
| 63 |
def testDefaultDirection(self): |
| 64 |
""" unicode: use RTL default """ |
| 65 |
self.assertEqual(pyfribidi.log2vis(u"hello - שלום"), |
| 66 |
pyfribidi.log2vis(u"hello - שלום", RTL)) |
| 67 |
|
| 68 |
def testAsRTL(self): |
| 69 |
""" unicode: reorder line as RTL """ |
| 70 |
self.assertEqual(pyfribidi.log2vis(u"hello - שלום", RTL), |
| 71 |
u"םולש - hello") |
| 72 |
|
| 73 |
def testAsLTR(self): |
| 74 |
""" unicode: reorder line as LTR """ |
| 75 |
self.assertEqual(pyfribidi.log2vis(u"hello - שלום", LTR), |
| 76 |
u"hello - םולש") |
| 77 |
|
| 78 |
def testNaturalLTR(self): |
| 79 |
""" unicode: reorder LTR line by natural order """ |
| 80 |
self.assertEqual(pyfribidi.log2vis(u"hello - שלום", ON), |
| 81 |
u"hello - םולש") |
| 82 |
|
| 83 |
def testNaturalRTL(self): |
| 84 |
""" unicode: reorder RTL line by natural order """ |
| 85 |
self.assertEqual(pyfribidi.log2vis(u"שלום - hello", ON), |
| 86 |
u"hello - םולש") |
| 87 |
|
| 88 |
def testNoReorderNonSpacingMarks(self): |
| 89 |
"""unicode: reorder non spacing marks""" |
| 90 |
self.assertEqual(pyfribidi.log2vis(u"חַיְפַא", RTL, reordernsm=False), |
| 91 |
u"אַפְיַח" |
| 92 |
) |
| 93 |
|
| 94 |
def testReorderNonSpacingMarks(self): |
| 95 |
"""unicode: reorder non spacing marks""" |
| 96 |
self.assertEqual(pyfribidi.log2vis(u"חַיְפַא", RTL), |
| 97 |
u"אפַיְחַ" |
| 98 |
) |
| 99 |
|
| 100 |
|
| 101 |
class UTF8Tests(unittest.TestCase): |
| 102 |
""" Same tests for utf8, used mainly on linux """ |
| 103 |
|
| 104 |
def testEmpty(self): |
| 105 |
""" utf8: empty string """ |
| 106 |
self.assertEqual(pyfribidi.log2vis(''), '') |
| 107 |
|
| 108 |
def testBigString(self): |
| 109 |
""" utf8: big string |
| 110 |
|
| 111 |
It does not make sense to order such big strings, this just |
| 112 |
checks that there are no size limits in pyfribidi. |
| 113 |
""" |
| 114 |
# About 2MB string |
| 115 |
big = ('א' * 1024) * 1024 |
| 116 |
self.assertEqual(pyfribidi.log2vis(big), big) |
| 117 |
|
| 118 |
def testDefaultDirection(self): |
| 119 |
""" utf8: use RTL default """ |
| 120 |
self.assertEqual(pyfribidi.log2vis("hello - שלום"), |
| 121 |
pyfribidi.log2vis("hello - שלום", RTL)) |
| 122 |
|
| 123 |
def testAsRTL(self): |
| 124 |
""" utf8: reorder line as RTL """ |
| 125 |
self.assertEqual(pyfribidi.log2vis("hello - שלום", RTL), |
| 126 |
"םולש - hello") |
| 127 |
|
| 128 |
def testAsLTR(self): |
| 129 |
""" utf8: reorder line as LTR """ |
| 130 |
self.assertEqual(pyfribidi.log2vis("hello - שלום", LTR), |
| 131 |
"hello - םולש") |
| 132 |
|
| 133 |
def testNaturalLTR(self): |
| 134 |
""" utf8: reorder LTR line by natural order """ |
| 135 |
self.assertEqual(pyfribidi.log2vis("hello - שלום", ON), |
| 136 |
"hello - םולש") |
| 137 |
|
| 138 |
def testNaturalRTL(self): |
| 139 |
""" utf8: reorder RTL line by natural order """ |
| 140 |
self.assertEqual(pyfribidi.log2vis("שלום - hello", ON), |
| 141 |
"hello - םולש") |
| 142 |
|
| 143 |
|
| 144 |
def testNoReorderNonSpacingMarks(self): |
| 145 |
"""utf8: reorder non spacing marks""" |
| 146 |
self.assertEqual(pyfribidi.log2vis("חַיְפַא", RTL, reordernsm=False), |
| 147 |
"אַפְיַח" |
| 148 |
) |
| 149 |
|
| 150 |
def testReorderNonSpacingMarks(self): |
| 151 |
"""unicode: reorder non spacing marks""" |
| 152 |
self.assertEqual(pyfribidi.log2vis("חַיְפַא", RTL), |
| 153 |
"אפַיְחַ" |
| 154 |
) |
| 155 |
|
| 156 |
|
| 157 |
class OtherEncodingsTests(unittest.TestCase): |
| 158 |
""" Minimal tests for other encodings """ |
| 159 |
|
| 160 |
def testIso8859_8NaturalRTL(self): |
| 161 |
""" other encodings: iso8859-8 """ |
| 162 |
charset = 'iso8859-8' |
| 163 |
self.assertEqual(pyfribidi.log2vis(u"שלום - hello".encode(charset), |
| 164 |
encoding=charset), |
| 165 |
u"hello - םולש".encode(charset)) |
| 166 |
|
| 167 |
def testCp1255NaturalRTL(self): |
| 168 |
""" other encodings: cp1255 """ |
| 169 |
charset = 'cp1255' |
| 170 |
self.assertEqual(pyfribidi.log2vis(u"שלום - hello".encode(charset), |
| 171 |
encoding=charset), |
| 172 |
u"hello - םולש".encode(charset)) |
| 173 |
|
| 174 |
def testUTF16NaturalRTL(self): |
| 175 |
""" other encodings: utf-16 """ |
| 176 |
charset = 'utf-16' |
| 177 |
self.assertEqual(pyfribidi.log2vis(u"שלום - hello".encode(charset), |
| 178 |
encoding=charset), |
| 179 |
u"hello - םולש".encode(charset)) |
| 180 |
|
| 181 |
class Crasher(unittest.TestCase): |
| 182 |
def test_glibc_free_invalid_next_size(self): |
| 183 |
# *** glibc detected *** /home/ralf/py27/bin/python2: free(): invalid next size (fast): 0x00000000011cff00 *** |
| 184 |
pyfribidi.log2vis('\xf0\x90\x8e\xa2\xf0\x90\x8e\xaf\xf0\x90\x8e\xb4\xf0\x90\x8e\xa1\xf0\x90\x8f\x83') |
| 185 |
|
| 186 |
if __name__ == '__main__': |
| 187 |
suite = unittest.defaultTestLoader.loadTestsFromName(__name__) |
| 188 |
res = unittest.TextTestRunner(verbosity=1).run(suite) |
| 189 |
sys.exit(not res.wasSuccessful()) |