From 05180e80e75073a4e1d76e4ace588482a7d4fbb8 Mon Sep 17 00:00:00 2001 From: Eric Wolf Date: Sun, 4 Sep 2022 05:59:35 +0200 Subject: [PATCH] improve CollisionError string representations --- lambda_calculus/__init__.py | 2 +- lambda_calculus/errors.py | 7 +++++-- pyproject.toml | 2 +- tests/test_errors.py | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 tests/test_errors.py diff --git a/lambda_calculus/__init__.py b/lambda_calculus/__init__.py index 52fab47..ba61366 100644 --- a/lambda_calculus/__init__.py +++ b/lambda_calculus/__init__.py @@ -4,7 +4,7 @@ from .terms import Variable, Abstraction, Application -__version__ = "2.0.0" +__version__ = "2.0.1" __author__ = "Eric Niklas Wolf" __email__ = "eric_niklas.wolf@mailbox.tu-dresden.de" __all__ = ( diff --git a/lambda_calculus/errors.py b/lambda_calculus/errors.py index 3866c07..0f43300 100644 --- a/lambda_calculus/errors.py +++ b/lambda_calculus/errors.py @@ -28,8 +28,11 @@ def __init__(self, message: str, collisions: Collection[V]) -> None: self.collisions = collisions def __repr__(self) -> str: - return f"{self.__class__.__name__}({self.message!r}, {self.collisions!r})" + return ( + f"{self.__class__.__module__}.{self.__class__.__qualname__}" + f"({self.message!r}, {self.collisions!r})" + ) def __str__(self) -> str: collisions = ", ".join(map(str, self.collisions)) - return f"[collisions: {collisions}] {self.message}" + return f"[collisions: {collisions or 'none'}] {self.message}" diff --git a/pyproject.toml b/pyproject.toml index 3f90e65..3a9de30 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "lambda_calculus" -version = "2.0.0" +version = "2.0.1" description = "Implementation of the Lambda calculus" requires-python = ">=3.10" keywords = [] diff --git a/tests/test_errors.py b/tests/test_errors.py new file mode 100644 index 0000000..6da2bbe --- /dev/null +++ b/tests/test_errors.py @@ -0,0 +1,35 @@ +#!/usr/bin/python3 + +"""Tests for custom errors""" + +from unittest import TestCase +from lambda_calculus import errors + + +class CollisionErrorTest(TestCase): + """Tests for Exception thrown when a variable already exists""" + + def test_exception(self) -> None: + """test exception subclass""" + exception: Exception = errors.CollisionError("test", []) + self.assertIsInstance(exception, ValueError) + self.assertEqual(exception.args, ("test", [])) + + def test_repr(self) -> None: + """test string representation""" + exception: Exception = errors.CollisionError("test", []) + self.assertEqual( + repr(exception), + f"lambda_calculus.errors.CollisionError({'test'!r}, {[]!r})" + ) + + def test_str(self) -> None: + """test exception message""" + self.assertEqual( + str(errors.CollisionError("test", [])), + "[collisions: none] test" + ) + self.assertEqual( + str(errors.CollisionError("test", range(3))), + "[collisions: 0, 1, 2] test" + )