147.1.3
by Robert Collins
test and deliver basic pending-merges into bzr so that merging is recorded |
1 |
#!/usr/bin/env python
|
2 |
# arch-tag: c821e9df-8e3d-4728-838a-0d78775eb4c6
|
|
3 |
# Copyright (C) 2004 David Allouche <david@allouche.net>
|
|
4 |
# Canonical Ltd.
|
|
5 |
#
|
|
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.
|
|
10 |
#
|
|
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.
|
|
15 |
#
|
|
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
|
|
19 |
||
20 |
"""Test cases for name escaping.
|
|
21 |
"""
|
|
22 |
||
23 |
import sys |
|
24 |
import unittest |
|
25 |
import arch |
|
26 |
from arch.tests import framework |
|
27 |
||
28 |
||
29 |
class NameEscaping(unittest.TestCase): |
|
30 |
||
31 |
tests = [] |
|
32 |
||
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)]) |
|
36 |
charset = ''.join(( |
|
37 |
uni_escape(0x1, 0x8), |
|
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 |
|
46 |
return charset |
|
47 |
||
48 |
def help_test_escape(self, command, null_too): |
|
49 |
if null_too: start = 0 |
|
50 |
else: start = 1 |
|
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) |
|
55 |
||
56 |
def tla_escape(self): |
|
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 |
|
61 |
### XXXX
|
|
62 |
self.help_test_escape(tla.tla_name_escape, False) |
|
63 |
tests.append('tla_escape') |
|
64 |
||
65 |
def python_escape(self): |
|
66 |
"""Native escaping works correctly."""
|
|
67 |
self.help_test_escape(arch.name_escape, True) |
|
68 |
tests.append('python_escape') |
|
69 |
||
70 |
def interesting_escapes(self): |
|
71 |
def symbols(syms): |
|
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)' |
|
75 |
||
76 |
def help_test_unescape(self, command, null_too): |
|
77 |
if null_too: start = 0 |
|
78 |
else: start = 1 |
|
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 |
|
85 |
||
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 |
|
91 |
### XXX
|
|
92 |
self.help_test_unescape(tla.tla_name_unescape, False) |
|
93 |
tests.append('tla_unescape') |
|
94 |
||
95 |
def python_unescape(self): |
|
96 |
"""Native unescaping works correctly."""
|
|
97 |
self.help_test_unescape(arch.name_unescape, False) |
|
98 |
tests.append('python_unescape') |
|
99 |
||
100 |
||
101 |
def test_suite(limit=()): |
|
102 |
classes = ( |
|
103 |
'NameEscaping', |
|
104 |
)
|
|
105 |
return framework.make_test_suite(globals(), classes, limit) |
|
106 |
||
107 |
def main(argv): |
|
108 |
return framework.run_test_suite(argv, test_suite) |
|
109 |
||
110 |
if __name__ == "__main__": |
|
111 |
sys.exit(main(sys.argv)) |
|
112 |