misc
[phpeclipse.git] / net.sourceforge.phpeclipse.tests / src / net / sourceforge / phpeclipse / phpeditor / php / test / PHPFormatterTest.java
1 /*
2  * Created on 28.04.2003
3  *
4  */
5 package net.sourceforge.phpeclipse.phpeditor.php.test;
6
7 import java.util.Map;
8
9 import junit.framework.TestCase;
10 import net.sourceforge.phpdt.core.JavaCore;
11 import net.sourceforge.phpdt.internal.corext.codemanipulation.StubUtility;
12 import net.sourceforge.phpdt.internal.corext.util.Strings;
13 import net.sourceforge.phpdt.internal.formatter.CodeFormatter;
14 import net.sourceforge.phpdt.internal.ui.preferences.CodeFormatterPreferencePage;
15
16 import org.eclipse.jface.text.IDocument;
17
18 /**
19  * Testcase for the PHP CodeFormatter
20  * 
21  * @author Stefan Langer
22  * @version $Revision: 1.4 $
23  */
24 public class PHPFormatterTest extends TestCase {
25   private CodeFormatter fFormatter;
26
27   private IDocument fDocument;
28
29   private String fInitialIndentation;
30
31   /*
32    * (non-Javadoc)
33    * 
34    * @see junit.framework.TestCase#setUp()
35    */
36   protected void setUp() throws Exception {
37     Map options = JavaCore.getOptions();
38     fFormatter = new CodeFormatter(options);
39     fDocument = new DummyDocument();
40   }
41
42   public void testFormatter0() {
43     System.out.println("----- testFormatter0 -----");
44     String text = "<?php\nheader(\"Location: http://www.phpeclipse.de/eclipse/space/eclipse/faq/longlinetester\"); exit; ?>";
45     fDocument.set(text);
46     String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
47     int indent = 0;
48     if (fInitialIndentation != null) {
49       indent = Strings.computeIndent(fInitialIndentation, CodeFormatterPreferencePage.getTabSize());
50     }
51     String formatedString = fFormatter.format(text, indent, null, lineDelimiter);
52     String testResult = "<?php " + lineDelimiter
53         + "header(\"Location: http://www.phpeclipse.de/eclipse/space/eclipse/faq/longlinetester\");" + lineDelimiter + "exit;"
54         + lineDelimiter + "?>";
55     junit.framework.Assert.assertEquals(formatedString, testResult);
56   }
57
58   public void testFormatter1() {
59     System.out.println("----- testFormatter1 -----");
60     String text = "<?php\n" + "function test()\n" + "{echo 'Test!';}\n" + "?>";
61     fDocument.set(text);
62     String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
63     int indent = 0;
64     if (fInitialIndentation != null) {
65       indent = Strings.computeIndent(fInitialIndentation, CodeFormatterPreferencePage.getTabSize());
66     }
67     String formatedString = fFormatter.format(text, indent, null, lineDelimiter);
68     String testResult = "<?php " + lineDelimiter + "function test() {" + lineDelimiter + "\techo 'Test!';" + lineDelimiter + "}"
69         + lineDelimiter + "?>";
70     junit.framework.Assert.assertEquals(formatedString, testResult);
71   }
72
73   public void testFormatter2() {
74     //  bug 741752
75     System.out.println("----- testFormatter2 -----");
76     String text = "<?php\n" + "if ( ${ $feldname }== $modellvar) $i=0;\n" + "?>";
77     fDocument.set(text);
78     String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
79     int indent = 0;
80     if (fInitialIndentation != null) {
81       indent = Strings.computeIndent(fInitialIndentation, CodeFormatterPreferencePage.getTabSize());
82     }
83     String formatedString = fFormatter.format(text, indent, null, lineDelimiter);
84     String testResult = "<?php " + lineDelimiter + "if (${ $feldname } == $modellvar)" + lineDelimiter + "\t$i = 0;"
85         + lineDelimiter + "?>";
86     junit.framework.Assert.assertEquals(formatedString, testResult);
87   }
88
89   public void testFormatter3() {
90     System.out.println("----- testFormatter1 -----");
91     String text = "<?php\n \r\n \r\n" + "function test()\n" + "{echo 'Test!';}\n" + "?>";
92     fDocument.set(text);
93     String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
94     int indent = 0;
95     if (fInitialIndentation != null) {
96       indent = Strings.computeIndent(fInitialIndentation, CodeFormatterPreferencePage.getTabSize());
97     }
98     String formatedString = fFormatter.format(text, indent, null, lineDelimiter);
99     String testResult = "<?php " + lineDelimiter + " " + lineDelimiter + " " + lineDelimiter + "function test() {" + lineDelimiter
100         + "\techo 'Test!';" + lineDelimiter + "}" + lineDelimiter + "?>";
101     junit.framework.Assert.assertEquals(formatedString, testResult);
102   }
103 }