misc changes
[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.phpdt.internal.ui.text.*;
9 import net.sourceforge.phpeclipse.phpeditor.php.*;
10
11 import org.eclipse.jface.text.*;
12 import org.eclipse.jface.text.rules.*;
13
14 /**
15  * Testcase for the PHPPartitionScanner
16  * @author Stefan Langer
17  * @version $Revision: 1.2 $
18  */
19 public class PHPPartitionScannerTest extends TestCase
20 {
21         private PHPPartitionScanner fScanner;
22         private IDocument fDocument;
23         
24     /* (non-Javadoc)
25      * @see junit.framework.TestCase#setUp()
26      */
27     protected void setUp() throws Exception
28     {
29         fScanner = new PHPPartitionScanner();   
30         fDocument = new DummyDocument();     
31     }
32
33     public void testPHPPartition()
34     {
35         String text =
36             "<?php\n"
37                 + "$test = \"<?php this is a dummy text ?>\";\n"
38                 + "function test()\n"
39                 + "{echo 'Test!';}\n"
40                 + "?>";
41         fDocument.set(text);
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());
51     }
52
53     public void testBrokenPHPPartition()
54     {
55         String text =
56             "<?php\n"
57                 + "$test = \"<?php this is a dummy text ?>;\n"
58                 + "function test()\n"
59                 + "{echo 'Tes";
60         fDocument.set(text);
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());
70     }
71     
72     public void testPHP_PHPCOMMENT_HTML_Partition()
73     {
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>";
78         String php3 =
79             "<? echo \"/** this is no comment */The ?> <?php Title\"?>";
80         String html2 = "</title></head><body>";
81         String php4 = "<? echo \"Some text ?>\"\n\r";
82         String phpcomment2 =
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>";
86
87         fDocument.set(
88             php1
89                 + phpcomment1
90                 + php2
91                 + html1
92                 + php3
93                 + html2
94                 + php4
95                 + phpcomment2
96                 + php5
97                 + html3);
98         fScanner.setRange(fDocument, 0, fDocument.getLength());
99         // php 1
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!",
107             php1.length(),
108             fScanner.getTokenLength());
109         junit.framework.Assert.assertEquals(
110             "Offset of PHP Partition 1 not correct!",
111             0,
112             fScanner.getTokenOffset());
113         // comment1  
114         token = fScanner.nextToken();
115         junit.framework.Assert.assertEquals(
116             "PHPComment Partition 1 not recognized!",
117             IPHPPartitions.PHP_MULTILINE_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!",
125             php1.length(),
126             fScanner.getTokenOffset());
127
128         // php 2
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!",
136             php2.length(),
137             fScanner.getTokenLength());
138         junit.framework.Assert.assertEquals(
139             "Offset of PHP Partition 2 not correct!",
140             php1.length() + phpcomment1.length(),
141             fScanner.getTokenOffset());
142
143         // check html 1
144         token = fScanner.nextToken();
145         junit.framework.Assert.assertEquals(
146             "HTML 1 not recognized!",
147             IPHPPartitions.HTML,
148             (String) token.getData());
149         junit.framework.Assert.assertEquals(
150             "Length of HTML 1 not correct!",
151             html1.length(),
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());
157
158         //      php 3
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!",
166             php3.length(),
167             fScanner.getTokenLength());
168         junit.framework.Assert.assertEquals(
169             "Offset of PHP Partition 3 not correct!",
170             php1.length()
171                 + phpcomment1.length()
172                 + php2.length()
173                 + html1.length(),
174             fScanner.getTokenOffset());
175
176         //      check html 2
177         token = fScanner.nextToken();
178         junit.framework.Assert.assertEquals(
179             "HTML 2 not recognized!",
180             IPHPPartitions.HTML,
181             (String) token.getData());
182         junit.framework.Assert.assertEquals(
183             "Length of HTML 2 not correct!",
184             html2.length(),
185             fScanner.getTokenLength());
186         junit.framework.Assert.assertEquals(
187             "Offset of HTML 2 not correct!",
188             php1.length()
189                 + phpcomment1.length()
190                 + php2.length()
191                 + html1.length()
192                 + php3.length(),
193             fScanner.getTokenOffset());
194
195         //      php 4
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!",
203             php4.length(),
204             fScanner.getTokenLength());
205         junit.framework.Assert.assertEquals(
206             "Offset of PHP Partition 4 not correct!",
207             php1.length()
208                 + phpcomment1.length()
209                 + php2.length()
210                 + html1.length()
211                 + php3.length()
212                 + html2.length(),
213             fScanner.getTokenOffset());
214
215         //      check comment 2
216         token = fScanner.nextToken();
217         junit.framework.Assert.assertEquals(
218             "PHP Multilinecomment 2 not recognized!",
219             IPHPPartitions.PHP_MULTILINE_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!",
227             php1.length()
228                 + phpcomment1.length()
229                 + php2.length()
230                 + html1.length()
231                 + php3.length()
232                 + html2.length()
233                 + php4.length(),
234             fScanner.getTokenOffset());
235
236         //      php 5
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!",
244             php5.length(),
245             fScanner.getTokenLength());
246         junit.framework.Assert.assertEquals(
247             "Offset of PHP Partition 5 not correct!",
248             php1.length()
249                 + phpcomment1.length()
250                 + php2.length()
251                 + html1.length()
252                 + php3.length()
253                 + html2.length()
254                 + php4.length()
255                 + phpcomment2.length(),
256             fScanner.getTokenOffset());
257         //      check html 3
258         token = fScanner.nextToken();
259         junit.framework.Assert.assertEquals(
260             "HTML 3 not recognized!",
261             IPHPPartitions.HTML,
262             (String) token.getData());
263         junit.framework.Assert.assertEquals(
264             "Length of HTML 3 not correct!",
265             html3.length(),
266             fScanner.getTokenLength());
267         junit.framework.Assert.assertEquals(
268             "Offset of HTML 3 not correct!",
269             php1.length()
270                 + phpcomment1.length()
271                 + php2.length()
272                 + html1.length()
273                 + php3.length()
274                 + html2.length()
275                 + php4.length()
276                 + phpcomment2.length()
277                 + php5.length(),
278             fScanner.getTokenOffset());
279
280     }
281
282     public void testPHP_HTML_Partition()
283     {
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>";
288         String php3 =
289             "<? echo \"Some text ?> just to fill up another php partition\"?>";
290         String html3 = "<p>Copyright 2003 by PHPEclipse</body></html>";
291
292         fDocument.set(php1 + html1 + php2 + html2 + php3 + html3);
293         fScanner.setRange(fDocument, 0, fDocument.getLength());
294         // php 1
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!",
302             php1.length(),
303             fScanner.getTokenLength());
304         junit.framework.Assert.assertEquals(
305             "Offset of PHP Partition 1 not correct!",
306             0,
307             fScanner.getTokenOffset());
308         // check html 1
309         token = fScanner.nextToken();
310         junit.framework.Assert.assertEquals(
311             "HTML 1 not recognized!",
312             IPHPPartitions.HTML,
313             (String) token.getData());
314         junit.framework.Assert.assertEquals(
315             "Length of HTML 1 not correct!",
316             html1.length(),
317             fScanner.getTokenLength());
318         junit.framework.Assert.assertEquals(
319             "Offset of HTML 1 not correct!",
320             php1.length(),
321             fScanner.getTokenOffset());
322         //      php 2
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!",
330             php2.length(),
331             fScanner.getTokenLength());
332         junit.framework.Assert.assertEquals(
333             "Offset of PHP Partition 2 not correct!",
334             html1.length() + php1.length(),
335             fScanner.getTokenOffset());
336         //      check html 2
337         token = fScanner.nextToken();
338         junit.framework.Assert.assertEquals(
339             "HTML 2 not recognized!",
340             IPHPPartitions.HTML,
341             (String) token.getData());
342         junit.framework.Assert.assertEquals(
343             "Length of HTML 2 not correct!",
344             html2.length(),
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());
350         //      php 3
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!",
358             php3.length(),
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());
364         //      check html 3
365         token = fScanner.nextToken();
366         junit.framework.Assert.assertEquals(
367             "HTML 3 not recognized!",
368             IPHPPartitions.HTML,
369             (String) token.getData());
370         junit.framework.Assert.assertEquals(
371             "Length of HTML 3 not correct!",
372             html3.length(),
373             fScanner.getTokenLength());
374         junit.framework.Assert.assertEquals(
375             "Offset of HTML 3 not correct!",
376             php1.length()
377                 + html1.length()
378                 + php2.length()
379                 + html2.length()
380                 + php3.length(),
381             fScanner.getTokenOffset());
382     }
383
384     public void testHTMLPartition()
385     {
386         String text =
387             "<html><head><title>Some Text</title></head><body>"
388                 + "<h1>Test</h1><p>Nothing particular</body></html>";
389         fDocument.set(text);
390         fScanner.setRange(fDocument, 0, fDocument.getLength());
391         IToken token = fScanner.nextToken();
392         junit.framework.Assert.assertEquals(
393             IPHPPartitions.HTML,
394             (String) token.getData());
395         junit.framework.Assert.assertEquals(
396             fDocument.getLength(),
397             fScanner.getTokenLength());
398         junit.framework.Assert.assertEquals(0, fScanner.getTokenOffset());
399     }
400
401     public void testPHPMultiLineCommentPartition()
402     {
403         String text = "<?php $test=\"Some <?php ?> /** */ data\";";
404         String text2 =
405             "/** A comment with <?php This is acutally not a php partition ?> some data \n"
406                 + " * @param test A test parameter \n"
407                 + " */";
408         String text3 =
409             "\nfunction test($test)\n"
410                 + "{\n"
411                 + "     echo \"Test <?php /** This is not a comment */function alsoItLooksLikeOne(){echo \"It is actually not a comment\";} ?>\";\n"
412                 + "}?>";
413         fDocument.set(text + text2 + text3);
414         fScanner.setRange(fDocument, 0, fDocument.getLength());
415         // first half of php
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!",
423             text.length(),
424             fScanner.getTokenLength());
425         junit.framework.Assert.assertEquals(
426             "Offset of PHP Partition part 1 not correct!",
427             0,
428             fScanner.getTokenOffset());
429         // check for multiline
430         token = fScanner.nextToken();
431         junit.framework.Assert.assertEquals(
432             "PHP Multiline not recognized!",
433             IPHPPartitions.PHP_MULTILINE_COMMENT,
434             (String) token.getData());
435         junit.framework.Assert.assertEquals(
436             "Length of PHP Multinline not correct!",
437             text2.length(),
438             fScanner.getTokenLength());
439         junit.framework.Assert.assertEquals(
440             "Offset of PHP Multiline not correct!",
441             text.length(),
442             fScanner.getTokenOffset());
443         // rest of php
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!",
451             text3.length(),
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());
457     }
458 }