1"""Testsuite for TokenRewriteStream class.""" 2 3# don't care about docstrings 4# pylint: disable-msg=C0111 5 6import unittest 7import antlr3 8import testbase 9 10class T1(testbase.ANTLRTest): 11 def setUp(self): 12 self.compileGrammar() 13 14 15 def _parse(self, input): 16 cStream = antlr3.StringStream(input) 17 lexer = self.getLexer(cStream) 18 tStream = antlr3.TokenRewriteStream(lexer) 19 tStream.fillBuffer() 20 21 return tStream 22 23 24 def testInsertBeforeIndex0(self): 25 tokens = self._parse("abc") 26 tokens.insertBefore(0, "0") 27 28 result = tokens.toString() 29 expecting = "0abc" 30 self.assertEqual(result, expecting) 31 32 33 def testInsertAfterLastIndex(self): 34 tokens = self._parse("abc") 35 tokens.insertAfter(2, "x") 36 37 result = tokens.toString() 38 expecting = "abcx" 39 self.assertEqual(result, expecting) 40 41 42 def test2InsertBeforeAfterMiddleIndex(self): 43 tokens = self._parse("abc") 44 tokens.insertBefore(1, "x") 45 tokens.insertAfter(1, "x") 46 47 result = tokens.toString() 48 expecting = "axbxc" 49 self.assertEqual(result, expecting) 50 51 52 def testReplaceIndex0(self): 53 tokens = self._parse("abc") 54 tokens.replace(0, "x") 55 56 result = tokens.toString() 57 expecting = "xbc" 58 self.assertEqual(result, expecting) 59 60 61 def testReplaceLastIndex(self): 62 tokens = self._parse("abc") 63 tokens.replace(2, "x") 64 65 result = tokens.toString() 66 expecting = "abx" 67 self.assertEqual(result, expecting) 68 69 70 def testReplaceMiddleIndex(self): 71 tokens = self._parse("abc") 72 tokens.replace(1, "x") 73 74 result = tokens.toString() 75 expecting = "axc" 76 self.assertEqual(result, expecting) 77 78 79 def test2ReplaceMiddleIndex(self): 80 tokens = self._parse("abc") 81 tokens.replace(1, "x") 82 tokens.replace(1, "y") 83 84 result = tokens.toString() 85 expecting = "ayc" 86 self.assertEqual(result, expecting) 87 88 89 def test2ReplaceMiddleIndex1InsertBefore(self): 90 tokens = self._parse("abc") 91 tokens.insertBefore(0, "_") 92 tokens.replace(1, "x") 93 tokens.replace(1, "y") 94 95 result = tokens.toString() 96 expecting = "_ayc" 97 self.assertEqual(expecting, result) 98 99 100 def testReplaceThenDeleteMiddleIndex(self): 101 tokens = self._parse("abc") 102 tokens.replace(1, "x") 103 tokens.delete(1) 104 105 result = tokens.toString() 106 expecting = "ac" 107 self.assertEqual(result, expecting) 108 109 110 def testInsertInPriorReplace(self): 111 tokens = self._parse("abc") 112 tokens.replace(0, 2, "x") 113 tokens.insertBefore(1, "0") 114 self.assertRaisesRegex( 115 ValueError, 116 (r'insert op <InsertBeforeOp@1:"0"> within boundaries of ' 117 r'previous <ReplaceOp@0\.\.2:"x">'), 118 tokens.toString) 119 120 def testInsertThenReplaceSameIndex(self): 121 tokens = self._parse("abc") 122 tokens.insertBefore(0, "0") 123 tokens.replace(0, "x") # supercedes insert at 0 124 125 result = tokens.toString() 126 expecting = "0xbc" 127 self.assertEqual(result, expecting) 128 129 130 def test2InsertMiddleIndex(self): 131 tokens = self._parse("abc") 132 tokens.insertBefore(1, "x") 133 tokens.insertBefore(1, "y") 134 135 result = tokens.toString() 136 expecting = "ayxbc" 137 self.assertEqual(result, expecting) 138 139 140 def test2InsertThenReplaceIndex0(self): 141 tokens = self._parse("abc") 142 tokens.insertBefore(0, "x") 143 tokens.insertBefore(0, "y") 144 tokens.replace(0, "z") 145 146 result = tokens.toString() 147 expecting = "yxzbc" 148 self.assertEqual(result, expecting) 149 150 151 def testReplaceThenInsertBeforeLastIndex(self): 152 tokens = self._parse("abc") 153 tokens.replace(2, "x") 154 tokens.insertBefore(2, "y") 155 156 result = tokens.toString() 157 expecting = "abyx" 158 self.assertEqual(result, expecting) 159 160 161 def testInsertThenReplaceLastIndex(self): 162 tokens = self._parse("abc") 163 tokens.insertBefore(2, "y") 164 tokens.replace(2, "x") 165 166 result = tokens.toString() 167 expecting = "abyx" 168 self.assertEqual(result, expecting) 169 170 171 def testReplaceThenInsertAfterLastIndex(self): 172 tokens = self._parse("abc") 173 tokens.replace(2, "x") 174 tokens.insertAfter(2, "y") 175 176 result = tokens.toString() 177 expecting = "abxy" 178 self.assertEqual(result, expecting) 179 180 181 def testReplaceRangeThenInsertAtLeftEdge(self): 182 tokens = self._parse("abcccba") 183 tokens.replace(2, 4, "x") 184 tokens.insertBefore(2, "y") 185 186 result = tokens.toString() 187 expecting = "abyxba" 188 self.assertEqual(result, expecting) 189 190 191 def testReplaceRangeThenInsertAtRightEdge(self): 192 tokens = self._parse("abcccba") 193 tokens.replace(2, 4, "x") 194 tokens.insertBefore(4, "y") # no effect; within range of a replace 195 196 self.assertRaisesRegex( 197 ValueError, 198 (r'insert op <InsertBeforeOp@4:"y"> within boundaries of ' 199 r'previous <ReplaceOp@2\.\.4:"x">'), 200 tokens.toString) 201 202 203 def testReplaceRangeThenInsertAfterRightEdge(self): 204 tokens = self._parse("abcccba") 205 tokens.replace(2, 4, "x") 206 tokens.insertAfter(4, "y") 207 208 result = tokens.toString() 209 expecting = "abxyba" 210 self.assertEqual(result, expecting) 211 212 213 def testReplaceAll(self): 214 tokens = self._parse("abcccba") 215 tokens.replace(0, 6, "x") 216 217 result = tokens.toString() 218 expecting = "x" 219 self.assertEqual(result, expecting) 220 221 222 def testReplaceSubsetThenFetch(self): 223 tokens = self._parse("abcccba") 224 tokens.replace(2, 4, "xyz") 225 226 result = tokens.toString(0, 6) 227 expecting = "abxyzba" 228 self.assertEqual(result, expecting) 229 230 231 def testReplaceThenReplaceSuperset(self): 232 tokens = self._parse("abcccba") 233 tokens.replace(2, 4, "xyz") 234 tokens.replace(3, 5, "foo") # overlaps, error 235 236 self.assertRaisesRegex( 237 ValueError, 238 (r'replace op boundaries of <ReplaceOp@3\.\.5:"foo"> overlap ' 239 r'with previous <ReplaceOp@2\.\.4:"xyz">'), 240 tokens.toString) 241 242 243 def testReplaceThenReplaceLowerIndexedSuperset(self): 244 tokens = self._parse("abcccba") 245 tokens.replace(2, 4, "xyz") 246 tokens.replace(1, 3, "foo") # overlap, error 247 248 self.assertRaisesRegex( 249 ValueError, 250 (r'replace op boundaries of <ReplaceOp@1\.\.3:"foo"> overlap ' 251 r'with previous <ReplaceOp@2\.\.4:"xyz">'), 252 tokens.toString) 253 254 255 def testReplaceSingleMiddleThenOverlappingSuperset(self): 256 tokens = self._parse("abcba") 257 tokens.replace(2, 2, "xyz") 258 tokens.replace(0, 3, "foo") 259 260 result = tokens.toString() 261 expecting = "fooa" 262 self.assertEqual(result, expecting) 263 264 265 def testCombineInserts(self): 266 tokens = self._parse("abc") 267 tokens.insertBefore(0, "x") 268 tokens.insertBefore(0, "y") 269 result = tokens.toString() 270 expecting = "yxabc" 271 self.assertEqual(expecting, result) 272 273 274 def testCombine3Inserts(self): 275 tokens = self._parse("abc") 276 tokens.insertBefore(1, "x") 277 tokens.insertBefore(0, "y") 278 tokens.insertBefore(1, "z") 279 result = tokens.toString() 280 expecting = "yazxbc" 281 self.assertEqual(expecting, result) 282 283 284 def testCombineInsertOnLeftWithReplace(self): 285 tokens = self._parse("abc") 286 tokens.replace(0, 2, "foo") 287 tokens.insertBefore(0, "z") # combine with left edge of rewrite 288 result = tokens.toString() 289 expecting = "zfoo" 290 self.assertEqual(expecting, result) 291 292 293 def testCombineInsertOnLeftWithDelete(self): 294 tokens = self._parse("abc") 295 tokens.delete(0, 2) 296 tokens.insertBefore(0, "z") # combine with left edge of rewrite 297 result = tokens.toString() 298 expecting = "z" # make sure combo is not znull 299 self.assertEqual(expecting, result) 300 301 302 def testDisjointInserts(self): 303 tokens = self._parse("abc") 304 tokens.insertBefore(1, "x") 305 tokens.insertBefore(2, "y") 306 tokens.insertBefore(0, "z") 307 result = tokens.toString() 308 expecting = "zaxbyc" 309 self.assertEqual(expecting, result) 310 311 312 def testOverlappingReplace(self): 313 tokens = self._parse("abcc") 314 tokens.replace(1, 2, "foo") 315 tokens.replace(0, 3, "bar") # wipes prior nested replace 316 result = tokens.toString() 317 expecting = "bar" 318 self.assertEqual(expecting, result) 319 320 321 def testOverlappingReplace2(self): 322 tokens = self._parse("abcc") 323 tokens.replace(0, 3, "bar") 324 tokens.replace(1, 2, "foo") # cannot split earlier replace 325 326 self.assertRaisesRegex( 327 ValueError, 328 (r'replace op boundaries of <ReplaceOp@1\.\.2:"foo"> overlap ' 329 r'with previous <ReplaceOp@0\.\.3:"bar">'), 330 tokens.toString) 331 332 333 def testOverlappingReplace3(self): 334 tokens = self._parse("abcc") 335 tokens.replace(1, 2, "foo") 336 tokens.replace(0, 2, "bar") # wipes prior nested replace 337 result = tokens.toString() 338 expecting = "barc" 339 self.assertEqual(expecting, result) 340 341 342 def testOverlappingReplace4(self): 343 tokens = self._parse("abcc") 344 tokens.replace(1, 2, "foo") 345 tokens.replace(1, 3, "bar") # wipes prior nested replace 346 result = tokens.toString() 347 expecting = "abar" 348 self.assertEqual(expecting, result) 349 350 351 def testDropIdenticalReplace(self): 352 tokens = self._parse("abcc") 353 tokens.replace(1, 2, "foo") 354 tokens.replace(1, 2, "foo") # drop previous, identical 355 result = tokens.toString() 356 expecting = "afooc" 357 self.assertEqual(expecting, result) 358 359 360 def testDropPrevCoveredInsert(self): 361 tokens = self._parse("abc") 362 tokens.insertBefore(1, "foo") 363 tokens.replace(1, 2, "foo") # kill prev insert 364 result = tokens.toString() 365 expecting = "afoofoo" 366 self.assertEqual(expecting, result) 367 368 369 def testLeaveAloneDisjointInsert(self): 370 tokens = self._parse("abcc") 371 tokens.insertBefore(1, "x") 372 tokens.replace(2, 3, "foo") 373 result = tokens.toString() 374 expecting = "axbfoo" 375 self.assertEqual(expecting, result) 376 377 378 def testLeaveAloneDisjointInsert2(self): 379 tokens = self._parse("abcc") 380 tokens.replace(2, 3, "foo") 381 tokens.insertBefore(1, "x") 382 result = tokens.toString() 383 expecting = "axbfoo" 384 self.assertEqual(expecting, result) 385 386 387 def testInsertBeforeTokenThenDeleteThatToken(self): 388 tokens = self._parse("abc") 389 tokens.insertBefore(2, "y") 390 tokens.delete(2) 391 result = tokens.toString() 392 expecting = "aby" 393 self.assertEqual(expecting, result) 394 395 396class T2(testbase.ANTLRTest): 397 def setUp(self): 398 self.compileGrammar('t048rewrite2.g') 399 400 401 def _parse(self, input): 402 cStream = antlr3.StringStream(input) 403 lexer = self.getLexer(cStream) 404 tStream = antlr3.TokenRewriteStream(lexer) 405 tStream.fillBuffer() 406 407 return tStream 408 409 410 def testToStringStartStop(self): 411 # Tokens: 0123456789 412 # Input: x = 3 * 0 413 tokens = self._parse("x = 3 * 0;") 414 tokens.replace(4, 8, "0") # replace 3 * 0 with 0 415 416 result = tokens.toOriginalString() 417 expecting = "x = 3 * 0;" 418 self.assertEqual(expecting, result) 419 420 result = tokens.toString() 421 expecting = "x = 0;" 422 self.assertEqual(expecting, result) 423 424 result = tokens.toString(0, 9) 425 expecting = "x = 0;" 426 self.assertEqual(expecting, result) 427 428 result = tokens.toString(4, 8) 429 expecting = "0" 430 self.assertEqual(expecting, result) 431 432 433 def testToStringStartStop2(self): 434 # Tokens: 012345678901234567 435 # Input: x = 3 * 0 + 2 * 0 436 tokens = self._parse("x = 3 * 0 + 2 * 0;") 437 438 result = tokens.toOriginalString() 439 expecting = "x = 3 * 0 + 2 * 0;" 440 self.assertEqual(expecting, result) 441 442 tokens.replace(4, 8, "0") # replace 3 * 0 with 0 443 result = tokens.toString() 444 expecting = "x = 0 + 2 * 0;" 445 self.assertEqual(expecting, result) 446 447 result = tokens.toString(0, 17) 448 expecting = "x = 0 + 2 * 0;" 449 self.assertEqual(expecting, result) 450 451 result = tokens.toString(4, 8) 452 expecting = "0" 453 self.assertEqual(expecting, result) 454 455 result = tokens.toString(0, 8) 456 expecting = "x = 0" 457 self.assertEqual(expecting, result) 458 459 result = tokens.toString(12, 16) 460 expecting = "2 * 0" 461 self.assertEqual(expecting, result) 462 463 tokens.insertAfter(17, "// comment") 464 result = tokens.toString(12, 18) 465 expecting = "2 * 0;// comment" 466 self.assertEqual(expecting, result) 467 468 result = tokens.toString(0, 8) # try again after insert at end 469 expecting = "x = 0" 470 self.assertEqual(expecting, result) 471 472 473if __name__ == '__main__': 474 unittest.main() 475