2 * Created on 28.04.2003
5 package net.sourceforge.phpeclipse.phpeditor.php.test;
7 import junit.framework.*;
8 import net.sourceforge.phpdt.internal.ui.text.*;
9 import net.sourceforge.phpeclipse.phpeditor.php.*;
11 import org.eclipse.jface.text.*;
12 import org.eclipse.jface.text.rules.*;
15 * Testcase for the PHPPartitionScanner
16 * @author Stefan Langer
17 * @version $Revision: 1.3 $
19 public class PHPPartitionScannerTest extends TestCase
21 private PHPPartitionScanner fScanner;
22 private IDocument fDocument;
25 * @see junit.framework.TestCase#setUp()
27 protected void setUp() throws Exception
29 fScanner = new PHPPartitionScanner();
30 fDocument = new DummyDocument();
33 public void testPHPPartition()
37 + "$test = \"<?php this is a dummy text ?>\";\n"
42 fScanner.setRange(fDocument, 0, fDocument.getLength());
43 IToken token = fScanner.nextToken();
44 junit.framework.Assert.assertEquals(
45 IPHPPartitions.PHP_PARTITIONING,
46 (String) token.getData());
47 junit.framework.Assert.assertEquals(
48 fDocument.getLength(),
49 fScanner.getTokenLength());
50 junit.framework.Assert.assertEquals(0, fScanner.getTokenOffset());
53 public void testBrokenPHPPartition()
57 + "$test = \"<?php this is a dummy text ?>;\n"
61 fScanner.setRange(fDocument, 0, fDocument.getLength());
62 IToken token = fScanner.nextToken();
63 junit.framework.Assert.assertEquals(
64 IPHPPartitions.PHP_PARTITIONING,
65 (String) token.getData());
66 junit.framework.Assert.assertEquals(
67 fDocument.getLength(),
68 fScanner.getTokenLength());
69 junit.framework.Assert.assertEquals(0, fScanner.getTokenOffset());
72 public void testPHP_PHPCOMMENT_HTML_Partition()
74 String php1 = "<? echo \"This is php with\"; ";
75 String phpcomment1 = "/** @param This is a comment ?> */";
76 String php2 = "echo \" short tags enabled!\"; ?>";
77 String html1 = "<html><head><title>";
79 "<? echo \"/** this is no comment */The ?> <?php Title\"?>";
80 String html2 = "</title></head><body>";
81 String php4 = "<? echo \"Some text ?>\"\n\r";
83 "/** This is another comment @param test @return test*/";
84 String php5 = " echo \" just to fill up another php partition\"?>";
85 String html3 = "<p>Copyright 2003 by PHPEclipse</body></html>";
98 fScanner.setRange(fDocument, 0, fDocument.getLength());
100 IToken token = fScanner.nextToken();
101 junit.framework.Assert.assertEquals(
102 "PHP Partition 1 not recognized!",
103 IPHPPartitions.PHP_PARTITIONING,
104 (String) token.getData());
105 junit.framework.Assert.assertEquals(
106 "Length of PHP Partition 1 not correct!",
108 fScanner.getTokenLength());
109 junit.framework.Assert.assertEquals(
110 "Offset of PHP Partition 1 not correct!",
112 fScanner.getTokenOffset());
114 token = fScanner.nextToken();
115 junit.framework.Assert.assertEquals(
116 "PHPComment Partition 1 not recognized!",
117 IPHPPartitions.PHP_PHPDOC_COMMENT,
118 (String) token.getData());
119 junit.framework.Assert.assertEquals(
120 "Length of PHPComment Partition 1 not correct!",
121 phpcomment1.length(),
122 fScanner.getTokenLength());
123 junit.framework.Assert.assertEquals(
124 "Offset of PHPComment Partition 1 not correct!",
126 fScanner.getTokenOffset());
129 token = fScanner.nextToken();
130 junit.framework.Assert.assertEquals(
131 "PHP Partition 2 not recognized!",
132 IPHPPartitions.PHP_PARTITIONING,
133 (String) token.getData());
134 junit.framework.Assert.assertEquals(
135 "Length of PHP Partition 2 not correct!",
137 fScanner.getTokenLength());
138 junit.framework.Assert.assertEquals(
139 "Offset of PHP Partition 2 not correct!",
140 php1.length() + phpcomment1.length(),
141 fScanner.getTokenOffset());
144 token = fScanner.nextToken();
145 junit.framework.Assert.assertEquals(
146 "HTML 1 not recognized!",
148 (String) token.getData());
149 junit.framework.Assert.assertEquals(
150 "Length of HTML 1 not correct!",
152 fScanner.getTokenLength());
153 junit.framework.Assert.assertEquals(
154 "Offset of HTML 1 not correct!",
155 php1.length() + phpcomment1.length() + php2.length(),
156 fScanner.getTokenOffset());
159 token = fScanner.nextToken();
160 junit.framework.Assert.assertEquals(
161 "PHP Partition 3 not recognized!",
162 IPHPPartitions.PHP_PARTITIONING,
163 (String) token.getData());
164 junit.framework.Assert.assertEquals(
165 "Length of PHP Partition 3 not correct!",
167 fScanner.getTokenLength());
168 junit.framework.Assert.assertEquals(
169 "Offset of PHP Partition 3 not correct!",
171 + phpcomment1.length()
174 fScanner.getTokenOffset());
177 token = fScanner.nextToken();
178 junit.framework.Assert.assertEquals(
179 "HTML 2 not recognized!",
181 (String) token.getData());
182 junit.framework.Assert.assertEquals(
183 "Length of HTML 2 not correct!",
185 fScanner.getTokenLength());
186 junit.framework.Assert.assertEquals(
187 "Offset of HTML 2 not correct!",
189 + phpcomment1.length()
193 fScanner.getTokenOffset());
196 token = fScanner.nextToken();
197 junit.framework.Assert.assertEquals(
198 "PHP Partition 4 not recognized!",
199 IPHPPartitions.PHP_PARTITIONING,
200 (String) token.getData());
201 junit.framework.Assert.assertEquals(
202 "Length of PHP Partition 4 not correct!",
204 fScanner.getTokenLength());
205 junit.framework.Assert.assertEquals(
206 "Offset of PHP Partition 4 not correct!",
208 + phpcomment1.length()
213 fScanner.getTokenOffset());
216 token = fScanner.nextToken();
217 junit.framework.Assert.assertEquals(
218 "PHP Multilinecomment 2 not recognized!",
219 IPHPPartitions.PHP_PHPDOC_COMMENT,
220 (String) token.getData());
221 junit.framework.Assert.assertEquals(
222 "Length of PHP Multilinecomment 2 not correct!",
223 phpcomment2.length(),
224 fScanner.getTokenLength());
225 junit.framework.Assert.assertEquals(
226 "Offset of PHP Multilinecomment 2 not correct!",
228 + phpcomment1.length()
234 fScanner.getTokenOffset());
237 token = fScanner.nextToken();
238 junit.framework.Assert.assertEquals(
239 "PHP Partition 5 not recognized!",
240 IPHPPartitions.PHP_PARTITIONING,
241 (String) token.getData());
242 junit.framework.Assert.assertEquals(
243 "Length of PHP Partition 5 not correct!",
245 fScanner.getTokenLength());
246 junit.framework.Assert.assertEquals(
247 "Offset of PHP Partition 5 not correct!",
249 + phpcomment1.length()
255 + phpcomment2.length(),
256 fScanner.getTokenOffset());
258 token = fScanner.nextToken();
259 junit.framework.Assert.assertEquals(
260 "HTML 3 not recognized!",
262 (String) token.getData());
263 junit.framework.Assert.assertEquals(
264 "Length of HTML 3 not correct!",
266 fScanner.getTokenLength());
267 junit.framework.Assert.assertEquals(
268 "Offset of HTML 3 not correct!",
270 + phpcomment1.length()
276 + phpcomment2.length()
278 fScanner.getTokenOffset());
282 public void testPHP_HTML_Partition()
284 String php1 = "<? echo \"This is php with short tags enabled!\"; ?>";
285 String html1 = "<html><head><title>";
286 String php2 = "<? echo \"The ?> <?php Title\"?>";
287 String html2 = "</title></head><body>";
289 "<? echo \"Some text ?> just to fill up another php partition\"?>";
290 String html3 = "<p>Copyright 2003 by PHPEclipse</body></html>";
292 fDocument.set(php1 + html1 + php2 + html2 + php3 + html3);
293 fScanner.setRange(fDocument, 0, fDocument.getLength());
295 IToken token = fScanner.nextToken();
296 junit.framework.Assert.assertEquals(
297 "PHP Partition 1 not recognized!",
298 IPHPPartitions.PHP_PARTITIONING,
299 (String) token.getData());
300 junit.framework.Assert.assertEquals(
301 "Length of PHP Partition 1 not correct!",
303 fScanner.getTokenLength());
304 junit.framework.Assert.assertEquals(
305 "Offset of PHP Partition 1 not correct!",
307 fScanner.getTokenOffset());
309 token = fScanner.nextToken();
310 junit.framework.Assert.assertEquals(
311 "HTML 1 not recognized!",
313 (String) token.getData());
314 junit.framework.Assert.assertEquals(
315 "Length of HTML 1 not correct!",
317 fScanner.getTokenLength());
318 junit.framework.Assert.assertEquals(
319 "Offset of HTML 1 not correct!",
321 fScanner.getTokenOffset());
323 token = fScanner.nextToken();
324 junit.framework.Assert.assertEquals(
325 "PHP Partition 2 not recognized!",
326 IPHPPartitions.PHP_PARTITIONING,
327 (String) token.getData());
328 junit.framework.Assert.assertEquals(
329 "Length of PHP Partition 2 not correct!",
331 fScanner.getTokenLength());
332 junit.framework.Assert.assertEquals(
333 "Offset of PHP Partition 2 not correct!",
334 html1.length() + php1.length(),
335 fScanner.getTokenOffset());
337 token = fScanner.nextToken();
338 junit.framework.Assert.assertEquals(
339 "HTML 2 not recognized!",
341 (String) token.getData());
342 junit.framework.Assert.assertEquals(
343 "Length of HTML 2 not correct!",
345 fScanner.getTokenLength());
346 junit.framework.Assert.assertEquals(
347 "Offset of HTML 2 not correct!",
348 php1.length() + html1.length() + php2.length(),
349 fScanner.getTokenOffset());
351 token = fScanner.nextToken();
352 junit.framework.Assert.assertEquals(
353 "PHP Partition 3 not recognized!",
354 IPHPPartitions.PHP_PARTITIONING,
355 (String) token.getData());
356 junit.framework.Assert.assertEquals(
357 "Length of PHP Partition 3 not correct!",
359 fScanner.getTokenLength());
360 junit.framework.Assert.assertEquals(
361 "Offset of PHP Partition 3 not correct!",
362 html2.length() + php2.length() + html1.length() + php1.length(),
363 fScanner.getTokenOffset());
365 token = fScanner.nextToken();
366 junit.framework.Assert.assertEquals(
367 "HTML 3 not recognized!",
369 (String) token.getData());
370 junit.framework.Assert.assertEquals(
371 "Length of HTML 3 not correct!",
373 fScanner.getTokenLength());
374 junit.framework.Assert.assertEquals(
375 "Offset of HTML 3 not correct!",
381 fScanner.getTokenOffset());
384 public void testHTMLPartition()
387 "<html><head><title>Some Text</title></head><body>"
388 + "<h1>Test</h1><p>Nothing particular</body></html>";
390 fScanner.setRange(fDocument, 0, fDocument.getLength());
391 IToken token = fScanner.nextToken();
392 junit.framework.Assert.assertEquals(
394 (String) token.getData());
395 junit.framework.Assert.assertEquals(
396 fDocument.getLength(),
397 fScanner.getTokenLength());
398 junit.framework.Assert.assertEquals(0, fScanner.getTokenOffset());
401 public void testPHPMultiLineCommentPartition()
403 String text = "<?php $test=\"Some <?php ?> /** */ data\";";
405 "/** A comment with <?php This is acutally not a php partition ?> some data \n"
406 + " * @param test A test parameter \n"
409 "\nfunction test($test)\n"
411 + " echo \"Test <?php /** This is not a comment */function alsoItLooksLikeOne(){echo \"It is actually not a comment\";} ?>\";\n"
413 fDocument.set(text + text2 + text3);
414 fScanner.setRange(fDocument, 0, fDocument.getLength());
416 IToken token = fScanner.nextToken();
417 junit.framework.Assert.assertEquals(
418 "PHP Partition part 1 not recognized!",
419 IPHPPartitions.PHP_PARTITIONING,
420 (String) token.getData());
421 junit.framework.Assert.assertEquals(
422 "Length of PHP Partition part 1 not correct!",
424 fScanner.getTokenLength());
425 junit.framework.Assert.assertEquals(
426 "Offset of PHP Partition part 1 not correct!",
428 fScanner.getTokenOffset());
429 // check for multiline
430 token = fScanner.nextToken();
431 junit.framework.Assert.assertEquals(
432 "PHP Multiline not recognized!",
433 IPHPPartitions.PHP_PHPDOC_COMMENT,
434 (String) token.getData());
435 junit.framework.Assert.assertEquals(
436 "Length of PHP Multinline not correct!",
438 fScanner.getTokenLength());
439 junit.framework.Assert.assertEquals(
440 "Offset of PHP Multiline not correct!",
442 fScanner.getTokenOffset());
444 token = fScanner.nextToken();
445 junit.framework.Assert.assertEquals(
446 "PHP Partition part 2 not recognized!",
447 IPHPPartitions.PHP_PARTITIONING,
448 (String) token.getData());
449 junit.framework.Assert.assertEquals(
450 "Length of PHP Partition part 2 not correct!",
452 fScanner.getTokenLength());
453 junit.framework.Assert.assertEquals(
454 "Offset of PHP Partition part 2 not correct!",
455 text.length() + text2.length(),
456 fScanner.getTokenOffset());