|
1 | 1 | (function(){
|
2 |
| - |
| 2 | + |
3 | 3 | var Assert = YUITest.Assert
|
4 | 4 | StringReader = parserlib.util.StringReader;
|
5 |
| - |
| 5 | + |
6 | 6 | //-------------------------------------------------------------------------
|
7 | 7 | // Base Test Suite
|
8 | 8 | //-------------------------------------------------------------------------
|
9 |
| - |
| 9 | + |
10 | 10 | var suite = new YUITest.TestSuite("StringReader");
|
11 |
| - |
| 11 | + |
12 | 12 | //-------------------------------------------------------------------------
|
13 | 13 | // Test Case for adding
|
14 | 14 | //-------------------------------------------------------------------------
|
15 |
| - |
| 15 | + |
16 | 16 | suite.add(new YUITest.TestCase({
|
17 |
| - |
| 17 | + |
18 | 18 | name : "read() Tests",
|
19 | 19 |
|
20 | 20 | //---------------------------------------------------------------------
|
21 | 21 | // Tests
|
22 | 22 | //---------------------------------------------------------------------
|
23 |
| - |
| 23 | + |
24 | 24 | /*
|
25 | 25 | * Tests that reading a string with no new lines works.
|
26 | 26 | */
|
|
29 | 29 | reader = new StringReader(testString),
|
30 | 30 | i = 0,
|
31 | 31 | c;
|
32 |
| - |
| 32 | + |
33 | 33 | Assert.areEqual(1, reader.getCol(), "Column should be 1.");
|
34 | 34 | c = reader.read();
|
35 |
| - |
| 35 | + |
36 | 36 | while(c){
|
37 |
| - Assert.areEqual(testString.charAt(i), c, "Character at position " + i + " is incorrect."); |
| 37 | + Assert.areEqual(testString.charAt(i), c, "Character at position " + i + " is incorrect."); |
38 | 38 | Assert.areEqual(i+2, reader.getCol(), "Column should be " + (i+2) + ".");
|
39 | 39 | c = reader.read();
|
40 | 40 | i++;
|
41 | 41 | }
|
42 |
| - |
| 42 | + |
43 | 43 | Assert.areEqual(testString.length, i, "All characters should be read.");
|
44 |
| - Assert.isNull(c, "Last character read should be null."); |
| 44 | + Assert.isNull(c, "Last character read should be null."); |
45 | 45 | },
|
46 |
| - |
| 46 | + |
47 | 47 | /*
|
48 | 48 | * Tests that reading a multi-line string works.
|
49 | 49 | */
|
|
52 | 52 | reader = new StringReader(testString),
|
53 | 53 | i = 0,
|
54 | 54 | c = reader.read();
|
55 |
| - |
| 55 | + |
56 | 56 | while(c){
|
57 |
| - Assert.areEqual(testString.charAt(i), c, "Character at position " + i + " is incorrect."); |
| 57 | + Assert.areEqual(testString.charAt(i), c, "Character at position " + i + " is incorrect."); |
58 | 58 | if (c == "\n"){
|
59 | 59 | Assert.areEqual(2, reader.getLine(), "Should now be on second row.");
|
60 | 60 | Assert.areEqual(1, reader.getCol(), "The new line should cause you to go to first char in second row.");
|
61 |
| - } |
| 61 | + } |
62 | 62 | c = reader.read();
|
63 | 63 | i++;
|
64 | 64 | }
|
65 |
| - |
66 |
| - Assert.isNull(c, "Last character read should be null."); |
| 65 | + |
| 66 | + Assert.isNull(c, "Last character read should be null."); |
67 | 67 | },
|
68 | 68 |
|
69 |
| - |
| 69 | + |
70 | 70 | /*
|
71 | 71 | * Tests that reading a multi-line string properly tracks rows and cols.
|
72 | 72 | */
|
73 | 73 | testLinearReadWithTwoLinesTrackCols: function(){
|
74 | 74 | var testString = "Hello world!\nNice day, isn't it?",
|
75 | 75 | reader = new StringReader(testString);
|
76 |
| - |
| 76 | + |
77 | 77 | reader.readTo("!");
|
78 | 78 | reader.read();
|
79 |
| - |
| 79 | + |
80 | 80 | Assert.areEqual(1, reader.getCol());
|
81 | 81 | Assert.areEqual(2, reader.getLine());
|
82 |
| - |
| 82 | + |
83 | 83 | },
|
84 |
| - |
| 84 | + |
85 | 85 | /*
|
86 | 86 | * Tests that reading a multi-line string works when the last character is a new-line.
|
87 | 87 | */
|
|
90 | 90 | reader = new StringReader(testString),
|
91 | 91 | i = 0,
|
92 | 92 | c = reader.read();
|
93 |
| - |
| 93 | + |
94 | 94 | while(c){
|
95 |
| - Assert.areEqual(testString.charAt(i), c, "Character at position " + i + " is incorrect."); |
| 95 | + Assert.areEqual(testString.charAt(i), c, "Character at position " + i + " is incorrect."); |
96 | 96 | c = reader.read();
|
97 | 97 | i++;
|
98 | 98 | }
|
99 |
| - |
100 |
| - Assert.isNull(c, "Last character read should be null."); |
101 |
| - } |
102 |
| - |
103 |
| - |
104 |
| - |
105 |
| - |
| 99 | + |
| 100 | + Assert.isNull(c, "Last character read should be null."); |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
106 | 106 | }));
|
107 | 107 |
|
108 | 108 | //-------------------------------------------------------------------------
|
109 | 109 | // Test Case for readTo
|
110 | 110 | //-------------------------------------------------------------------------
|
111 |
| - |
| 111 | + |
112 | 112 | suite.add(new YUITest.TestCase({
|
113 |
| - |
| 113 | + |
114 | 114 | name : "readTo() Tests",
|
115 | 115 |
|
116 | 116 |
|
117 | 117 | //---------------------------------------------------------------------
|
118 | 118 | // Tests
|
119 | 119 | //---------------------------------------------------------------------
|
120 |
| - |
| 120 | + |
121 | 121 | /*
|
122 | 122 | * Tests that reading a string with no new lines works.
|
123 | 123 | */
|
124 | 124 | testLinearReadToWithOneLine: function(){
|
125 | 125 | var testString = "Hello world!",
|
126 | 126 | reader = new StringReader(testString);
|
127 |
| - |
128 |
| - |
129 |
| - Assert.areEqual("Hello ", reader.readTo(" ")); |
| 127 | + |
| 128 | + |
| 129 | + Assert.areEqual("Hello ", reader.readTo(" ")); |
130 | 130 | Assert.areEqual("w", reader.read());
|
131 | 131 | },
|
132 |
| - |
| 132 | + |
133 | 133 | /*
|
134 | 134 | * Tests that reading a multi-line string works.
|
135 | 135 | */
|
136 | 136 | testLinearReadToWithTwoLines: function(){
|
137 | 137 | var testString = "Hello world!\nNice day, isn't it?",
|
138 | 138 | reader = new StringReader(testString);
|
139 |
| - |
140 |
| - |
141 |
| - Assert.areEqual("Hello world!\nN", reader.readTo("N")); |
| 139 | + |
| 140 | + |
| 141 | + Assert.areEqual("Hello world!\nN", reader.readTo("N")); |
142 | 142 | Assert.areEqual(2, reader.getLine());
|
143 | 143 | Assert.areEqual(2, reader.getCol());
|
144 | 144 | }
|
145 |
| - |
146 |
| - })); |
147 |
| - |
| 145 | + |
| 146 | + })); |
| 147 | + |
148 | 148 | //-------------------------------------------------------------------------
|
149 | 149 | // Test Case for readWhile()
|
150 | 150 | //-------------------------------------------------------------------------
|
151 |
| - |
| 151 | + |
152 | 152 | suite.add(new YUITest.TestCase({
|
153 |
| - |
| 153 | + |
154 | 154 | name : "readWhile() Tests",
|
155 | 155 |
|
156 | 156 |
|
157 | 157 | //---------------------------------------------------------------------
|
158 | 158 | // Tests
|
159 | 159 | //---------------------------------------------------------------------
|
160 |
| - |
| 160 | + |
161 | 161 | /*
|
162 | 162 | * Tests that the entire string can be read..
|
163 | 163 | */
|
164 | 164 | testReadWhileSimple: function(){
|
165 | 165 | var testString = "Hello world!",
|
166 | 166 | reader = new StringReader(testString);
|
167 |
| - |
| 167 | + |
168 | 168 | var result = reader.readWhile(function(){
|
169 | 169 | return true;
|
170 | 170 | });
|
|
173 | 173 | Assert.areEqual(1, reader.getLine());
|
174 | 174 | Assert.areEqual(13, reader.getCol());
|
175 | 175 | }
|
176 |
| - })); |
177 |
| - |
| 176 | + })); |
| 177 | + |
178 | 178 | //-------------------------------------------------------------------------
|
179 | 179 | // Test Case for readCount()
|
180 | 180 | //-------------------------------------------------------------------------
|
181 |
| - |
| 181 | + |
182 | 182 | suite.add(new YUITest.TestCase({
|
183 |
| - |
| 183 | + |
184 | 184 | name : "readCount() Tests",
|
185 | 185 |
|
186 | 186 |
|
187 | 187 | //---------------------------------------------------------------------
|
188 | 188 | // Tests
|
189 | 189 | //---------------------------------------------------------------------
|
190 |
| - |
| 190 | + |
191 | 191 | /*
|
192 | 192 | * Tests that a set number of characters are read correctly.
|
193 | 193 | */
|
194 | 194 | testReadCountSimple: function(){
|
195 | 195 | var testString = "Hello world!",
|
196 | 196 | reader = new StringReader(testString);
|
197 |
| - |
| 197 | + |
198 | 198 | var result = reader.readCount(6);
|
199 | 199 |
|
200 | 200 | Assert.areEqual("Hello ", result);
|
201 |
| - |
| 201 | + |
202 | 202 | result = reader.readCount(2);
|
203 | 203 | Assert.areEqual("wo", result);
|
204 | 204 | }
|
205 |
| - })); |
206 |
| - |
| 205 | + })); |
| 206 | + |
207 | 207 | //-------------------------------------------------------------------------
|
208 | 208 | // Test Case for readMatch()
|
209 | 209 | //-------------------------------------------------------------------------
|
210 |
| - |
| 210 | + |
211 | 211 | suite.add(new YUITest.TestCase({
|
212 |
| - |
| 212 | + |
213 | 213 | name : "readMatch() Tests",
|
214 | 214 |
|
215 | 215 |
|
216 | 216 | //---------------------------------------------------------------------
|
217 | 217 | // Tests
|
218 | 218 | //---------------------------------------------------------------------
|
219 |
| - |
| 219 | + |
220 | 220 | /*
|
221 | 221 | * Tests that a text pattern is read correctly.
|
222 | 222 | */
|
223 | 223 | testReadMatchSimple: function(){
|
224 | 224 | var testString = "Hello world!",
|
225 | 225 | reader = new StringReader(testString);
|
226 |
| - |
| 226 | + |
227 | 227 | var result = reader.readMatch("Hello");
|
228 | 228 |
|
229 | 229 | Assert.areEqual("Hello", result);
|
230 | 230 | },
|
231 |
| - |
| 231 | + |
232 | 232 | /*
|
233 | 233 | * Tests that a regex pattern is read correctly.
|
234 | 234 | */
|
235 | 235 | testReadMatchRegEx: function(){
|
236 | 236 | var testString = "Hello world!",
|
237 | 237 | reader = new StringReader(testString);
|
238 |
| - |
| 238 | + |
239 | 239 | var result = reader.readMatch(/^Hello/);
|
240 | 240 |
|
241 | 241 | Assert.areEqual("Hello", result);
|
242 | 242 | }
|
243 |
| - |
244 |
| - |
245 |
| - })); |
246 |
| - |
247 |
| - |
248 |
| - |
| 243 | + |
| 244 | + |
| 245 | + })); |
| 246 | + |
| 247 | + |
| 248 | + |
249 | 249 | //-------------------------------------------------------------------------
|
250 | 250 | // Test Case for eof()
|
251 | 251 | //-------------------------------------------------------------------------
|
252 |
| - |
| 252 | + |
253 | 253 | suite.add(new YUITest.TestCase({
|
254 |
| - |
| 254 | + |
255 | 255 | name : "eof() Tests",
|
256 | 256 |
|
257 | 257 |
|
258 | 258 | //---------------------------------------------------------------------
|
259 | 259 | // Tests
|
260 | 260 | //---------------------------------------------------------------------
|
261 |
| - |
| 261 | + |
262 | 262 | /*
|
263 | 263 | * Tests that eof() works after reading to end of string.
|
264 | 264 | */
|
265 | 265 | testTestEofSimple: function(){
|
266 | 266 | var testString = "Hello world!",
|
267 | 267 | reader = new StringReader(testString);
|
268 |
| - |
| 268 | + |
269 | 269 | reader.readTo("!");
|
270 | 270 | Assert.isTrue(reader.eof());
|
271 |
| - } |
272 |
| - |
273 |
| - |
274 |
| - })); |
275 |
| - |
| 271 | + } |
| 272 | + |
| 273 | + |
| 274 | + })); |
| 275 | + |
276 | 276 | //-------------------------------------------------------------------------
|
277 | 277 | // Test Case for mark() and reset()
|
278 | 278 | //-------------------------------------------------------------------------
|
279 |
| - |
| 279 | + |
280 | 280 | suite.add(new YUITest.TestCase({
|
281 |
| - |
| 281 | + |
282 | 282 | name : "mark() and reset() Tests",
|
283 | 283 |
|
284 | 284 |
|
285 | 285 | //---------------------------------------------------------------------
|
286 | 286 | // Tests
|
287 | 287 | //---------------------------------------------------------------------
|
288 |
| - |
| 288 | + |
289 | 289 | /*
|
290 | 290 | * Tests that mark() and reset() preserve lines/cols correctly.
|
291 | 291 | */
|
292 | 292 | testMarkResetSimple: function(){
|
293 | 293 | var testString = "Hello world!",
|
294 | 294 | reader = new StringReader(testString);
|
295 |
| - |
| 295 | + |
296 | 296 | reader.mark();
|
297 | 297 | reader.readTo("!");
|
298 | 298 | reader.reset();
|
299 |
| - |
| 299 | + |
300 | 300 | Assert.areEqual(1, reader.getLine(), "Row should be 1");
|
301 | 301 | Assert.areEqual(1, reader.getCol(), "Column should be 1");
|
302 |
| - } |
303 |
| - |
304 |
| - |
305 |
| - })); |
306 |
| - |
| 302 | + } |
| 303 | + |
| 304 | + |
| 305 | + })); |
| 306 | + |
307 | 307 | YUITest.TestRunner.add(suite);
|
308 | 308 |
|
309 | 309 | })();
|
0 commit comments