package txt import ( "testing" "github.com/stretchr/testify/require" ) func TestPositiveI64ToStr(t *testing.T) { r := require.New(t) s := I64(1) r.Equal("1", s) } func TestNegativeI64ToStr(t *testing.T) { r := require.New(t) s := I64(-1) r.Equal("-1", s) } func TestZeroI64ToStr(t *testing.T) { r := require.New(t) s := I64(0) r.Equal("0", s) } func TestUcfirst(t *testing.T) { r := require.New(t) res := Ucfirst("abc") r.Equal("Abc", res) } func TestUcfirstRu(t *testing.T) { r := require.New(t) res := Ucfirst("абв") r.Equal("Абв", res) } func TestLcfirst(t *testing.T) { r := require.New(t) res := Lcfirst("ABC") r.Equal("aBC", res) } func TestLcfirstRu(t *testing.T) { r := require.New(t) res := Lcfirst("АБВ") r.Equal("аБВ", res) } func TestInsertExtAfterHeader(t *testing.T) { r := require.New(t) content := AddHeaderAndText("#### 1 January 1970, Thursday\nExisting\ncontent", "#### 1 January 1970, Thursday", "New\ncontent") r.Equal("#### 1 January 1970, Thursday\nExisting\ncontent\nNew\ncontent", content) } func TestInsertTextAfterHeaderNoHeader(t *testing.T) { r := require.New(t) content := AddHeaderAndText("### header 1\nitem1\nitem2", "### header 5", "new item") r.Equal("### header 5\nnew item\n\n### header 1\nitem1\nitem2", content) } func TestInsertTextAfterHeaderAtEnd(t *testing.T) { r := require.New(t) content := AddHeaderAndText("### header 1\nitem1\nitem2\n### header 2", "### header 1", "new item") r.Equal("### header 1\nitem1\nitem2\nnew item\n\n### header 2", content) } func TestInsertTextAfterHeaderInMiddle(t *testing.T) { r := require.New(t) content := AddHeaderAndText("### header 0\n### header 1\nitem1\nitem2\n### header 2", "### header 1", "new item") r.Equal("### header 0\n### header 1\nitem1\nitem2\nnew item\n\n### header 2", content) } func TestInsertTextAfterHeaderWithOnlyHeader(t *testing.T) { r := require.New(t) content := AddHeaderAndText("### header 0\n### header 1\n### header 2", "### header 1", "new item") r.Equal("### header 0\n### header 1\nnew item\n\n### header 2", content) } func TestInsertTextAfterHeaderAtVeryEnd(t *testing.T) { r := require.New(t) content := AddHeaderAndText("### header 1\nitem1\nitem2", "### header 1", "new item") r.Equal("### header 1\nitem1\nitem2\nnew item", content) } func TestInsertTextAfterNoHeader(t *testing.T) { r := require.New(t) content := AddHeaderAndText("item1\nitem2", "### header 1", "new item") r.Equal("### header 1\nnew item\n\nitem1\nitem2", content) } func TestSplitTextIntoChunks(t *testing.T) { tests := []struct { name string input string maxLen int expected []string }{ { name: "basic split with spaces", input: "This is a test to check the splitting of text", maxLen: 10, expected: []string{"This is a", "test to", "check the", "splitting", "of text"}, }, { name: "split with newlines", input: "Line one\nLine two\nLine three", maxLen: 15, expected: []string{"Line one", "Line two", "Line three"}, }, { name: "long string without spaces", input: "supercalifragilisticexpialidocious", maxLen: 10, expected: []string{"supercalif", "ragilistic", "expialidoc", "ious"}, }, { name: "exact match", input: "ExactMatch", maxLen: 10, expected: []string{"ExactMatch"}, }, { name: "trailing and leading spaces", input: " Leading and trailing spaces ", maxLen: 15, expected: []string{"Leading and", "trailing spaces"}, }, { name: "no split needed", input: "Short text", maxLen: 50, expected: []string{"Short text"}, }, { name: "empty string", input: "", maxLen: 10, expected: []string{""}, }, { name: "string with only spaces", input: " ", maxLen: 10, expected: []string{""}, }, { name: "string with only spaces", input: "aaa", maxLen: 2, expected: []string{"aa", "a"}, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { res := SplitTextIntoChunks(tt.input, tt.maxLen) require.Equal(t, tt.expected, res) }) } } func TestNormNewLines(t *testing.T) { // Initialize the test cases testCases := []struct { input string expectedOutput string }{ // Test Case 1: Windows-style line endings {input: "Hello\r\nWorld", expectedOutput: "Hello\nWorld"}, // Test Case 2: Mac OS line endings {input: "Line1\rLine2\nLine3", expectedOutput: "Line1\nLine2\nLine3"}, // Test Case 3: Unix-style line endings (no change expected) {input: "This\nis\na\ntest", expectedOutput: "This\nis\na\ntest"}, // Test Case 4: No line endings {input: "NoLineEndingsHere", expectedOutput: "NoLineEndingsHere"}, } for _, tc := range testCases { output := NormNewLines(tc.input) require.Equal(t, tc.expectedOutput, output, "Input: %s", tc.input) } } func TestEmoji(t *testing.T) { // Test with a valid emoji res := Emoji("😊", "Hello") require.Equal(t, "😊 Hello", res) // Test with an empty emoji (should just return the string) res = Emoji("", "Hello") require.Equal(t, "Hello", res) // Test with a different emoji res = Emoji("🎉", "Congratulations") require.Equal(t, "🎉 Congratulations", res) // Test with an empty string for str res = Emoji("🎉", "") require.Equal(t, "🎉 ", res) // Test with both emoji and str empty res = Emoji("", "") require.Equal(t, "", res) } func TestSubstr(t *testing.T) { // Basic test case require.Equal(t, "ello", Substr("hello", 1, 4), "Should return 'ello'") // Test start index beyond string length require.Equal(t, "", Substr("hello", 10, 2), "Should return empty string for out-of-bound start index") // Test length exceeding string length require.Equal(t, "hello", Substr("hello", 0, 10), "Should return entire string when length exceeds") // Test empty string require.Equal(t, "", Substr("", 0, 1), "Should return empty string when input is empty") // Test Unicode characters (basic) require.Equal(t, "界", Substr("世界", 1, 1), "Should return '界' for unicode characters") // Test handling of emoji (simple) require.Equal(t, "👋", Substr("👋🌍", 0, 1), "Should return first emoji '👋'") // Test handling of emoji with skin tone (skin tone modifier counts as 2 codepoints) require.Equal(t, "👋🏻", Substr("👋🏻🌍", 0, 2), "Should return first emoji with skin tone modifier '👋🏻'") // Test for slicing emoji across boundaries (shouldn't break) require.Equal(t, "👋🏻", Substr("👋🏻🌍", 0, 2), "Should return the entire emoji with modifier '👋🏻'") // Test when slicing exceeds bounds with emoji and unicode characters require.Equal(t, "👋🏻🌍", Substr("👋🏻🌍", 0, 5), "Should return the whole string with emoji and modifier") // Test invalid range where length is 0 require.Equal(t, "", Substr("hello", 0, 0), "Should return an empty string when length is 0") } func TestFirstWord(t *testing.T) { // Basic test case require.Equal(t, "Hello", FirstWord("Hello world!"), "Should return 'Hello' as the first word") // Test with leading spaces require.Equal(t, "Hello", FirstWord(" Hello world!"), "Should trim leading spaces and return 'Hello'") // Test with trailing spaces require.Equal(t, "Hello", FirstWord("Hello world! "), "Should trim trailing spaces and return 'Hello'") // Test with punctuation require.Equal(t, "Hello", FirstWord("Hello, world!"), "Should return 'Hello' ignoring punctuation") // Test empty string require.Equal(t, "", FirstWord(""), "Should return an empty string for an empty input") // Test with only spaces require.Equal(t, "", FirstWord(" "), "Should return an empty string for spaces only") // Test with only punctuation require.Equal(t, "", FirstWord(",.!?"), "Should return an empty string for only punctuation") // Test with multiple words and punctuation require.Equal(t, "This", FirstWord("This is a sentence!"), "Should return 'This'") // Test with non-ASCII characters require.Equal(t, "первое", FirstWord("первое слово"), "Should return the first non-ASCII word 'первое'") // Test with hyphenated word require.Equal(t, "Hello", FirstWord("Hello-world!"), "Should return 'Hello' before the hyphen") } func TestEscapeHTML(t *testing.T) { // Basic test case require.Equal(t, "<div>", EscapeHTML("
"), "Should escape '<' and '>' into '<' and '>'") // Test escaping ampersand require.Equal(t, "&hello&", EscapeHTML("&hello&"), "Should escape '&' into '&'") // Test escaping mixed characters require.Equal(t, "<div> & text", EscapeHTML("
& text"), "Should escape both '<', '>' and '&'") // Test string without special characters require.Equal(t, "Hello World", EscapeHTML("Hello World"), "Should return the same string if no HTML special characters") // Test string with all special characters require.Equal(t, "&<>", EscapeHTML("&<>"), "Should escape '&', '<', and '>'") // Test empty string require.Equal(t, "", EscapeHTML(""), "Should return an empty string when input is empty") // Test string with multiple occurrences of the same special characters require.Equal(t, "<<<tag>>>", EscapeHTML("<<>>"), "Should escape all occurrences of '<' and '>'") } func TestStripHTMLTags(t *testing.T) { // Basic test case require.Equal(t, "Hello World", StripHTMLTags("
Hello World
"), "Should strip basic HTML tags") // Test with multiple tags require.Equal(t, "Hello World", StripHTMLTags("

Hello

World
"), "Should strip multiple HTML tags") // Test with self-closing tags require.Equal(t, "Hello", StripHTMLTags("Hello"), "Should strip self-closing tags") // Test with no HTML tags require.Equal(t, "Plain text", StripHTMLTags("Plain text"), "Should return the same string if no HTML tags are present") // Test with empty string require.Equal(t, "", StripHTMLTags(""), "Should return an empty string when input is empty") // Test with tag attributes require.Equal(t, "Hello", StripHTMLTags("Hello"), "Should strip tags and their attributes") // Test with nested tags require.Equal(t, "Nested tags", StripHTMLTags("
Nested tags
"), "Should strip nested HTML tags") // Test with special characters require.Equal(t, "1 > 0", StripHTMLTags("1 > 0"), "Should retain special characters like '>' when not in a tag") // Test with incomplete tags require.Equal(t, "Text with