Move unit tests into tests/unit

This commit is contained in:
Ian Cordasco 2016-01-09 12:55:31 -06:00
parent 93369e112f
commit 1e9878611a
4 changed files with 52 additions and 0 deletions

0
tests/unit/__init__.py Normal file
View file

View file

@ -0,0 +1,35 @@
import pytest
from flake8 import notifier
class _Listener(object):
def __init__(self, error_code):
self.error_code = error_code
self.was_notified = False
def notify(self, error_code, *args, **kwargs):
assert error_code.startswith(self.error_code)
self.was_notified = True
class TestNotifier(object):
@pytest.fixture(autouse=True)
def setup(self):
self.notifier = notifier.Notifier()
self.listener_map = {}
def add_listener(error_code):
listener = _Listener(error_code)
self.listener_map[error_code] = listener
self.notifier.register_listener(error_code, listener)
for i in range(10):
add_listener('E{0}'.format(i))
for j in range(30):
add_listener('E{0}{1:02d}'.format(i, j))
def test_notify(self):
"""Show that we notify a specific error code."""
self.notifier.notify('E111', 'extra', 'args')
assert self.listener_map['E111'].was_notified is True
assert self.listener_map['E1'].was_notified is True

52
tests/unit/test_option.py Normal file
View file

@ -0,0 +1,52 @@
"""Unit tests for flake8.options.manager.Option."""
import mock
from flake8.options import manager
def test_to_optparse():
"""Test conversion to an optparse.Option class."""
opt = manager.Option(
short_option_name='-t',
long_option_name='--test',
action='count',
parse_from_config=True,
normalize_paths=True,
)
assert opt.normalize_paths is True
assert opt.parse_from_config is True
optparse_opt = opt.to_optparse()
assert not hasattr(optparse_opt, 'parse_from_config')
assert not hasattr(optparse_opt, 'normalize_paths')
assert optparse_opt.action == 'count'
@mock.patch('optparse.Option')
def test_to_optparse_creates_an_option_as_we_expect(Option):
"""Show that we pass all keyword args to optparse.Option."""
opt = manager.Option('-t', '--test', action='count')
opt.to_optparse()
option_kwargs = {
'action': 'count',
'default': None,
'type': None,
'dest': None,
'callback': None,
'callback_args': None,
'callback_kwargs': None,
'help': None,
'metavar': None,
}
Option.assert_called_once_with(
'-t', '--test', **option_kwargs
)
def test_config_name_generation():
"""Show that we generate the config name deterministically."""
opt = manager.Option(long_option_name='--some-very-long-option-name',
parse_from_config=True)
assert opt.config_name == 'some_very_long_option_name'

121
tests/unit/test_trie.py Normal file
View file

@ -0,0 +1,121 @@
from flake8 import _trie as trie
class TestTrie(object):
"""Collection of tests for the Trie class."""
def test_traverse_without_data(self):
"""Verify the behaviour when traversing an empty Trie."""
tree = trie.Trie()
assert list(tree.traverse()) == []
def test_traverse_with_data(self):
"""Verify that traversal of our Trie is depth-first and pre-order."""
tree = trie.Trie()
tree.add('A', 'A')
tree.add('a', 'a')
tree.add('AB', 'B')
tree.add('Ab', 'b')
tree.add('AbC', 'C')
tree.add('Abc', 'c')
# The trie tree here should look something like
#
# <root>
# / \
# A a
# / |
# B b
# / \
# C c
#
# And the traversal should look like:
#
# A B b C c a
expected_order = ['A', 'B', 'b', 'C', 'c', 'a']
for expected, actual_node in zip(expected_order, tree.traverse()):
assert actual_node.prefix == expected
def test_find(self):
"""Exercise the Trie.find method."""
tree = trie.Trie()
tree.add('A', 'A')
tree.add('a', 'a')
tree.add('AB', 'AB')
tree.add('Ab', 'Ab')
tree.add('AbC', 'AbC')
tree.add('Abc', 'Abc')
assert tree.find('AB').data == ['AB']
assert tree.find('AbC').data == ['AbC']
assert tree.find('A').data == ['A']
assert tree.find('X') is None
class TestTrieNode(object):
"""Collection of tests for the TrieNode class."""
def test_add_child(self):
"""Verify we add children appropriately."""
node = trie.TrieNode('E', 'E is for Eat')
assert node.find_prefix('a') is None
added = node.add_child('a', 'a is for Apple')
assert node.find_prefix('a') is added
def test_add_child_overrides_previous_child(self):
"""Verify adding a child will replace the previous child."""
node = trie.TrieNode('E', 'E is for Eat', children={
'a': trie.TrieNode('a', 'a is for Apple')
})
previous = node.find_prefix('a')
assert previous is not None
added = node.add_child('a', 'a is for Ascertain')
assert node.find_prefix('a') is added
def test_find_prefix(self):
"""Verify we can find a child with the specified prefix."""
node = trie.TrieNode('E', 'E is for Eat', children={
'a': trie.TrieNode('a', 'a is for Apple')
})
child = node.find_prefix('a')
assert child is not None
assert child.prefix == 'a'
assert child.data == 'a is for Apple'
def test_find_prefix_returns_None_when_no_children_have_the_prefix(self):
"""Verify we receive None from find_prefix for missing children."""
node = trie.TrieNode('E', 'E is for Eat', children={
'a': trie.TrieNode('a', 'a is for Apple')
})
assert node.find_prefix('b') is None
def test_traverse_does_nothing_when_a_node_has_no_children(self):
"""Verify traversing a node with no children does nothing."""
node = trie.TrieNode('E', 'E is for Eat')
assert list(node.traverse()) == []
def test_traverse(self):
"""Verify traversal is depth-first and pre-order."""
root = trie.TrieNode(None, None)
node = root.add_child('A', 'A')
root.add_child('a', 'a')
node.add_child('B', 'B')
node = node.add_child('b', 'b')
node.add_child('C', 'C')
node.add_child('c', 'c')
# The sub-tree here should look something like
#
# <root>
# / \
# A a
# / |
# B b
# / \
# C c
#
# And the traversal should look like:
#
# A B b C c a
expected_order = ['A', 'B', 'b', 'C', 'c', 'a']
for expected, actual_node in zip(expected_order, root.traverse()):
assert actual_node.prefix == expected