Files
advent-of-code-2022/03/py/test.py
2022-12-15 08:09:09 +01:00

58 lines
1.2 KiB
Python

from typing import ContextManager, Optional
from solution import part1, part2, priority, chunks
from contextlib import nullcontext as no_raise
import pytest
@pytest.fixture
def example_data() -> str:
return """vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw"""
def test_part1(example_data: str) -> None:
assert part1(example_data) == 157
@pytest.mark.parametrize(
"s,expected",
[
("a", 1),
("b", 2),
("z", 26),
("A", 27),
("C", 29),
("Z", 52),
],
)
def test_priority(s: str, expected: int) -> None:
assert priority(s) == expected
@pytest.mark.parametrize(
"line,expected,raises",
[
("a\nb\nc", [["a", "b", "c"]], no_raise()),
("a\nb\nc\nd", None, pytest.raises(ValueError)),
],
)
def test_chunks(
line: str,
expected: Optional[list[tuple[str, str, str]]],
raises: ContextManager,
):
with raises:
actual = list(chunks(line.splitlines(), 3))
if expected:
assert actual == expected
def test_part2(example_data: str) -> None:
assert part2(example_data) == 70