c94f098ed5e349f883055b85e19953f799ca34a4
[phpeclipse.git] / net.sourceforge.phpeclipse.tests / src / net / sourceforge / phpeclipse / phpeditor / php / test / PHPPartitionScannerTest.java
1 /*
2  * Created on 28.04.2003
3  *
4  */
5 package net.sourceforge.phpeclipse.phpeditor.php.test;
6
7 import junit.framework.*;
8 import net.sourceforge.phpeclipse.phpeditor.php.*;
9
10 import org.eclipse.jface.text.*;
11 import org.eclipse.jface.text.rules.*;
12
13 /**
14  * Testcase for the PHPPartitionScanner
15  * @author Stefan Langer
16  * @version $Revision: 1.1 $
17  */
18 public class PHPPartitionScannerTest extends TestCase
19 {
20         private PHPPartitionScanner fScanner;
21         private IDocument fDocument;
22         
23     /* (non-Javadoc)
24      * @see junit.framework.TestCase#setUp()
25      */
26     protected void setUp() throws Exception
27     {
28         fScanner = new PHPPartitionScanner();   
29         fDocument = new DummyDocument();     
30     }
31
32     public void testPHPPartition()
33     {
34         String text =
35             "<?php\n"
36                 + "$test = \"<?php this is a dummy text ?>\";\n"
37                 + "function test()\n"
38                 + "{echo 'Test!';}\n"
39                 + "?>";
40         fDocument.set(text);
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());
50     }
51
52     public void testBrokenPHPPartition()
53     {
54         String text =
55             "<?php\n"
56                 + "$test = \"<?php this is a dummy text ?>;\n"
57                 + "function test()\n"
58                 + "{echo 'Tes";
59         fDocument.set(text);
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());
69     }
70     
71     public void testPHP_PHPCOMMENT_HTML_Partition()
72     {
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>";
77         String php3 =
78             "<? echo \"/** this is no comment */The ?> <?php Title\"?>";
79         String html2 = "</title></head><body>";
80         String php4 = "<? echo \"Some text ?>\"\n\r";
81         String phpcomment2 =
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>";
85
86         fDocument.set(
87             php1
88                 + phpcomment1
89                 + php2
90                 + html1
91                 + php3
92                 + html2
93                 + php4
94                 + phpcomment2
95                 + php5
96                 + html3);
97         fScanner.setRange(fDocument, 0, fDocument.getLength());
98         // php 1
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!",
106             php1.length(),
107             fScanner.getTokenLength());
108         junit.framework.Assert.assertEquals(
109             "Offset of PHP Partition 1 not correct!",
110             0,
111             fScanner.getTokenOffset());
112         // comment1  
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!",
124             php1.length(),
125             fScanner.getTokenOffset());
126
127         // php 2
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!",
135             php2.length(),
136             fScanner.getTokenLength());
137         junit.framework.Assert.assertEquals(
138             "Offset of PHP Partition 2 not correct!",
139             php1.length() + phpcomment1.length(),
140             fScanner.getTokenOffset());
141
142         // check html 1
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!",
150             html1.length(),
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());
156
157         //      php 3
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!",
165             php3.length(),
166             fScanner.getTokenLength());
167         junit.framework.Assert.assertEquals(
168             "Offset of PHP Partition 3 not correct!",
169             php1.length()
170                 + phpcomment1.length()
171                 + php2.length()
172                 + html1.length(),
173             fScanner.getTokenOffset());
174
175         //      check html 2
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!",
183             html2.length(),
184             fScanner.getTokenLength());
185         junit.framework.Assert.assertEquals(
186             "Offset of HTML 2 not correct!",
187             php1.length()
188                 + phpcomment1.length()
189                 + php2.length()
190                 + html1.length()
191                 + php3.length(),
192             fScanner.getTokenOffset());
193
194         //      php 4
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!",
202             php4.length(),
203             fScanner.getTokenLength());
204         junit.framework.Assert.assertEquals(
205             "Offset of PHP Partition 4 not correct!",
206             php1.length()
207                 + phpcomment1.length()
208                 + php2.length()
209                 + html1.length()
210                 + php3.length()
211                 + html2.length(),
212             fScanner.getTokenOffset());
213
214         //      check comment 2
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!",
226             php1.length()
227                 + phpcomment1.length()
228                 + php2.length()
229                 + html1.length()
230                 + php3.length()
231                 + html2.length()
232                 + php4.length(),
233             fScanner.getTokenOffset());
234
235         //      php 5
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!",
243             php5.length(),
244             fScanner.getTokenLength());
245         junit.framework.Assert.assertEquals(
246             "Offset of PHP Partition 5 not correct!",
247             php1.length()
248                 + phpcomment1.length()
249                 + php2.length()
250                 + html1.length()
251                 + php3.length()
252                 + html2.length()
253                 + php4.length()
254                 + phpcomment2.length(),
255             fScanner.getTokenOffset());
256         //      check html 3
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!",
264             html3.length(),
265             fScanner.getTokenLength());
266         junit.framework.Assert.assertEquals(
267             "Offset of HTML 3 not correct!",
268             php1.length()
269                 + phpcomment1.length()
270                 + php2.length()
271                 + html1.length()
272                 + php3.length()
273                 + html2.length()
274                 + php4.length()
275                 + phpcomment2.length()
276                 + php5.length(),
277             fScanner.getTokenOffset());
278
279     }
280
281     public void testPHP_HTML_Partition()
282     {
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>";
287         String php3 =
288             "<? echo \"Some text ?> just to fill up another php partition\"?>";
289         String html3 = "<p>Copyright 2003 by PHPEclipse</body></html>";
290
291         fDocument.set(php1 + html1 + php2 + html2 + php3 + html3);
292         fScanner.setRange(fDocument, 0, fDocument.getLength());
293         // php 1
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!",
301             php1.length(),
302             fScanner.getTokenLength());
303         junit.framework.Assert.assertEquals(
304             "Offset of PHP Partition 1 not correct!",
305             0,
306             fScanner.getTokenOffset());
307         // check html 1
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!",
315             html1.length(),
316             fScanner.getTokenLength());
317         junit.framework.Assert.assertEquals(
318             "Offset of HTML 1 not correct!",
319             php1.length(),
320             fScanner.getTokenOffset());
321         //      php 2
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!",
329             php2.length(),
330             fScanner.getTokenLength());
331         junit.framework.Assert.assertEquals(
332             "Offset of PHP Partition 2 not correct!",
333             html1.length() + php1.length(),
334             fScanner.getTokenOffset());
335         //      check html 2
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!",
343             html2.length(),
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());
349         //      php 3
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!",
357             php3.length(),
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());
363         //      check html 3
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!",
371             html3.length(),
372             fScanner.getTokenLength());
373         junit.framework.Assert.assertEquals(
374             "Offset of HTML 3 not correct!",
375             php1.length()
376                 + html1.length()
377                 + php2.length()
378                 + html2.length()
379                 + php3.length(),
380             fScanner.getTokenOffset());
381     }
382
383     public void testHTMLPartition()
384     {
385         String text =
386             "<html><head><title>Some Text</title></head><body>"
387                 + "<h1>Test</h1><p>Nothing particular</body></html>";
388         fDocument.set(text);
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());
398     }
399
400     public void testPHPMultiLineCommentPartition()
401     {
402         String text = "<?php $test=\"Some <?php ?> /** */ data\";";
403         String text2 =
404             "/** A comment with <?php This is acutally not a php partition ?> some data \n"
405                 + " * @param test A test parameter \n"
406                 + " */";
407         String text3 =
408             "\nfunction test($test)\n"
409                 + "{\n"
410                 + "     echo \"Test <?php /** This is not a comment */function alsoItLooksLikeOne(){echo \"It is actually not a comment\";} ?>\";\n"
411                 + "}?>";
412         fDocument.set(text + text2 + text3);
413         fScanner.setRange(fDocument, 0, fDocument.getLength());
414         // first half of php
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!",
422             text.length(),
423             fScanner.getTokenLength());
424         junit.framework.Assert.assertEquals(
425             "Offset of PHP Partition part 1 not correct!",
426             0,
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!",
436             text2.length(),
437             fScanner.getTokenLength());
438         junit.framework.Assert.assertEquals(
439             "Offset of PHP Multiline not correct!",
440             text.length(),
441             fScanner.getTokenOffset());
442         // rest of php
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!",
450             text3.length(),
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());
456     }
457 }