-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTextEditManagerSpanMergeTest.kt
More file actions
338 lines (283 loc) · 9.27 KB
/
TextEditManagerSpanMergeTest.kt
File metadata and controls
338 lines (283 loc) · 9.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package texteditmanager
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextDecoration
import com.darkrockstudios.texteditor.state.SpanManager
import com.darkrockstudios.texteditor.utils.mergeAnnotatedStrings
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
class TextEditManagerSpanMergeTest {
private lateinit var manager: SpanManager
@Before
fun setup() {
manager = SpanManager()
}
@Test
fun `test insert text with no spans into unstyled text`() {
// Arrange
val original = AnnotatedString("Hello World")
val newText = AnnotatedString("new ")
val insertionIndex = 6
// Act
val result =
manager.mergeAnnotatedStrings(original, start = insertionIndex, newText = newText)
// Assert
assertEquals("Hello new World", result.text)
assertTrue(result.spanStyles.isEmpty())
}
@Test
fun `test insert unstyled text into styled text`() {
// Arrange
val original = buildAnnotatedString {
append("Hello World")
addStyle(SpanStyle(color = Color.Red), 0, 11)
}
val newText = AnnotatedString("new ")
val insertionIndex = 6
// Act
val result =
manager.mergeAnnotatedStrings(original, start = insertionIndex, newText = newText)
// Assert
assertEquals("Hello new World", result.text)
assertEquals(1, result.spanStyles.size)
val span = result.spanStyles[0]
assertEquals(0, span.start)
assertEquals(15, span.end)
assertEquals(Color.Red, (span.item as SpanStyle).color)
}
@Test
fun `test insert styled text into unstyled text`() {
// Arrange
val original = AnnotatedString("Hello World")
val newText = buildAnnotatedString {
append("new ")
addStyle(SpanStyle(color = Color.Blue), 0, 4)
}
val insertionIndex = 6
// Act
val result =
manager.mergeAnnotatedStrings(original, start = insertionIndex, newText = newText)
// Assert
assertEquals("Hello new World", result.text)
assertEquals(1, result.spanStyles.size)
val span = result.spanStyles[0]
assertEquals(6, span.start)
assertEquals(10, span.end)
assertEquals(Color.Blue, (span.item as SpanStyle).color)
}
@Test
fun `test insert styled text into differently styled text`() {
// Arrange
val original = buildAnnotatedString {
append("Hello World")
addStyle(SpanStyle(color = Color.Red), 0, 11)
}
val newText = buildAnnotatedString {
append("new ")
addStyle(SpanStyle(color = Color.Blue), 0, 4)
}
val insertionIndex = 6
// Act
val result =
manager.mergeAnnotatedStrings(original, start = insertionIndex, newText = newText)
// Assert
assertEquals("Hello new World", result.text)
assertEquals(2, result.spanStyles.size)
with(result.spanStyles) {
val redSpan = first { it.start == 0 && it.end == 15 }
assertEquals(Color.Red, (redSpan.item as SpanStyle).color)
val blueSpan = first { it.start == 6 && it.end == 10 }
assertEquals(Color.Blue, (blueSpan.item as SpanStyle).color)
}
}
@Test
fun `test insert text at boundary of styled region`() {
// Arrange
val original = buildAnnotatedString {
append("Hello World")
addStyle(SpanStyle(color = Color.Red), 0, 5)
}
val newText = AnnotatedString("nice ")
val insertionIndex = 5
// Act
val result =
manager.mergeAnnotatedStrings(original, start = insertionIndex, newText = newText)
// Assert
assertEquals("Hellonice World", result.text)
assertEquals(1, result.spanStyles.size)
val span = result.spanStyles[0]
assertEquals(0, span.start)
assertEquals(5, span.end)
assertEquals(Color.Red, (span.item as SpanStyle).color)
}
@Test
fun `test merge adjacent identical styles`() {
// Arrange
val original = buildAnnotatedString {
append("Hello World")
addStyle(SpanStyle(color = Color.Red), 0, 5)
}
val newText = buildAnnotatedString {
append(" new")
addStyle(SpanStyle(color = Color.Red), 0, 4)
}
val insertionIndex = 5
// Act
val result =
manager.mergeAnnotatedStrings(original, start = insertionIndex, newText = newText)
// Assert
assertEquals("Hello new World", result.text)
assertEquals(1, result.spanStyles.size)
val span = result.spanStyles[0]
assertEquals(0, span.start)
assertEquals(9, span.end)
assertEquals(Color.Red, (span.item as SpanStyle).color)
}
@Test
fun `test insert at start of text`() {
// Arrange
val original = buildAnnotatedString {
append("Hello")
addStyle(SpanStyle(color = Color.Red), 0, 5)
}
val newText = buildAnnotatedString {
append("Hey ")
addStyle(SpanStyle(color = Color.Blue), 0, 4)
}
val insertionIndex = 0
// Act
val result =
manager.mergeAnnotatedStrings(original, start = insertionIndex, newText = newText)
// Assert
assertEquals("Hey Hello", result.text)
assertEquals(2, result.spanStyles.size)
with(result.spanStyles) {
val blueSpan = first { it.start == 0 }
assertEquals(0, blueSpan.start)
assertEquals(4, blueSpan.end)
assertEquals(Color.Blue, (blueSpan.item as SpanStyle).color)
val redSpan = first { it.start == 4 }
assertEquals(4, redSpan.start)
assertEquals(9, redSpan.end)
assertEquals(Color.Red, (redSpan.item as SpanStyle).color)
}
}
@Test
fun `test insert at end of text`() {
// Arrange
val original = buildAnnotatedString {
append("Hello")
addStyle(SpanStyle(color = Color.Red), 0, 5)
}
val newText = buildAnnotatedString {
append(" World")
addStyle(SpanStyle(color = Color.Blue), 0, 6)
}
val insertionIndex = 5
// Act
val result =
manager.mergeAnnotatedStrings(original, start = insertionIndex, newText = newText)
// Assert
assertEquals("Hello World", result.text)
assertEquals(2, result.spanStyles.size)
with(result.spanStyles) {
val redSpan = first { it.start == 0 }
assertEquals(0, redSpan.start)
assertEquals(5, redSpan.end)
assertEquals(Color.Red, (redSpan.item as SpanStyle).color)
val blueSpan = first { it.start == 5 }
assertEquals(5, blueSpan.start)
assertEquals(11, blueSpan.end)
assertEquals(Color.Blue, (blueSpan.item as SpanStyle).color)
}
}
@Test
fun `test delete text with spans`() {
// Arrange
val original = buildAnnotatedString {
append("Hello World")
addStyle(SpanStyle(color = Color.Red), 0, 5) // "Hello"
addStyle(SpanStyle(color = Color.Blue), 6, 11) // "World"
}
val startIndex = 3
val endIndex = 8
// Act
val result = manager.mergeAnnotatedStrings(original, start = startIndex, end = endIndex)
// Assert
assertEquals("Helrld", result.text)
assertEquals(2, result.spanStyles.size)
val redSpan = result.spanStyles.first()
assertEquals(0, redSpan.start)
assertEquals(3, redSpan.end)
assertEquals(Color.Red, redSpan.item.color)
val blueSpan = result.spanStyles.last()
assertEquals(3, blueSpan.start)
assertEquals(6, blueSpan.end)
assertEquals(Color.Blue, blueSpan.item.color)
}
@Test
fun `test replace text with overlapping spans`() {
// Arrange
val original = buildAnnotatedString {
append("Hello World")
addStyle(SpanStyle(color = Color.Red), 0, 5) // "Hello"
addStyle(SpanStyle(color = Color.Blue), 6, 11) // "World"
}
val newText = buildAnnotatedString {
append("Beautiful")
addStyle(SpanStyle(color = Color.Green), 0, 9) // "Beautiful"
}
val startIndex = 3
val endIndex = 8
// Act
val result = manager.mergeAnnotatedStrings(
original,
start = startIndex,
end = endIndex,
newText = newText
)
// Assert
assertEquals("HelBeautifulrld", result.text)
assertEquals(3, result.spanStyles.size)
val redSpan = result.spanStyles.first { it.start == 0 && it.end == 3 }
assertEquals(Color.Red, redSpan.item.color)
val greenSpan = result.spanStyles.first { it.start == 3 && it.end == 12 }
assertEquals(Color.Green, greenSpan.item.color)
val blueSpan = result.spanStyles.first { it.start == 12 && it.end == 15 }
assertEquals(Color.Blue, blueSpan.item.color)
}
@Test
fun `test insert character into overlapping styled text - bold and underline`() {
// Arrange - create text with overlapping bold and underline spans
val original = buildAnnotatedString {
append("Welcome")
addStyle(SpanStyle(fontWeight = FontWeight.Bold), 0, 7) // bold
addStyle(SpanStyle(textDecoration = TextDecoration.Underline), 0, 7) // underline
}
// Simulate inserting "a" at position 3 (between "l" and "c")
val newText = buildAnnotatedString {
append("a")
addStyle(SpanStyle(fontWeight = FontWeight.Bold), 0, 1) // bold
addStyle(SpanStyle(textDecoration = TextDecoration.Underline), 0, 1) // underline
}
val insertionIndex = 3
// Act
val result = manager.mergeAnnotatedStrings(original, start = insertionIndex, newText = newText)
// Assert
assertEquals("Welacome", result.text)
// Should have 2 spans: bold(0-8) and underline(0-8)
assertEquals(2, result.spanStyles.size)
val boldSpan = result.spanStyles.find { it.item.fontWeight == FontWeight.Bold }
val underlineSpan = result.spanStyles.find { it.item.textDecoration == TextDecoration.Underline }
assertNotNull(boldSpan)
assertNotNull(underlineSpan)
boldSpan?.let { assertEquals(0, it.start); assertEquals(8, it.end) }
underlineSpan?.let { assertEquals(0, it.start); assertEquals(8, it.end) }
}
}