2 * Created on 28.04.2003
5 package net.sourceforge.phpeclipse.phpeditor.php.test;
7 import org.eclipse.jface.text.*;
8 import org.eclipse.jface.text.rules.*;
10 import net.sourceforge.phpeclipse.phpeditor.php.*;
11 import junit.framework.*;
14 * Testcase for the PHPPartitionScanner
15 * @author Stefan Langer
16 * @version $Revision: 1.2 $
18 public class PHPPartitionScannerTest extends TestCase
20 private PHPPartitionScanner fScanner;
21 private IDocument fDocument;
24 * @see junit.framework.TestCase#setUp()
26 protected void setUp() throws Exception
28 fScanner = new PHPPartitionScanner();
29 fDocument = new DummyDocument();
32 public void testPHPPartition()
36 + "$test = \"<?php this is a dummy text ?>\";\n"
41 fScanner.setRange(fDocument, 0, fDocument.getLength());
42 IToken token = fScanner.nextToken();
43 junit.framework.Assert.assertEquals(
44 IPHPPartitionScannerConstants.PHP,
45 (String) token.getData());
46 junit.framework.Assert.assertEquals(
47 fDocument.getLength(),
48 fScanner.getTokenLength());
49 junit.framework.Assert.assertEquals(0, fScanner.getTokenOffset());
52 public void testBrokenPHPPartition()
56 + "$test = \"<?php this is a dummy text ?>;\n"
60 fScanner.setRange(fDocument, 0, fDocument.getLength());
61 IToken token = fScanner.nextToken();
62 junit.framework.Assert.assertEquals(
63 IPHPPartitionScannerConstants.PHP,
64 (String) token.getData());
65 junit.framework.Assert.assertEquals(
66 fDocument.getLength(),
67 fScanner.getTokenLength());
68 junit.framework.Assert.assertEquals(0, fScanner.getTokenOffset());
71 public void testPHP_PHPCOMMENT_HTML_Partition()
73 String php1 = "<? echo \"This is php with\"; ";
74 String phpcomment1 = "/** @param This is a comment ?> */";
75 String php2 = "echo \" short tags enabled!\"; ?>";
76 String html1 = "<html><head><title>";
78 "<? echo \"/** this is no comment */The ?> <?php Title\"?>";
79 String html2 = "</title></head><body>";
80 String php4 = "<? echo \"Some text ?>\"\n\r";
82 "/** This is another comment @param test @return test*/";
83 String php5 = " echo \" just to fill up another php partition\"?>";
84 String html3 = "<p>Copyright 2003 by PHPEclipse</body></html>";
97 fScanner.setRange(fDocument, 0, fDocument.getLength());
99 IToken token = fScanner.nextToken();
100 junit.framework.Assert.assertEquals(
101 "PHP Partition 1 not recognized!",
102 IPHPPartitionScannerConstants.PHP,
103 (String) token.getData());
104 junit.framework.Assert.assertEquals(
105 "Length of PHP Partition 1 not correct!",
107 fScanner.getTokenLength());
108 junit.framework.Assert.assertEquals(
109 "Offset of PHP Partition 1 not correct!",
111 fScanner.getTokenOffset());
113 token = fScanner.nextToken();
114 junit.framework.Assert.assertEquals(
115 "PHPComment Partition 1 not recognized!",
116 IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT,
117 (String) token.getData());
118 junit.framework.Assert.assertEquals(
119 "Length of PHPComment Partition 1 not correct!",
120 phpcomment1.length(),
121 fScanner.getTokenLength());
122 junit.framework.Assert.assertEquals(
123 "Offset of PHPComment Partition 1 not correct!",
125 fScanner.getTokenOffset());
128 token = fScanner.nextToken();
129 junit.framework.Assert.assertEquals(
130 "PHP Partition 2 not recognized!",
131 IPHPPartitionScannerConstants.PHP,
132 (String) token.getData());
133 junit.framework.Assert.assertEquals(
134 "Length of PHP Partition 2 not correct!",
136 fScanner.getTokenLength());
137 junit.framework.Assert.assertEquals(
138 "Offset of PHP Partition 2 not correct!",
139 php1.length() + phpcomment1.length(),
140 fScanner.getTokenOffset());
143 token = fScanner.nextToken();
144 junit.framework.Assert.assertEquals(
145 "HTML 1 not recognized!",
146 IPHPPartitionScannerConstants.HTML,
147 (String) token.getData());
148 junit.framework.Assert.assertEquals(
149 "Length of HTML 1 not correct!",
151 fScanner.getTokenLength());
152 junit.framework.Assert.assertEquals(
153 "Offset of HTML 1 not correct!",
154 php1.length() + phpcomment1.length() + php2.length(),
155 fScanner.getTokenOffset());
158 token = fScanner.nextToken();
159 junit.framework.Assert.assertEquals(
160 "PHP Partition 3 not recognized!",
161 IPHPPartitionScannerConstants.PHP,
162 (String) token.getData());
163 junit.framework.Assert.assertEquals(
164 "Length of PHP Partition 3 not correct!",
166 fScanner.getTokenLength());
167 junit.framework.Assert.assertEquals(
168 "Offset of PHP Partition 3 not correct!",
170 + phpcomment1.length()
173 fScanner.getTokenOffset());
176 token = fScanner.nextToken();
177 junit.framework.Assert.assertEquals(
178 "HTML 2 not recognized!",
179 IPHPPartitionScannerConstants.HTML,
180 (String) token.getData());
181 junit.framework.Assert.assertEquals(
182 "Length of HTML 2 not correct!",
184 fScanner.getTokenLength());
185 junit.framework.Assert.assertEquals(
186 "Offset of HTML 2 not correct!",
188 + phpcomment1.length()
192 fScanner.getTokenOffset());
195 token = fScanner.nextToken();
196 junit.framework.Assert.assertEquals(
197 "PHP Partition 4 not recognized!",
198 IPHPPartitionScannerConstants.PHP,
199 (String) token.getData());
200 junit.framework.Assert.assertEquals(
201 "Length of PHP Partition 4 not correct!",
203 fScanner.getTokenLength());
204 junit.framework.Assert.assertEquals(
205 "Offset of PHP Partition 4 not correct!",
207 + phpcomment1.length()
212 fScanner.getTokenOffset());
215 token = fScanner.nextToken();
216 junit.framework.Assert.assertEquals(
217 "PHP Multilinecomment 2 not recognized!",
218 IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT,
219 (String) token.getData());
220 junit.framework.Assert.assertEquals(
221 "Length of PHP Multilinecomment 2 not correct!",
222 phpcomment2.length(),
223 fScanner.getTokenLength());
224 junit.framework.Assert.assertEquals(
225 "Offset of PHP Multilinecomment 2 not correct!",
227 + phpcomment1.length()
233 fScanner.getTokenOffset());
236 token = fScanner.nextToken();
237 junit.framework.Assert.assertEquals(
238 "PHP Partition 5 not recognized!",
239 IPHPPartitionScannerConstants.PHP,
240 (String) token.getData());
241 junit.framework.Assert.assertEquals(
242 "Length of PHP Partition 5 not correct!",
244 fScanner.getTokenLength());
245 junit.framework.Assert.assertEquals(
246 "Offset of PHP Partition 5 not correct!",
248 + phpcomment1.length()
254 + phpcomment2.length(),
255 fScanner.getTokenOffset());
257 token = fScanner.nextToken();
258 junit.framework.Assert.assertEquals(
259 "HTML 3 not recognized!",
260 IPHPPartitionScannerConstants.HTML,
261 (String) token.getData());
262 junit.framework.Assert.assertEquals(
263 "Length of HTML 3 not correct!",
265 fScanner.getTokenLength());
266 junit.framework.Assert.assertEquals(
267 "Offset of HTML 3 not correct!",
269 + phpcomment1.length()
275 + phpcomment2.length()
277 fScanner.getTokenOffset());
281 public void testPHP_HTML_Partition()
283 String php1 = "<? echo \"This is php with short tags enabled!\"; ?>";
284 String html1 = "<html><head><title>";
285 String php2 = "<? echo \"The ?> <?php Title\"?>";
286 String html2 = "</title></head><body>";
288 "<? echo \"Some text ?> just to fill up another php partition\"?>";
289 String html3 = "<p>Copyright 2003 by PHPEclipse</body></html>";
291 fDocument.set(php1 + html1 + php2 + html2 + php3 + html3);
292 fScanner.setRange(fDocument, 0, fDocument.getLength());
294 IToken token = fScanner.nextToken();
295 junit.framework.Assert.assertEquals(
296 "PHP Partition 1 not recognized!",
297 IPHPPartitionScannerConstants.PHP,
298 (String) token.getData());
299 junit.framework.Assert.assertEquals(
300 "Length of PHP Partition 1 not correct!",
302 fScanner.getTokenLength());
303 junit.framework.Assert.assertEquals(
304 "Offset of PHP Partition 1 not correct!",
306 fScanner.getTokenOffset());
308 token = fScanner.nextToken();
309 junit.framework.Assert.assertEquals(
310 "HTML 1 not recognized!",
311 IPHPPartitionScannerConstants.HTML,
312 (String) token.getData());
313 junit.framework.Assert.assertEquals(
314 "Length of HTML 1 not correct!",
316 fScanner.getTokenLength());
317 junit.framework.Assert.assertEquals(
318 "Offset of HTML 1 not correct!",
320 fScanner.getTokenOffset());
322 token = fScanner.nextToken();
323 junit.framework.Assert.assertEquals(
324 "PHP Partition 2 not recognized!",
325 IPHPPartitionScannerConstants.PHP,
326 (String) token.getData());
327 junit.framework.Assert.assertEquals(
328 "Length of PHP Partition 2 not correct!",
330 fScanner.getTokenLength());
331 junit.framework.Assert.assertEquals(
332 "Offset of PHP Partition 2 not correct!",
333 html1.length() + php1.length(),
334 fScanner.getTokenOffset());
336 token = fScanner.nextToken();
337 junit.framework.Assert.assertEquals(
338 "HTML 2 not recognized!",
339 IPHPPartitionScannerConstants.HTML,
340 (String) token.getData());
341 junit.framework.Assert.assertEquals(
342 "Length of HTML 2 not correct!",
344 fScanner.getTokenLength());
345 junit.framework.Assert.assertEquals(
346 "Offset of HTML 2 not correct!",
347 php1.length() + html1.length() + php2.length(),
348 fScanner.getTokenOffset());
350 token = fScanner.nextToken();
351 junit.framework.Assert.assertEquals(
352 "PHP Partition 3 not recognized!",
353 IPHPPartitionScannerConstants.PHP,
354 (String) token.getData());
355 junit.framework.Assert.assertEquals(
356 "Length of PHP Partition 3 not correct!",
358 fScanner.getTokenLength());
359 junit.framework.Assert.assertEquals(
360 "Offset of PHP Partition 3 not correct!",
361 html2.length() + php2.length() + html1.length() + php1.length(),
362 fScanner.getTokenOffset());
364 token = fScanner.nextToken();
365 junit.framework.Assert.assertEquals(
366 "HTML 3 not recognized!",
367 IPHPPartitionScannerConstants.HTML,
368 (String) token.getData());
369 junit.framework.Assert.assertEquals(
370 "Length of HTML 3 not correct!",
372 fScanner.getTokenLength());
373 junit.framework.Assert.assertEquals(
374 "Offset of HTML 3 not correct!",
380 fScanner.getTokenOffset());
383 public void testHTMLPartition()
386 "<html><head><title>Some Text</title></head><body>"
387 + "<h1>Test</h1><p>Nothing particular</body></html>";
389 fScanner.setRange(fDocument, 0, fDocument.getLength());
390 IToken token = fScanner.nextToken();
391 junit.framework.Assert.assertEquals(
392 IPHPPartitionScannerConstants.HTML,
393 (String) token.getData());
394 junit.framework.Assert.assertEquals(
395 fDocument.getLength(),
396 fScanner.getTokenLength());
397 junit.framework.Assert.assertEquals(0, fScanner.getTokenOffset());
400 public void testPHPMultiLineCommentPartition()
402 String text = "<?php $test=\"Some <?php ?> /** */ data\";";
404 "/** A comment with <?php This is acutally not a php partition ?> some data \n"
405 + " * @param test A test parameter \n"
408 "\nfunction test($test)\n"
410 + " echo \"Test <?php /** This is not a comment */function alsoItLooksLikeOne(){echo \"It is actually not a comment\";} ?>\";\n"
412 fDocument.set(text + text2 + text3);
413 fScanner.setRange(fDocument, 0, fDocument.getLength());
415 IToken token = fScanner.nextToken();
416 junit.framework.Assert.assertEquals(
417 "PHP Partition part 1 not recognized!",
418 IPHPPartitionScannerConstants.PHP,
419 (String) token.getData());
420 junit.framework.Assert.assertEquals(
421 "Length of PHP Partition part 1 not correct!",
423 fScanner.getTokenLength());
424 junit.framework.Assert.assertEquals(
425 "Offset of PHP Partition part 1 not correct!",
427 fScanner.getTokenOffset());
428 // check for multiline
429 token = fScanner.nextToken();
430 junit.framework.Assert.assertEquals(
431 "PHP Multiline not recognized!",
432 IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT,
433 (String) token.getData());
434 junit.framework.Assert.assertEquals(
435 "Length of PHP Multinline not correct!",
437 fScanner.getTokenLength());
438 junit.framework.Assert.assertEquals(
439 "Offset of PHP Multiline not correct!",
441 fScanner.getTokenOffset());
443 token = fScanner.nextToken();
444 junit.framework.Assert.assertEquals(
445 "PHP Partition part 2 not recognized!",
446 IPHPPartitionScannerConstants.PHP,
447 (String) token.getData());
448 junit.framework.Assert.assertEquals(
449 "Length of PHP Partition part 2 not correct!",
451 fScanner.getTokenLength());
452 junit.framework.Assert.assertEquals(
453 "Offset of PHP Partition part 2 not correct!",
454 text.length() + text2.length(),
455 fScanner.getTokenOffset());