From 437306f5336ea2aa0e9a5e5e8d12c82c6651c8d0 Mon Sep 17 00:00:00 2001 From: alexo Date: Wed, 27 Dec 2023 01:50:21 +1100 Subject: [PATCH] day01: coverage --- .pre-commit-config.yaml | 8 ++++++-- day01/day1a.py | 24 ++++++++++++++++-------- day01/day1b.py | 18 ++++++++++++------ day01/input-small.txt | 4 ++++ day01/input-small2.txt | 7 +++++++ day01/tests/test_day1a.py | 17 ++++++++++++++++- day01/tests/test_day1b.py | 17 ++++++++++++++++- pyproject.toml | 1 + 8 files changed, 78 insertions(+), 18 deletions(-) create mode 100644 day01/input-small.txt create mode 100644 day01/input-small2.txt diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a8508e0..4bf7c1b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -6,10 +6,12 @@ repos: rev: v0.1.8 hooks: - id: ruff + stages: [commit, manual] - repo: https://github.com/pre-commit/mirrors-mypy rev: v1.8.0 # Use the sha / tag you want to point at hooks: - id: mypy + stages: [commit, manual] args: - "--check-untyped-defs" - "--strict" @@ -19,6 +21,7 @@ repos: types-requests==2.31.0.10, types-tqdm==4.66.0.5, python-dotenv==1.0.0, + pytest==7.4.3, graphviz==0.20.1, vpython==7.6.3, z3-solver==4.12.4.0, @@ -27,13 +30,14 @@ repos: rev: v4.5.0 hooks: - id: mixed-line-ending + stages: [commit, manual] args: - "--fix=lf" - repo: local hooks: - id: pytest-check name: pytest-check - stages: [pre-push] + stages: [pre-push, manual] types: [python] entry: pytest -v --color=yes language: system @@ -41,7 +45,7 @@ repos: pass_filenames: false - id: coverage-check # technically runs pytest again. oh well name: coverage-check - stages: [pre-push] + stages: [pre-push, manual] types: [python] entry: coverage run language: system diff --git a/day01/day1a.py b/day01/day1a.py index 258375e..f912436 100644 --- a/day01/day1a.py +++ b/day01/day1a.py @@ -3,6 +3,9 @@ from typing import Optional +INPUT = "day01/input.txt" +INPUT_SMALL = "day01/input-small.txt" + def get_first_last(line: str) -> tuple[str, str]: first: Optional[str] = None @@ -18,14 +21,19 @@ def get_first_last(line: str) -> tuple[str, str]: return (first, last) -def main() -> None: - with open("day01/input.txt", "r", encoding="utf8") as file: - total = 0 - for line in file: - first, last = get_first_last(line) - data = int(first + last) - total += data - print(total) +def get_input(input_file: str) -> list[str]: + with open(input_file, "r", encoding="utf8") as file: + return list(file) + + +def main(input_file: str = INPUT) -> int: + lines = get_input(input_file) + total = 0 + for line in lines: + first, last = get_first_last(line) + data = int(first + last) + total += data + return total if __name__ == "__main__": diff --git a/day01/day1b.py b/day01/day1b.py index 13f659a..078a853 100644 --- a/day01/day1b.py +++ b/day01/day1b.py @@ -3,6 +3,9 @@ from dataclasses import dataclass +INPUT = "day01/input.txt" +INPUT_SMALL = "day01/input-small2.txt" + @dataclass class IndexValue: @@ -52,13 +55,16 @@ def process_line(line: str) -> int: return int(index_to_chars[first_index] + index_to_chars[last_index]) -def main() -> None: - with open("day01/input.txt", "r", encoding="utf8") as file: - total = 0 - for line in file: - total += process_line(line) +def get_input(input_file: str) -> list[str]: + with open(input_file, "r", encoding="utf8") as file: + return list(file) + - print(total) +def main(input_file: str = INPUT) -> int: + lines = get_input(input_file) + total = sum(process_line(line) for line in lines) + print(total) + return total if __name__ == "__main__": diff --git a/day01/input-small.txt b/day01/input-small.txt new file mode 100644 index 0000000..7bbc69a --- /dev/null +++ b/day01/input-small.txt @@ -0,0 +1,4 @@ +1abc2 +pqr3stu8vwx +a1b2c3d4e5f +treb7uchet diff --git a/day01/input-small2.txt b/day01/input-small2.txt new file mode 100644 index 0000000..41aa89c --- /dev/null +++ b/day01/input-small2.txt @@ -0,0 +1,7 @@ +two1nine +eightwothree +abcone2threexyz +xtwone3four +4nineeightseven2 +zoneight234 +7pqrstsixteen diff --git a/day01/tests/test_day1a.py b/day01/tests/test_day1a.py index 7f54f32..981ac03 100644 --- a/day01/tests/test_day1a.py +++ b/day01/tests/test_day1a.py @@ -1,4 +1,6 @@ -from day01.day1a import get_first_last +import pytest + +from day01.day1a import INPUT_SMALL, get_first_last, get_input, main def test_get_first_last() -> None: @@ -10,3 +12,16 @@ def test_get_first_last() -> None: assert get_first_last("pqr3stu8vwx") == ("3", "8") assert get_first_last("a1b2c3d4e5f") == ("1", "5") assert get_first_last("treb7uchet") == ("7", "7") + + with pytest.raises(ValueError): + get_first_last("") + + +def test_main() -> None: + assert main(INPUT_SMALL) == 142 + + +def test_get_input() -> None: + lines: list[str] = get_input(INPUT_SMALL) + assert len(lines) == 4 + assert lines[0].strip() == "1abc2" diff --git a/day01/tests/test_day1b.py b/day01/tests/test_day1b.py index e63874d..d7d589a 100644 --- a/day01/tests/test_day1b.py +++ b/day01/tests/test_day1b.py @@ -1,4 +1,4 @@ -from day01.day1b import process_line +from day01.day1b import INPUT_SMALL, IndexValue, get_input, main, process_line def test_process_line() -> None: @@ -10,3 +10,18 @@ def test_process_line() -> None: assert process_line("zoneight234") == 14 assert process_line("7pqrstsixteen") == 76 assert process_line("zoneight") == 18 + + +def test_get_input() -> None: + lines: list[str] = get_input(INPUT_SMALL) + assert len(lines) == 7 + assert lines[0].strip() == "two1nine" + + +def test_index_value() -> None: + index_val: IndexValue = IndexValue(0, "1") + assert str(index_val) == ("(i:0, v:1)") + + +def test_main() -> None: + assert main(INPUT_SMALL) == 281 diff --git a/pyproject.toml b/pyproject.toml index 09c03dc..4b3aaea 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -32,6 +32,7 @@ source = ['.'] relative_files = true [tool.coverage.report] +exclude_lines = ["if __name__ == .__main__.:"] precision = 2 skip_covered = true fail_under = 0