Skip to content

Commit

Permalink
Test against example input
Browse files Browse the repository at this point in the history
  • Loading branch information
Ted Cassirer committed Dec 1, 2023
1 parent 47aa48e commit a49d8c2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 32 deletions.
10 changes: 3 additions & 7 deletions aoc_cas/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import importlib
from .util import load_module

from aocd import get_data


def plugin(year, day, **kwargs):
module_name = f"aoc_cas.aoc{year}.day{day}"
mod = importlib.import_module(module_name)
data = get_data(year=year, day=day)
def plugin(year: int, day: int, data: str) -> tuple:
mod = load_module(year, day)
return mod.part_a(data), mod.part_b(data)
15 changes: 4 additions & 11 deletions aoc_cas/aoc2023/day1.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from aoc_cas.util import test_with_examples


def part_a(data) -> int:
result = 0
for line in data.splitlines():
Expand Down Expand Up @@ -36,14 +39,4 @@ def part_b(data) -> int:


if __name__ == "__main__":
from aocd.models import Puzzle

puzzle = Puzzle(year=2023, day=1)
for example in puzzle.examples:
print(f"Running with example data: \n\n{example.input_data}\n")
if example.answer_a is not None:
part_a_result = part_a(example.input_data)
print(f"[Part A] Actual: {part_a_result} - Expected: {example.answer_a}")
if example.answer_b is not None:
part_b_result = part_b(example.input_data)
print(f"[Part B] Actual: {part_b_result} - Expected: {example.answer_b}\n")
test_with_examples(2023, 1)
24 changes: 24 additions & 0 deletions aoc_cas/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import importlib

from aocd.models import Puzzle


def load_module(year: int, day: int):
module_name = f"aoc_cas.aoc{year}.day{day}"
return importlib.import_module(module_name)


def test_with_examples(year: int, day: int) -> None:
mod = load_module(year, day)
puzzle = Puzzle(year, day)
print(f"Testing example data")
for example in puzzle.examples:
print(f"\n{example.input_data}\n")
if example.answer_a is not None:
part_a_result = str(mod.part_a(example.input_data))
correct = part_a_result == example.answer_a
print(f"{'❌✅'[correct]} [Part A] Actual: {part_a_result} - Expected: {example.answer_a}")
if example.answer_b is not None:
part_b_result = mod.part_b(example.input_data)
correct = part_b_result == example.answer_a
print(f"{'❌✅'[correct]} [Part B] Actual: {part_b_result} - Expected: {example.answer_b}\n")
15 changes: 4 additions & 11 deletions challenge_template.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from aoc_cas.util import test_with_examples


def part_a(data) -> int:
pass

Expand All @@ -6,14 +9,4 @@ def part_b(data) -> int:
pass

if __name__ == "__main__":
from aocd.models import Puzzle

puzzle = Puzzle(year={}, day={})
for example in puzzle.examples:
print(f"Running with example data: \n\n{example.input_data}\n")
if example.answer_a is not None:
part_a_result = part_a(example.input_data)
print(f"[Part A] Actual: {part_a_result} - Expected: {example.answer_a}")
if example.answer_b is not None:
part_b_result = part_b(example.input_data)
print(f"[Part B] Actual: {part_b_result} - Expected: {example.answer_b}\n")
test_with_examples({}, {})
5 changes: 2 additions & 3 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import importlib
import pathlib

import pytest

from aoc_cas.util import load_module

here = pathlib.Path(__file__).parent.resolve()
input_files = sorted(here.glob("fixtures/20*/*.txt"))
Expand All @@ -25,8 +25,7 @@ def test_examples(input_file) -> None:
pytest.fail(f"test data {input_file} is malformed")
*lines, expected_answer_a, expected_answer_b = lines
input_data = "\n".join(lines).rstrip()
module_name = f"aoc_cas.aoc{year}.day{day}"
mod = importlib.import_module(module_name)
mod = load_module(year, day)
if expected_answer_a != "-":
actual_answer_a = str(mod.part_a(input_data))
assert actual_answer_a == expected_answer_a, f"{actual_answer_a=} {expected_answer_a=}"
Expand Down

0 comments on commit a49d8c2

Please sign in to comment.