1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3<head>
4<meta http-equiv="content-type" content="text/html;charset=utf-8" />
5<title>t048rewrite</title>
6
7<!-- ANTLR includes -->
8<script type="text/javascript" src="../../lib/antlr3-all.js"></script>
9<script type="text/javascript" src="t048rewrite.js"></script>
10
11<!-- JsUnit include -->
12<script type="text/javascript" src="../jsunit/app/jsUnitCore.js"></script>
13
14<!-- Test Code -->
15<script type="text/javascript">
16    var TLexer = function() {
17        TLexer.superclass.constructor.apply(this, arguments);
18    };
19    org.antlr.lang.extend(TLexer, t048rewrite, {
20        recover: function(re) {
21            /* don't recover, just crash */
22            throw re;
23        }
24    });
25
26    function _parse(xinput) {
27        var cstream = new org.antlr.runtime.ANTLRStringStream(xinput),
28            lexer = new TLexer(cstream),
29            tstream = new org.antlr.runtime.TokenRewriteStream(lexer);
30        tstream.LT(1); // fill buffer
31        return tstream;
32    }
33
34    function testInsertBeforeIndex0() {
35        var tokens = _parse("abc");
36        tokens.insertBefore(0,"0");
37
38        var result = tokens.toString(),
39            expecting = "0abc";
40        assertEquals(result, expecting);
41    }
42
43    function testInsertAfterLastIndex() {
44        var tokens = _parse("abc");
45        tokens.insertAfter(2,"x");
46
47        var result = tokens.toString(),
48            expecting = "abcx";
49        assertEquals(result, expecting);
50    }
51
52    function test2InsertBeforeAfterMiddleIndex() {
53        var tokens = _parse("abc");
54        tokens.insertBefore(1,"x");
55        tokens.insertAfter(1, "x");
56
57        var result = tokens.toString(),
58            expecting = "axbxc";
59        assertEquals(result, expecting);
60    }
61
62    function testReplaceIndex0() {
63        var tokens = _parse("abc");
64        tokens.replace(0,"x");
65
66        var result = tokens.toString(),
67            expecting = "xbc";
68        assertEquals(result, expecting);
69    }
70
71    function testReplaceLastIndex() {
72        var tokens = _parse("abc");
73        tokens.replace(2,"x");
74
75        var result = tokens.toString(),
76            expecting = "abx";
77        assertEquals(result, expecting);
78    }
79
80    function testReplaceMiddleIndex() {
81        var tokens = _parse("abc");
82        tokens.replace(1,"x");
83
84        var result = tokens.toString(),
85            expecting = "axc";
86        assertEquals(result, expecting);
87    }
88
89    function test2ReplaceMiddleIndex() {
90        var tokens = _parse("abc");
91        tokens.replace(1,"x");
92        tokens.replace(1,"y");
93
94        var result = tokens.toString(),
95            expecting = "ayc";
96        assertEquals(result, expecting);
97    }
98
99    function testReplaceThenDeleteMiddleIndex() {
100        var tokens = _parse("abc");
101        tokens.replace(1,"x");
102        tokens.remove(1);
103
104        var result = tokens.toString(),
105            expecting = "ac";
106        assertEquals(result, expecting);
107    }
108
109    function testReplaceThenInsertSameIndex() {
110        var tokens = _parse("abc");
111        tokens.replace(0,"x");
112        tokens.insertBefore(0,"0");
113
114        var result = tokens.toString(),
115            expecting = "0xbc";
116        assertEquals(result, expecting);
117    }
118
119    function testReplaceThen2InsertSameIndex() {
120        var tokens = _parse("abc");
121        tokens.replace(0,"x");
122        tokens.insertBefore(0,"y");
123        tokens.insertBefore(0,"z");
124
125        var result = tokens.toString(),
126            expecting = "zyxbc";
127        assertEquals(result, expecting);
128    }
129
130    function testInsertThenReplaceSameIndex() {
131        var tokens = _parse("abc");
132        tokens.insertBefore(0,"0");
133        tokens.replace(0,"x");
134
135        var result = tokens.toString(),
136            expecting = "0xbc";
137        assertEquals(result, expecting);
138    }
139
140    function test2InsertMiddleIndex() {
141        var tokens = _parse("abc");
142        tokens.insertBefore(1,"x");
143        tokens.insertBefore(1,"y");
144
145        var result = tokens.toString(),
146            expecting = "ayxbc";
147        assertEquals(result, expecting);
148    }
149
150    function test2InsertThenReplaceIndex0() {
151        var tokens = _parse("abc");
152        tokens.insertBefore(0,"x");
153        tokens.insertBefore(0,"y");
154        tokens.replace(0,"z");
155
156        var result = tokens.toString(),
157            expecting = "yxzbc";
158        assertEquals(result, expecting);
159    }
160
161    function testReplaceThenInsertBeforeLastIndex() {
162        var tokens = _parse("abc");
163        tokens.replace(2,"x");
164        tokens.insertBefore(2,"y");
165
166        var result = tokens.toString(),
167            expecting = "abyx";
168        assertEquals(result, expecting);
169    }
170
171    function testInsertThenReplaceLastIndex() {
172        var tokens = _parse("abc");
173        tokens.insertBefore(2,"y");
174        tokens.replace(2,"x");
175
176        var result = tokens.toString(),
177            expecting = "abyx";
178        assertEquals(result, expecting);
179    }
180
181    function testReplaceThenInsertLastIndex() {
182        var tokens = _parse("abc");
183        tokens.replace(2,"x");
184        tokens.insertAfter(2,"y");
185
186        var result = tokens.toString(),
187            expecting = "abxy";
188        assertEquals(result, expecting);
189    }
190
191    function testReplaceRangeThenInsertInMiddle() {
192        var tokens = _parse("abcccba");
193        tokens.replace(2,4,"x");
194        tokens.insertBefore(3,"y");
195
196        var result = tokens.toString(),
197            expecting = "abxba";
198        assertEquals(result, expecting);
199    }
200
201    function testReplaceRangeThenInsertAtLeftEdge() {
202        var tokens = _parse("abcccba");
203        tokens.replace(2,4,"x");
204        tokens.insertBefore(2,"y");
205
206        var result = tokens.toString(),
207            expecting = "abyxba";
208        assertEquals(result, expecting);
209    }
210
211    function testReplaceRangeThenInsertAtRightEdge() {
212        var tokens = _parse("abcccba");
213        tokens.replace(2,4,"x");
214        tokens.insertBefore(4,"y");
215
216        var result = tokens.toString(),
217            expecting = "abxba";
218        assertEquals(result, expecting);
219    }
220
221    function testReplaceRangeThenInsertAfterRightEdge() {
222        var tokens = _parse("abcccba");
223        tokens.replace(2,4,"x");
224        tokens.insertAfter(4,"y");
225
226        var result = tokens.toString(),
227            expecting = "abxyba";
228        assertEquals(result, expecting);
229    }
230
231    function testReplaceAll() {
232        var tokens = _parse("abcccba");
233        tokens.replace(0,6,"x");
234
235        var result = tokens.toString(),
236            expecting = "x";
237        assertEquals(result, expecting);
238    }
239
240    function testReplaceSubsetThenFetch() {
241        var tokens = _parse("abcccba");
242        tokens.replace(2,4,"xyz");
243
244        var result = tokens.toString(0,6),
245            expecting = "abxyzba";
246        assertEquals(result, expecting);
247    }
248
249    function testReplaceThenReplaceSuperset() {
250        var tokens = _parse("abcccba");
251        tokens.replace(2,4,"xyz");
252        tokens.replace(2,5,"foo");
253
254        var result = tokens.toString(),
255            expecting = "abfooa";
256        assertEquals(result, expecting);
257    }
258
259    function testReplaceThenReplaceLowerIndexedSuperset() {
260        var tokens = _parse("abcccba");
261        tokens.replace(2,4,"xyz");
262        tokens.replace(1,3,"foo");
263
264        var result = tokens.toString(),
265            expecting = "afoocba";
266        assertEquals(result, expecting);
267    }
268
269    function testReplaceSingleMiddleThenOverlappingSuperset() {
270        var tokens = _parse("abcba");
271        tokens.replace(2,2,"xyz");
272        tokens.replace(0,3,"foo");
273
274        var result = tokens.toString(),
275            expecting = "fooa";
276        assertEquals(result, expecting);
277    }
278
279</script>
280
281</head>
282<body>
283    <h1>t048rewrite</h1>
284</body>
285</html>
286