Skip to content

Commit

Permalink
[TESTS] Port tests to use data_driven_test
Browse files Browse the repository at this point in the history
  • Loading branch information
pradyunsg committed Jun 20, 2015
1 parent 3456ede commit f820592
Show file tree
Hide file tree
Showing 16 changed files with 1,046 additions and 1,140 deletions.
54 changes: 27 additions & 27 deletions py2c/abc/tests/test_manager.py
@@ -1,7 +1,7 @@
"""Unit-tests for `py2c.abc.manager.Manager`
"""

from py2c.tests import Test
from py2c.tests import Test, data_driven_test
from nose.tools import assert_raises

from py2c.abc.manager import Manager
Expand Down Expand Up @@ -43,6 +43,30 @@ class SuperCallingManager(Manager):
def run(self, node):
super().run(node)

initialization_invalid_cases = [
(
"without options attribute or run method",
EmptyManager, TypeError, ["EmptyManager"]
),
(
"without run method",
NoRunManager, TypeError, ["NoRunManager", "run"]
),
(
"without options attribute",
NoOptionsManager, AttributeError,
["NoOptionsManager", "options", "attribute"]
),
(
"with a non-dictionary options attribute",
OptionsNotADictManager, TypeError,
[
"OptionsNotADictManager", "options", "should be", "instance",
"dict"
]
),
]


# -----------------------------------------------------------------------------
# Tests
Expand All @@ -54,37 +78,13 @@ class TestBaseManager(Test):
def test_initializes_a_subclass_with_all_required_methods(self):
GoodManager()

def check_bad_initialization(self, manager_class, err, required_phrases):
@data_driven_test(initialization_invalid_cases, True, "raises error initializing: ") # noqa
def test_initialization_invalid_cases(self, manager_class, err, required_phrases): # noqa
with assert_raises(err) as context:
manager_class()

self.assert_error_message_contains(context.exception, required_phrases)

def test_does_not_do_bad_initialization(self):
yield from self.yield_tests(self.check_bad_initialization, [
(
"without options attribute or run method",
EmptyManager, TypeError, ["EmptyManager"]
),
(
"without run method",
NoRunManager, TypeError, ["NoRunManager", "run"]
),
(
"without options attribute",
NoOptionsManager, AttributeError,
["NoOptionsManager", "options", "attribute"]
),
(
"with a non-dictionary options attribute",
OptionsNotADictManager, TypeError,
[
"OptionsNotADictManager", "options", "should be",
"instance", dict.__qualname__
]
),
], described=True, prefix="does not initialize subclass ")

def test_blocks_subclass_with_calling_super_run_method(self):
manager = SuperCallingManager()

Expand Down
52 changes: 26 additions & 26 deletions py2c/abc/tests/test_source_handler.py
@@ -1,7 +1,7 @@
"""Unit-tests for `py2c.abc.source_handler.SourceHandler`
"""

from py2c.tests import Test
from py2c.tests import Test, data_driven_test
from nose.tools import assert_raises

from py2c.abc.source_handler import SourceHandler
Expand Down Expand Up @@ -64,6 +64,29 @@ def get_source(self, file_name):
def write_source(self, file_name, source):
super().write_source(file_name, source)

initialization_invalid_cases = [
(
"without any method",
EmptySourceHandler, TypeError, ["EmptySourceHandler"]
),
(
"without get_files method",
NoGetFilesSourceHandler, TypeError,
["NoGetFilesSourceHandler", "get_files"]
),
(
"without get_source method",
NoGetSourceSourceHandler, TypeError,
["NoGetSourceSourceHandler", "get_source"]
),
(
"without write_source method",
NoWriteSourceSourceHandler, TypeError,
["NoWriteSourceSourceHandler", "write_source"]
),
# MARK:: Should I add the only-one method cases as well?
]


# -----------------------------------------------------------------------------
# Tests
Expand All @@ -75,36 +98,13 @@ class TestBaseSourceHandler(Test):
def test_initializes_a_subclass_with_all_required_methods(self):
GoodSourceHandler()

def check_bad_initialization(self, source_handler_class, err, required_phrases):
@data_driven_test(initialization_invalid_cases, True, "raises error initializing subclass: ") # noqa
def test_initialization_invalid_cases(self, source_handler_class, err, required_phrases): # noqa
with assert_raises(err) as context:
source_handler_class()

self.assert_error_message_contains(context.exception, required_phrases)

