Skip to content

Commit

Permalink
fix set separator without content in line
Browse files Browse the repository at this point in the history
  • Loading branch information
faissaloux committed Dec 1, 2023
1 parent c116724 commit b09b232
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 47 deletions.
11 changes: 11 additions & 0 deletions termspark/exceptions/emptyException.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import termspark.termspark


class EmptyException(Exception):
def __str__(self):
message = termspark.TermSpark().print_left(
"can't be empty! Set a line or fill content.",
"red",
)

return str(message)
99 changes: 52 additions & 47 deletions termspark/termspark.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Dict, List, Optional

from .exceptions.combinationException import CombinationException
from .exceptions.emptyException import EmptyException
from .exceptions.lenNotSupportedException import LenNotSupportedException
from .exceptions.maxLenNotSupported import MaxLenNotSupported
from .exceptions.minNotReachedException import MinNotReachedException
Expand Down Expand Up @@ -319,64 +320,68 @@ def render(self) -> str:
if self.mode == "color":
self.paint_separator()

if self.line_is_set and self.separator_is_set:
raise CombinationException("line", "separator")

if (
not any(
[
"content" in self.left,
"content" in self.right,
"content" in self.center,
]
)
and not self.line_is_set
):
raise EmptyException

half_separator_length = int(self.separator["length"]) // 2
separator_mid_width = self.separator["content"] * half_separator_length
separator_painted_mid_width = (
self.separator["painted_content"] * half_separator_length
)

if self.line_is_set and self.separator_is_set:
raise CombinationException("line", "separator")

if (
"content" in self.left
or "content" in self.right
or "content" in self.center
or self.line_is_set
):
center_content = ExistenceChecker().dictionary_key(self.center, "content")
if len(center_content) > 0:
if self.mode == "raw":
center = (
separator_mid_width + self.center["content"]
if isinstance(self.center["content"], str)
else " ".join(center_content) + separator_mid_width
)
else:
center = (
separator_painted_mid_width
+ self.center["painted_content"]
+ separator_painted_mid_width
)
center_content = ExistenceChecker().dictionary_key(self.center, "content")
if len(center_content) > 0:
if self.mode == "raw":
center = (
separator_mid_width + self.center["content"]
if isinstance(self.center["content"], str)
else " ".join(center_content) + separator_mid_width
)
else:
center = self.separator["painted_content"] * int(
self.separator["length"]
center = (
separator_painted_mid_width
+ self.center["painted_content"]
+ separator_painted_mid_width
)
else:
center = self.separator["painted_content"] * int(self.separator["length"])

if self.mode == "raw":
left_content = ExistenceChecker().dictionary_key(self.left, "content")
right_content = ExistenceChecker().dictionary_key(self.right, "content")

if len(left_content) > 0:
left_content = (
self.left["content"]
if isinstance(self.left["content"], str)
else " ".join(left_content)
)

if len(right_content) > 0:
right_content = (
self.right["content"]
if isinstance(self.right["content"], str)
else " ".join(right_content)
)
else:
left_content = ExistenceChecker().dictionary_key(
self.left, "painted_content"
if self.mode == "raw":
left_content = ExistenceChecker().dictionary_key(self.left, "content")
right_content = ExistenceChecker().dictionary_key(self.right, "content")

if len(left_content) > 0:
left_content = (
self.left["content"]
if isinstance(self.left["content"], str)
else " ".join(left_content)
)
right_content = ExistenceChecker().dictionary_key(
self.right, "painted_content"

if len(right_content) > 0:
right_content = (
self.right["content"]
if isinstance(self.right["content"], str)
else " ".join(right_content)
)
else:
left_content = ExistenceChecker().dictionary_key(
self.left, "painted_content"
)
right_content = ExistenceChecker().dictionary_key(
self.right, "painted_content"
)

return left_content + center + right_content

Expand Down
14 changes: 14 additions & 0 deletions tests/empty_exception_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from termspark.exceptions.emptyException import EmptyException
from termspark.painter.constants.fore import Fore


class TestEmptyException:
def test_exception_message(self):
exception = EmptyException()
assert all(
word in str(exception)
for word in [
"can't be empty! Set a line or fill content.",
str(Fore.RED),
]
)
14 changes: 14 additions & 0 deletions tests/termspark_return_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from termspark.exceptions.combinationException import CombinationException
from termspark.exceptions.emptyException import EmptyException
from termspark.termspark import TermSpark


Expand Down Expand Up @@ -305,3 +306,16 @@ def test_cant_combine_line_with_separator(self):

with pytest.raises(CombinationException):
termspark.spark()

def test_cant_spark_without_content(self):
termspark = TermSpark()

with pytest.raises(EmptyException):
termspark.spark()

def test_cant_spark_separator_without_content_in_line(self):
termspark = TermSpark()
termspark.set_separator(".")

with pytest.raises(EmptyException):
termspark.spark()

0 comments on commit b09b232

Please sign in to comment.