diff options
| author | Ian Cordasco <graffatcolmingov@gmail.com> | 2016-07-12 08:21:57 -0500 |
|---|---|---|
| committer | Ian Cordasco <graffatcolmingov@gmail.com> | 2016-07-12 08:21:57 -0500 |
| commit | 2d3f06219199a4a7307a3c9a5c14b69be8608dca (patch) | |
| tree | 5c30f94e45a9efd7f7c0a938327702c436344643 /tests | |
| parent | 1a722bbe7bb1918d26a073108ae362e731296a3e (diff) | |
| download | flake8-add-statistics.tar.gz | |
Add actual tests around statistics moduleadd-statistics
Also refactor our statistics module to be a bit smarter and less
namedtuple happy. The Statistic class had no reason to be a tuple,
I have no clue why I wrote it that way last night.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/unit/test_statistics.py | 71 |
1 files changed, 69 insertions, 2 deletions
diff --git a/tests/unit/test_statistics.py b/tests/unit/test_statistics.py index 015e94b..c60af33 100644 --- a/tests/unit/test_statistics.py +++ b/tests/unit/test_statistics.py @@ -1,5 +1,72 @@ +"""Tests for the statistics module in Flake8.""" +import pytest + from flake8 import statistics as stats +from flake8 import style_guide + +DEFAULT_ERROR_CODE = 'E100' +DEFAULT_FILENAME = 'file.py' +DEFAULT_TEXT = 'Default text' + + +def make_error(**kwargs): + """Create errors with a bunch of default values.""" + return style_guide.Error( + code=kwargs.pop('code', DEFAULT_ERROR_CODE), + filename=kwargs.pop('filename', DEFAULT_FILENAME), + line_number=kwargs.pop('line_number', 1), + column_number=kwargs.pop('column_number', 1), + text=kwargs.pop('text', DEFAULT_TEXT), + physical_line=None, + ) + + +def test_key_creation(): + """Verify how we create Keys from Errors.""" + key = stats.Key.create_from(make_error()) + assert key == (DEFAULT_FILENAME, DEFAULT_ERROR_CODE) + assert key.filename == DEFAULT_FILENAME + assert key.code == DEFAULT_ERROR_CODE + + +@pytest.mark.parametrize('code, filename, args, expected_result', [ + # Error prefix matches + ('E123', 'file000.py', ('E', None), True), + ('E123', 'file000.py', ('E1', None), True), + ('E123', 'file000.py', ('E12', None), True), + ('E123', 'file000.py', ('E123', None), True), + # Error prefix and filename match + ('E123', 'file000.py', ('E', 'file000.py'), True), + ('E123', 'file000.py', ('E1', 'file000.py'), True), + ('E123', 'file000.py', ('E12', 'file000.py'), True), + ('E123', 'file000.py', ('E123', 'file000.py'), True), + # Error prefix does not match + ('E123', 'file000.py', ('W', None), False), + # Error prefix matches but filename does not + ('E123', 'file000.py', ('E', 'file001.py'), False), + # Error prefix does not match but filename does + ('E123', 'file000.py', ('W', 'file000.py'), False), + # Neither error prefix match nor filename + ('E123', 'file000.py', ('W', 'file001.py'), False), +]) +def test_key_matching(code, filename, args, expected_result): + """Verify Key#matches behaves as we expect with fthe above input.""" + key = stats.Key.create_from(make_error(code=code, filename=filename)) + assert key.matches(*args) is expected_result + + +def test_statistic_creation(): + """Verify how we create Statistic objects from Errors.""" + stat = stats.Statistic.create_from(make_error()) + assert stat.error_code == DEFAULT_ERROR_CODE + assert stat.message == DEFAULT_TEXT + assert stat.filename == DEFAULT_FILENAME + assert stat.count == 0 -def test_nothing(): - pass +def test_statistic_increment(): + """Verify we update the count.""" + stat = stats.Statistic.create_from(make_error()) + assert stat.count == 0 + stat.increment() + assert stat.count == 1 |
