-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbases.py
80 lines (58 loc) · 2.26 KB
/
bases.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Copyright 2018 Rolf van Kleef
# This library is licensed under the BSD 3-clause license. This means that you
# are allowed to do almost anything with it. For exact terms, please refer to
# the attached license file.
import os
from abc import ABC, abstractmethod
from unittest import TestCase
from typing import Tuple, Any, List
class Day(ABC):
data: str
examples_1: List[Tuple[str, Any]]
examples_2: List[Tuple[str, Any]]
def __init__(self, load_data=True):
if not load_data:
return
path = os.path.join(*type(self).__module__.split('.')[:-1])
file = open("{}/input.txt".format(path), "r")
self.data = file.read().strip()
file.close()
@abstractmethod
def part1(self):
pass
@abstractmethod
def part2(self):
pass
class DayTest(TestCase):
solution: Day
puzzles: Tuple
# noinspection PyPep8Naming
def __init__(self, testName, solution, puzzles=(1, 2)):
super(DayTest, self).__init__(testName)
self.solution = solution
self.not_skip = puzzles
def test_part1(self):
if 1 not in self.not_skip:
self.skipTest('This test is not specified.')
if not hasattr(self.solution, 'examples_1'):
self.fail('There are no tests for part 1')
if len(self.solution.examples_1) <= 0:
self.skipTest('No tests are provided.')
for data, answer in self.solution.examples_1:
self.solution.data = data
self.assertEqual(answer, self.solution.part1(istest=True))
def test_part2(self):
if 2 not in self.not_skip:
self.skipTest('This test is not specified.')
if not hasattr(self.solution, 'examples_2'):
self.fail('There are no tests for part 2')
if len(self.solution.examples_2) <= 0:
self.skipTest('No tests are provided.')
for data, answer in self.solution.examples_2:
self.solution.data = data
self.assertEqual(answer, self.solution.part2(istest=True))
@staticmethod
def new(day, solution, puzzle, puzzles):
cls = type('{}'.format(day), (DayTest, ), {'__module__': 'days'})
return cls('test_part{}'.format(puzzle), solution=solution,
puzzles=puzzles)