Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
76 lines
2.4 KiB
Python
76 lines
2.4 KiB
Python
import unittest
|
|
|
|
from textkit import (
|
|
count_words,
|
|
snake_case,
|
|
title_case,
|
|
top_words,
|
|
truncate_words,
|
|
word_frequencies,
|
|
wrap_text,
|
|
)
|
|
|
|
|
|
class HiddenWrap(unittest.TestCase):
|
|
def test_long_word_exact_multiple(self):
|
|
self.assertEqual(wrap_text("abcdefgh", 4), ["abcd", "efgh"])
|
|
|
|
def test_width_one(self):
|
|
self.assertEqual(wrap_text("ab c", 1), ["a", "b", "c"])
|
|
|
|
def test_long_word_no_characters_lost(self):
|
|
word = "supercalifragilisticexpialidocious"
|
|
for width in range(1, 12):
|
|
lines = wrap_text(word, width)
|
|
self.assertEqual("".join(lines), word, width)
|
|
self.assertTrue(all(len(line) <= width for line in lines), width)
|
|
|
|
def test_long_word_tail_joins_next_word(self):
|
|
self.assertEqual(wrap_text("abcdefg hi", 5), ["abcde", "fg hi"])
|
|
|
|
def test_text_is_preserved_word_for_word(self):
|
|
text = "pack my box with five dozen liquor jugs and then some more words to wrap"
|
|
for width in (6, 8, 13, 40):
|
|
lines = wrap_text(text, width)
|
|
self.assertEqual(" ".join(lines).split(), text.split(), width)
|
|
self.assertTrue(all(len(line) <= width for line in lines), width)
|
|
|
|
|
|
class HiddenWords(unittest.TestCase):
|
|
def test_limit_one(self):
|
|
self.assertEqual(truncate_words("alpha beta gamma", 1), "alpha...")
|
|
|
|
def test_custom_suffix(self):
|
|
self.assertEqual(truncate_words("a b c d e f", 4, suffix=" [more]"), "a b c d [more]")
|
|
|
|
def test_empty_suffix(self):
|
|
self.assertEqual(truncate_words("a b c d", 2, suffix=""), "a b")
|
|
|
|
def test_exactly_at_limit(self):
|
|
self.assertEqual(truncate_words(" a b ", 2), "a b")
|
|
|
|
def test_one_over_limit(self):
|
|
self.assertEqual(truncate_words("a b c", 2), "a b...")
|
|
|
|
def test_empty_text(self):
|
|
self.assertEqual(truncate_words("", 3), "")
|
|
|
|
|
|
class HiddenUnchanged(unittest.TestCase):
|
|
def test_count_words(self):
|
|
self.assertEqual(count_words("a\tb\nc "), 3)
|
|
|
|
def test_title_case(self):
|
|
self.assertEqual(title_case("war and PEACE"), "War and Peace")
|
|
|
|
def test_snake_case(self):
|
|
self.assertEqual(snake_case("HTTPServer error-code"), "httpserver_error_code")
|
|
|
|
def test_stats(self):
|
|
self.assertEqual(word_frequencies("a A b"), {"a": 2, "b": 1})
|
|
self.assertEqual(top_words("x y y z z z", 2), [("z", 3), ("y", 2)])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|