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