Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
This can't be tested at the system level on case-sensitive file systems.
  • Loading branch information
myint committed Feb 19, 2016
1 parent 1ff3e22 commit 235d500
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
22 changes: 12 additions & 10 deletions cpp/find_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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


Expand Down
36 changes: 36 additions & 0 deletions test_find_warnings.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 235d500

Please sign in to comment.