From 673f214e32e84d5963b61ef58171fee978ce2672 Mon Sep 17 00:00:00 2001 From: Brian Pugh Date: Sat, 21 May 2022 07:52:36 -0700 Subject: [PATCH] Add tests explicitly for ABC usage --- tests/test_abc.py | 84 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 tests/test_abc.py diff --git a/tests/test_abc.py b/tests/test_abc.py new file mode 100644 index 0000000..02348b9 --- /dev/null +++ b/tests/test_abc.py @@ -0,0 +1,84 @@ +from abc import ABC, abstractmethod + +import pytest + +from autoregistry import Registry + + +def test_abc_undefined_abstractmethod(): + class Pokemon(Registry): + @abstractmethod + def attack(self): + pass + + class Charmander(Pokemon): + def attack(self): + return 1 + + class Pikachu(Pokemon): + pass + + with pytest.raises(TypeError): + Pokemon() + + with pytest.raises(TypeError): + Pokemon["pikachu"]() + + with pytest.raises(TypeError): + Pikachu() + + Charmander() + + +def test_abc_multiple_inheritence_first(): + """The programmer doesn't know inheritting from ABC is superfluous.""" + + class Pokemon(ABC, Registry): + @abstractmethod + def attack(self): + pass + + class Charmander(Pokemon): + def attack(self): + return 1 + + class Pikachu(Pokemon): + pass + + with pytest.raises(TypeError): + Pokemon() + + with pytest.raises(TypeError): + Pokemon["pikachu"]() + + with pytest.raises(TypeError): + Pikachu() + + Charmander() + + +def test_abc_multiple_inheritence_last(): + """The programmer doesn't know inheritting from ABC is superfluous.""" + + class Pokemon(Registry, ABC): + @abstractmethod + def attack(self): + pass + + class Charmander(Pokemon): + def attack(self): + return 1 + + class Pikachu(Pokemon): + pass + + with pytest.raises(TypeError): + Pokemon() + + with pytest.raises(TypeError): + Pokemon["pikachu"]() + + with pytest.raises(TypeError): + Pikachu() + + Charmander()