diff options
author | 2025-02-16 13:40:26 +0000 | |
---|---|---|
committer | 2025-02-16 13:40:26 +0000 | |
commit | f00ad300608c9ec4e7691dcf34e34902d437e980 (patch) | |
tree | 1e989913da2dec89f0e52083225a8bc2c98f89aa | |
parent | Merge pull request #3259 from bast0006/bast0006-textless-remindme (diff) | |
parent | test: add test cases for `bot/utils/helpers.py` (diff) |
Merge pull request #3260 from dd2480-spring-2025-group-1/kim/test-cases-for-bot-utils-helpers
fix: `has_lines` returns empty str instead of bool
-rw-r--r-- | bot/utils/helpers.py | 2 | ||||
-rw-r--r-- | tests/bot/utils/test_helpers.py | 113 |
2 files changed, 114 insertions, 1 deletions
diff --git a/bot/utils/helpers.py b/bot/utils/helpers.py index 7b5474a2f..4a85f46f5 100644 --- a/bot/utils/helpers.py +++ b/bot/utils/helpers.py @@ -25,7 +25,7 @@ def has_lines(string: str, count: int) -> bool: split = string.split("\n", count - 1) # Make sure the last part isn't empty, which would happen if there was a final newline. - return split[-1] and len(split) == count + return split[-1] != "" and len(split) == count def pad_base64(data: str) -> str: diff --git a/tests/bot/utils/test_helpers.py b/tests/bot/utils/test_helpers.py new file mode 100644 index 000000000..e8ab6ba80 --- /dev/null +++ b/tests/bot/utils/test_helpers.py @@ -0,0 +1,113 @@ +import unittest + +from bot.utils import helpers + + +class TestHelpers(unittest.TestCase): + """Tests for the helper functions in the `bot.utils.helpers` module.""" + + def test_find_nth_occurrence_returns_index(self): + """Test if `find_nth_occurrence` returns the index correctly when substring is found.""" + test_values = ( + ("hello", "l", 1, 2), + ("hello", "l", 2, 3), + ("hello world", "world", 1, 6), + ("hello world", " ", 1, 5), + ("hello world", "o w", 1, 4) + ) + + for string, substring, n, expected_index in test_values: + with self.subTest(string=string, substring=substring, n=n): + index = helpers.find_nth_occurrence(string, substring, n) + self.assertEqual(index, expected_index) + + def test_find_nth_occurrence_returns_none(self): + """Test if `find_nth_occurrence` returns None when substring is not found.""" + test_values = ( + ("hello", "w", 1, None), + ("hello", "w", 2, None), + ("hello world", "world", 2, None), + ("hello world", " ", 2, None), + ("hello world", "o w", 2, None) + ) + + for string, substring, n, expected_index in test_values: + with self.subTest(string=string, substring=substring, n=n): + index = helpers.find_nth_occurrence(string, substring, n) + self.assertEqual(index, expected_index) + + def test_has_lines_handles_normal_cases(self): + """Test if `has_lines` returns True for strings with at least `count` lines.""" + test_values = ( + ("hello\nworld", 1, True), + ("hello\nworld", 2, True), + ("hello\nworld", 3, False), + ) + + for string, count, expected in test_values: + with self.subTest(string=string, count=count): + result = helpers.has_lines(string, count) + self.assertEqual(result, expected) + + def test_has_lines_handles_empty_string(self): + """Test if `has_lines` returns False for empty strings.""" + test_values = ( + ("", 0, False), + ("", 1, False), + ) + + for string, count, expected in test_values: + with self.subTest(string=string, count=count): + result = helpers.has_lines(string, count) + self.assertEqual(result, expected) + + def test_has_lines_handles_newline_at_end(self): + """Test if `has_lines` ignores one newline at the end.""" + test_values = ( + ("hello\nworld\n", 2, True), + ("hello\nworld\n", 3, False), + ("hello\nworld\n\n", 3, True), + ) + + for string, count, expected in test_values: + with self.subTest(string=string, count=count): + result = helpers.has_lines(string, count) + self.assertEqual(result, expected) + + def test_pad_base64_correctly(self): + """Test if `pad_base64` correctly pads a base64 string.""" + test_values = ( + ("", ""), + ("a", "a==="), + ("aa", "aa=="), + ("aaa", "aaa="), + ("aaaa", "aaaa"), + ("aaaaa", "aaaaa==="), + ("aaaaaa", "aaaaaa=="), + ("aaaaaaa", "aaaaaaa=") + ) + + for data, expected in test_values: + with self.subTest(data=data): + result = helpers.pad_base64(data) + self.assertEqual(result, expected) + + def test_remove_subdomain_from_url_correctly(self): + """Test if `remove_subdomain_from_url` correctly removes subdomains from URLs.""" + test_values = ( + ("https://example.com", "https://example.com"), + ("https://www.example.com", "https://example.com"), + ("https://sub.example.com", "https://example.com"), + ("https://sub.sub.example.com", "https://example.com"), + ("https://sub.example.co.uk", "https://example.co.uk"), + ("https://sub.sub.example.co.uk", "https://example.co.uk"), + ("https://sub.example.co.uk/path", "https://example.co.uk/path"), + ("https://sub.sub.example.co.uk/path", "https://example.co.uk/path"), + ("https://sub.example.co.uk/path?query", "https://example.co.uk/path?query"), + ("https://sub.sub.example.co.uk/path?query", "https://example.co.uk/path?query"), + ) + + for url, expected in test_values: + with self.subTest(url=url): + result = helpers.remove_subdomain_from_url(url) + self.assertEqual(result, expected) |