From 63030ddf5414bf5e0c1e984e2069eb5a9c6c4c0d Mon Sep 17 00:00:00 2001 From: Jonathan Dayton Date: Mon, 29 Oct 2018 16:49:13 -0600 Subject: [PATCH] Start unit testing (#6) Add unit tests for identifier splitting along with instructions on how to run tests. --- .gitignore | 7 ++++- README.md | 18 ++++++++++++ setup.py | 1 + tests/__init__.py | 0 tests/codeutils/__init__.py | 0 tests/codeutils/test_identifiersplitting.py | 31 +++++++++++++++++++++ 6 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 tests/__init__.py create mode 100644 tests/codeutils/__init__.py create mode 100644 tests/codeutils/test_identifiersplitting.py diff --git a/.gitignore b/.gitignore index 8773840..f73190e 100644 --- a/.gitignore +++ b/.gitignore @@ -269,4 +269,9 @@ ModelManifest.xml /dist/ # Python egg metadata, regenerated from source files by setuptools. -/*.egg-info \ No newline at end of file +/*.egg-info +.eggs/ + +# Files generated by Coverage.py. +.coverage +htmlcov/ diff --git a/README.md b/README.md index 3c41668..a97c1bc 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,24 @@ Code-related Utilities * `dpu_utils.codeutils.split_identifier_into_parts` split identifiers into subtokens on CamelCase and snake_case. * `dpu_utils.codeutils.{Lattice, CSharpLattice}` represent lattices and some useful operations in Python. +## Tests + +### Run the unit tests + +```bash +python setup.py test +``` + +### Generate code coverage reports + +```bash +# pip install coverage +coverage run --source dpu_utils/ setup.py test && \ + coverage html +``` + +The resulting HTML file will be in `htmlcov/index.html`. + # Contributing This project welcomes contributions and suggestions. Most contributions require you to agree to a diff --git a/setup.py b/setup.py index bbabbed..5fbcde9 100755 --- a/setup.py +++ b/setup.py @@ -11,4 +11,5 @@ setuptools.setup( install_requires=[ 'azure-storage', 'numpy' ], + test_suite="tests", zip_safe=False) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/codeutils/__init__.py b/tests/codeutils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/codeutils/test_identifiersplitting.py b/tests/codeutils/test_identifiersplitting.py new file mode 100644 index 0000000..e79810f --- /dev/null +++ b/tests/codeutils/test_identifiersplitting.py @@ -0,0 +1,31 @@ +import unittest +import dpu_utils.codeutils.identifiersplitting as split + +class TestSplitCamelCase(unittest.TestCase): + def run_test(self, identifier, expected): + actual = split.split_camelcase(identifier) + self.assertEqual(expected, actual) + + def test_empty_string_returns_empty_list(self): + self.run_test("", []) + + def test_single_word_is_not_split(self): + self.run_test("variable", ["variable"]) + + def test_two_words_are_split(self): + self.run_test("camelCase", ["camel", "Case"]) + + def test_three_words_are_split(self): + self.run_test("camelCaseIdentifier", ["camel", "Case", "Identifier"]) + + def test_upper_camelcase_is_split(self): + self.run_test("CamelCase", ["Camel", "Case"]) + + def test_abbreviations_are_split_correctly(self): + self.run_test("HTMLParser", ["HTML", "Parser"]) + + def test_digits_are_split(self): + self.run_test("var12var3", ["var", "12", "var", "3"]) + + def test_special_characters_are_split(self): + self.run_test("@var-$var", ["@", "var", "-$", "var"])