OLD | NEW |
1 # Copyright (C) 2010 Chris Jerdonek (cjerdonek@webkit.org) | 1 # Copyright (C) 2010 Chris Jerdonek (cjerdonek@webkit.org) |
2 # | 2 # |
3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
4 # modification, are permitted provided that the following conditions | 4 # modification, are permitted provided that the following conditions |
5 # are met: | 5 # are met: |
6 # 1. Redistributions of source code must retain the above copyright | 6 # 1. Redistributions of source code must retain the above copyright |
7 # notice, this list of conditions and the following disclaimer. | 7 # notice, this list of conditions and the following disclaimer. |
8 # 2. Redistributions in binary form must reproduce the above copyright | 8 # 2. Redistributions in binary form must reproduce the above copyright |
9 # notice, this list of conditions and the following disclaimer in the | 9 # notice, this list of conditions and the following disclaimer in the |
10 # documentation and/or other materials provided with the distribution. | 10 # documentation and/or other materials provided with the distribution. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 filter_rules=[], | 43 filter_rules=[], |
44 git_commit=None): | 44 git_commit=None): |
45 return ProcessorOptions(filter_rules=filter_rules, | 45 return ProcessorOptions(filter_rules=filter_rules, |
46 git_commit=git_commit, | 46 git_commit=git_commit, |
47 min_confidence=min_confidence, | 47 min_confidence=min_confidence, |
48 output_format=output_format) | 48 output_format=output_format) |
49 | 49 |
50 def test_to_flag_string(self): | 50 def test_to_flag_string(self): |
51 options = self._create_options('vs7', 5, ['+foo', '-bar'], 'git') | 51 options = self._create_options('vs7', 5, ['+foo', '-bar'], 'git') |
52 self.assertEqual('--filter=+foo,-bar --git-commit=git ' | 52 self.assertEqual('--filter=+foo,-bar --git-commit=git ' |
53 '--min-confidence=5 --output=vs7', | 53 '--min-confidence=5 --output=vs7', |
54 self._printer.to_flag_string(options)) | 54 self._printer.to_flag_string(options)) |
55 | 55 |
56 # This is to check that --filter and --git-commit do not | 56 # This is to check that --filter and --git-commit do not |
57 # show up when not user-specified. | 57 # show up when not user-specified. |
58 options = self._create_options() | 58 options = self._create_options() |
59 self.assertEqual('--min-confidence=3 --output=emacs', | 59 self.assertEqual('--min-confidence=3 --output=emacs', |
60 self._printer.to_flag_string(options)) | 60 self._printer.to_flag_string(options)) |
61 | 61 |
62 | 62 |
63 class ArgumentParserTest(LoggingTestCase): | 63 class ArgumentParserTest(LoggingTestCase): |
64 | 64 |
65 """Test the ArgumentParser class.""" | 65 """Test the ArgumentParser class.""" |
66 | 66 |
67 class _MockStdErr(object): | 67 class _MockStdErr(object): |
68 | 68 |
69 def write(self, message): | 69 def write(self, message): |
70 # We do not want the usage string or style categories | 70 # We do not want the usage string or style categories |
71 # to print during unit tests, so print nothing. | 71 # to print during unit tests, so print nothing. |
72 return | 72 return |
73 | 73 |
74 def _parse(self, args): | 74 def _parse(self, args): |
75 """Call a test parser.parse().""" | 75 """Call a test parser.parse().""" |
76 parser = self._create_parser() | 76 parser = self._create_parser() |
77 return parser.parse(args) | 77 return parser.parse(args) |
78 | 78 |
79 def _create_defaults(self): | 79 def _create_defaults(self): |
80 """Return a DefaultCommandOptionValues instance for testing.""" | 80 """Return a DefaultCommandOptionValues instance for testing.""" |
81 base_filter_rules = ["-", "+whitespace"] | 81 base_filter_rules = ["-", "+whitespace"] |
82 return DefaultCommandOptionValues(min_confidence=3, | 82 return DefaultCommandOptionValues(min_confidence=3, |
83 output_format="vs7") | 83 output_format="vs7") |
84 | 84 |
85 def _create_parser(self): | 85 def _create_parser(self): |
86 """Return an ArgumentParser instance for testing.""" | 86 """Return an ArgumentParser instance for testing.""" |
87 default_options = self._create_defaults() | 87 default_options = self._create_defaults() |
88 | 88 |
89 all_categories = ["build" ,"whitespace"] | 89 all_categories = ["build", "whitespace"] |
90 | 90 |
91 mock_stderr = self._MockStdErr() | 91 mock_stderr = self._MockStdErr() |
92 | 92 |
93 return ArgumentParser(all_categories=all_categories, | 93 return ArgumentParser(all_categories=all_categories, |
94 base_filter_rules=[], | 94 base_filter_rules=[], |
95 default_options=default_options, | 95 default_options=default_options, |
96 mock_stderr=mock_stderr, | 96 mock_stderr=mock_stderr, |
97 usage="test usage") | 97 usage="test usage") |
98 | 98 |
99 def test_parse_documentation(self): | 99 def test_parse_documentation(self): |
(...skipping 16 matching lines...) Expand all Loading... |
116 | 116 |
117 self.assertRaises(SystemExit, parse, ['--min-confidence=bad']) | 117 self.assertRaises(SystemExit, parse, ['--min-confidence=bad']) |
118 self.assertLog(['ERROR: option --min-confidence: ' | 118 self.assertLog(['ERROR: option --min-confidence: ' |
119 "invalid integer value: 'bad'\n"]) | 119 "invalid integer value: 'bad'\n"]) |
120 self.assertRaises(SystemExit, parse, ['--min-confidence=0']) | 120 self.assertRaises(SystemExit, parse, ['--min-confidence=0']) |
121 self.assertLog(['ERROR: option --min-confidence: invalid integer: 0: ' | 121 self.assertLog(['ERROR: option --min-confidence: invalid integer: 0: ' |
122 'value must be between 1 and 5\n']) | 122 'value must be between 1 and 5\n']) |
123 self.assertRaises(SystemExit, parse, ['--min-confidence=6']) | 123 self.assertRaises(SystemExit, parse, ['--min-confidence=6']) |
124 self.assertLog(['ERROR: option --min-confidence: invalid integer: 6: ' | 124 self.assertLog(['ERROR: option --min-confidence: invalid integer: 6: ' |
125 'value must be between 1 and 5\n']) | 125 'value must be between 1 and 5\n']) |
126 parse(['--min-confidence=1']) # works | 126 parse(['--min-confidence=1']) # works |
127 parse(['--min-confidence=5']) # works | 127 parse(['--min-confidence=5']) # works |
128 | 128 |
129 self.assertRaises(SystemExit, parse, ['--output=bad']) | 129 self.assertRaises(SystemExit, parse, ['--output=bad']) |
130 self.assertLog(['ERROR: option --output-format: invalid choice: ' | 130 self.assertLog(['ERROR: option --output-format: invalid choice: ' |
131 "'bad' (choose from 'emacs', 'vs7')\n"]) | 131 "'bad' (choose from 'emacs', 'vs7')\n"]) |
132 parse(['--output=vs7']) # works | 132 parse(['--output=vs7']) # works |
133 | 133 |
134 # Pass a filter rule not beginning with + or -. | 134 # Pass a filter rule not beginning with + or -. |
135 self.assertRaises(SystemExit, parse, ['--filter=build']) | 135 self.assertRaises(SystemExit, parse, ['--filter=build']) |
136 self.assertLog(['ERROR: Invalid filter rule "build": ' | 136 self.assertLog(['ERROR: Invalid filter rule "build": ' |
137 'every rule must start with + or -.\n']) | 137 'every rule must start with + or -.\n']) |
138 parse(['--filter=+build']) # works | 138 parse(['--filter=+build']) # works |
139 | 139 |
140 def test_parse_default_arguments(self): | 140 def test_parse_default_arguments(self): |
141 parse = self._parse | 141 parse = self._parse |
142 | 142 |
143 (files, options) = parse([]) | 143 (files, options) = parse([]) |
144 | 144 |
145 self.assertEqual(files, []) | 145 self.assertEqual(files, []) |
146 | 146 |
147 self.assertEqual(options.filter_rules, []) | 147 self.assertEqual(options.filter_rules, []) |
148 self.assertIsNone(options.git_commit) | 148 self.assertIsNone(options.git_commit) |
(...skipping 17 matching lines...) Expand all Loading... |
166 (files, options) = parse(['--git-diff=commit']) | 166 (files, options) = parse(['--git-diff=commit']) |
167 self.assertEqual(options.git_commit, 'commit') | 167 self.assertEqual(options.git_commit, 'commit') |
168 (files, options) = parse(['--verbose']) | 168 (files, options) = parse(['--verbose']) |
169 self.assertTrue(options.is_verbose) | 169 self.assertTrue(options.is_verbose) |
170 (files, options) = parse(['--diff-files', 'file.txt']) | 170 (files, options) = parse(['--diff-files', 'file.txt']) |
171 self.assertTrue(options.diff_files) | 171 self.assertTrue(options.diff_files) |
172 | 172 |
173 # Pass user_rules. | 173 # Pass user_rules. |
174 (files, options) = parse(['--filter=+build,-whitespace']) | 174 (files, options) = parse(['--filter=+build,-whitespace']) |
175 self.assertEqual(options.filter_rules, | 175 self.assertEqual(options.filter_rules, |
176 ["+build", "-whitespace"]) | 176 ["+build", "-whitespace"]) |
177 | 177 |
178 # Pass spurious white space in user rules. | 178 # Pass spurious white space in user rules. |
179 (files, options) = parse(['--filter=+build, -whitespace']) | 179 (files, options) = parse(['--filter=+build, -whitespace']) |
180 self.assertEqual(options.filter_rules, | 180 self.assertEqual(options.filter_rules, |
181 ["+build", "-whitespace"]) | 181 ["+build", "-whitespace"]) |
182 | 182 |
183 def test_parse_files(self): | 183 def test_parse_files(self): |
184 parse = self._parse | 184 parse = self._parse |
185 | 185 |
186 (files, options) = parse(['foo.cpp']) | 186 (files, options) = parse(['foo.cpp']) |
187 self.assertEqual(files, ['foo.cpp']) | 187 self.assertEqual(files, ['foo.cpp']) |
188 | 188 |
189 # Pass multiple files. | 189 # Pass multiple files. |
190 (files, options) = parse(['--output=emacs', 'foo.cpp', 'bar.cpp']) | 190 (files, options) = parse(['--output=emacs', 'foo.cpp', 'bar.cpp']) |
191 self.assertEqual(files, ['foo.cpp', 'bar.cpp']) | 191 self.assertEqual(files, ['foo.cpp', 'bar.cpp']) |
192 | 192 |
193 | 193 |
194 class CommandOptionValuesTest(unittest.TestCase): | 194 class CommandOptionValuesTest(unittest.TestCase): |
195 | 195 |
196 """Tests CommandOptionValues class.""" | 196 """Tests CommandOptionValues class.""" |
197 | 197 |
198 def test_init(self): | 198 def test_init(self): |
199 """Test __init__ constructor.""" | 199 """Test __init__ constructor.""" |
200 # Check default parameters. | 200 # Check default parameters. |
201 options = ProcessorOptions() | 201 options = ProcessorOptions() |
202 self.assertEqual(options.filter_rules, []) | 202 self.assertEqual(options.filter_rules, []) |
203 self.assertIsNone(options.git_commit) | 203 self.assertIsNone(options.git_commit) |
204 self.assertFalse(options.is_verbose) | 204 self.assertFalse(options.is_verbose) |
205 self.assertEqual(options.min_confidence, 1) | 205 self.assertEqual(options.min_confidence, 1) |
206 self.assertEqual(options.output_format, "emacs") | 206 self.assertEqual(options.output_format, "emacs") |
207 | 207 |
208 # Check argument validation. | 208 # Check argument validation. |
209 self.assertRaises(ValueError, ProcessorOptions, output_format="bad") | 209 self.assertRaises(ValueError, ProcessorOptions, output_format="bad") |
210 ProcessorOptions(output_format="emacs") # No ValueError: works | 210 ProcessorOptions(output_format="emacs") # No ValueError: works |
211 ProcessorOptions(output_format="vs7") # works | 211 ProcessorOptions(output_format="vs7") # works |
212 self.assertRaises(ValueError, ProcessorOptions, min_confidence=0) | 212 self.assertRaises(ValueError, ProcessorOptions, min_confidence=0) |
213 self.assertRaises(ValueError, ProcessorOptions, min_confidence=6) | 213 self.assertRaises(ValueError, ProcessorOptions, min_confidence=6) |
214 ProcessorOptions(min_confidence=1) # works | 214 ProcessorOptions(min_confidence=1) # works |
215 ProcessorOptions(min_confidence=5) # works | 215 ProcessorOptions(min_confidence=5) # works |
216 | 216 |
217 # Check attributes. | 217 # Check attributes. |
218 options = ProcessorOptions(filter_rules=["+"], | 218 options = ProcessorOptions(filter_rules=["+"], |
219 git_commit="commit", | 219 git_commit="commit", |
220 is_verbose=True, | 220 is_verbose=True, |
221 min_confidence=3, | 221 min_confidence=3, |
222 output_format="vs7") | 222 output_format="vs7") |
223 self.assertEqual(options.filter_rules, ["+"]) | 223 self.assertEqual(options.filter_rules, ["+"]) |
224 self.assertEqual(options.git_commit, "commit") | 224 self.assertEqual(options.git_commit, "commit") |
225 self.assertTrue(options.is_verbose) | 225 self.assertTrue(options.is_verbose) |
(...skipping 22 matching lines...) Expand all Loading... |
248 self.assertFalse(options.__eq__(ProcessorOptions(is_verbose=True))) | 248 self.assertFalse(options.__eq__(ProcessorOptions(is_verbose=True))) |
249 self.assertFalse(options.__eq__(ProcessorOptions(min_confidence=2))) | 249 self.assertFalse(options.__eq__(ProcessorOptions(min_confidence=2))) |
250 self.assertFalse(options.__eq__(ProcessorOptions(output_format="vs7"))) | 250 self.assertFalse(options.__eq__(ProcessorOptions(output_format="vs7"))) |
251 | 251 |
252 def test_ne(self): | 252 def test_ne(self): |
253 """Test __ne__ inequality function.""" | 253 """Test __ne__ inequality function.""" |
254 # By default, __ne__ always returns true on different objects. | 254 # By default, __ne__ always returns true on different objects. |
255 # Thus, just check the distinguishing case to verify that the | 255 # Thus, just check the distinguishing case to verify that the |
256 # code defines __ne__. | 256 # code defines __ne__. |
257 self.assertFalse(ProcessorOptions().__ne__(ProcessorOptions())) | 257 self.assertFalse(ProcessorOptions().__ne__(ProcessorOptions())) |
258 | |
OLD | NEW |