From d754f916e7ecfa8bd66a9bfe8338f31f33287f86 Mon Sep 17 00:00:00 2001 From: faissaloux Date: Fri, 1 Dec 2023 03:05:32 +0100 Subject: [PATCH] PrinterValidator --- termspark/termspark.py | 29 ++++-------------------- termspark/validators/__init__.py | 0 termspark/validators/printerValidator.py | 13 +++++++++++ tests/printer_validaor_test.py | 22 ++++++++++++++++++ 4 files changed, 39 insertions(+), 25 deletions(-) create mode 100644 termspark/validators/__init__.py create mode 100644 termspark/validators/printerValidator.py create mode 100644 tests/printer_validaor_test.py diff --git a/termspark/termspark.py b/termspark/termspark.py index cd8c55b..bf28fb2 100644 --- a/termspark/termspark.py +++ b/termspark/termspark.py @@ -7,10 +7,10 @@ from .exceptions.lenNotSupportedException import LenNotSupportedException from .exceptions.minNotReachedException import MinNotReachedException from .exceptions.multiplePositionsNotSupported import MultiplePositionsNotSupported -from .exceptions.printerArgException import PrinterArgException from .helpers.existenceChecker import ExistenceChecker from .painter.painter import Painter from .structurer.structurer import Structurer +from .validators.printerValidator import PrinterValidator class TermSpark: @@ -45,14 +45,7 @@ def __init__(self): def print_left( self, content: str, color: Optional[str] = None, highlight: Optional[str] = None ): - if any( - [ - isinstance(content, list), - isinstance(color, list), - isinstance(highlight, list), - ] - ): - raise PrinterArgException("left") + PrinterValidator().validate(content, color, highlight) self.spark_left([content, color, highlight]) # type: ignore @@ -61,14 +54,7 @@ def print_left( def print_right( self, content: str, color: Optional[str] = None, highlight: Optional[str] = None ): - if any( - [ - isinstance(content, list), - isinstance(color, list), - isinstance(highlight, list), - ] - ): - raise PrinterArgException("right") + PrinterValidator().validate(content, color, highlight) self.spark_right([content, color, highlight]) # type: ignore @@ -77,14 +63,7 @@ def print_right( def print_center( self, content: str, color: Optional[str] = None, highlight: Optional[str] = None ): - if any( - [ - isinstance(content, list), - isinstance(color, list), - isinstance(highlight, list), - ] - ): - raise PrinterArgException("center") + PrinterValidator().validate(content, color, highlight) self.spark_center([content, color, highlight]) # type: ignore diff --git a/termspark/validators/__init__.py b/termspark/validators/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/termspark/validators/printerValidator.py b/termspark/validators/printerValidator.py new file mode 100644 index 0000000..0cfb7d5 --- /dev/null +++ b/termspark/validators/printerValidator.py @@ -0,0 +1,13 @@ +from ..exceptions.printerArgException import PrinterArgException + + +class PrinterValidator: + def validate(self, content, color, highlight): + if any( + [ + isinstance(content, list), + isinstance(color, list), + isinstance(highlight, list), + ] + ): + raise PrinterArgException("left") diff --git a/tests/printer_validaor_test.py b/tests/printer_validaor_test.py new file mode 100644 index 0000000..af2f860 --- /dev/null +++ b/tests/printer_validaor_test.py @@ -0,0 +1,22 @@ +import pytest + +from termspark.exceptions.printerArgException import PrinterArgException +from termspark.validators.printerValidator import PrinterValidator + + +class TestPrinterValidator: + def test_validate_content(self): + with pytest.raises(PrinterArgException): + PrinterValidator().validate(["termspark"], "black", "green") + + def test_validate_color(self): + with pytest.raises(PrinterArgException): + PrinterValidator().validate("termspark", ["black"], "green") + + def test_validate_highlight(self): + with pytest.raises(PrinterArgException): + PrinterValidator().validate("termspark", "black", ["green"]) + + def test_validate_all(self): + with pytest.raises(PrinterArgException): + PrinterValidator().validate(["termspark"], ["black"], ["green"])