5813d6e3d53776bd2191c110fd38f9b062691315
[phpeclipse.git] /
1 /*
2  * Created on 28.04.2003
3  *
4  */
5 package net.sourceforge.phpeclipse.phpeditor.php.test;
6
7 import org.eclipse.jface.text.*;
8 import org.eclipse.jface.text.rules.*;
9
10 import net.sourceforge.phpeclipse.phpeditor.php.*;
11 import junit.framework.*;
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 testHTMLPartition()
53     {
54         String text =
55             "<html><head><title>Some Text</title></head><body>"
56                 + "<h1>Test</h1><p>Nothing particular</body></html>";
57         fDocument.set(text);
58         fScanner.setRange(fDocument, 0, fDocument.getLength());
59         IToken token = fScanner.nextToken();
60         junit.framework.Assert.assertEquals(
61             IPHPPartitionScannerConstants.HTML,
62             (String) token.getData());
63         junit.framework.Assert.assertEquals(
64             fDocument.getLength(),
65             fScanner.getTokenLength());
66         junit.framework.Assert.assertEquals(0, fScanner.getTokenOffset());
67     }
68
69     public void testPHPMultiLineCommentPartition()
70     {
71         String text = "<?php $test=\"Some data\";";
72         String text2 =
73             "/** A comment with some data \n"
74                 + " * @param test A test parameter \n"
75                 + " */";
76         String text3 =
77             "\nfunction test($test)\n"
78                 + "{\n"
79                 + "     echo \"Test <?php ?>\";\n"
80                 + "}?>";
81         fDocument.set(text + text2 + text3);
82         fScanner.setRange(fDocument, 0, fDocument.getLength());
83         // first half of php
84         IToken token = fScanner.nextToken();
85         junit.framework.Assert.assertEquals(
86             "PHP Partition part 1 not recognized!",
87             IPHPPartitionScannerConstants.PHP,
88             (String) token.getData());
89         junit.framework.Assert.assertEquals(
90             "Length of PHP Partition part 1 not correct!",
91             text.length(),
92             fScanner.getTokenLength());
93         junit.framework.Assert.assertEquals(
94             "Offset of PHP Partition part 1 not correct!",
95             0,
96             fScanner.getTokenOffset());
97         // check for multiline
98         token = fScanner.nextToken();
99         junit.framework.Assert.assertEquals(
100             "PHP Multiline not recognized!",
101             IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT,
102             (String) token.getData());
103         junit.framework.Assert.assertEquals(
104             "Length of PHP Multinline not correct!",
105             text2.length(),
106             fScanner.getTokenLength());
107         junit.framework.Assert.assertEquals(
108             "Offset of PHP Multiline not correct!",
109             text.length(),
110             fScanner.getTokenOffset());
111         // rest of php
112         token = fScanner.nextToken();
113         junit.framework.Assert.assertEquals(
114             "PHP Partition part 2 not recognized!",
115             IPHPPartitionScannerConstants.PHP,
116             (String) token.getData());
117         junit.framework.Assert.assertEquals(
118             "Length of PHP Partition part 2 not correct!",
119             text3.length(),
120             fScanner.getTokenLength());
121         junit.framework.Assert.assertEquals(
122             "Offset of PHP Partition part 2 not correct!",
123             text.length() + text2.length(),
124             fScanner.getTokenOffset());
125     }
126 }