def test_does_not_do_bad_initialization(self):
yield from self.yield_tests(self.check_bad_initialization, [
(
"without any method",
EmptySourceHandler, TypeError, ["EmptySourceHandler"]
),
(
"without get_files method",
NoGetFilesSourceHandler, TypeError,
["NoGetFilesSourceHandler", "get_files"]
),
(
"without get_source method",
NoGetSourceSourceHandler, TypeError,
["NoGetSourceSourceHandler", "get_source"]
),
(
"without write_source method",
NoWriteSourceSourceHandler, TypeError,
["NoWriteSourceSourceHandler", "write_source"]
),
# MARK:: Should I add the only-one method cases as well?
], described=True, prefix="does not initialize subclass ")

def test_raises_error_when_subclass_calls_an_abstract_method(self):
source_handler = SuperCallingSourceHandler()

Expand Down
22 changes: 11 additions & 11 deletions py2c/abc/tests/test_worker.py
Expand Up @@ -5,7 +5,7 @@

from py2c.abc.worker import Worker

from py2c.tests import Test, mock
from py2c.tests import Test, mock, data_driven_test
from nose.tools import assert_raises, assert_true, assert_is_instance


Expand All @@ -29,6 +29,13 @@ class SuperCallingWorker(Worker):
def work(self):
super().work()

initialization_invalid_cases = [
(
"without work method",
BadWorker, TypeError, ["BadWorker", "work"]
),
]


# -----------------------------------------------------------------------------
# Tests
Expand All @@ -40,20 +47,13 @@ class TestBaseWorker(Test):
def test_initializes_a_subclass_with_all_required_methods(self):
GoodWorker()

def check_bad_initialization(self, manager_class, err, required_phrases):
@data_driven_test(initialization_invalid_cases, True, "raises error initializing subclass: ") # noqa
def test_initialization_invalid_cases(self, worker_class, err, required_phrases): # noqa
with assert_raises(err) as context:
manager_class()
worker_class()

self.assert_error_message_contains(context.exception, required_phrases)

def test_does_not_do_bad_initialization(self):
yield from self.yield_tests(self.check_bad_initialization, [
(
"without work method",
BadWorker, TypeError, ["BadWorker", "work"]
),
], described=True, prefix="does not initialize subclass ")

def test_blocks_subclass_with_calling_super_work_method(self):
worker = SuperCallingWorker()

Expand Down
44 changes: 24 additions & 20 deletions py2c/common/tests/test_configuration.py
Expand Up @@ -7,7 +7,7 @@
)

from nose.tools import assert_is, assert_raises, assert_equal
from py2c.tests import Test, runmodule
from py2c.tests import Test, data_driven_test


class TestConfiguration(Test):
Expand Down Expand Up @@ -40,27 +40,29 @@ def test_does_set_and_get_a_registered_option(self):
val = self.config.get_option("registered_set_option")
assert_is(val, obj)

def check_option_registeration(self, option_name, should_work=True):
@data_driven_test(described=True, prefix="registers valid option: ", data=[
("a simple name", "a_simple_name"),
("a dotted name", "a.dotted.name"),
])
def test_registers_valid_option(self, name):
try:
Configuration().register_option(option_name)
except InvalidOptionError:
assert not should_work, "Should have registered..."
Configuration().register_option(name)
except Exception:
self.fail("Should have registered name: {}".format(name))

@data_driven_test(described=True, prefix="raises error when registering invalid option: ", data=[ # noqa
("a name starting with dot", ".invalid"),
("a dotted name starting with dot", ".invalid.name"),
("a name with spaces", "invalid name"),
("a non-string name", 1200),
])
def test_raises_error_registering_invalid_option(self, name):
try:
Configuration().register_option(name)
except Exception:
pass
else:
assert should_work, "Should not have registered..."

def test_z_does_valid_option_registeration(self):
yield from self.yield_tests(self.check_option_registeration, [
("a simple name", "a_simple_name"),
("a dotted name", "a.dotted.name"),
], described=True, prefix="does register option with ")

def test_z_does_not_do_invalid_option_registeration(self):
yield from self.yield_tests(self.check_option_registeration, [
("a name starting with dot", ".invalid", False),
("a dotted name starting with dot", ".invalid.name", False),
("a name with spaces", "invalid name", False),
("a non-string name", 1200, False),
], described=True, prefix="does not register option with ")
self.fail("Should not have registered name: {}".format(name))

def test_does_reset_options_to_default_value_correctly(self):
self.config.register_option("option_name", "Yo!")
Expand All @@ -76,4 +78,6 @@ def test_does_reset_options_to_default_value_correctly(self):
assert_equal(self.config.get_option("option_name"), "Yo!")

