OLD | NEW |
1 # -*- coding: utf-8; -*- | 1 # -*- coding: utf-8; -*- |
2 # | 2 # |
3 # Copyright (C) 2009 Google Inc. All rights reserved. | 3 # Copyright (C) 2009 Google Inc. All rights reserved. |
4 # Copyright (C) 2009 Torch Mobile Inc. | 4 # Copyright (C) 2009 Torch Mobile Inc. |
5 # Copyright (C) 2009 Apple Inc. All rights reserved. | 5 # Copyright (C) 2009 Apple Inc. All rights reserved. |
6 # Copyright (C) 2010 Chris Jerdonek (chris.jerdonek@gmail.com) | 6 # Copyright (C) 2010 Chris Jerdonek (chris.jerdonek@gmail.com) |
7 # | 7 # |
8 # Redistribution and use in source and binary forms, with or without | 8 # Redistribution and use in source and binary forms, with or without |
9 # modification, are permitted provided that the following conditions are | 9 # modification, are permitted provided that the following conditions are |
10 # met: | 10 # met: |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 | 250 |
251 """Tests the "should skip" methods of the CheckerDispatcher class.""" | 251 """Tests the "should skip" methods of the CheckerDispatcher class.""" |
252 | 252 |
253 def setUp(self): | 253 def setUp(self): |
254 self._dispatcher = CheckerDispatcher() | 254 self._dispatcher = CheckerDispatcher() |
255 | 255 |
256 def test_should_skip_with_warning(self): | 256 def test_should_skip_with_warning(self): |
257 """Test should_skip_with_warning().""" | 257 """Test should_skip_with_warning().""" |
258 # Check skipped files. | 258 # Check skipped files. |
259 paths_to_skip = [ | 259 paths_to_skip = [ |
260 "Source/WebKit/gtk/tests/testatk.c", | 260 "Source/WebKit/gtk/tests/testatk.c", |
261 "Source/WebKit2/UIProcess/API/gtk/webkit2.h", | 261 "Source/WebKit2/UIProcess/API/gtk/webkit2.h", |
262 "Source/WebKit2/UIProcess/API/gtk/WebKitWebView.h", | 262 "Source/WebKit2/UIProcess/API/gtk/WebKitWebView.h", |
263 "Source/WebKit2/UIProcess/API/gtk/WebKitLoader.h", | 263 "Source/WebKit2/UIProcess/API/gtk/WebKitLoader.h", |
264 ] | 264 ] |
265 | 265 |
266 for path in paths_to_skip: | 266 for path in paths_to_skip: |
267 self.assertTrue(self._dispatcher.should_skip_with_warning(path), | 267 self.assertTrue(self._dispatcher.should_skip_with_warning(path), |
268 "Checking: " + path) | 268 "Checking: " + path) |
269 | 269 |
270 # Verify that some files are not skipped. | 270 # Verify that some files are not skipped. |
271 paths_not_to_skip = [ | 271 paths_not_to_skip = [ |
272 "foo.txt", | 272 "foo.txt", |
273 "Source/WebKit2/UIProcess/API/gtk/HelperClass.cpp", | 273 "Source/WebKit2/UIProcess/API/gtk/HelperClass.cpp", |
274 "Source/WebKit2/UIProcess/API/gtk/HelperClass.h", | 274 "Source/WebKit2/UIProcess/API/gtk/HelperClass.h", |
275 "Source/WebKit2/UIProcess/API/gtk/WebKitWebView.cpp", | 275 "Source/WebKit2/UIProcess/API/gtk/WebKitWebView.cpp", |
276 "Source/WebKit2/UIProcess/API/gtk/WebKitWebViewPrivate.h", | 276 "Source/WebKit2/UIProcess/API/gtk/WebKitWebViewPrivate.h", |
277 "Source/WebKit2/UIProcess/API/gtk/tests/WebViewTest.cpp", | 277 "Source/WebKit2/UIProcess/API/gtk/tests/WebViewTest.cpp", |
278 "Source/WebKit2/UIProcess/API/gtk/tests/WebViewTest.h", | 278 "Source/WebKit2/UIProcess/API/gtk/tests/WebViewTest.h", |
279 ] | 279 ] |
280 | 280 |
281 for path in paths_not_to_skip: | 281 for path in paths_not_to_skip: |
282 self.assertFalse(self._dispatcher.should_skip_with_warning(path)) | 282 self.assertFalse(self._dispatcher.should_skip_with_warning(path)) |
283 | 283 |
284 def _assert_should_skip_without_warning(self, path, is_checker_none, | 284 def _assert_should_skip_without_warning(self, path, is_checker_none, |
285 expected): | 285 expected): |
286 # Check the file type before asserting the return value. | 286 # Check the file type before asserting the return value. |
287 checker = self._dispatcher.dispatch(file_path=path, | 287 checker = self._dispatcher.dispatch(file_path=path, |
288 handle_style_error=None, | 288 handle_style_error=None, |
289 min_confidence=3) | 289 min_confidence=3) |
290 message = 'while checking: %s' % path | 290 message = 'while checking: %s' % path |
291 self.assertEqual(checker is None, is_checker_none, message) | 291 self.assertEqual(checker is None, is_checker_none, message) |
292 self.assertEqual(self._dispatcher.should_skip_without_warning(path), | 292 self.assertEqual(self._dispatcher.should_skip_without_warning(path), |
293 expected, message) | 293 expected, message) |
294 | 294 |
295 def test_should_skip_without_warning__true(self): | 295 def test_should_skip_without_warning__true(self): |
296 """Test should_skip_without_warning() for True return values.""" | 296 """Test should_skip_without_warning() for True return values.""" |
297 # Check a file with NONE file type. | 297 # Check a file with NONE file type. |
298 path = 'foo.asdf' # Non-sensical file extension. | 298 path = 'foo.asdf' # Non-sensical file extension. |
299 self._assert_should_skip_without_warning(path, | 299 self._assert_should_skip_without_warning(path, |
300 is_checker_none=True, | 300 is_checker_none=True, |
301 expected=True) | 301 expected=True) |
302 | 302 |
303 # Check files with non-NONE file type. These examples must be | 303 # Check files with non-NONE file type. These examples must be |
304 # drawn from the _SKIPPED_FILES_WITHOUT_WARNING configuration | 304 # drawn from the _SKIPPED_FILES_WITHOUT_WARNING configuration |
305 # variable. | 305 # variable. |
306 path = os.path.join('LayoutTests', 'foo.txt') | 306 path = os.path.join('LayoutTests', 'foo.txt') |
307 self._assert_should_skip_without_warning(path, | 307 self._assert_should_skip_without_warning(path, |
308 is_checker_none=False, | 308 is_checker_none=False, |
309 expected=True) | 309 expected=True) |
310 | 310 |
311 def test_should_skip_without_warning__false(self): | 311 def test_should_skip_without_warning__false(self): |
312 """Test should_skip_without_warning() for False return values.""" | 312 """Test should_skip_without_warning() for False return values.""" |
313 paths = ['foo.txt', | 313 paths = ['foo.txt', |
314 os.path.join('LayoutTests', 'TestExpectations'), | 314 os.path.join('LayoutTests', 'TestExpectations'), |
315 ] | 315 ] |
316 | 316 |
317 for path in paths: | 317 for path in paths: |
318 self._assert_should_skip_without_warning(path, | 318 self._assert_should_skip_without_warning(path, |
319 is_checker_none=False, | 319 is_checker_none=False, |
320 expected=False) | 320 expected=False) |
321 | 321 |
322 | 322 |
323 class CheckerDispatcherCarriageReturnTest(unittest.TestCase): | 323 class CheckerDispatcherCarriageReturnTest(unittest.TestCase): |
| 324 |
324 def test_should_check_and_strip_carriage_returns(self): | 325 def test_should_check_and_strip_carriage_returns(self): |
325 files = { | 326 files = { |
326 'foo.txt': True, | 327 'foo.txt': True, |
327 'foo.cpp': True, | 328 'foo.cpp': True, |
328 'foo.vcproj': False, | 329 'foo.vcproj': False, |
329 'foo.vsprops': False, | 330 'foo.vsprops': False, |
330 } | 331 } |
331 | 332 |
332 dispatcher = CheckerDispatcher() | 333 dispatcher = CheckerDispatcher() |
333 for file_path, expected_result in files.items(): | 334 for file_path, expected_result in files.items(): |
334 self.assertEqual(dispatcher.should_check_and_strip_carriage_returns(
file_path), expected_result, 'Checking: %s' % file_path) | 335 self.assertEqual(dispatcher.should_check_and_strip_carriage_returns( |
| 336 file_path), expected_result, 'Checking: %s' % file_path) |
335 | 337 |
336 | 338 |
337 class CheckerDispatcherDispatchTest(unittest.TestCase): | 339 class CheckerDispatcherDispatchTest(unittest.TestCase): |
338 | 340 |
339 """Tests dispatch() method of CheckerDispatcher class.""" | 341 """Tests dispatch() method of CheckerDispatcher class.""" |
340 | 342 |
341 def dispatch(self, file_path): | 343 def dispatch(self, file_path): |
342 """Call dispatch() with the given file path.""" | 344 """Call dispatch() with the given file path.""" |
343 dispatcher = CheckerDispatcher() | 345 dispatcher = CheckerDispatcher() |
344 self.mock_handle_style_error = DefaultStyleErrorHandler('', None, None,
[]) | 346 self.mock_handle_style_error = DefaultStyleErrorHandler('', None, None,
[]) |
345 checker = dispatcher.dispatch(file_path, | 347 checker = dispatcher.dispatch(file_path, |
346 self.mock_handle_style_error, | 348 self.mock_handle_style_error, |
347 min_confidence=3) | 349 min_confidence=3) |
348 return checker | 350 return checker |
349 | 351 |
350 def assert_checker_none(self, file_path): | 352 def assert_checker_none(self, file_path): |
351 """Assert that the dispatched checker is None.""" | 353 """Assert that the dispatched checker is None.""" |
352 checker = self.dispatch(file_path) | 354 checker = self.dispatch(file_path) |
353 self.assertIsNone(checker, 'Checking: "%s"' % file_path) | 355 self.assertIsNone(checker, 'Checking: "%s"' % file_path) |
354 | 356 |
355 def assert_checker(self, file_path, expected_class): | 357 def assert_checker(self, file_path, expected_class): |
356 """Assert the type of the dispatched checker.""" | 358 """Assert the type of the dispatched checker.""" |
357 checker = self.dispatch(file_path) | 359 checker = self.dispatch(file_path) |
358 got_class = checker.__class__ | 360 got_class = checker.__class__ |
359 self.assertEqual(got_class, expected_class, | 361 self.assertEqual(got_class, expected_class, |
360 'For path "%(file_path)s" got %(got_class)s when ' | 362 'For path "%(file_path)s" got %(got_class)s when ' |
361 "expecting %(expected_class)s." | 363 "expecting %(expected_class)s." |
362 % {"file_path": file_path, | 364 % {"file_path": file_path, |
363 "got_class": got_class, | 365 "got_class": got_class, |
364 "expected_class": expected_class}) | 366 "expected_class": expected_class}) |
365 | 367 |
366 def assert_checker_cpp(self, file_path): | 368 def assert_checker_cpp(self, file_path): |
367 """Assert that the dispatched checker is a CppChecker.""" | 369 """Assert that the dispatched checker is a CppChecker.""" |
368 self.assert_checker(file_path, CppChecker) | 370 self.assert_checker(file_path, CppChecker) |
369 | 371 |
370 def assert_checker_json(self, file_path): | 372 def assert_checker_json(self, file_path): |
371 """Assert that the dispatched checker is a JSONChecker.""" | 373 """Assert that the dispatched checker is a JSONChecker.""" |
372 self.assert_checker(file_path, JSONChecker) | 374 self.assert_checker(file_path, JSONChecker) |
(...skipping 10 matching lines...) Expand all Loading... |
383 """Assert that the dispatched checker is a XMLChecker.""" | 385 """Assert that the dispatched checker is a XMLChecker.""" |
384 self.assert_checker(file_path, XMLChecker) | 386 self.assert_checker(file_path, XMLChecker) |
385 | 387 |
386 def test_cpp_paths(self): | 388 def test_cpp_paths(self): |
387 """Test paths that should be checked as C++.""" | 389 """Test paths that should be checked as C++.""" |
388 paths = [ | 390 paths = [ |
389 "-", | 391 "-", |
390 "foo.c", | 392 "foo.c", |
391 "foo.cpp", | 393 "foo.cpp", |
392 "foo.h", | 394 "foo.h", |
393 ] | 395 ] |
394 | 396 |
395 for path in paths: | 397 for path in paths: |
396 self.assert_checker_cpp(path) | 398 self.assert_checker_cpp(path) |
397 | 399 |
398 # Check checker attributes on a typical input. | 400 # Check checker attributes on a typical input. |
399 file_base = "foo" | 401 file_base = "foo" |
400 file_extension = "c" | 402 file_extension = "c" |
401 file_path = file_base + "." + file_extension | 403 file_path = file_base + "." + file_extension |
402 self.assert_checker_cpp(file_path) | 404 self.assert_checker_cpp(file_path) |
403 checker = self.dispatch(file_path) | 405 checker = self.dispatch(file_path) |
404 self.assertEqual(checker.file_extension, file_extension) | 406 self.assertEqual(checker.file_extension, file_extension) |
405 self.assertEqual(checker.file_path, file_path) | 407 self.assertEqual(checker.file_path, file_path) |
406 self.assertEqual(checker.handle_style_error, self.mock_handle_style_erro
r) | 408 self.assertEqual(checker.handle_style_error, self.mock_handle_style_erro
r) |
407 self.assertEqual(checker.min_confidence, 3) | 409 self.assertEqual(checker.min_confidence, 3) |
408 # Check "-" for good measure. | 410 # Check "-" for good measure. |
409 file_base = "-" | 411 file_base = "-" |
410 file_extension = "" | 412 file_extension = "" |
411 file_path = file_base | 413 file_path = file_base |
412 self.assert_checker_cpp(file_path) | 414 self.assert_checker_cpp(file_path) |
413 checker = self.dispatch(file_path) | 415 checker = self.dispatch(file_path) |
414 self.assertEqual(checker.file_extension, file_extension) | 416 self.assertEqual(checker.file_extension, file_extension) |
415 self.assertEqual(checker.file_path, file_path) | 417 self.assertEqual(checker.file_path, file_path) |
416 | 418 |
417 def test_json_paths(self): | 419 def test_json_paths(self): |
418 """Test paths that should be checked as JSON.""" | 420 """Test paths that should be checked as JSON.""" |
419 paths = [ | 421 paths = [ |
420 "Source/WebCore/inspector/Inspector.json", | 422 "Source/WebCore/inspector/Inspector.json", |
421 "Tools/BuildSlaveSupport/build.webkit.org-config/config.json", | 423 "Tools/BuildSlaveSupport/build.webkit.org-config/config.json", |
422 ] | 424 ] |
423 | 425 |
424 for path in paths: | 426 for path in paths: |
425 self.assert_checker_json(path) | 427 self.assert_checker_json(path) |
426 | 428 |
427 # Check checker attributes on a typical input. | 429 # Check checker attributes on a typical input. |
428 file_base = "foo" | 430 file_base = "foo" |
429 file_extension = "json" | 431 file_extension = "json" |
430 file_path = file_base + "." + file_extension | 432 file_path = file_base + "." + file_extension |
431 self.assert_checker_json(file_path) | 433 self.assert_checker_json(file_path) |
432 checker = self.dispatch(file_path) | 434 checker = self.dispatch(file_path) |
433 self.assertEqual(checker._handle_style_error, | 435 self.assertEqual(checker._handle_style_error, |
434 self.mock_handle_style_error) | 436 self.mock_handle_style_error) |
435 | 437 |
436 def test_python_paths(self): | 438 def test_python_paths(self): |
437 """Test paths that should be checked as Python.""" | 439 """Test paths that should be checked as Python.""" |
438 paths = [ | 440 paths = [ |
439 "foo.py", | 441 "foo.py", |
440 "Tools/Scripts/modules/text_style.py", | 442 "Tools/Scripts/modules/text_style.py", |
441 ] | 443 ] |
442 | 444 |
443 for path in paths: | 445 for path in paths: |
444 self.assert_checker_python(path) | 446 self.assert_checker_python(path) |
445 | 447 |
446 # Check checker attributes on a typical input. | 448 # Check checker attributes on a typical input. |
447 file_base = "foo" | 449 file_base = "foo" |
448 file_extension = "css" | 450 file_extension = "css" |
449 file_path = file_base + "." + file_extension | 451 file_path = file_base + "." + file_extension |
450 self.assert_checker_text(file_path) | 452 self.assert_checker_text(file_path) |
451 checker = self.dispatch(file_path) | 453 checker = self.dispatch(file_path) |
452 self.assertEqual(checker.file_path, file_path) | 454 self.assertEqual(checker.file_path, file_path) |
453 self.assertEqual(checker.handle_style_error, | 455 self.assertEqual(checker.handle_style_error, |
454 self.mock_handle_style_error) | 456 self.mock_handle_style_error) |
455 | 457 |
456 def test_text_paths(self): | 458 def test_text_paths(self): |
457 """Test paths that should be checked as text.""" | 459 """Test paths that should be checked as text.""" |
458 paths = [ | 460 paths = [ |
459 "foo.cc", | 461 "foo.cc", |
460 "foo.cgi", | 462 "foo.cgi", |
461 "foo.css", | 463 "foo.css", |
462 "foo.gyp", | 464 "foo.gyp", |
463 "foo.gypi", | 465 "foo.gypi", |
464 "foo.html", | 466 "foo.html", |
465 "foo.idl", | 467 "foo.idl", |
466 "foo.in", | 468 "foo.in", |
467 "foo.js", | 469 "foo.js", |
468 "foo.mm", | 470 "foo.mm", |
469 "foo.php", | 471 "foo.php", |
470 "foo.pl", | 472 "foo.pl", |
471 "foo.pm", | 473 "foo.pm", |
472 "foo.rb", | 474 "foo.rb", |
473 "foo.sh", | 475 "foo.sh", |
474 "foo.txt", | 476 "foo.txt", |
475 "foo.xhtml", | 477 "foo.xhtml", |
476 "foo.y", | 478 "foo.y", |
477 os.path.join("Source", "WebCore", "inspector", "front-end", "Main.js"
), | 479 os.path.join("Source", "WebCore", "inspector", "front-end", "Main.js
"), |
478 os.path.join("Tools", "Scripts", "check-webkit-style"), | 480 os.path.join("Tools", "Scripts", "check-webkit-style"), |
479 ] | 481 ] |
480 | 482 |
481 for path in paths: | 483 for path in paths: |
482 self.assert_checker_text(path) | 484 self.assert_checker_text(path) |
483 | 485 |
484 # Check checker attributes on a typical input. | 486 # Check checker attributes on a typical input. |
485 file_base = "foo" | 487 file_base = "foo" |
486 file_extension = "css" | 488 file_extension = "css" |
487 file_path = file_base + "." + file_extension | 489 file_path = file_base + "." + file_extension |
488 self.assert_checker_text(file_path) | 490 self.assert_checker_text(file_path) |
489 checker = self.dispatch(file_path) | 491 checker = self.dispatch(file_path) |
490 self.assertEqual(checker.file_path, file_path) | 492 self.assertEqual(checker.file_path, file_path) |
491 self.assertEqual(checker.handle_style_error, self.mock_handle_style_erro
r) | 493 self.assertEqual(checker.handle_style_error, self.mock_handle_style_erro
r) |
492 | 494 |
493 def test_xml_paths(self): | 495 def test_xml_paths(self): |
494 """Test paths that should be checked as XML.""" | 496 """Test paths that should be checked as XML.""" |
495 paths = [ | 497 paths = [ |
496 "Source/WebCore/WebCore.vcproj/WebCore.vcproj", | 498 "Source/WebCore/WebCore.vcproj/WebCore.vcproj", |
497 "WebKitLibraries/win/tools/vsprops/common.vsprops", | 499 "WebKitLibraries/win/tools/vsprops/common.vsprops", |
498 ] | 500 ] |
499 | 501 |
500 for path in paths: | 502 for path in paths: |
501 self.assert_checker_xml(path) | 503 self.assert_checker_xml(path) |
502 | 504 |
503 # Check checker attributes on a typical input. | 505 # Check checker attributes on a typical input. |
504 file_base = "foo" | 506 file_base = "foo" |
505 file_extension = "vcproj" | 507 file_extension = "vcproj" |
506 file_path = file_base + "." + file_extension | 508 file_path = file_base + "." + file_extension |
507 self.assert_checker_xml(file_path) | 509 self.assert_checker_xml(file_path) |
508 checker = self.dispatch(file_path) | 510 checker = self.dispatch(file_path) |
509 self.assertEqual(checker._handle_style_error, | 511 self.assertEqual(checker._handle_style_error, |
510 self.mock_handle_style_error) | 512 self.mock_handle_style_error) |
511 | 513 |
512 def test_none_paths(self): | 514 def test_none_paths(self): |
513 """Test paths that have no file type..""" | 515 """Test paths that have no file type..""" |
514 paths = [ | 516 paths = [ |
515 "Makefile", | 517 "Makefile", |
516 "foo.asdf", # Non-sensical file extension. | 518 "foo.asdf", # Non-sensical file extension. |
517 "foo.exe", | 519 "foo.exe", |
518 ] | 520 ] |
519 | 521 |
520 for path in paths: | 522 for path in paths: |
521 self.assert_checker_none(path) | 523 self.assert_checker_none(path) |
522 | 524 |
523 | 525 |
524 class StyleProcessorConfigurationTest(unittest.TestCase): | 526 class StyleProcessorConfigurationTest(unittest.TestCase): |
525 | 527 |
526 """Tests the StyleProcessorConfiguration class.""" | 528 """Tests the StyleProcessorConfiguration class.""" |
527 | 529 |
528 def setUp(self): | 530 def setUp(self): |
529 self._error_messages = [] | 531 self._error_messages = [] |
530 """The messages written to _mock_stderr_write() of this class.""" | 532 """The messages written to _mock_stderr_write() of this class.""" |
531 | 533 |
532 def _mock_stderr_write(self, message): | 534 def _mock_stderr_write(self, message): |
533 self._error_messages.append(message) | 535 self._error_messages.append(message) |
534 | 536 |
535 def _style_checker_configuration(self, output_format="vs7"): | 537 def _style_checker_configuration(self, output_format="vs7"): |
536 """Return a StyleProcessorConfiguration instance for testing.""" | 538 """Return a StyleProcessorConfiguration instance for testing.""" |
537 base_rules = ["-whitespace", "+whitespace/tab"] | 539 base_rules = ["-whitespace", "+whitespace/tab"] |
538 filter_configuration = FilterConfiguration(base_rules=base_rules) | 540 filter_configuration = FilterConfiguration(base_rules=base_rules) |
539 | 541 |
540 return StyleProcessorConfiguration( | 542 return StyleProcessorConfiguration( |
541 filter_configuration=filter_configuration, | 543 filter_configuration=filter_configuration, |
542 max_reports_per_category={"whitespace/newline": 1}, | 544 max_reports_per_category={"whitespace/newline": 1}, |
543 min_confidence=3, | 545 min_confidence=3, |
544 output_format=output_format, | 546 output_format=output_format, |
545 stderr_write=self._mock_stderr_write) | 547 stderr_write=self._mock_stderr_write) |
546 | 548 |
547 def test_init(self): | 549 def test_init(self): |
548 """Test the __init__() method.""" | 550 """Test the __init__() method.""" |
549 configuration = self._style_checker_configuration() | 551 configuration = self._style_checker_configuration() |
550 | 552 |
551 # Check that __init__ sets the "public" data attributes correctly. | 553 # Check that __init__ sets the "public" data attributes correctly. |
552 self.assertEqual(configuration.max_reports_per_category, | 554 self.assertEqual(configuration.max_reports_per_category, |
553 {"whitespace/newline": 1}) | 555 {"whitespace/newline": 1}) |
554 self.assertEqual(configuration.stderr_write, self._mock_stderr_write) | 556 self.assertEqual(configuration.stderr_write, self._mock_stderr_write) |
555 self.assertEqual(configuration.min_confidence, 3) | 557 self.assertEqual(configuration.min_confidence, 3) |
556 | 558 |
557 def test_is_reportable(self): | 559 def test_is_reportable(self): |
558 """Test the is_reportable() method.""" | 560 """Test the is_reportable() method.""" |
559 config = self._style_checker_configuration() | 561 config = self._style_checker_configuration() |
560 | 562 |
561 self.assertTrue(config.is_reportable("whitespace/tab", 3, "foo.txt")) | 563 self.assertTrue(config.is_reportable("whitespace/tab", 3, "foo.txt")) |
562 | 564 |
563 # Test the confidence check code path by varying the confidence. | 565 # Test the confidence check code path by varying the confidence. |
564 self.assertFalse(config.is_reportable("whitespace/tab", 2, "foo.txt")) | 566 self.assertFalse(config.is_reportable("whitespace/tab", 2, "foo.txt")) |
565 | 567 |
566 # Test the category check code path by varying the category. | 568 # Test the category check code path by varying the category. |
567 self.assertFalse(config.is_reportable("whitespace/line", 4, "foo.txt")) | 569 self.assertFalse(config.is_reportable("whitespace/line", 4, "foo.txt")) |
568 | 570 |
569 def _call_write_style_error(self, output_format): | 571 def _call_write_style_error(self, output_format): |
570 config = self._style_checker_configuration(output_format=output_format) | 572 config = self._style_checker_configuration(output_format=output_format) |
571 config.write_style_error(category="whitespace/tab", | 573 config.write_style_error(category="whitespace/tab", |
572 confidence_in_error=5, | 574 confidence_in_error=5, |
573 file_path="foo.h", | 575 file_path="foo.h", |
574 line_number=100, | 576 line_number=100, |
575 message="message") | 577 message="message") |
576 | 578 |
577 def test_write_style_error_emacs(self): | 579 def test_write_style_error_emacs(self): |
578 """Test the write_style_error() method.""" | 580 """Test the write_style_error() method.""" |
579 self._call_write_style_error("emacs") | 581 self._call_write_style_error("emacs") |
580 self.assertEqual(self._error_messages, | 582 self.assertEqual(self._error_messages, |
581 ["foo.h:100: message [whitespace/tab] [5]\n"]) | 583 ["foo.h:100: message [whitespace/tab] [5]\n"]) |
582 | 584 |
583 def test_write_style_error_vs7(self): | 585 def test_write_style_error_vs7(self): |
584 """Test the write_style_error() method.""" | 586 """Test the write_style_error() method.""" |
585 self._call_write_style_error("vs7") | 587 self._call_write_style_error("vs7") |
586 self.assertEqual(self._error_messages, | 588 self.assertEqual(self._error_messages, |
587 ["foo.h(100): message [whitespace/tab] [5]\n"]) | 589 ["foo.h(100): message [whitespace/tab] [5]\n"]) |
588 | 590 |
589 | 591 |
590 class StyleProcessor_EndToEndTest(LoggingTestCase): | 592 class StyleProcessor_EndToEndTest(LoggingTestCase): |
591 | 593 |
592 """Test the StyleProcessor class with an emphasis on end-to-end tests.""" | 594 """Test the StyleProcessor class with an emphasis on end-to-end tests.""" |
593 | 595 |
594 def setUp(self): | 596 def setUp(self): |
595 LoggingTestCase.setUp(self) | 597 LoggingTestCase.setUp(self) |
596 self._messages = [] | 598 self._messages = [] |
597 | 599 |
598 def _mock_stderr_write(self, message): | 600 def _mock_stderr_write(self, message): |
599 """Save a message so it can later be asserted.""" | 601 """Save a message so it can later be asserted.""" |
600 self._messages.append(message) | 602 self._messages.append(message) |
601 | 603 |
602 def test_init(self): | 604 def test_init(self): |
603 """Test __init__ constructor.""" | 605 """Test __init__ constructor.""" |
604 configuration = StyleProcessorConfiguration( | 606 configuration = StyleProcessorConfiguration( |
605 filter_configuration=FilterConfiguration(), | 607 filter_configuration=FilterConfiguration(), |
606 max_reports_per_category={}, | 608 max_reports_per_category={}, |
607 min_confidence=3, | 609 min_confidence=3, |
608 output_format="vs7", | 610 output_format="vs7", |
609 stderr_write=self._mock_stderr_write) | 611 stderr_write=self._mock_stderr_write) |
610 processor = StyleProcessor(configuration) | 612 processor = StyleProcessor(configuration) |
611 | 613 |
612 self.assertEqual(processor.error_count, 0) | 614 self.assertEqual(processor.error_count, 0) |
613 self.assertEqual(self._messages, []) | 615 self.assertEqual(self._messages, []) |
614 | 616 |
615 def test_process(self): | 617 def test_process(self): |
616 configuration = StyleProcessorConfiguration( | 618 configuration = StyleProcessorConfiguration( |
617 filter_configuration=FilterConfiguration(), | 619 filter_configuration=FilterConfiguration(), |
618 max_reports_per_category={}, | 620 max_reports_per_category={}, |
619 min_confidence=3, | 621 min_confidence=3, |
620 output_format="vs7", | 622 output_format="vs7", |
621 stderr_write=self._mock_stderr_write) | 623 stderr_write=self._mock_stderr_write) |
622 processor = StyleProcessor(configuration) | 624 processor = StyleProcessor(configuration) |
623 | 625 |
624 processor.process(lines=['line1', 'Line with tab:\t'], | 626 processor.process(lines=['line1', 'Line with tab:\t'], |
625 file_path='foo.txt') | 627 file_path='foo.txt') |
626 self.assertEqual(processor.error_count, 1) | 628 self.assertEqual(processor.error_count, 1) |
627 expected_messages = ['foo.txt(2): Line contains tab character. ' | 629 expected_messages = ['foo.txt(2): Line contains tab character. ' |
628 '[whitespace/tab] [5]\n'] | 630 '[whitespace/tab] [5]\n'] |
629 self.assertEqual(self._messages, expected_messages) | 631 self.assertEqual(self._messages, expected_messages) |
630 | 632 |
631 | 633 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
663 return file_path.endswith('skip_without_warning.txt') | 665 return file_path.endswith('skip_without_warning.txt') |
664 | 666 |
665 def should_check_and_strip_carriage_returns(self, file_path): | 667 def should_check_and_strip_carriage_returns(self, file_path): |
666 return not file_path.endswith('carriage_returns_allowed.txt') | 668 return not file_path.endswith('carriage_returns_allowed.txt') |
667 | 669 |
668 def dispatch(self, file_path, style_error_handler, min_confidence): | 670 def dispatch(self, file_path, style_error_handler, min_confidence): |
669 if file_path.endswith('do_not_process.txt'): | 671 if file_path.endswith('do_not_process.txt'): |
670 return None | 672 return None |
671 | 673 |
672 checker = StyleProcessor_CodeCoverageTest.MockDispatchedChecker( | 674 checker = StyleProcessor_CodeCoverageTest.MockDispatchedChecker( |
673 file_path, | 675 file_path, |
674 min_confidence, | 676 min_confidence, |
675 style_error_handler) | 677 style_error_handler) |
676 | 678 |
677 # Save the dispatched checker so the current test case has a | 679 # Save the dispatched checker so the current test case has a |
678 # way to access and check it. | 680 # way to access and check it. |
679 self.dispatched_checker = checker | 681 self.dispatched_checker = checker |
680 | 682 |
681 return checker | 683 return checker |
682 | 684 |
683 def setUp(self): | 685 def setUp(self): |
684 LoggingTestCase.setUp(self) | 686 LoggingTestCase.setUp(self) |
685 # We can pass an error-message swallower here because error message | 687 # We can pass an error-message swallower here because error message |
686 # output is tested instead in the end-to-end test case above. | 688 # output is tested instead in the end-to-end test case above. |
687 configuration = StyleProcessorConfiguration( | 689 configuration = StyleProcessorConfiguration( |
688 filter_configuration=FilterConfiguration(), | 690 filter_configuration=FilterConfiguration(), |
689 max_reports_per_category={"whitespace/newline": 1}, | 691 max_reports_per_category={"whitespace/newline": 1}, |
690 min_confidence=3, | 692 min_confidence=3, |
691 output_format="vs7", | 693 output_format="vs7", |
692 stderr_write=self._swallow_stderr_message) | 694 stderr_write=self._swallow_stderr_message) |
693 | 695 |
694 mock_carriage_checker_class = self._create_carriage_checker_class() | 696 mock_carriage_checker_class = self._create_carriage_checker_class() |
695 mock_dispatcher = self.MockDispatcher() | 697 mock_dispatcher = self.MockDispatcher() |
696 # We do not need to use a real incrementer here because error-count | 698 # We do not need to use a real incrementer here because error-count |
697 # incrementing is tested instead in the end-to-end test case above. | 699 # incrementing is tested instead in the end-to-end test case above. |
698 mock_increment_error_count = self._do_nothing | 700 mock_increment_error_count = self._do_nothing |
699 | 701 |
700 processor = StyleProcessor(configuration=configuration, | 702 processor = StyleProcessor(configuration=configuration, |
701 mock_carriage_checker_class=mock_carriage_checker_class, | 703 mock_carriage_checker_class=mock_carriage_che
cker_class, |
702 mock_dispatcher=mock_dispatcher, | 704 mock_dispatcher=mock_dispatcher, |
703 mock_increment_error_count=mock_increment_error_count) | 705 mock_increment_error_count=mock_increment_err
or_count) |
704 | 706 |
705 self._configuration = configuration | 707 self._configuration = configuration |
706 self._mock_dispatcher = mock_dispatcher | 708 self._mock_dispatcher = mock_dispatcher |
707 self._processor = processor | 709 self._processor = processor |
708 | 710 |
709 def _do_nothing(self): | 711 def _do_nothing(self): |
710 # We provide this function so the caller can pass it to the | 712 # We provide this function so the caller can pass it to the |
711 # StyleProcessor constructor. This lets us assert the equality of | 713 # StyleProcessor constructor. This lets us assert the equality of |
712 # the DefaultStyleErrorHandler instance generated by the process() | 714 # the DefaultStyleErrorHandler instance generated by the process() |
713 # method with an expected instance. | 715 # method with an expected instance. |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
779 line_numbers=line_numbers) | 781 line_numbers=line_numbers) |
780 | 782 |
781 self._processor.process(lines=lines, | 783 self._processor.process(lines=lines, |
782 file_path=file_path, | 784 file_path=file_path, |
783 line_numbers=line_numbers) | 785 line_numbers=line_numbers) |
784 | 786 |
785 # Check that the carriage-return checker was instantiated correctly | 787 # Check that the carriage-return checker was instantiated correctly |
786 # and was passed lines correctly. | 788 # and was passed lines correctly. |
787 carriage_checker = self.carriage_checker | 789 carriage_checker = self.carriage_checker |
788 self.assertEqual(carriage_checker.style_error_handler, | 790 self.assertEqual(carriage_checker.style_error_handler, |
789 expected_error_handler) | 791 expected_error_handler) |
790 self.assertEqual(carriage_checker.lines, ['line1', 'line2']) | 792 self.assertEqual(carriage_checker.lines, ['line1', 'line2']) |
791 | 793 |
792 # Check that the style checker was dispatched correctly and was | 794 # Check that the style checker was dispatched correctly and was |
793 # passed lines correctly. | 795 # passed lines correctly. |
794 checker = self._mock_dispatcher.dispatched_checker | 796 checker = self._mock_dispatcher.dispatched_checker |
795 self.assertEqual(checker.file_path, 'foo.txt') | 797 self.assertEqual(checker.file_path, 'foo.txt') |
796 self.assertEqual(checker.min_confidence, 3) | 798 self.assertEqual(checker.min_confidence, 3) |
797 self.assertEqual(checker.style_error_handler, expected_error_handler) | 799 self.assertEqual(checker.style_error_handler, expected_error_handler) |
798 | 800 |
799 self.assertEqual(checker.lines, ['line1', 'line2']) | 801 self.assertEqual(checker.lines, ['line1', 'line2']) |
800 | 802 |
801 def test_process__no_checker_dispatched(self): | 803 def test_process__no_checker_dispatched(self): |
802 """Test the process() method for a path with no dispatched checker.""" | 804 """Test the process() method for a path with no dispatched checker.""" |
803 path = os.path.join('foo', 'do_not_process.txt') | 805 path = os.path.join('foo', 'do_not_process.txt') |
804 self.assertRaises(AssertionError, self._processor.process, | 806 self.assertRaises(AssertionError, self._processor.process, |
805 lines=['line1', 'line2'], file_path=path, | 807 lines=['line1', 'line2'], file_path=path, |
806 line_numbers=[100]) | 808 line_numbers=[100]) |
807 | 809 |
808 def test_process__carriage_returns_not_stripped(self): | 810 def test_process__carriage_returns_not_stripped(self): |
809 """Test that carriage returns aren't stripped from files that are allowe
d to contain them.""" | 811 """Test that carriage returns aren't stripped from files that are allowe
d to contain them.""" |
810 file_path = 'carriage_returns_allowed.txt' | 812 file_path = 'carriage_returns_allowed.txt' |
811 lines = ['line1\r', 'line2\r'] | 813 lines = ['line1\r', 'line2\r'] |
812 line_numbers = [100] | 814 line_numbers = [100] |
813 self._processor.process(lines=lines, | 815 self._processor.process(lines=lines, |
814 file_path=file_path, | 816 file_path=file_path, |
815 line_numbers=line_numbers) | 817 line_numbers=line_numbers) |
816 # The carriage return checker should never have been invoked, and so | 818 # The carriage return checker should never have been invoked, and so |
817 # should not have saved off any lines. | 819 # should not have saved off any lines. |
818 self.assertFalse(hasattr(self.carriage_checker, 'lines')) | 820 self.assertFalse(hasattr(self.carriage_checker, 'lines')) |
OLD | NEW |