Skip to content

Commit

Permalink
improve CollisionError string representations
Browse files Browse the repository at this point in the history
  • Loading branch information
Deric-W committed Sep 4, 2022
1 parent 2714e32 commit 05180e8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lambda_calculus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__ = (
Expand Down
7 changes: 5 additions & 2 deletions lambda_calculus/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 = []
Expand Down
35 changes: 35 additions & 0 deletions tests/test_errors.py
Original file line number Diff line number Diff line change
@@ -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"
)

0 comments on commit 05180e8

Please sign in to comment.