OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import optparse | 6 import argparse |
7 import os | 7 import os |
8 import sys | 8 import sys |
9 import unittest | 9 import unittest |
10 | 10 |
11 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) | 11 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
12 PARENT_DIR = os.path.dirname(SCRIPT_DIR) | 12 PARENT_DIR = os.path.dirname(SCRIPT_DIR) |
13 | 13 |
14 sys.path.append(PARENT_DIR) | 14 sys.path.append(PARENT_DIR) |
15 | 15 |
16 import quote | 16 import quote |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
85 self.check_invertible('abcdefg', 'bc') | 85 self.check_invertible('abcdefg', 'bc') |
86 self.check_invertible('a\\bcdefg', 'bc') | 86 self.check_invertible('a\\bcdefg', 'bc') |
87 self.check_invertible('ab\\cdefg', 'bc') | 87 self.check_invertible('ab\\cdefg', 'bc') |
88 self.check_invertible('\\ab\\cdefg', 'abc') | 88 self.check_invertible('\\ab\\cdefg', 'abc') |
89 self.check_invertible('abcde\\fg', 'efg') | 89 self.check_invertible('abcde\\fg', 'efg') |
90 self.check_invertible('a\\b', '') | 90 self.check_invertible('a\\b', '') |
91 | 91 |
92 | 92 |
93 # Invoke this file directly for simple manual testing. For running | 93 # Invoke this file directly for simple manual testing. For running |
94 # the unittests, use the -t flag. Any flags to be passed to the | 94 # the unittests, use the -t flag. Any flags to be passed to the |
95 # unittest module should be passed as after the optparse processing, | 95 # unittest module should be passed as after the argparse processing, |
96 # e.g., "quote_test.py -t -- -v" to pass the -v flag to the unittest | 96 # e.g., "quote_test.py -t -- -v" to pass the -v flag to the unittest |
97 # module. | 97 # module. |
98 | 98 |
99 def main(argv): | 99 def main(argv): |
100 global verbose | 100 global verbose |
101 parser = optparse.OptionParser( | 101 parser = argparse.ArgumentParser( |
102 usage='Usage: %prog [options] word...') | 102 usage='Usage: %prog [options] word...') |
103 parser.add_option('-s', '--special-chars', dest='special_chars', default=':', | 103 parser.add_argument('-s', '--special-chars', |
104 help='Special characters to quote (default is ":")') | 104 dest='special_chars', default=':', |
105 parser.add_option('-q', '--quote', dest='quote', default='\\', | 105 help='Special characters to quote (default is ":")') |
106 help='Quote or escape character (default is "\")') | 106 parser.add_argument('-q', '--quote', dest='quote', default='\\', |
107 parser.add_option('-t', '--run-tests', dest='tests', action='store_true', | 107 help='Quote or escape character (default is "\")') |
108 help='Run built-in tests\n') | 108 parser.add_argument('-t', '--run-tests', dest='tests', action='store_true', |
109 parser.add_option('-u', '--unquote-input', dest='unquote_input', | 109 help='Run built-in tests\n') |
110 action='store_true', help='Unquote command line argument') | 110 parser.add_argument('-u', '--unquote-input', dest='unquote_input', |
111 parser.add_option('-v', '--verbose', dest='verbose', action='store_true', | 111 action='store_true', help='Unquote command line argument') |
112 help='Verbose test output') | 112 parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', |
113 help='Verbose test output') | |
113 options, args = parser.parse_args(argv) | 114 options, args = parser.parse_args(argv) |
binji
2014/11/13 23:57:04
fix
| |
114 | 115 |
115 if options.verbose: | 116 if options.verbose: |
116 verbose = True | 117 verbose = True |
117 | 118 |
118 num_errors = 0 | 119 num_errors = 0 |
119 if options.tests: | 120 if options.tests: |
120 sys.argv = [sys.argv[0]] + args | 121 sys.argv = [sys.argv[0]] + args |
121 unittest.main() | 122 unittest.main() |
122 else: | 123 else: |
123 for word in args: | 124 for word in args: |
(...skipping 12 matching lines...) Expand all Loading... | |
136 sys.stdout.write('quote(%s) = %s, unquote(%s) = %s\n' | 137 sys.stdout.write('quote(%s) = %s, unquote(%s) = %s\n' |
137 % (word, q, q, ''.join(qq))) | 138 % (word, q, q, ''.join(qq))) |
138 if word != ''.join(qq): | 139 if word != ''.join(qq): |
139 num_errors += 1 | 140 num_errors += 1 |
140 if num_errors > 0: | 141 if num_errors > 0: |
141 sys.stderr.write('[ FAILED ] %d test failures\n' % num_errors) | 142 sys.stderr.write('[ FAILED ] %d test failures\n' % num_errors) |
142 return num_errors | 143 return num_errors |
143 | 144 |
144 if __name__ == '__main__': | 145 if __name__ == '__main__': |
145 sys.exit(main(sys.argv[1:])) | 146 sys.exit(main(sys.argv[1:])) |
OLD | NEW |