summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Tom van der Lee <tom@vanderlee.io>2021-12-03 14:15:14 +0100
committerGravatar Tom van der Lee <tom@vanderlee.io>2021-12-03 14:15:51 +0100
commitb062e0f2abc189f352e019bac6854f469e9dfc8c (patch)
tree95a7a72cf56061278297f6326c8e3f9519432d53
parentf3c3fa9b94bc61b90a029bb74c98215b45c5be15 (diff)
download2021-b062e0f2abc189f352e019bac6854f469e9dfc8c.tar.gz
2021-b062e0f2abc189f352e019bac6854f469e9dfc8c.tar.bz2
2021-b062e0f2abc189f352e019bac6854f469e9dfc8c.zip
Added tests
-rw-r--r--aoc/__init__.py6
-rw-r--r--aoc/__main__.py3
-rw-r--r--aoc/test_init.py2
-rw-r--r--conftest.py35
-rw-r--r--day1/__init__.py4
-rw-r--r--day2/__init__.py4
-rw-r--r--requirements.txt2
7 files changed, 53 insertions, 3 deletions
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
4 4
5 5
6class BaseAssignment(ABC): 6class BaseAssignment(ABC):
7 example_result = NotImplemented
7 def __init__(self, path): 8 def __init__(self, path):
8 self.path = path 9 self.path = path
9 10
11 def __str__(self):
12 return f'{self.__module__}.{self.__class__.__name__}'
13
10 def parse_item(self, item: str) -> Any: 14 def parse_item(self, item: str) -> Any:
11 return item 15 return item
12 16
@@ -20,4 +24,4 @@ class BaseAssignment(ABC):
20 yield self.parse_item(line.strip()) 24 yield self.parse_item(line.strip())
21 25
22 def run(self, input: Iterator) -> Any: 26 def run(self, input: Iterator) -> Any:
23 raise NotImplementedError('Please implement run') 27 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 @@
1import argparse 1import argparse
2import os 2import os
3import sys
4import importlib 3import importlib
5from typing import Tuple, List 4from typing import List
6 5
7 6
8def day(assignment_part: str) -> str: 7def 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 @@
1def test_assingment_examples(assignment):
2 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 @@
1import importlib
2import os
3from pkgutil import walk_packages
4
5from _pytest.python import Metafunc
6
7from aoc.__main__ import day
8
9dir_path = os.path.dirname(os.path.realpath(__file__))
10
11def pytest_generate_tests(metafunc: Metafunc):
12 if 'assignment' in metafunc.fixturenames:
13 packages = [
14 importlib.import_module(package.name)
15 for package
16 in walk_packages([dir_path])
17 if package.name.startswith('day')
18 ]
19
20 assignments = [
21 (getattr(package, f'Assignment{day(part)}'), package)
22 for package in packages
23 for part in ['1', '2']
24 ]
25
26 metafunc.parametrize(
27 argnames=f'assignment',
28 argvalues=[
29 Assignment(path=package.__path__[0])
30 for (Assignment, package)
31 in assignments
32 if Assignment is not None
33 ],
34 ids=lambda assignment: str(assignment)
35 )
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):
11 return list(super().read_input(example)) 11 return list(super().read_input(example))
12 12
13class AssignmentOne(Assignment): 13class AssignmentOne(Assignment):
14 example_result = 7
15
14 def run(self, input: List) -> int: 16 def run(self, input: List) -> int:
15 result = 0 17 result = 0
16 18
@@ -21,6 +23,8 @@ class AssignmentOne(Assignment):
21 23
22 24
23class AssignmentTwo(Assignment): 25class AssignmentTwo(Assignment):
26 example_result = 5
27
24 def run(self, input: List) -> int: 28 def run(self, input: List) -> int:
25 new_input = [ 29 new_input = [
26 input[i - 2] + input[i - 1] + input[i] 30 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):
17 return list(super().read_input(example)) 17 return list(super().read_input(example))
18 18
19class AssignmentOne(Assignment): 19class AssignmentOne(Assignment):
20 example_result = 150
21
20 depth = 0 22 depth = 0
21 horizontal = 0 23 horizontal = 0
22 24
@@ -34,6 +36,8 @@ class AssignmentOne(Assignment):
34 36
35 37
36class AssignmentTwo(Assignment): 38class AssignmentTwo(Assignment):
39 example_result = 900
40
37 aim = 0 41 aim = 0
38 depth = 0 42 depth = 0
39 horizontal = 0 43 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 @@
1pytest
2coverage