Skip to content

Commit

Permalink
Using new utils interface in tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimdeekepler committed Mar 12, 2024
1 parent 044f2c4 commit 23bfc9e
Showing 1 changed file with 62 additions and 17 deletions.
79 changes: 62 additions & 17 deletions tests/utils_tests.py
Original file line number Diff line number Diff line change
@@ -1,83 +1,124 @@
"""Unittests for utils class."""
import configparser
import unittest
from scrolltext.utils import CharacterScroller, parse_int


class CharacterScrollTests(unittest.TestCase):
"""Test cases for CharacterScroller class"""
cfg = configparser.ConfigParser()
cfg.read_dict({
"main": {"action": "linescroller"},
"cursestext": {"box": "1"},
"scrolltext.text 1": {
"direction": "0",
"text": "Hello, world",
"line": "0",
"speed": "0",
}
})

argv = {
"test": True,
"term_rows": 20,
"min_scroll_line": 0
# "term_columns": 1, # NOTE: explicitly set in each test-case
}

def test_invalid_params1(self):
""""Test creation of CharacterScroller with no parameters."""
with self.assertRaises(Exception):
CharacterScroller()
argv = {}
CharacterScroller(self.cfg, **argv)

def test_dunno_visible_text_length_is_zero(self):
""""Test with visibile window size set to 0."""
cnt = 0
scroll_text = "Hello, world"
for text in CharacterScroller(0, 1, scroll_text, 0, 0):
self.argv["term_columns"] = 0
self.argv["blanks"] = 1
for text in CharacterScroller(self.cfg, **self.argv):
self.assertEqual("", text)
cnt += 1
self.assertEqual(cnt, len(scroll_text) + 2)
if cnt > 20:
raise RuntimeError("exceeded")
self.assertEqual(cnt, len(self.cfg["scrolltext.text 1"]["text"]) + 2)

def test_scroll_character_for_character(self):
""""Test with visibile window size set to 1, or character by
character respectively."""
self.argv["term_columns"] = 1
self.argv["blanks"] = 0
scroll_text = "Hello, world"
expected = list(scroll_text)
self.cfg["scrolltext.text 1"]["text"] = scroll_text
self.cfg["scrolltext.text 1"]["direction"] = "0"
expected = list(self.cfg["scrolltext.text 1"]["text"])
cnt = 0
for text in CharacterScroller(1, 0, scroll_text, 0, 0):
for text in CharacterScroller(self.cfg, **self.argv):
self.assertEqual(expected[cnt], text)
cnt += 1
self.assertEqual(cnt, len(scroll_text))
self.assertEqual(cnt, len(self.cfg["scrolltext.text 1"]["text"]))

def test_scroll_character_for_character_with_spaces(self):
""""Test with visibile window size set to 1, and a leading
and trailing blank character."""
self.argv["term_columns"] = 1
self.argv["blanks"] = 1
scroll_text = "Hello, world"
self.cfg["scrolltext.text 1"]["text"] = scroll_text
self.cfg["scrolltext.text 1"]["direction"] = "0"
scroll_text2 = " " + scroll_text + " "
expected = list(scroll_text2)
cnt = 0
for text in CharacterScroller(1, 1, scroll_text, 0, 0):
for text in CharacterScroller(self.cfg, **self.argv):
self.assertEqual(expected[cnt], text)
cnt += 1
self.assertEqual(cnt, len(scroll_text) + 2)

def test_scroll_two_characters_with_spaces(self):
""""Test with visibile window size set to two, or two chars at a time."""
scroll_text = "Hello, world"
self.cfg["scrolltext.text 1"]["text"] = scroll_text
self.cfg["scrolltext.text 1"]["direction"] = "0"
self.argv["term_columns"] = 2
self.argv["blanks"] = 1
expected = [" H", "He", "el", "ll", "lo", "o,", ", ", " w", "wo", "or",
"rl", "ld", "d ", " "]
cnt = 0
for text in CharacterScroller(2, 1, scroll_text, 0, 0):
for text in CharacterScroller(self.cfg, **self.argv):
self.assertEqual(expected[cnt], text)
cnt += 1
self.assertEqual(cnt, len(scroll_text) + 2)

def test_scroll_in_broad_window_with_spaces(self):
""""Test with a much larger window size, than the given text."""
scroll_text = "Hello, world"
self.cfg["scrolltext.text 1"]["text"] = scroll_text
self.cfg["scrolltext.text 1"]["direction"] = "0"
expected = [" Hello, world ", "Hello, world ", "ello, world ", "llo, world ",
"lo, world ", "o, world ", ", world ", " world ", "world ",
"orld ", "rld ", "ld ", "d ", " "]
self.argv["term_columns"] = 80
self.argv["blanks"] = 1
cnt = 0
for text in CharacterScroller(80, 1, scroll_text, 0, 0):
for text in CharacterScroller(self.cfg, **self.argv):
self.assertEqual(expected[cnt], text)
cnt += 1
self.assertEqual(cnt, len(scroll_text) + 2)

def test_scroll_character_for_character_right_to_left2(self):
""""Right-to-Left text. Test with visibile window size set to 1, or character by
character respectively."""
scroll_text_list = list("Hello, world")
scroll_text = "Hello, world"
self.cfg["scrolltext.text 1"]["text"] = scroll_text
self.cfg["scrolltext.text 1"]["direction"] = "1"
self.argv["term_columns"] = 1
self.argv["blanks"] = 0
scroll_text_list = list(self.cfg["scrolltext.text 1"]["text"])
scroll_text_list.reverse()
scroll_text = "".join(scroll_text_list)
print("orig", scroll_text)
expected = list(scroll_text)
expected.reverse()
print("copy", expected)
cnt = 0
for text in CharacterScroller(1, 0, scroll_text, 1, 0):
for text in CharacterScroller(self.cfg, **self.argv): # 1, 0, scroll_text, 1, 0):
print("round", str(cnt))
try:
self.assertEqual(expected[cnt], text)
Expand All @@ -89,11 +130,15 @@ def test_scroll_character_for_character_right_to_left2(self):
def test_scroll_character_for_character_right_to_left(self):
""""Right-to-Left text. Test with visibile window size set to 1, or character by
character respectively."""
self.cfg["scrolltext.text 1"]["direction"] = "1"
self.argv["term_columns"] = 1
self.argv["blanks"] = 0
scroll_text = "مرحباً فيلت"
self.cfg["scrolltext.text 1"]["text"] = scroll_text
expected = list(scroll_text)
expected.reverse()
cnt = 0
for text in CharacterScroller(1, 0, scroll_text, 1, 0):
for text in CharacterScroller(self.cfg, **self.argv): # 1, 0, scroll_text, 1, 0):
print("round", str(cnt), " ", text)
try:
self.assertEqual(expected[cnt], text)
Expand All @@ -103,7 +148,7 @@ def test_scroll_character_for_character_right_to_left(self):
self.assertTrue(((cnt + len(scroll_text)) // 2) == len(scroll_text))


class ParseIntTests(unittest.TestCase):
class ParseIntTests(unittest.TestCase): # x xx maybe remove
"""Test cases for parse int utility"""
def test_parse_none_returns_0(self):
"""Given None returns 0"""
Expand Down

0 comments on commit 23bfc9e

Please sign in to comment.