2
# arch-tag: c821e9df-8e3d-4728-838a-0d78775eb4c6
3
# Copyright (C) 2004 David Allouche <david@allouche.net>
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
"""Test cases for name escaping.
26
from arch.tests import framework
29
class NameEscaping(unittest.TestCase):
33
def escaped_charset(self, null_too):
34
def uni_escape(start, end):
35
return ''.join(['\\(U+%X)' % C for C in range(start, end+1)])
38
'\\(tab)\\(nl)\\(U+B)\\(np)\\(cr)',
39
uni_escape(0xE, 0x1F),
40
'\\(sp)!\\"#$%&\'()*+,-./0123456789:;<=>?@',
41
'ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`',
42
'abcdefghijklmnopqrstuvwxyz{|}~',
43
'\x7f', ### That looks like a bug in tla
44
uni_escape(0x80, 0xFF)))
45
if null_too: charset = uni_escape(0,0) + charset
48
def help_test_escape(self, command, null_too):
49
if null_too: start = 0
51
charset = ''.join([chr(C) for C in range(start, 256)])
52
expected = self.escaped_charset(null_too)
53
result = command(charset)
54
self.assertEqual(expected, result, False)
57
"""tla escape does what is expected."""
58
from arch.backends import tla
59
### XXX disable test case if tla does not support escaping
60
if 1 == tla.status_cmd(['escape'], (0,1)): return
62
self.help_test_escape(tla.tla_name_escape, False)
63
tests.append('tla_escape')
65
def python_escape(self):
66
"""Native escaping works correctly."""
67
self.help_test_escape(arch.name_escape, True)
68
tests.append('python_escape')
70
def interesting_escapes(self):
72
return ''.join(['\\(%s)' % S for S in syms])
73
return symbols(('TaB', 'NL', 'Np', 'Cr', 'SP')) + \
74
'\\\\' '\\"' '\\(U+4A)\\(u+4B)\\(U+4c)\\(U+00004D)'
76
def help_test_unescape(self, command, null_too):
77
if null_too: start = 0
79
expected = ''.join([chr(C) for C in range(start, 256)])
80
result = command(self.escaped_charset(null_too))
81
self.assertEqual(expected, result) # whole charset
82
expected = ''.join(map(chr, [9, 10, 12, 13, 32]) + ['\\', '"', 'JKLM'])
83
result = command(self.interesting_escapes())
84
self.assertEqual(expected, result) # interesting escapes
86
def tla_unescape(self):
87
"""tla unescape does what is expected."""
88
from arch.backends import tla
89
### XXX disable test case if tla does not support escaping
90
if 1 == tla.status_cmd(['escape'], (0,1)): return
92
self.help_test_unescape(tla.tla_name_unescape, False)
93
tests.append('tla_unescape')
95
def python_unescape(self):
96
"""Native unescaping works correctly."""
97
self.help_test_unescape(arch.name_unescape, False)
98
tests.append('python_unescape')
101
def test_suite(limit=()):
105
return framework.make_test_suite(globals(), classes, limit)
108
return framework.run_test_suite(argv, test_suite)
110
if __name__ == "__main__":
111
sys.exit(main(sys.argv))