From b062e0f2abc189f352e019bac6854f469e9dfc8c Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Fri, 3 Dec 2021 14:15:14 +0100 Subject: Added tests --- aoc/__init__.py | 6 +++++- aoc/__main__.py | 3 +-- aoc/test_init.py | 2 ++ conftest.py | 35 +++++++++++++++++++++++++++++++++++ day1/__init__.py | 4 ++++ day2/__init__.py | 4 ++++ requirements.txt | 2 ++ 7 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 aoc/test_init.py create mode 100644 conftest.py create mode 100644 requirements.txt diff --git a/aoc/__init__.py b/aoc/__init__.py index 07a5fe7..b13489c 100644 --- a/aoc/__init__.py +++ b/aoc/__init__.py @@ -4,9 +4,13 @@ from typing import Generator, Any, Iterator class BaseAssignment(ABC): + example_result = NotImplemented def __init__(self, path): self.path = path + def __str__(self): + return f'{self.__module__}.{self.__class__.__name__}' + def parse_item(self, item: str) -> Any: return item @@ -20,4 +24,4 @@ class BaseAssignment(ABC): yield self.parse_item(line.strip()) def run(self, input: Iterator) -> Any: - raise NotImplementedError('Please implement run') + raise NotImplementedError('Please implement run') \ No newline at end of file diff --git a/aoc/__main__.py b/aoc/__main__.py index efb215d..6dffd61 100644 --- a/aoc/__main__.py +++ b/aoc/__main__.py @@ -1,8 +1,7 @@ import argparse import os -import sys import importlib -from typing import Tuple, List +from typing import List def day(assignment_part: str) -> str: diff --git a/aoc/test_init.py b/aoc/test_init.py new file mode 100644 index 0000000..8524086 --- /dev/null +++ b/aoc/test_init.py @@ -0,0 +1,2 @@ +def test_assingment_examples(assignment): + assert assignment.run(input=assignment.read_input(example=True)) == assignment.example_result diff --git a/conftest.py b/conftest.py new file mode 100644 index 0000000..3bae1e2 --- /dev/null +++ b/conftest.py @@ -0,0 +1,35 @@ +import importlib +import os +from pkgutil import walk_packages + +from _pytest.python import Metafunc + +from aoc.__main__ import day + +dir_path = os.path.dirname(os.path.realpath(__file__)) + +def pytest_generate_tests(metafunc: Metafunc): + if 'assignment' in metafunc.fixturenames: + packages = [ + importlib.import_module(package.name) + for package + in walk_packages([dir_path]) + if package.name.startswith('day') + ] + + assignments = [ + (getattr(package, f'Assignment{day(part)}'), package) + for package in packages + for part in ['1', '2'] + ] + + metafunc.parametrize( + argnames=f'assignment', + argvalues=[ + Assignment(path=package.__path__[0]) + for (Assignment, package) + in assignments + if Assignment is not None + ], + ids=lambda assignment: str(assignment) + ) diff --git a/day1/__init__.py b/day1/__init__.py index bc44089..be17935 100644 --- a/day1/__init__.py +++ b/day1/__init__.py @@ -11,6 +11,8 @@ class Assignment(BaseAssignment): return list(super().read_input(example)) class AssignmentOne(Assignment): + example_result = 7 + def run(self, input: List) -> int: result = 0 @@ -21,6 +23,8 @@ class AssignmentOne(Assignment): class AssignmentTwo(Assignment): + example_result = 5 + def run(self, input: List) -> int: new_input = [ input[i - 2] + input[i - 1] + input[i] diff --git a/day2/__init__.py b/day2/__init__.py index 14229a9..d99a0be 100644 --- a/day2/__init__.py +++ b/day2/__init__.py @@ -17,6 +17,8 @@ class Assignment(BaseAssignment): return list(super().read_input(example)) class AssignmentOne(Assignment): + example_result = 150 + depth = 0 horizontal = 0 @@ -34,6 +36,8 @@ class AssignmentOne(Assignment): class AssignmentTwo(Assignment): + example_result = 900 + aim = 0 depth = 0 horizontal = 0 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..49ec960 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +pytest +coverage -- cgit v1.2.3