|
| 1 | +@testable import SwiftlyCore |
| 2 | +import Testing |
| 3 | +import XCTest |
| 4 | + |
| 5 | +@Suite struct StringExtensionsTests { |
| 6 | + @Test("Basic text wrapping at column width") |
| 7 | + func testBasicWrapping() { |
| 8 | + let input = "This is a simple test string that should be wrapped at the specified width." |
| 9 | + let expected = """ |
| 10 | + This is a |
| 11 | + simple test |
| 12 | + string that |
| 13 | + should be |
| 14 | + wrapped at |
| 15 | + the |
| 16 | + specified |
| 17 | + width. |
| 18 | + """ |
| 19 | + |
| 20 | + XCTAssertEqual(input.wrapText(to: 10), expected) |
| 21 | + } |
| 22 | + |
| 23 | + @Test("Preserve existing line breaks") |
| 24 | + func testPreserveLineBreaks() { |
| 25 | + let input = "First line\nSecond line\nThird line" |
| 26 | + let expected = "First line\nSecond line\nThird line" |
| 27 | + |
| 28 | + XCTAssertEqual(input.wrapText(to: 20), expected) |
| 29 | + } |
| 30 | + |
| 31 | + @Test("Combine wrapping with existing line breaks") |
| 32 | + func testCombineWrappingAndLineBreaks() { |
| 33 | + let input = "Short line\nThis is a very long line that needs to be wrapped\nAnother short line" |
| 34 | + let expected = """ |
| 35 | + Short line |
| 36 | + This is a very |
| 37 | + long line that |
| 38 | + needs to be |
| 39 | + wrapped |
| 40 | + Another short line |
| 41 | + """ |
| 42 | + |
| 43 | + XCTAssertEqual(input.wrapText(to: 15), expected) |
| 44 | + } |
| 45 | + |
| 46 | + @Test("Words longer than column width") |
| 47 | + func testLongWords() { |
| 48 | + let input = "This has a supercalifragilisticexpialidocious word" |
| 49 | + let expected = """ |
| 50 | + This has a |
| 51 | + supercalifragilisticexpialidocious |
| 52 | + word |
| 53 | + """ |
| 54 | + |
| 55 | + XCTAssertEqual(input.wrapText(to: 10), expected) |
| 56 | + } |
| 57 | + |
| 58 | + @Test("Text with no spaces") |
| 59 | + func testNoSpaces() { |
| 60 | + let input = "ThisIsALongStringWithNoSpaces" |
| 61 | + let expected = "ThisIsALongStringWithNoSpaces" |
| 62 | + |
| 63 | + XCTAssertEqual(input.wrapText(to: 10), expected) |
| 64 | + } |
| 65 | + |
| 66 | + @Test("Empty string") |
| 67 | + func testEmptyString() { |
| 68 | + let input = "" |
| 69 | + let expected = "" |
| 70 | + |
| 71 | + XCTAssertEqual(input.wrapText(to: 10), expected) |
| 72 | + } |
| 73 | + |
| 74 | + @Test("Single character") |
| 75 | + func testSingleCharacter() { |
| 76 | + let input = "X" |
| 77 | + let expected = "X" |
| 78 | + |
| 79 | + XCTAssertEqual(input.wrapText(to: 10), expected) |
| 80 | + } |
| 81 | + |
| 82 | + @Test("Single line not exceeding width") |
| 83 | + func testSingleLineNoWrapping() { |
| 84 | + let input = "Short text" |
| 85 | + let expected = "Short text" |
| 86 | + |
| 87 | + XCTAssertEqual(input.wrapText(to: 10), expected) |
| 88 | + } |
| 89 | + |
| 90 | + @Test("Wrapping with indentation") |
| 91 | + func testWrappingWithIndent() { |
| 92 | + let input = "This is text that should be wrapped with indentation on new lines." |
| 93 | + let expected = """ |
| 94 | + This is |
| 95 | + text that |
| 96 | + should be |
| 97 | + wrapped |
| 98 | + with |
| 99 | + indentation |
| 100 | + on new |
| 101 | + lines. |
| 102 | + """ |
| 103 | + |
| 104 | + XCTAssertEqual(input.wrapText(to: 10, wrappingIndent: 2), expected) |
| 105 | + } |
| 106 | + |
| 107 | + @Test("Zero or negative column width") |
| 108 | + func testZeroOrNegativeWidth() { |
| 109 | + let input = "This should not be wrapped" |
| 110 | + |
| 111 | + XCTAssertEqual(input.wrapText(to: 0), input) |
| 112 | + XCTAssertEqual(input.wrapText(to: -5), input) |
| 113 | + } |
| 114 | + |
| 115 | + @Test("Very narrow column width") |
| 116 | + func testVeryNarrowWidth() { |
| 117 | + let input = "A B C" |
| 118 | + let expected = "A\nB\nC" |
| 119 | + |
| 120 | + XCTAssertEqual(input.wrapText(to: 1), expected) |
| 121 | + } |
| 122 | + |
| 123 | + @Test("Special characters") |
| 124 | + func testSpecialCharacters() { |
| 125 | + let input = "Special !@#$%^&*() chars" |
| 126 | + let expected = """ |
| 127 | + Special |
| 128 | + !@#$%^&*() |
| 129 | + chars |
| 130 | + """ |
| 131 | + |
| 132 | + XCTAssertEqual(input.wrapText(to: 10), expected) |
| 133 | + } |
| 134 | + |
| 135 | + @Test("Unicode characters") |
| 136 | + func testUnicodeCharacters() { |
| 137 | + let input = "Unicode: 你好世界 😀🚀🌍" |
| 138 | + let expected = """ |
| 139 | + Unicode: 你好世界 |
| 140 | + 😀🚀🌍 |
| 141 | + """ |
| 142 | + |
| 143 | + XCTAssertEqual(input.wrapText(to: 15), expected) |
| 144 | + } |
| 145 | + |
| 146 | + @Test("Irregular spacing") |
| 147 | + func testIrregularSpacing() { |
| 148 | + let input = "Words with irregular spacing" |
| 149 | + let expected = """ |
| 150 | + Words with |
| 151 | + irregular |
| 152 | + spacing |
| 153 | + """ |
| 154 | + |
| 155 | + XCTAssertEqual(input.wrapText(to: 10), expected) |
| 156 | + } |
| 157 | + |
| 158 | + @Test("Tab characters") |
| 159 | + func testTabCharacters() { |
| 160 | + let input = "Text\twith\ttabs" |
| 161 | + let expected = """ |
| 162 | + Text\twith |
| 163 | + \ttabs |
| 164 | + """ |
| 165 | + |
| 166 | + XCTAssertEqual(input.wrapText(to: 10), expected) |
| 167 | + } |
| 168 | + |
| 169 | + @Test("Trailing spaces") |
| 170 | + func testTrailingSpaces() { |
| 171 | + let input = "Text with trailing spaces " |
| 172 | + let expected = """ |
| 173 | + Text with |
| 174 | + trailing |
| 175 | + spaces |
| 176 | + """ |
| 177 | + |
| 178 | + XCTAssertEqual(input.wrapText(to: 10), expected) |
| 179 | + } |
| 180 | + |
| 181 | + @Test("Leading spaces") |
| 182 | + func testLeadingSpaces() { |
| 183 | + let input = " Leading spaces with text" |
| 184 | + let expected = """ |
| 185 | + Leading |
| 186 | + spaces with |
| 187 | + text |
| 188 | + """ |
| 189 | + |
| 190 | + XCTAssertEqual(input.wrapText(to: 10), expected) |
| 191 | + } |
| 192 | + |
| 193 | + @Test("Multiple consecutive newlines") |
| 194 | + func testMultipleNewlines() { |
| 195 | + let input = "First\n\nSecond\n\n\nThird" |
| 196 | + let expected = "First\n\nSecond\n\n\nThird" |
| 197 | + |
| 198 | + XCTAssertEqual(input.wrapText(to: 10), expected) |
| 199 | + } |
| 200 | + |
| 201 | + @Test("Edge case - exactly at column width") |
| 202 | + func testExactColumnWidth() { |
| 203 | + let input = "1234567890 abcdefghij" |
| 204 | + let expected = "1234567890\nabcdefghij" |
| 205 | + |
| 206 | + XCTAssertEqual(input.wrapText(to: 10), expected) |
| 207 | + } |
| 208 | +} |
0 commit comments