|
| 1 | +import unittest |
| 2 | + |
| 3 | +from bot.utils import helpers |
| 4 | + |
| 5 | + |
| 6 | +class TestHelpers(unittest.TestCase): |
| 7 | + """Tests for the helper functions in the `bot.utils.helpers` module.""" |
| 8 | + |
| 9 | + def test_find_nth_occurrence_returns_index(self): |
| 10 | + """Test if `find_nth_occurrence` returns the index correctly when substring is found.""" |
| 11 | + test_values = ( |
| 12 | + ("hello", "l", 1, 2), |
| 13 | + ("hello", "l", 2, 3), |
| 14 | + ("hello world", "world", 1, 6), |
| 15 | + ("hello world", " ", 1, 5), |
| 16 | + ("hello world", "o w", 1, 4) |
| 17 | + ) |
| 18 | + |
| 19 | + for string, substring, n, expected_index in test_values: |
| 20 | + with self.subTest(string=string, substring=substring, n=n): |
| 21 | + index = helpers.find_nth_occurrence(string, substring, n) |
| 22 | + self.assertEqual(index, expected_index) |
| 23 | + |
| 24 | + def test_find_nth_occurrence_returns_none(self): |
| 25 | + """Test if `find_nth_occurrence` returns None when substring is not found.""" |
| 26 | + test_values = ( |
| 27 | + ("hello", "w", 1, None), |
| 28 | + ("hello", "w", 2, None), |
| 29 | + ("hello world", "world", 2, None), |
| 30 | + ("hello world", " ", 2, None), |
| 31 | + ("hello world", "o w", 2, None) |
| 32 | + ) |
| 33 | + |
| 34 | + for string, substring, n, expected_index in test_values: |
| 35 | + with self.subTest(string=string, substring=substring, n=n): |
| 36 | + index = helpers.find_nth_occurrence(string, substring, n) |
| 37 | + self.assertEqual(index, expected_index) |
| 38 | + |
| 39 | + def test_has_lines_handles_normal_cases(self): |
| 40 | + """Test if `has_lines` returns True for strings with at least `count` lines.""" |
| 41 | + test_values = ( |
| 42 | + ("hello\nworld", 1, True), |
| 43 | + ("hello\nworld", 2, True), |
| 44 | + ("hello\nworld", 3, False), |
| 45 | + ) |
| 46 | + |
| 47 | + for string, count, expected in test_values: |
| 48 | + with self.subTest(string=string, count=count): |
| 49 | + result = helpers.has_lines(string, count) |
| 50 | + self.assertEqual(result, expected) |
| 51 | + |
| 52 | + def test_has_lines_handles_empty_string(self): |
| 53 | + """Test if `has_lines` returns False for empty strings.""" |
| 54 | + test_values = ( |
| 55 | + ("", 0, False), |
| 56 | + ("", 1, False), |
| 57 | + ) |
| 58 | + |
| 59 | + for string, count, expected in test_values: |
| 60 | + with self.subTest(string=string, count=count): |
| 61 | + result = helpers.has_lines(string, count) |
| 62 | + self.assertEqual(result, expected) |
| 63 | + |
| 64 | + def test_has_lines_handles_newline_at_end(self): |
| 65 | + """Test if `has_lines` ignores one newline at the end.""" |
| 66 | + test_values = ( |
| 67 | + ("hello\nworld\n", 2, True), |
| 68 | + ("hello\nworld\n", 3, False), |
| 69 | + ("hello\nworld\n\n", 3, True), |
| 70 | + ) |
| 71 | + |
| 72 | + for string, count, expected in test_values: |
| 73 | + with self.subTest(string=string, count=count): |
| 74 | + result = helpers.has_lines(string, count) |
| 75 | + self.assertEqual(result, expected) |
| 76 | + |
| 77 | + def test_pad_base64_correctly(self): |
| 78 | + """Test if `pad_base64` correctly pads a base64 string.""" |
| 79 | + test_values = ( |
| 80 | + ("", ""), |
| 81 | + ("a", "a==="), |
| 82 | + ("aa", "aa=="), |
| 83 | + ("aaa", "aaa="), |
| 84 | + ("aaaa", "aaaa"), |
| 85 | + ("aaaaa", "aaaaa==="), |
| 86 | + ("aaaaaa", "aaaaaa=="), |
| 87 | + ("aaaaaaa", "aaaaaaa=") |
| 88 | + ) |
| 89 | + |
| 90 | + for data, expected in test_values: |
| 91 | + with self.subTest(data=data): |
| 92 | + result = helpers.pad_base64(data) |
| 93 | + self.assertEqual(result, expected) |
| 94 | + |
| 95 | + def test_remove_subdomain_from_url_correctly(self): |
| 96 | + """Test if `remove_subdomain_from_url` correctly removes subdomains from URLs.""" |
| 97 | + test_values = ( |
| 98 | + ("https://example.com", "https://example.com"), |
| 99 | + ("https://www.example.com", "https://example.com"), |
| 100 | + ("https://sub.example.com", "https://example.com"), |
| 101 | + ("https://sub.sub.example.com", "https://example.com"), |
| 102 | + ("https://sub.example.co.uk", "https://example.co.uk"), |
| 103 | + ("https://sub.sub.example.co.uk", "https://example.co.uk"), |
| 104 | + ("https://sub.example.co.uk/path", "https://example.co.uk/path"), |
| 105 | + ("https://sub.sub.example.co.uk/path", "https://example.co.uk/path"), |
| 106 | + ("https://sub.example.co.uk/path?query", "https://example.co.uk/path?query"), |
| 107 | + ("https://sub.sub.example.co.uk/path?query", "https://example.co.uk/path?query"), |
| 108 | + ) |
| 109 | + |
| 110 | + for url, expected in test_values: |
| 111 | + with self.subTest(url=url): |
| 112 | + result = helpers.remove_subdomain_from_url(url) |
| 113 | + self.assertEqual(result, expected) |
0 commit comments