+++ /dev/null
-/*
- * Created on 28.04.2003
- *
- */
-package net.sourceforge.phpeclipse.phpeditor.php.test;
-
-import junit.framework.*;
-import net.sourceforge.phpdt.internal.ui.text.*;
-import net.sourceforge.phpeclipse.phpeditor.php.*;
-
-import org.eclipse.jface.text.*;
-import org.eclipse.jface.text.rules.*;
-
-/**
- * Testcase for the PHPPartitionScanner
- * @author Stefan Langer
- * @version $Revision: 1.3 $
- */
-public class PHPPartitionScannerTest extends TestCase
-{
- private PHPPartitionScanner fScanner;
- private IDocument fDocument;
-
- /* (non-Javadoc)
- * @see junit.framework.TestCase#setUp()
- */
- protected void setUp() throws Exception
- {
- fScanner = new PHPPartitionScanner();
- fDocument = new DummyDocument();
- }
-
- public void testPHPPartition()
- {
- String text =
- "<?php\n"
- + "$test = \"<?php this is a dummy text ?>\";\n"
- + "function test()\n"
- + "{echo 'Test!';}\n"
- + "?>";
- fDocument.set(text);
- fScanner.setRange(fDocument, 0, fDocument.getLength());
- IToken token = fScanner.nextToken();
- junit.framework.Assert.assertEquals(
- IPHPPartitions.PHP_PARTITIONING,
- (String) token.getData());
- junit.framework.Assert.assertEquals(
- fDocument.getLength(),
- fScanner.getTokenLength());
- junit.framework.Assert.assertEquals(0, fScanner.getTokenOffset());
- }
-
- public void testBrokenPHPPartition()
- {
- String text =
- "<?php\n"
- + "$test = \"<?php this is a dummy text ?>;\n"
- + "function test()\n"
- + "{echo 'Tes";
- fDocument.set(text);
- fScanner.setRange(fDocument, 0, fDocument.getLength());
- IToken token = fScanner.nextToken();
- junit.framework.Assert.assertEquals(
- IPHPPartitions.PHP_PARTITIONING,
- (String) token.getData());
- junit.framework.Assert.assertEquals(
- fDocument.getLength(),
- fScanner.getTokenLength());
- junit.framework.Assert.assertEquals(0, fScanner.getTokenOffset());
- }
-
- public void testPHP_PHPCOMMENT_HTML_Partition()
- {
- String php1 = "<? echo \"This is php with\"; ";
- String phpcomment1 = "/** @param This is a comment ?> */";
- String php2 = "echo \" short tags enabled!\"; ?>";
- String html1 = "<html><head><title>";
- String php3 =
- "<? echo \"/** this is no comment */The ?> <?php Title\"?>";
- String html2 = "</title></head><body>";
- String php4 = "<? echo \"Some text ?>\"\n\r";
- String phpcomment2 =
- "/** This is another comment @param test @return test*/";
- String php5 = " echo \" just to fill up another php partition\"?>";
- String html3 = "<p>Copyright 2003 by PHPEclipse</body></html>";
-
- fDocument.set(
- php1
- + phpcomment1
- + php2
- + html1
- + php3
- + html2
- + php4
- + phpcomment2
- + php5
- + html3);
- fScanner.setRange(fDocument, 0, fDocument.getLength());
- // php 1
- IToken token = fScanner.nextToken();
- junit.framework.Assert.assertEquals(
- "PHP Partition 1 not recognized!",
- IPHPPartitions.PHP_PARTITIONING,
- (String) token.getData());
- junit.framework.Assert.assertEquals(
- "Length of PHP Partition 1 not correct!",
- php1.length(),
- fScanner.getTokenLength());
- junit.framework.Assert.assertEquals(
- "Offset of PHP Partition 1 not correct!",
- 0,
- fScanner.getTokenOffset());
- // comment1
- token = fScanner.nextToken();
- junit.framework.Assert.assertEquals(
- "PHPComment Partition 1 not recognized!",
- IPHPPartitions.PHP_PHPDOC_COMMENT,
- (String) token.getData());
- junit.framework.Assert.assertEquals(
- "Length of PHPComment Partition 1 not correct!",
- phpcomment1.length(),
- fScanner.getTokenLength());
- junit.framework.Assert.assertEquals(
- "Offset of PHPComment Partition 1 not correct!",
- php1.length(),
- fScanner.getTokenOffset());
-
- // php 2
- token = fScanner.nextToken();
- junit.framework.Assert.assertEquals(
- "PHP Partition 2 not recognized!",
- IPHPPartitions.PHP_PARTITIONING,
- (String) token.getData());
- junit.framework.Assert.assertEquals(
- "Length of PHP Partition 2 not correct!",
- php2.length(),
- fScanner.getTokenLength());
- junit.framework.Assert.assertEquals(
- "Offset of PHP Partition 2 not correct!",
- php1.length() + phpcomment1.length(),
- fScanner.getTokenOffset());
-
- // check html 1
- token = fScanner.nextToken();
- junit.framework.Assert.assertEquals(
- "HTML 1 not recognized!",
- IPHPPartitions.HTML,
- (String) token.getData());
- junit.framework.Assert.assertEquals(
- "Length of HTML 1 not correct!",
- html1.length(),
- fScanner.getTokenLength());
- junit.framework.Assert.assertEquals(
- "Offset of HTML 1 not correct!",
- php1.length() + phpcomment1.length() + php2.length(),
- fScanner.getTokenOffset());
-
- // php 3
- token = fScanner.nextToken();
- junit.framework.Assert.assertEquals(
- "PHP Partition 3 not recognized!",
- IPHPPartitions.PHP_PARTITIONING,
- (String) token.getData());
- junit.framework.Assert.assertEquals(
- "Length of PHP Partition 3 not correct!",
- php3.length(),
- fScanner.getTokenLength());
- junit.framework.Assert.assertEquals(
- "Offset of PHP Partition 3 not correct!",
- php1.length()
- + phpcomment1.length()
- + php2.length()
- + html1.length(),
- fScanner.getTokenOffset());
-
- // check html 2
- token = fScanner.nextToken();
- junit.framework.Assert.assertEquals(
- "HTML 2 not recognized!",
- IPHPPartitions.HTML,
- (String) token.getData());
- junit.framework.Assert.assertEquals(
- "Length of HTML 2 not correct!",
- html2.length(),
- fScanner.getTokenLength());
- junit.framework.Assert.assertEquals(
- "Offset of HTML 2 not correct!",
- php1.length()
- + phpcomment1.length()
- + php2.length()
- + html1.length()
- + php3.length(),
- fScanner.getTokenOffset());
-
- // php 4
- token = fScanner.nextToken();
- junit.framework.Assert.assertEquals(
- "PHP Partition 4 not recognized!",
- IPHPPartitions.PHP_PARTITIONING,
- (String) token.getData());
- junit.framework.Assert.assertEquals(
- "Length of PHP Partition 4 not correct!",
- php4.length(),
- fScanner.getTokenLength());
- junit.framework.Assert.assertEquals(
- "Offset of PHP Partition 4 not correct!",
- php1.length()
- + phpcomment1.length()
- + php2.length()
- + html1.length()
- + php3.length()
- + html2.length(),
- fScanner.getTokenOffset());
-
- // check comment 2
- token = fScanner.nextToken();
- junit.framework.Assert.assertEquals(
- "PHP Multilinecomment 2 not recognized!",
- IPHPPartitions.PHP_PHPDOC_COMMENT,
- (String) token.getData());
- junit.framework.Assert.assertEquals(
- "Length of PHP Multilinecomment 2 not correct!",
- phpcomment2.length(),
- fScanner.getTokenLength());
- junit.framework.Assert.assertEquals(
- "Offset of PHP Multilinecomment 2 not correct!",
- php1.length()
- + phpcomment1.length()
- + php2.length()
- + html1.length()
- + php3.length()
- + html2.length()
- + php4.length(),
- fScanner.getTokenOffset());
-
- // php 5
- token = fScanner.nextToken();
- junit.framework.Assert.assertEquals(
- "PHP Partition 5 not recognized!",
- IPHPPartitions.PHP_PARTITIONING,
- (String) token.getData());
- junit.framework.Assert.assertEquals(
- "Length of PHP Partition 5 not correct!",
- php5.length(),
- fScanner.getTokenLength());
- junit.framework.Assert.assertEquals(
- "Offset of PHP Partition 5 not correct!",
- php1.length()
- + phpcomment1.length()
- + php2.length()
- + html1.length()
- + php3.length()
- + html2.length()
- + php4.length()
- + phpcomment2.length(),
- fScanner.getTokenOffset());
- // check html 3
- token = fScanner.nextToken();
- junit.framework.Assert.assertEquals(
- "HTML 3 not recognized!",
- IPHPPartitions.HTML,
- (String) token.getData());
- junit.framework.Assert.assertEquals(
- "Length of HTML 3 not correct!",
- html3.length(),
- fScanner.getTokenLength());
- junit.framework.Assert.assertEquals(
- "Offset of HTML 3 not correct!",
- php1.length()
- + phpcomment1.length()
- + php2.length()
- + html1.length()
- + php3.length()
- + html2.length()
- + php4.length()
- + phpcomment2.length()
- + php5.length(),
- fScanner.getTokenOffset());
-
- }
-
- public void testPHP_HTML_Partition()
- {
- String php1 = "<? echo \"This is php with short tags enabled!\"; ?>";
- String html1 = "<html><head><title>";
- String php2 = "<? echo \"The ?> <?php Title\"?>";
- String html2 = "</title></head><body>";
- String php3 =
- "<? echo \"Some text ?> just to fill up another php partition\"?>";
- String html3 = "<p>Copyright 2003 by PHPEclipse</body></html>";
-
- fDocument.set(php1 + html1 + php2 + html2 + php3 + html3);
- fScanner.setRange(fDocument, 0, fDocument.getLength());
- // php 1
- IToken token = fScanner.nextToken();
- junit.framework.Assert.assertEquals(
- "PHP Partition 1 not recognized!",
- IPHPPartitions.PHP_PARTITIONING,
- (String) token.getData());
- junit.framework.Assert.assertEquals(
- "Length of PHP Partition 1 not correct!",
- php1.length(),
- fScanner.getTokenLength());
- junit.framework.Assert.assertEquals(
- "Offset of PHP Partition 1 not correct!",
- 0,
- fScanner.getTokenOffset());
- // check html 1
- token = fScanner.nextToken();
- junit.framework.Assert.assertEquals(
- "HTML 1 not recognized!",
- IPHPPartitions.HTML,
- (String) token.getData());
- junit.framework.Assert.assertEquals(
- "Length of HTML 1 not correct!",
- html1.length(),
- fScanner.getTokenLength());
- junit.framework.Assert.assertEquals(
- "Offset of HTML 1 not correct!",
- php1.length(),
- fScanner.getTokenOffset());
- // php 2
- token = fScanner.nextToken();
- junit.framework.Assert.assertEquals(
- "PHP Partition 2 not recognized!",
- IPHPPartitions.PHP_PARTITIONING,
- (String) token.getData());
- junit.framework.Assert.assertEquals(
- "Length of PHP Partition 2 not correct!",
- php2.length(),
- fScanner.getTokenLength());
- junit.framework.Assert.assertEquals(
- "Offset of PHP Partition 2 not correct!",
- html1.length() + php1.length(),
- fScanner.getTokenOffset());
- // check html 2
- token = fScanner.nextToken();
- junit.framework.Assert.assertEquals(
- "HTML 2 not recognized!",
- IPHPPartitions.HTML,
- (String) token.getData());
- junit.framework.Assert.assertEquals(
- "Length of HTML 2 not correct!",
- html2.length(),
- fScanner.getTokenLength());
- junit.framework.Assert.assertEquals(
- "Offset of HTML 2 not correct!",
- php1.length() + html1.length() + php2.length(),
- fScanner.getTokenOffset());
- // php 3
- token = fScanner.nextToken();
- junit.framework.Assert.assertEquals(
- "PHP Partition 3 not recognized!",
- IPHPPartitions.PHP_PARTITIONING,
- (String) token.getData());
- junit.framework.Assert.assertEquals(
- "Length of PHP Partition 3 not correct!",
- php3.length(),
- fScanner.getTokenLength());
- junit.framework.Assert.assertEquals(
- "Offset of PHP Partition 3 not correct!",
- html2.length() + php2.length() + html1.length() + php1.length(),
- fScanner.getTokenOffset());
- // check html 3
- token = fScanner.nextToken();
- junit.framework.Assert.assertEquals(
- "HTML 3 not recognized!",
- IPHPPartitions.HTML,
- (String) token.getData());
- junit.framework.Assert.assertEquals(
- "Length of HTML 3 not correct!",
- html3.length(),
- fScanner.getTokenLength());
- junit.framework.Assert.assertEquals(
- "Offset of HTML 3 not correct!",
- php1.length()
- + html1.length()
- + php2.length()
- + html2.length()
- + php3.length(),
- fScanner.getTokenOffset());
- }
-
- public void testHTMLPartition()
- {
- String text =
- "<html><head><title>Some Text</title></head><body>"
- + "<h1>Test</h1><p>Nothing particular</body></html>";
- fDocument.set(text);
- fScanner.setRange(fDocument, 0, fDocument.getLength());
- IToken token = fScanner.nextToken();
- junit.framework.Assert.assertEquals(
- IPHPPartitions.HTML,
- (String) token.getData());
- junit.framework.Assert.assertEquals(
- fDocument.getLength(),
- fScanner.getTokenLength());
- junit.framework.Assert.assertEquals(0, fScanner.getTokenOffset());
- }
-
- public void testPHPMultiLineCommentPartition()
- {
- String text = "<?php $test=\"Some <?php ?> /** */ data\";";
- String text2 =
- "/** A comment with <?php This is acutally not a php partition ?> some data \n"
- + " * @param test A test parameter \n"
- + " */";
- String text3 =
- "\nfunction test($test)\n"
- + "{\n"
- + " echo \"Test <?php /** This is not a comment */function alsoItLooksLikeOne(){echo \"It is actually not a comment\";} ?>\";\n"
- + "}?>";
- fDocument.set(text + text2 + text3);
- fScanner.setRange(fDocument, 0, fDocument.getLength());
- // first half of php
- IToken token = fScanner.nextToken();
- junit.framework.Assert.assertEquals(
- "PHP Partition part 1 not recognized!",
- IPHPPartitions.PHP_PARTITIONING,
- (String) token.getData());
- junit.framework.Assert.assertEquals(
- "Length of PHP Partition part 1 not correct!",
- text.length(),
- fScanner.getTokenLength());
- junit.framework.Assert.assertEquals(
- "Offset of PHP Partition part 1 not correct!",
- 0,
- fScanner.getTokenOffset());
- // check for multiline
- token = fScanner.nextToken();
- junit.framework.Assert.assertEquals(
- "PHP Multiline not recognized!",
- IPHPPartitions.PHP_PHPDOC_COMMENT,
- (String) token.getData());
- junit.framework.Assert.assertEquals(
- "Length of PHP Multinline not correct!",
- text2.length(),
- fScanner.getTokenLength());
- junit.framework.Assert.assertEquals(
- "Offset of PHP Multiline not correct!",
- text.length(),
- fScanner.getTokenOffset());
- // rest of php
- token = fScanner.nextToken();
- junit.framework.Assert.assertEquals(
- "PHP Partition part 2 not recognized!",
- IPHPPartitions.PHP_PARTITIONING,
- (String) token.getData());
- junit.framework.Assert.assertEquals(
- "Length of PHP Partition part 2 not correct!",
- text3.length(),
- fScanner.getTokenLength());
- junit.framework.Assert.assertEquals(
- "Offset of PHP Partition part 2 not correct!",
- text.length() + text2.length(),
- fScanner.getTokenOffset());
- }
-}