Best Python code snippet using fMBT_python
t048rewrite.py
Source:t048rewrite.py  
...6import testbase7class T1(testbase.ANTLRTest):8    def setUp(self):9        self.compileGrammar()10    def _parse(self, input):11        cStream = antlr3.StringStream(input)12        lexer = self.getLexer(cStream)13        tStream = antlr3.TokenRewriteStream(lexer)14        tStream.fillBuffer()15        return tStream16    def testInsertBeforeIndex0(self):17        tokens = self._parse("abc")18        tokens.insertBefore(0, "0")19        result = tokens.toString()20        expecting = "0abc"21        self.assertEqual(result, expecting)22    def testInsertAfterLastIndex(self):23        tokens = self._parse("abc")24        tokens.insertAfter(2, "x")25        result = tokens.toString()26        expecting = "abcx"27        self.assertEqual(result, expecting)28    def test2InsertBeforeAfterMiddleIndex(self):29        tokens = self._parse("abc")30        tokens.insertBefore(1, "x")31        tokens.insertAfter(1, "x")32        result = tokens.toString()33        expecting = "axbxc"34        self.assertEqual(result, expecting)35    def testReplaceIndex0(self):36        tokens = self._parse("abc")37        tokens.replace(0, "x")38        result = tokens.toString()39        expecting = "xbc"40        self.assertEqual(result, expecting)41    def testReplaceLastIndex(self):42        tokens = self._parse("abc")43        tokens.replace(2, "x")44        result = tokens.toString()45        expecting = "abx"46        self.assertEqual(result, expecting)47    def testReplaceMiddleIndex(self):48        tokens = self._parse("abc")49        tokens.replace(1, "x")50        result = tokens.toString()51        expecting = "axc"52        self.assertEqual(result, expecting)53    def test2ReplaceMiddleIndex(self):54        tokens = self._parse("abc")55        tokens.replace(1, "x")56        tokens.replace(1, "y")57        result = tokens.toString()58        expecting = "ayc"59        self.assertEqual(result, expecting)60    def test2ReplaceMiddleIndex1InsertBefore(self):61        tokens = self._parse("abc")62        tokens.insertBefore(0, "_")63        tokens.replace(1, "x")64        tokens.replace(1, "y")65        result = tokens.toString()66        expecting = "_ayc"67        self.assertEqual(expecting, result)68    def testReplaceThenDeleteMiddleIndex(self):69        tokens = self._parse("abc")70        tokens.replace(1, "x")71        tokens.delete(1)72        result = tokens.toString()73        expecting = "ac"74        self.assertEqual(result, expecting)75    def testInsertInPriorReplace(self):76        tokens = self._parse("abc")77        tokens.replace(0, 2, "x")78        tokens.insertBefore(1, "0")79        self.assertRaisesRegex(80            ValueError,81            (r'insert op <InsertBeforeOp@1:"0"> within boundaries of '82             r'previous <ReplaceOp@0\.\.2:"x">'),83            tokens.toString)84    def testInsertThenReplaceSameIndex(self):85        tokens = self._parse("abc")86        tokens.insertBefore(0, "0")87        tokens.replace(0, "x")  # supercedes insert at 088        result = tokens.toString()89        expecting = "0xbc"90        self.assertEqual(result, expecting)91    def test2InsertMiddleIndex(self):92        tokens = self._parse("abc")93        tokens.insertBefore(1, "x")94        tokens.insertBefore(1, "y")95        result = tokens.toString()96        expecting = "ayxbc"97        self.assertEqual(result, expecting)98    def test2InsertThenReplaceIndex0(self):99        tokens = self._parse("abc")100        tokens.insertBefore(0, "x")101        tokens.insertBefore(0, "y")102        tokens.replace(0, "z")103        result = tokens.toString()104        expecting = "yxzbc"105        self.assertEqual(result, expecting)106    def testReplaceThenInsertBeforeLastIndex(self):107        tokens = self._parse("abc")108        tokens.replace(2, "x")109        tokens.insertBefore(2, "y")110        result = tokens.toString()111        expecting = "abyx"112        self.assertEqual(result, expecting)113    def testInsertThenReplaceLastIndex(self):114        tokens = self._parse("abc")115        tokens.insertBefore(2, "y")116        tokens.replace(2, "x")117        result = tokens.toString()118        expecting = "abyx"119        self.assertEqual(result, expecting)120    def testReplaceThenInsertAfterLastIndex(self):121        tokens = self._parse("abc")122        tokens.replace(2, "x")123        tokens.insertAfter(2, "y")124        result = tokens.toString()125        expecting = "abxy"126        self.assertEqual(result, expecting)127    def testReplaceRangeThenInsertAtLeftEdge(self):128        tokens = self._parse("abcccba")129        tokens.replace(2, 4, "x")130        tokens.insertBefore(2, "y")131        result = tokens.toString()132        expecting = "abyxba"133        self.assertEqual(result, expecting)134    def testReplaceRangeThenInsertAtRightEdge(self):135        tokens = self._parse("abcccba")136        tokens.replace(2, 4, "x")137        tokens.insertBefore(4, "y") # no effect; within range of a replace138        self.assertRaisesRegex(139            ValueError,140            (r'insert op <InsertBeforeOp@4:"y"> within boundaries of '141             r'previous <ReplaceOp@2\.\.4:"x">'),142            tokens.toString)143    def testReplaceRangeThenInsertAfterRightEdge(self):144        tokens = self._parse("abcccba")145        tokens.replace(2, 4, "x")146        tokens.insertAfter(4, "y")147        result = tokens.toString()148        expecting = "abxyba"149        self.assertEqual(result, expecting)150    def testReplaceAll(self):151        tokens = self._parse("abcccba")152        tokens.replace(0, 6, "x")153        result = tokens.toString()154        expecting = "x"155        self.assertEqual(result, expecting)156    def testReplaceSubsetThenFetch(self):157        tokens = self._parse("abcccba")158        tokens.replace(2, 4, "xyz")159        result = tokens.toString(0, 6)160        expecting = "abxyzba"161        self.assertEqual(result, expecting)162    def testReplaceThenReplaceSuperset(self):163        tokens = self._parse("abcccba")164        tokens.replace(2, 4, "xyz")165        tokens.replace(3, 5, "foo") # overlaps, error166        self.assertRaisesRegex(167            ValueError,168            (r'replace op boundaries of <ReplaceOp@3\.\.5:"foo"> overlap '169             r'with previous <ReplaceOp@2\.\.4:"xyz">'),170            tokens.toString)171    def testReplaceThenReplaceLowerIndexedSuperset(self):172        tokens = self._parse("abcccba")173        tokens.replace(2, 4, "xyz")174        tokens.replace(1, 3, "foo") # overlap, error175        self.assertRaisesRegex(176            ValueError,177            (r'replace op boundaries of <ReplaceOp@1\.\.3:"foo"> overlap '178             r'with previous <ReplaceOp@2\.\.4:"xyz">'),179            tokens.toString)180    def testReplaceSingleMiddleThenOverlappingSuperset(self):181        tokens = self._parse("abcba")182        tokens.replace(2, 2, "xyz")183        tokens.replace(0, 3, "foo")184        result = tokens.toString()185        expecting = "fooa"186        self.assertEqual(result, expecting)187    def testCombineInserts(self):188        tokens = self._parse("abc")189        tokens.insertBefore(0, "x")190        tokens.insertBefore(0, "y")191        result = tokens.toString()192        expecting = "yxabc"193        self.assertEqual(expecting, result)194    def testCombine3Inserts(self):195        tokens = self._parse("abc")196        tokens.insertBefore(1, "x")197        tokens.insertBefore(0, "y")198        tokens.insertBefore(1, "z")199        result = tokens.toString()200        expecting = "yazxbc"201        self.assertEqual(expecting, result)202    def testCombineInsertOnLeftWithReplace(self):203        tokens = self._parse("abc")204        tokens.replace(0, 2, "foo")205        tokens.insertBefore(0, "z") # combine with left edge of rewrite206        result = tokens.toString()207        expecting = "zfoo"208        self.assertEqual(expecting, result)209    def testCombineInsertOnLeftWithDelete(self):210        tokens = self._parse("abc")211        tokens.delete(0, 2)212        tokens.insertBefore(0, "z") # combine with left edge of rewrite213        result = tokens.toString()214        expecting = "z" # make sure combo is not znull215        self.assertEqual(expecting, result)216    def testDisjointInserts(self):217        tokens = self._parse("abc")218        tokens.insertBefore(1, "x")219        tokens.insertBefore(2, "y")220        tokens.insertBefore(0, "z")221        result = tokens.toString()222        expecting = "zaxbyc"223        self.assertEqual(expecting, result)224    def testOverlappingReplace(self):225        tokens = self._parse("abcc")226        tokens.replace(1, 2, "foo")227        tokens.replace(0, 3, "bar") # wipes prior nested replace228        result = tokens.toString()229        expecting = "bar"230        self.assertEqual(expecting, result)231    def testOverlappingReplace2(self):232        tokens = self._parse("abcc")233        tokens.replace(0, 3, "bar")234        tokens.replace(1, 2, "foo") # cannot split earlier replace235        self.assertRaisesRegex(236            ValueError,237            (r'replace op boundaries of <ReplaceOp@1\.\.2:"foo"> overlap '238             r'with previous <ReplaceOp@0\.\.3:"bar">'),239            tokens.toString)240    def testOverlappingReplace3(self):241        tokens = self._parse("abcc")242        tokens.replace(1, 2, "foo")243        tokens.replace(0, 2, "bar") # wipes prior nested replace244        result = tokens.toString()245        expecting = "barc"246        self.assertEqual(expecting, result)247    def testOverlappingReplace4(self):248        tokens = self._parse("abcc")249        tokens.replace(1, 2, "foo")250        tokens.replace(1, 3, "bar") # wipes prior nested replace251        result = tokens.toString()252        expecting = "abar"253        self.assertEqual(expecting, result)254    def testDropIdenticalReplace(self):255        tokens = self._parse("abcc")256        tokens.replace(1, 2, "foo")257        tokens.replace(1, 2, "foo") # drop previous, identical258        result = tokens.toString()259        expecting = "afooc"260        self.assertEqual(expecting, result)261    def testDropPrevCoveredInsert(self):262        tokens = self._parse("abc")263        tokens.insertBefore(1, "foo")264        tokens.replace(1, 2, "foo") # kill prev insert265        result = tokens.toString()266        expecting = "afoofoo"267        self.assertEqual(expecting, result)268    def testLeaveAloneDisjointInsert(self):269        tokens = self._parse("abcc")270        tokens.insertBefore(1, "x")271        tokens.replace(2, 3, "foo")272        result = tokens.toString()273        expecting = "axbfoo"274        self.assertEqual(expecting, result)275    def testLeaveAloneDisjointInsert2(self):276        tokens = self._parse("abcc")277        tokens.replace(2, 3, "foo")278        tokens.insertBefore(1, "x")279        result = tokens.toString()280        expecting = "axbfoo"281        self.assertEqual(expecting, result)282    def testInsertBeforeTokenThenDeleteThatToken(self):283        tokens = self._parse("abc")284        tokens.insertBefore(2, "y")285        tokens.delete(2)286        result = tokens.toString()287        expecting = "aby"288        self.assertEqual(expecting, result)289class T2(testbase.ANTLRTest):290    def setUp(self):291        self.compileGrammar('t048rewrite2.g')292    def _parse(self, input):293        cStream = antlr3.StringStream(input)294        lexer = self.getLexer(cStream)295        tStream = antlr3.TokenRewriteStream(lexer)296        tStream.fillBuffer()297        return tStream298    def testToStringStartStop(self):299        # Tokens: 0123456789300        # Input:  x = 3 * 0301        tokens = self._parse("x = 3 * 0;")302        tokens.replace(4, 8, "0") # replace 3 * 0 with 0303        result = tokens.toOriginalString()304        expecting = "x = 3 * 0;"305        self.assertEqual(expecting, result)306        result = tokens.toString()307        expecting = "x = 0;"308        self.assertEqual(expecting, result)309        result = tokens.toString(0, 9)310        expecting = "x = 0;"311        self.assertEqual(expecting, result)312        result = tokens.toString(4, 8)313        expecting = "0"314        self.assertEqual(expecting, result)315    def testToStringStartStop2(self):316        # Tokens: 012345678901234567317        # Input:  x = 3 * 0 + 2 * 0318        tokens = self._parse("x = 3 * 0 + 2 * 0;")319        result = tokens.toOriginalString()320        expecting = "x = 3 * 0 + 2 * 0;"321        self.assertEqual(expecting, result)322        tokens.replace(4, 8, "0") # replace 3 * 0 with 0323        result = tokens.toString()324        expecting = "x = 0 + 2 * 0;"325        self.assertEqual(expecting, result)326        result = tokens.toString(0, 17)327        expecting = "x = 0 + 2 * 0;"328        self.assertEqual(expecting, result)329        result = tokens.toString(4, 8)330        expecting = "0"331        self.assertEqual(expecting, result)332        result = tokens.toString(0, 8)...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
