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.
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
):
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
):
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
):
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
)
53 test_tuples
= [[VerboseQuote
,
54 ['foo, bar, baz, and quux too!', 'abc'],
55 'foo, \\b\\ar, \\b\\az, \\and quux too!'],
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']],
66 ['about that sep\\ar\\ator in the beginning', 'ab'],
67 ['', 'ab', 'out th', 'a', 't separator in the ',
70 ['the rain in spain fall\\s ma\\i\\nly on the plains',
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
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
)
113 sys
.argv
= [sys
.argv
[0]] + 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.
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
):
134 sys
.stderr
.write('[ FAILED ] %d test failures\n' % num_errors
)
137 if __name__
== '__main__':
138 sys
.exit(main(sys
.argv
[1:]))