| 1 | import unittest |
| 2 | |
| 3 | import pyglet |
| 4 | import pyglet.text.layout |
| 5 | from pyglet.text import runlist |
| 6 | from pyglet.text.formats.attributed import AttributedTextDecoder |
| 7 | |
| 8 | |
| 9 | class TestStyleRuns(unittest.TestCase): |
| 10 | |
| 11 | def check_value(self, runs, value): |
| 12 | for i, style in enumerate(value): |
| 13 | self.assertTrue(runs[i] == style, repr(runs.runs)) |
| 14 | self.check_optimal(runs) |
| 15 | self.check_continuous(runs) |
| 16 | self.check_iter(runs, value) |
| 17 | self.check_iter_range(runs, value) |
| 18 | |
| 19 | def check_optimal(self, runs): |
| 20 | last_style = None |
| 21 | for _, _, style in runs: |
| 22 | self.assertTrue(style != last_style) |
| 23 | last_style = style |
| 24 | |
| 25 | def check_continuous(self, runs): |
| 26 | next_start = 0 |
| 27 | for start, end, _ in runs: |
| 28 | self.assertTrue(start == next_start) |
| 29 | next_start = end |
| 30 | |
| 31 | def check_iter(self, runs, value): |
| 32 | for start, end, style in runs: |
| 33 | for v in value[start:end]: |
| 34 | self.assertTrue(v == style) |
| 35 | |
| 36 | def check_iter_range(self, runs, value): |
| 37 | for interval in range(1, len(value)): |
| 38 | it = runs.get_run_iterator() |
| 39 | for start in range(0, len(value), interval): |
| 40 | end = min(start + interval, len(value)) |
| 41 | for s, e, style in it.ranges(start, end): |
| 42 | for v in value[s:e]: |
| 43 | self.assertTrue(v == style, (start, end, s, e, style)) |
| 44 | |
| 45 | def check_empty(self, runs, value): |
| 46 | start, end, style = next(iter(runs)) |
| 47 | self.assertTrue(start == 0) |
| 48 | self.assertTrue(end == 0) |
| 49 | self.assertTrue(style == value) |
| 50 | |
| 51 | def test_zero(self): |
| 52 | runs = runlist.RunList(0, 'x') |
| 53 | it = iter(runs) |
| 54 | |
| 55 | start, end, s = next(it) |
| 56 | self.assertTrue(start == 0) |
| 57 | self.assertTrue(end == 0) |
| 58 | self.assertTrue(s == 'x') |
| 59 | |
| 60 | with self.assertRaises(StopIteration): |
| 61 | next(it) |
| 62 | self.check_optimal(runs) |
| 63 | |
| 64 | def test_initial(self): |
| 65 | runs = runlist.RunList(10, 'x') |
| 66 | it = iter(runs) |
| 67 | |
| 68 | start, end, s = next(it) |
| 69 | self.assertTrue(start == 0) |
| 70 | self.assertTrue(end == 10) |
| 71 | self.assertTrue(s == 'x') |
| 72 | |
| 73 | with self.assertRaises(StopIteration): |
| 74 | next(it) |
| 75 | |
| 76 | self.check_value(runs, 'x' * 10) |
| 77 | |
| 78 | def test_set1(self): |
| 79 | runs = runlist.RunList(10, 'a') |
| 80 | runs.set_run(2, 8, 'b') |
| 81 | self.check_value(runs, 'aabbbbbbaa') |
| 82 | |
| 83 | def test_set1_start(self): |
| 84 | runs = runlist.RunList(10, 'a') |
| 85 | runs.set_run(0, 5, 'b') |
| 86 | self.check_value(runs, 'bbbbbaaaaa') |
| 87 | |
| 88 | def test_set1_end(self): |
| 89 | runs = runlist.RunList(10, 'a') |
| 90 | runs.set_run(5, 10, 'b') |
| 91 | self.check_value(runs, 'aaaaabbbbb') |
| 92 | |
| 93 | def test_set1_all(self): |
| 94 | runs = runlist.RunList(10, 'a') |
| 95 | runs.set_run(0, 10, 'b') |
| 96 | self.check_value(runs, 'bbbbbbbbbb') |
| 97 | |
| 98 | def test_set1_1(self): |
| 99 | runs = runlist.RunList(10, 'a') |
| 100 | runs.set_run(1, 2, 'b') |
| 101 | self.check_value(runs, 'abaaaaaaaa') |
| 102 | |
| 103 | def test_set_overlapped(self): |
| 104 | runs = runlist.RunList(10, 'a') |
| 105 | runs.set_run(0, 5, 'b') |
| 106 | self.check_value(runs, 'bbbbbaaaaa') |
| 107 | runs.set_run(5, 10, 'c') |
| 108 | self.check_value(runs, 'bbbbbccccc') |
| 109 | runs.set_run(3, 7, 'd') |
| 110 | self.check_value(runs, 'bbbddddccc') |
| 111 | runs.set_run(4, 6, 'e') |
| 112 | self.check_value(runs, 'bbbdeedccc') |
| 113 | runs.set_run(5, 9, 'f') |
| 114 | self.check_value(runs, 'bbbdeffffc') |
| 115 | runs.set_run(2, 3, 'g') |
| 116 | self.check_value(runs, 'bbgdeffffc') |
| 117 | runs.set_run(1, 3, 'h') |
| 118 | self.check_value(runs, 'bhhdeffffc') |
| 119 | runs.set_run(1, 9, 'i') |
| 120 | self.check_value(runs, 'biiiiiiiic') |
| 121 | runs.set_run(0, 10, 'j') |
| 122 | self.check_value(runs, 'jjjjjjjjjj') |
| 123 | |
| 124 | def test_insert_empty(self): |
| 125 | runs = runlist.RunList(0, 'a') |
| 126 | runs.insert(0, 10) |
| 127 | self.check_value(runs, 'aaaaaaaaaa') |
| 128 | |
| 129 | def test_insert_beginning(self): |
| 130 | runs = runlist.RunList(5, 'a') |
| 131 | runs.set_run(1, 4, 'b') |
| 132 | self.check_value(runs, 'abbba') |
| 133 | runs.insert(0, 3) |
| 134 | self.check_value(runs, 'aaaabbba') |
| 135 | |
| 136 | def test_insert_beginning_1(self): |
| 137 | runs = runlist.RunList(5, 'a') |
| 138 | self.check_value(runs, 'aaaaa') |
| 139 | runs.insert(0, 1) |
| 140 | runs.set_run(0, 1, 'a') |
| 141 | self.check_value(runs, 'aaaaaa') |
| 142 | runs.insert(0, 1) |
| 143 | runs.set_run(0, 1, 'a') |
| 144 | self.check_value(runs, 'aaaaaaa') |
| 145 | runs.insert(0, 1) |
| 146 | runs.set_run(0, 1, 'a') |
| 147 | self.check_value(runs, 'aaaaaaaa') |
| 148 | |
| 149 | def test_insert_beginning_2(self): |
| 150 | runs = runlist.RunList(5, 'a') |
| 151 | self.check_value(runs, 'aaaaa') |
| 152 | runs.insert(0, 1) |
| 153 | runs.set_run(0, 1, 'b') |
| 154 | self.check_value(runs, 'baaaaa') |
| 155 | runs.insert(0, 1) |
| 156 | runs.set_run(0, 1, 'c') |
| 157 | self.check_value(runs, 'cbaaaaa') |
| 158 | runs.insert(0, 1) |
| 159 | runs.set_run(0, 1, 'c') |
| 160 | self.check_value(runs, 'ccbaaaaa') |
| 161 | |
| 162 | def test_insert_1(self): |
| 163 | runs = runlist.RunList(5, 'a') |
| 164 | runs.set_run(1, 4, 'b') |
| 165 | self.check_value(runs, 'abbba') |
| 166 | runs.insert(1, 3) |
| 167 | self.check_value(runs, 'aaaabbba') |
| 168 | |
| 169 | def test_insert_2(self): |
| 170 | runs = runlist.RunList(5, 'a') |
| 171 | runs.set_run(1, 2, 'b') |
| 172 | self.check_value(runs, 'abaaa') |
| 173 | runs.insert(2, 3) |
| 174 | self.check_value(runs, 'abbbbaaa') |
| 175 | |
| 176 | def test_insert_end(self): |
| 177 | runs = runlist.RunList(5, 'a') |
| 178 | runs.set_run(4, 5, 'b') |
| 179 | self.check_value(runs, 'aaaab') |
| 180 | runs.insert(5, 3) |
| 181 | self.check_value(runs, 'aaaabbbb') |
| 182 | |
| 183 | def test_insert_almost_end(self): |
| 184 | runs = runlist.RunList(5, 'a') |
| 185 | runs.set_run(0, 3, 'b') |
| 186 | runs.set_run(4, 5, 'c') |
| 187 | self.check_value(runs, 'bbbac') |
| 188 | runs.insert(4, 3) |
| 189 | self.check_value(runs, 'bbbaaaac') |
| 190 | |
| 191 | def test_delete_1_beginning(self): |
| 192 | runs = runlist.RunList(5, 'a') |
| 193 | self.check_value(runs, 'aaaaa') |
| 194 | runs.delete(0, 3) |
| 195 | self.check_value(runs, 'aa') |
| 196 | |
| 197 | def test_delete_1_middle(self): |
| 198 | runs = runlist.RunList(5, 'a') |
| 199 | self.check_value(runs, 'aaaaa') |
| 200 | runs.delete(1, 4) |
| 201 | self.check_value(runs, 'aa') |
| 202 | |
| 203 | def test_delete_1_end(self): |
| 204 | runs = runlist.RunList(5, 'a') |
| 205 | self.check_value(runs, 'aaaaa') |
| 206 | runs.delete(2, 5) |
| 207 | self.check_value(runs, 'aa') |
| 208 | |
| 209 | def test_delete_1_all(self): |
| 210 | runs = runlist.RunList(5, 'a') |
| 211 | self.check_value(runs, 'aaaaa') |
| 212 | runs.delete(0, 5) |
| 213 | self.check_value(runs, '') |
| 214 | self.check_empty(runs, 'a') |
| 215 | |
| 216 | def create_runs1(self): |
| 217 | runs = runlist.RunList(10, 'a') |
| 218 | runs.set_run(1, 10, 'b') |
| 219 | runs.set_run(2, 10, 'c') |
| 220 | runs.set_run(3, 10, 'd') |
| 221 | runs.set_run(4, 10, 'e') |
| 222 | runs.set_run(5, 10, 'f') |
| 223 | runs.set_run(6, 10, 'g') |
| 224 | runs.set_run(7, 10, 'h') |
| 225 | runs.set_run(8, 10, 'i') |
| 226 | runs.set_run(9, 10, 'j') |
| 227 | self.check_value(runs, 'abcdefghij') |
| 228 | return runs |
| 229 | |
| 230 | def create_runs2(self): |
| 231 | runs = runlist.RunList(10, 'a') |
| 232 | runs.set_run(4, 7, 'b') |
| 233 | runs.set_run(7, 10, 'c') |
| 234 | self.check_value(runs, 'aaaabbbccc') |
| 235 | return runs |
| 236 | |
| 237 | def test_delete2(self): |
| 238 | runs = self.create_runs1() |
| 239 | runs.delete(0, 5) |
| 240 | self.check_value(runs, 'fghij') |
| 241 | |
| 242 | def test_delete3(self): |
| 243 | runs = self.create_runs1() |
| 244 | runs.delete(2, 8) |
| 245 | self.check_value(runs, 'abij') |
| 246 | |
| 247 | def test_delete4(self): |
| 248 | runs = self.create_runs2() |
| 249 | runs.delete(0, 5) |
| 250 | self.check_value(runs, 'bbccc') |
| 251 | |
| 252 | def test_delete5(self): |
| 253 | runs = self.create_runs2() |
| 254 | runs.delete(5, 10) |
| 255 | self.check_value(runs, 'aaaab') |
| 256 | |
| 257 | def test_delete6(self): |
| 258 | runs = self.create_runs2() |
| 259 | runs.delete(0, 8) |
| 260 | self.check_value(runs, 'cc') |
| 261 | |
| 262 | def test_delete7(self): |
| 263 | runs = self.create_runs2() |
| 264 | runs.delete(2, 10) |
| 265 | self.check_value(runs, 'aa') |
| 266 | |
| 267 | def test_delete8(self): |
| 268 | runs = self.create_runs2() |
| 269 | runs.delete(3, 8) |
| 270 | self.check_value(runs, 'aaacc') |
| 271 | |
| 272 | def test_delete9(self): |
| 273 | runs = self.create_runs2() |
| 274 | runs.delete(7, 8) |
| 275 | self.check_value(runs, 'aaaabbbcc') |
| 276 | |
| 277 | def test_delete10(self): |
| 278 | runs = self.create_runs2() |
| 279 | runs.delete(8, 9) |
| 280 | self.check_value(runs, 'aaaabbbcc') |
| 281 | |
| 282 | def test_delete11(self): |
| 283 | runs = self.create_runs2() |
| 284 | runs.delete(9, 10) |
| 285 | self.check_value(runs, 'aaaabbbcc') |
| 286 | |
| 287 | def test_delete12(self): |
| 288 | runs = self.create_runs2() |
| 289 | runs.delete(4, 5) |
| 290 | self.check_value(runs, 'aaaabbccc') |
| 291 | |
| 292 | def test_delete13(self): |
| 293 | runs = self.create_runs2() |
| 294 | runs.delete(5, 6) |
| 295 | self.check_value(runs, 'aaaabbccc') |
| 296 | |
| 297 | def test_delete14(self): |
| 298 | runs = self.create_runs2() |
| 299 | runs.delete(6, 7) |
| 300 | self.check_value(runs, 'aaaabbccc') |
| 301 | |
| 302 | |
| 303 | class TestIssues(unittest.TestCase): |
| 304 | |
| 305 | def test_issue471(self): |
| 306 | doc = pyglet.text.document.FormattedDocument() |
| 307 | layout = pyglet.text.layout.IncrementalTextLayout(doc, 100, 100, width=500, height=100) |
| 308 | doc.insert_text(0, "hello", {'bold': True}) |
| 309 | doc.text = "" |
| 310 | |
| 311 | def test_issue471_comment2(self): |
| 312 | doc2 = pyglet.text.decode_attributed('{bold True}a') |
| 313 | layout = pyglet.text.layout.IncrementalTextLayout(doc2, 100, 10, width=500, height=100) |
| 314 | layout.document.delete_text(0, len(layout.document.text)) |
| 315 | |
| 316 | def test_issue241_comment4a(self): |
| 317 | document = pyglet.text.document.FormattedDocument("") |
| 318 | layout = pyglet.text.layout.IncrementalTextLayout(document, 50, 50, width=500, height=100) |
| 319 | document.set_style(0, len(document.text), {"font_name": "Arial"}) |
| 320 | |
| 321 | def test_issue241_comment4b(self): |
| 322 | document = pyglet.text.document.FormattedDocument("test") |
| 323 | layout = pyglet.text.layout.IncrementalTextLayout(document, 50, 50, width=500, height=100) |
| 324 | document.set_style(0, len(document.text), {"font_name": "Arial"}) |
| 325 | document.delete_text(0, len(document.text)) |
| 326 | |
| 327 | def test_issue241_comment5(self): |
| 328 | document = pyglet.text.document.FormattedDocument('A') |
| 329 | document.set_style(0, 1, dict(bold=True)) |
| 330 | layout = pyglet.text.layout.IncrementalTextLayout(document, 100, 100, width=500, height=100) |
| 331 | document.delete_text(0, 1) |
| 332 | |
| 333 | def test_issue429_comment4a(self): |
| 334 | doc = pyglet.text.decode_attributed( |
| 335 | '{bold True}Hello{bold False}\n\n\n\n') |
| 336 | doc2 = pyglet.text.decode_attributed( |
| 337 | '{bold True}Goodbye{bold False}\n\n\n\n') |
| 338 | layout = pyglet.text.layout.IncrementalTextLayout(doc, 100, 10, width=500, height=100) |
| 339 | layout.document = doc2 |
| 340 | layout.document.delete_text(0, len(layout.document.text)) |
| 341 | |
| 342 | def test_issue429_comment4b(self): |
| 343 | doc2 = pyglet.text.decode_attributed('{bold True}a{bold False}b') |
| 344 | layout = pyglet.text.layout.IncrementalTextLayout(doc2, 100, 10, width=500, height=100) |
| 345 | layout.document.delete_text(0, len(layout.document.text)) |
| 346 | |
| 347 | |
| 348 | class AttributedTextDecoderTests(unittest.TestCase): |
| 349 | |
| 350 | def testOneNewlineBecomesSpace(self): |
| 351 | doc = AttributedTextDecoder().decode('one\ntwo') |
| 352 | self.assertEqual('one two', doc.text) |
| 353 | |
| 354 | def testTwoNewlinesBecomesParagraph(self): |
| 355 | from pyglet.text.formats.attributed import AttributedTextDecoder |
| 356 | |
| 357 | doc = AttributedTextDecoder().decode('one\n\ntwo') |
| 358 | self.assertEqual('one\ntwo', doc.text) |