if __name__ == '__main__':
from py2c.tests import runmodule

runmodule(capture=False)
52 changes: 17 additions & 35 deletions py2c/processing/tests/test_to_ast.py
Expand Up @@ -6,48 +6,30 @@

from py2c.processing.to_ast import SourceToAST, SourceToASTTranslationError

from py2c.tests import Test
from py2c.tests import Test, data_driven_test
from nose.tools import assert_equal, assert_raises


class TestPythonSourceToPythonAST(Test):
"""py2c.processing.to_ast.SourceToAST
"""

def check_conversion(self, code, node, error=None):
"""Test the conversion of Python source to Python's AST
"""
code = textwrap.dedent(code)

convertor = SourceToAST()

if node is None:
if error is None:
self.fail("Only one of node and error should be non-zero.")
with assert_raises(error):
convertor.work(code)
else:
if error is not None:
self.fail("Only one of node and error should be non-zero.")
assert_equal(ast.dump(convertor.work(code)), ast.dump(node))

# Could make more extenstive, I guess don't need to
def test_does_source_to_AST_conversion_correctly(self):
yield from self.yield_tests(self.check_conversion, [
[
"a simple statement",
"pass", ast.Module(body=[ast.Pass()])
]
], described=True, prefix="does convert correctly ")

def test_does_not_convert_invalid_code(self):
yield from self.yield_tests(self.check_conversion, [
[
"invalid code",
"$", None, SourceToASTTranslationError
]
], described=True, prefix="does not convert ")

# Could make more extensive, I guess don't need to
@data_driven_test(described=True, prefix="converts valid input correctly: ", data=[ # noqa
["a pass statement", "pass", ast.Module(body=[ast.Pass()])]
])
def test_valid_input_cases(self, code, node):
assert_equal(
ast.dump(SourceToAST().work(textwrap.dedent(code))),
ast.dump(node)
)

@data_driven_test(described=True, prefix="raises error when given: ", data=[ # noqa
["invalid code", "$", SourceToASTTranslationError]
])
def test_invalid_input_cases(self, code, error):
with assert_raises(error):
SourceToAST().work(textwrap.dedent(code))

if __name__ == '__main__':
from py2c.tests import runmodule
Expand Down
22 changes: 6 additions & 16 deletions py2c/source_handlers/tests/test_file_source_handler.py
Expand Up @@ -11,7 +11,7 @@
from py2c.source_handlers import FileSourceHandler

from nose.tools import assert_raises, assert_equal
from py2c.tests import Test
from py2c.tests import Test, data_driven_test


class TestFileSourceHandler(Test):
Expand Down Expand Up @@ -53,7 +53,11 @@ def test_needs_an_argument_to_initialize(self):
FileSourceHandler()
self.assert_error_message_contains(context.exception, ["require", "1"])

def check_file_name_matches(self, error, method, required_phrases, *args):
@data_driven_test(described=True, prefix="checks file name before ", data=[ # noqa
("getting sources", CouldNotGetSourceError, "get_source"),
("writing sources", CouldNotWriteSourceError, "write_source", ""),
])
def check_file_name_matches(self, error, method, *args):
file_name = self.get_temporary_file_name()
fsh = FileSourceHandler(file_name)

Expand All @@ -64,20 +68,6 @@ def check_file_name_matches(self, error, method, required_phrases, *args):
context.exception, ["unexpected", "file name"]
)

def test_does_check_file_name_before_operation(self):
yield from self.yield_tests(self.check_file_name_matches, [
(
"getting sources",
CouldNotGetSourceError, "get_source",
["unexpected", "file name"]
),
(
"writing sources",
CouldNotWriteSourceError, "write_source",
["unexpected", "file name"], ""
),
], described=True, prefix="does check file name before ")

def test_lists_only_passed_file_name(self):
fsh = FileSourceHandler("magic.py")

Expand Down
1 change: 1 addition & 0 deletions py2c/tests/test_utils.py
Expand Up @@ -58,6 +58,7 @@ def test_does_not_allow_attribute_to_be_of_wrong_type(self):
class TestIsValidDottedIdentifier(Test):
"""py2c.utils.is_valid_dotted_identifier
"""
# MARK:: Should this be into a data_driven_test?

def test_verifies_valid_case(self):
# If we see an error, it's a failed test.
Expand Down

0 comments on commit f820592

Please sign in to comment.