| 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(args): |
| 100 global verbose | 100 global verbose |
| 101 parser = optparse.OptionParser( | 101 parser = argparse.ArgumentParser() |
| 102 usage='Usage: %prog [options] word...') | 102 parser.add_argument('-s', '--special-chars', |
| 103 parser.add_option('-s', '--special-chars', dest='special_chars', default=':', | 103 dest='special_chars', default=':', |
| 104 help='Special characters to quote (default is ":")') | 104 help='Special characters to quote (default is ":")') |
| 105 parser.add_option('-q', '--quote', dest='quote', default='\\', | 105 parser.add_argument('-q', '--quote', dest='quote', default='\\', |
| 106 help='Quote or escape character (default is "\")') | 106 help='Quote or escape character (default is "\")') |
| 107 parser.add_option('-t', '--run-tests', dest='tests', action='store_true', | 107 parser.add_argument('-u', '--unquote-input', dest='unquote_input', |
| 108 help='Run built-in tests\n') | 108 action='store_true', help='Unquote command line argument') |
| 109 parser.add_option('-u', '--unquote-input', dest='unquote_input', | 109 parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', |
| 110 action='store_true', help='Unquote command line argument') | 110 help='Verbose test output') |
| 111 parser.add_option('-v', '--verbose', dest='verbose', action='store_true', | 111 parser.add_argument('words', nargs='*') |
| 112 help='Verbose test output') | 112 options = parser.parse_args(args) |
| 113 options, args = parser.parse_args(argv) | |
| 114 | 113 |
| 115 if options.verbose: | 114 if options.verbose: |
| 116 verbose = True | 115 verbose = True |
| 117 | 116 |
| 117 if not options.words: |
| 118 unittest.main() |
| 119 |
| 118 num_errors = 0 | 120 num_errors = 0 |
| 119 if options.tests: | 121 for word in options.words: |
| 120 sys.argv = [sys.argv[0]] + args | 122 # NB: there are inputs x for which quote(unquote(x) != x, but |
| 121 unittest.main() | 123 # there should be no input x for which unquote(quote(x)) != x. |
| 122 else: | 124 if options.unquote_input: |
| 123 for word in args: | 125 qq = quote.unquote(word, options.special_chars, options.quote) |
| 124 # NB: there are inputs x for which quote(unquote(x) != x, but | 126 sys.stdout.write('unquote(%s) = %s\n' |
| 125 # there should be no input x for which unquote(quote(x)) != x. | 127 % (word, ''.join(qq))) |
| 126 if options.unquote_input: | 128 # There is no expected output for unquote -- this is just for |
| 127 qq = quote.unquote(word, options.special_chars, options.quote) | 129 # manual testing, so it is okay that we do not (and cannot) |
| 128 sys.stdout.write('unquote(%s) = %s\n' | 130 # update num_errors here. |
| 129 % (word, ''.join(qq))) | 131 else: |
| 130 # There is no expected output for unquote -- this is just for | 132 q = quote.quote(word, options.special_chars, options.quote) |
| 131 # manual testing, so it is okay that we do not (and cannot) | 133 qq = quote.unquote(q, options.special_chars, options.quote) |
| 132 # update num_errors here. | 134 sys.stdout.write('quote(%s) = %s, unquote(%s) = %s\n' |
| 133 else: | 135 % (word, q, q, ''.join(qq))) |
| 134 q = quote.quote(word, options.special_chars, options.quote) | 136 if word != ''.join(qq): |
| 135 qq = quote.unquote(q, options.special_chars, options.quote) | 137 num_errors += 1 |
| 136 sys.stdout.write('quote(%s) = %s, unquote(%s) = %s\n' | 138 if num_errors > 0: |
| 137 % (word, q, q, ''.join(qq))) | 139 sys.stderr.write('[ FAILED ] %d test failures\n' % num_errors) |
| 138 if word != ''.join(qq): | |
| 139 num_errors += 1 | |
| 140 if num_errors > 0: | |
| 141 sys.stderr.write('[ FAILED ] %d test failures\n' % num_errors) | |
| 142 return num_errors | 140 return num_errors |
| 143 | 141 |
| 144 if __name__ == '__main__': | 142 if __name__ == '__main__': |
| 145 sys.exit(main(sys.argv[1:])) | 143 sys.exit(main(sys.argv[1:])) |
| OLD | NEW |