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.failUnlessEqual(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.failUnlessEqual(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.failUnlessEqual(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.failUnlessEqual(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.failUnlessEqual(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.failUnlessEqual(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.failUnlessEqual(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.failUnlessEqual(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.failUnlessEqual(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        try:
115            tokens.toString()
116            self.fail()
117        except ValueError, exc:
118            self.failUnlessEqual(
119                str(exc),
120                "insert op <InsertBeforeOp@1:\"0\"> within boundaries of "
121                "previous <ReplaceOp@0..2:\"x\">"
122                )
123
124    def testInsertThenReplaceSameIndex(self):
125        tokens = self._parse("abc")
126        tokens.insertBefore(0, "0")
127        tokens.replace(0, "x")  # supercedes insert at 0
128
129        result = tokens.toString()
130        expecting = "0xbc"
131        self.failUnlessEqual(result, expecting)
132
133
134    def test2InsertMiddleIndex(self):
135        tokens = self._parse("abc")
136        tokens.insertBefore(1, "x")
137        tokens.insertBefore(1, "y")
138
139        result = tokens.toString()
140        expecting = "ayxbc"
141        self.failUnlessEqual(result, expecting)
142
143
144    def test2InsertThenReplaceIndex0(self):
145        tokens = self._parse("abc")
146        tokens.insertBefore(0, "x")
147        tokens.insertBefore(0, "y")
148        tokens.replace(0, "z")
149
150        result = tokens.toString()
151        expecting = "yxzbc"
152        self.failUnlessEqual(result, expecting)
153
154
155    def testReplaceThenInsertBeforeLastIndex(self):
156        tokens = self._parse("abc")
157        tokens.replace(2, "x")
158        tokens.insertBefore(2, "y")
159
160        result = tokens.toString()
161        expecting = "abyx"
162        self.failUnlessEqual(result, expecting)
163
164
165    def testInsertThenReplaceLastIndex(self):
166        tokens = self._parse("abc")
167        tokens.insertBefore(2, "y")
168        tokens.replace(2, "x")
169
170        result = tokens.toString()
171        expecting = "abyx"
172        self.failUnlessEqual(result, expecting)
173
174
175    def testReplaceThenInsertAfterLastIndex(self):
176        tokens = self._parse("abc")
177        tokens.replace(2, "x")
178        tokens.insertAfter(2, "y")
179
180        result = tokens.toString()
181        expecting = "abxy"
182        self.failUnlessEqual(result, expecting)
183
184
185    def testReplaceRangeThenInsertAtLeftEdge(self):
186        tokens = self._parse("abcccba")
187        tokens.replace(2, 4, "x")
188        tokens.insertBefore(2, "y")
189
190        result = tokens.toString()
191        expecting = "abyxba"
192        self.failUnlessEqual(result, expecting)
193
194
195    def testReplaceRangeThenInsertAtRightEdge(self):
196        tokens = self._parse("abcccba")
197        tokens.replace(2, 4, "x")
198        tokens.insertBefore(4, "y") # no effect; within range of a replace
199
200        try:
201            tokens.toString()
202            self.fail()
203        except ValueError, exc:
204            self.failUnlessEqual(
205                str(exc),
206                "insert op <InsertBeforeOp@4:\"y\"> within boundaries of "
207                "previous <ReplaceOp@2..4:\"x\">")
208
209
210    def testReplaceRangeThenInsertAfterRightEdge(self):
211        tokens = self._parse("abcccba")
212        tokens.replace(2, 4, "x")
213        tokens.insertAfter(4, "y")
214
215        result = tokens.toString()
216        expecting = "abxyba"
217        self.failUnlessEqual(result, expecting)
218
219
220    def testReplaceAll(self):
221        tokens = self._parse("abcccba")
222        tokens.replace(0, 6, "x")
223
224        result = tokens.toString()
225        expecting = "x"
226        self.failUnlessEqual(result, expecting)
227
228
229    def testReplaceSubsetThenFetch(self):
230        tokens = self._parse("abcccba")
231        tokens.replace(2, 4, "xyz")
232
233        result = tokens.toString(0, 6)
234        expecting = "abxyzba"
235        self.failUnlessEqual(result, expecting)
236
237
238    def testReplaceThenReplaceSuperset(self):
239        tokens = self._parse("abcccba")
240        tokens.replace(2, 4, "xyz")
241        tokens.replace(3, 5, "foo") # overlaps, error
242
243        try:
244            tokens.toString()
245            self.fail()
246        except ValueError, exc:
247            self.failUnlessEqual(
248                str(exc),
249                "replace op boundaries of <ReplaceOp@3..5:\"foo\"> overlap "
250                "with previous <ReplaceOp@2..4:\"xyz\">")
251
252
253    def testReplaceThenReplaceLowerIndexedSuperset(self):
254        tokens = self._parse("abcccba")
255        tokens.replace(2, 4, "xyz")
256        tokens.replace(1, 3, "foo") # overlap, error
257
258        try:
259            tokens.toString()
260            self.fail()
261        except ValueError, exc:
262            self.failUnlessEqual(
263                str(exc),
264                "replace op boundaries of <ReplaceOp@1..3:\"foo\"> overlap "
265                "with previous <ReplaceOp@2..4:\"xyz\">")
266
267
268    def testReplaceSingleMiddleThenOverlappingSuperset(self):
269        tokens = self._parse("abcba")
270        tokens.replace(2, 2, "xyz")
271        tokens.replace(0, 3, "foo")
272
273        result = tokens.toString()
274        expecting = "fooa"
275        self.failUnlessEqual(result, expecting)
276
277
278    def testCombineInserts(self):
279        tokens = self._parse("abc")
280        tokens.insertBefore(0, "x")
281        tokens.insertBefore(0, "y")
282        result = tokens.toString()
283        expecting = "yxabc"
284        self.failUnlessEqual(expecting, result)
285
286
287    def testCombine3Inserts(self):
288        tokens = self._parse("abc")
289        tokens.insertBefore(1, "x")
290        tokens.insertBefore(0, "y")
291        tokens.insertBefore(1, "z")
292        result = tokens.toString()
293        expecting = "yazxbc"
294        self.failUnlessEqual(expecting, result)
295
296
297    def testCombineInsertOnLeftWithReplace(self):
298        tokens = self._parse("abc")
299        tokens.replace(0, 2, "foo")
300        tokens.insertBefore(0, "z") # combine with left edge of rewrite
301        result = tokens.toString()
302        expecting = "zfoo"
303        self.failUnlessEqual(expecting, result)
304
305
306    def testCombineInsertOnLeftWithDelete(self):
307        tokens = self._parse("abc")
308        tokens.delete(0, 2)
309        tokens.insertBefore(0, "z") # combine with left edge of rewrite
310        result = tokens.toString()
311        expecting = "z" # make sure combo is not znull
312        self.failUnlessEqual(expecting, result)
313
314
315    def testDisjointInserts(self):
316        tokens = self._parse("abc")
317        tokens.insertBefore(1, "x")
318        tokens.insertBefore(2, "y")
319        tokens.insertBefore(0, "z")
320        result = tokens.toString()
321        expecting = "zaxbyc"
322        self.failUnlessEqual(expecting, result)
323
324
325    def testOverlappingReplace(self):
326        tokens = self._parse("abcc")
327        tokens.replace(1, 2, "foo")
328        tokens.replace(0, 3, "bar") # wipes prior nested replace
329        result = tokens.toString()
330        expecting = "bar"
331        self.failUnlessEqual(expecting, result)
332
333
334    def testOverlappingReplace2(self):
335        tokens = self._parse("abcc")
336        tokens.replace(0, 3, "bar")
337        tokens.replace(1, 2, "foo") # cannot split earlier replace
338
339        try:
340            tokens.toString()
341            self.fail()
342        except ValueError, exc:
343            self.failUnlessEqual(
344                str(exc),
345                "replace op boundaries of <ReplaceOp@1..2:\"foo\"> overlap "
346                "with previous <ReplaceOp@0..3:\"bar\">")
347
348
349    def testOverlappingReplace3(self):
350        tokens = self._parse("abcc")
351        tokens.replace(1, 2, "foo")
352        tokens.replace(0, 2, "bar") # wipes prior nested replace
353        result = tokens.toString()
354        expecting = "barc"
355        self.failUnlessEqual(expecting, result)
356
357
358    def testOverlappingReplace4(self):
359        tokens = self._parse("abcc")
360        tokens.replace(1, 2, "foo")
361        tokens.replace(1, 3, "bar") # wipes prior nested replace
362        result = tokens.toString()
363        expecting = "abar"
364        self.failUnlessEqual(expecting, result)
365
366
367    def testDropIdenticalReplace(self):
368        tokens = self._parse("abcc")
369        tokens.replace(1, 2, "foo")
370        tokens.replace(1, 2, "foo") # drop previous, identical
371        result = tokens.toString()
372        expecting = "afooc"
373        self.failUnlessEqual(expecting, result)
374
375
376    def testDropPrevCoveredInsert(self):
377        tokens = self._parse("abc")
378        tokens.insertBefore(1, "foo")
379        tokens.replace(1, 2, "foo") # kill prev insert
380        result = tokens.toString()
381        expecting = "afoofoo"
382        self.failUnlessEqual(expecting, result)
383
384
385    def testLeaveAloneDisjointInsert(self):
386        tokens = self._parse("abcc")
387        tokens.insertBefore(1, "x")
388        tokens.replace(2, 3, "foo")
389        result = tokens.toString()
390        expecting = "axbfoo"
391        self.failUnlessEqual(expecting, result)
392
393
394    def testLeaveAloneDisjointInsert2(self):
395        tokens = self._parse("abcc")
396        tokens.replace(2, 3, "foo")
397        tokens.insertBefore(1, "x")
398        result = tokens.toString()
399        expecting = "axbfoo"
400        self.failUnlessEqual(expecting, result)
401
402
403    def testInsertBeforeTokenThenDeleteThatToken(self):
404        tokens = self._parse("abc")
405        tokens.insertBefore(2, "y")
406        tokens.delete(2)
407        result = tokens.toString()
408        expecting = "aby"
409        self.failUnlessEqual(expecting, result)
410
411
412class T2(testbase.ANTLRTest):
413    def setUp(self):
414        self.compileGrammar('t048rewrite2.g')
415
416
417    def _parse(self, input):
418        cStream = antlr3.StringStream(input)
419        lexer = self.getLexer(cStream)
420        tStream = antlr3.TokenRewriteStream(lexer)
421        tStream.fillBuffer()
422
423        return tStream
424
425
426    def testToStringStartStop(self):
427        # Tokens: 0123456789
428        # Input:  x = 3 * 0
429        tokens = self._parse("x = 3 * 0;")
430        tokens.replace(4, 8, "0") # replace 3 * 0 with 0
431
432        result = tokens.toOriginalString()
433        expecting = "x = 3 * 0;"
434        self.failUnlessEqual(expecting, result)
435
436        result = tokens.toString()
437        expecting = "x = 0;"
438        self.failUnlessEqual(expecting, result)
439
440        result = tokens.toString(0, 9)
441        expecting = "x = 0;"
442        self.failUnlessEqual(expecting, result)
443
444        result = tokens.toString(4, 8)
445        expecting = "0"
446        self.failUnlessEqual(expecting, result)
447
448
449    def testToStringStartStop2(self):
450        # Tokens: 012345678901234567
451        # Input:  x = 3 * 0 + 2 * 0
452        tokens = self._parse("x = 3 * 0 + 2 * 0;")
453
454        result = tokens.toOriginalString()
455        expecting = "x = 3 * 0 + 2 * 0;"
456        self.failUnlessEqual(expecting, result)
457
458        tokens.replace(4, 8, "0") # replace 3 * 0 with 0
459        result = tokens.toString()
460        expecting = "x = 0 + 2 * 0;"
461        self.failUnlessEqual(expecting, result)
462
463        result = tokens.toString(0, 17)
464        expecting = "x = 0 + 2 * 0;"
465        self.failUnlessEqual(expecting, result)
466
467        result = tokens.toString(4, 8)
468        expecting = "0"
469        self.failUnlessEqual(expecting, result)
470
471        result = tokens.toString(0, 8)
472        expecting = "x = 0"
473        self.failUnlessEqual(expecting, result)
474
475        result = tokens.toString(12, 16)
476        expecting = "2 * 0"
477        self.failUnlessEqual(expecting, result)
478
479        tokens.insertAfter(17, "// comment")
480        result = tokens.toString(12, 18)
481        expecting = "2 * 0;// comment"
482        self.failUnlessEqual(expecting, result)
483
484        result = tokens.toString(0, 8) # try again after insert at end
485        expecting = "x = 0"
486        self.failUnlessEqual(expecting, result)
487
488
489if __name__ == '__main__':
490    unittest.main()
491