Revert "Merged all Chromoting Host code into remoting_core.dll (Windows)."
[chromium-blink-merge.git] / native_client_sdk / src / tools / quote_test.py
blob17216287ecefe6dcc0e81435d52967d3d92d838c
1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 import optparse
7 import quote
8 import sys
9 import unittest
11 verbose = False
14 # Wrapped versions of the functions that we're testing, so that during
15 # debugging we can more easily see what their inputs were.
17 def VerboseQuote(in_string, specials, *args, **kwargs):
18 if verbose:
19 sys.stdout.write('Invoking quote(%s, %s, %s)\n' %
20 (repr(in_string), repr(specials),
21 ', '.join([repr(a) for a in args] +
22 [repr(k) + ':' + repr(v)
23 for k, v in kwargs])))
24 return quote.quote(in_string, specials, *args, **kwargs)
27 def VerboseUnquote(in_string, specials, *args, **kwargs):
28 if verbose:
29 sys.stdout.write('Invoking unquote(%s, %s, %s)\n' %
30 (repr(in_string), repr(specials),
31 ', '.join([repr(a) for a in args] +
32 [repr(k) + ':' + repr(v)
33 for k, v in kwargs])))
34 return quote.unquote(in_string, specials, *args, **kwargs)
37 class TestQuote(unittest.TestCase):
38 # test utilities
39 def generic_test(self, fn, in_args, expected_out_obj):
40 actual = apply(fn, in_args)
41 self.assertEqual(actual, expected_out_obj)
43 def check_invertible(self, in_string, specials, escape='\\'):
44 q = VerboseQuote(in_string, specials, escape)
45 qq = VerboseUnquote(q, specials, escape)
46 self.assertEqual(''.join(qq), in_string)
48 def run_test_tuples(self, test_tuples):
49 for func, in_args, expected in test_tuples:
50 self.generic_test(func, in_args, expected)
52 def testQuote(self):
53 test_tuples = [[VerboseQuote,
54 ['foo, bar, baz, and quux too!', 'abc'],
55 'foo, \\b\\ar, \\b\\az, \\and quux too!'],
56 [VerboseQuote,
57 ['when \\ appears in the input', 'a'],
58 'when \\\\ \\appe\\ars in the input']]
59 self.run_test_tuples(test_tuples)
61 def testUnquote(self):
62 test_tuples = [[VerboseUnquote,
63 ['key\\:still_key:value\\:more_value', ':'],
64 ['key:still_key', ':', 'value:more_value']],
65 [VerboseUnquote,
66 ['about that sep\\ar\\ator in the beginning', 'ab'],
67 ['', 'ab', 'out th', 'a', 't separator in the ',
68 'b', 'eginning']],
69 [VerboseUnquote,
70 ['the rain in spain fall\\s ma\\i\\nly on the plains',
71 'ins'],
72 ['the ra', 'in', ' ', 'in', ' ', 's', 'pa', 'in',
73 ' falls mainly o', 'n', ' the pla', 'ins']],
75 self.run_test_tuples(test_tuples)
77 def testInvertible(self):
78 self.check_invertible('abcdefg', 'bc')
79 self.check_invertible('a\\bcdefg', 'bc')
80 self.check_invertible('ab\\cdefg', 'bc')
81 self.check_invertible('\\ab\\cdefg', 'abc')
82 self.check_invertible('abcde\\fg', 'efg')
83 self.check_invertible('a\\b', '')
86 # Invoke this file directly for simple manual testing. For running
87 # the unittests, use the -t flag. Any flags to be passed to the
88 # unittest module should be passed as after the optparse processing,
89 # e.g., "quote_test.py -t -- -v" to pass the -v flag to the unittest
90 # module.
92 def main(argv):
93 global verbose
94 parser = optparse.OptionParser(
95 usage='Usage: %prog [options] word...')
96 parser.add_option('-s', '--special-chars', dest='special_chars', default=':',
97 help='Special characters to quote (default is ":")')
98 parser.add_option('-q', '--quote', dest='quote', default='\\',
99 help='Quote or escape character (default is "\")')
100 parser.add_option('-t', '--run-tests', dest='tests', action='store_true',
101 help='Run built-in tests\n')
102 parser.add_option('-u', '--unquote-input', dest='unquote_input',
103 action='store_true', help='Unquote command line argument')
104 parser.add_option('-v', '--verbose', dest='verbose', action='store_true',
105 help='Verbose test output')
106 options, args = parser.parse_args(argv)
108 if options.verbose:
109 verbose = True
111 num_errors = 0
112 if options.tests:
113 sys.argv = [sys.argv[0]] + args
114 unittest.main()
115 else:
116 for word in args:
117 # NB: there are inputs x for which quote(unquote(x) != x, but
118 # there should be no input x for which unquote(quote(x)) != x.
119 if options.unquote_input:
120 qq = quote.unquote(word, options.special_chars, options.quote)
121 sys.stdout.write('unquote(%s) = %s\n'
122 % (word, ''.join(qq)))
123 # There is no expected output for unquote -- this is just for
124 # manual testing, so it is okay that we do not (and cannot)
125 # update num_errors here.
126 else:
127 q = quote.quote(word, options.special_chars, options.quote)
128 qq = quote.unquote(q, options.special_chars, options.quote)
129 sys.stdout.write('quote(%s) = %s, unquote(%s) = %s\n'
130 % (word, q, q, ''.join(qq)))
131 if word != ''.join(qq):
132 num_errors += 1
133 if num_errors > 0:
134 sys.stderr.write('[ FAILED ] %d test failures\n' % num_errors)
135 return num_errors
137 if __name__ == '__main__':
138 sys.exit(main(sys.argv[1:]))