From 235d500b50b5389ce9c4e3706bcc149db67ee199 Mon Sep 17 00:00:00 2001 From: Steven Myint Date: Fri, 19 Feb 2016 13:33:25 -0800 Subject: [PATCH] Add unit tests This can't be tested at the system level on case-sensitive file systems. --- cpp/find_warnings.py | 22 ++++++++++++---------- test_find_warnings.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 10 deletions(-) create mode 100755 test_find_warnings.py diff --git a/cpp/find_warnings.py b/cpp/find_warnings.py index 73426d0..4e3f309 100644 --- a/cpp/find_warnings.py +++ b/cpp/find_warnings.py @@ -439,12 +439,13 @@ def _find_incorrect_case(self, included_files): candidates = os.listdir(os.path.dirname(filename)) except OSError: continue - if base_name not in candidates: - match = get_include_filename_match(base_name, candidates) - if match: - self._add_warning( - "'{}' should be '{}'".format(base_name, match), - node_and_module[0]) + + correct_filename = get_correct_include_filename(base_name, + candidates) + if correct_filename: + self._add_warning( + "'{}' should be '{}'".format(base_name, correct_filename), + node_and_module[0]) def _find_header_warnings(self): included_files, forward_declarations = self._read_and_parse_includes() @@ -575,10 +576,11 @@ def get_line_number(metrics_instance, node): return metrics_instance.get_line_number(node.start) -def get_include_filename_match(bad_filename, candidate_filenames): - for candidate in candidate_filenames: - if bad_filename.lower() == candidate.lower(): - return candidate +def get_correct_include_filename(filename, candidate_filenames): + if filename not in candidate_filenames: + for candidate in candidate_filenames: + if filename.lower() == candidate.lower(): + return candidate return None diff --git a/test_find_warnings.py b/test_find_warnings.py new file mode 100755 index 0000000..fd9e570 --- /dev/null +++ b/test_find_warnings.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python + +"""Tests for find_warnings module.""" + +from __future__ import absolute_import + +import unittest + +from cpp import find_warnings + + +class Tests(unittest.TestCase): + + def test_get_correct_include_filename(self): + self.assertEqual( + 'FOO.h', + find_warnings.get_correct_include_filename( + 'foo.h', + ['FOO.h', 'apple.h'])) + + def test_get_correct_include_filename_without_match(self): + self.assertEqual( + None, + find_warnings.get_correct_include_filename( + 'foo.h', + ['f.h', 'apple.h'])) + + self.assertEqual( + None, + find_warnings.get_correct_include_filename( + 'foo.h', + [])) + + +if __name__ == '__main__': + unittest.main()