2 * Created on 28.04.2003
5 package net.sourceforge.phpdt.internal.formatter;
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 import net.sourceforge.phpeclipse.phpeditor.php.test.DummyDocument;
17 import org.eclipse.jface.text.IDocument;
20 * Testcase for the PHP CodeFormatter
22 * @author Stefan Langer
23 * @version $Revision: 1.7 $
25 public class CodeFormatterTest extends TestCase {
26 private CodeFormatter fFormatter;
28 private IDocument fDocument;
30 private String fInitialIndentation;
35 * @see junit.framework.TestCase#setUp()
37 protected void setUp() throws Exception {
38 Map options = JavaCore.getOptions();
39 fFormatter = new CodeFormatter(options);
40 fDocument = new DummyDocument();
43 public void testFormatter0() {
44 System.out.println("----- testFormatter0 -----");
45 String text = "<?php\nheader(\"Location: http://www.phpeclipse.de/eclipse/space/eclipse/faq/longlinetester\"); exit; ?>";
47 String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
49 if (fInitialIndentation != null) {
50 indent = Strings.computeIndent(fInitialIndentation,
51 CodeFormatterPreferencePage.getTabSize());
53 String formatedString = fFormatter.format(text, indent, null,
55 String testResult = "<?php"
57 + "header(\"Location: http://www.phpeclipse.de/eclipse/space/eclipse/faq/longlinetester\");"
58 + lineDelimiter + "exit;" + lineDelimiter + "?>";
59 junit.framework.Assert.assertEquals(formatedString, testResult);
62 public void testFormatter1() {
63 System.out.println("----- testFormatter1 -----");
64 String text = "<?php\n" + "function test()\n" + "{echo 'Test!';}\n"
67 String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
69 if (fInitialIndentation != null) {
70 indent = Strings.computeIndent(fInitialIndentation,
71 CodeFormatterPreferencePage.getTabSize());
73 String formatedString = fFormatter.format(text, indent, null,
75 String testResult = "<?php" + lineDelimiter + "function test() {"
76 + lineDelimiter + "\techo 'Test!';" + lineDelimiter + "}"
77 + lineDelimiter + "?>";
78 junit.framework.Assert.assertEquals(formatedString, testResult);
81 public void testFormatter2() {
83 System.out.println("----- testFormatter2 -----");
84 String text = "<?php\n" + "if ( ${ $feldname }== $modellvar) $i=0;\n"
87 String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
89 if (fInitialIndentation != null) {
90 indent = Strings.computeIndent(fInitialIndentation,
91 CodeFormatterPreferencePage.getTabSize());
93 String formatedString = fFormatter.format(text, indent, null,
95 String testResult = "<?php" + lineDelimiter
96 + "if (${ $feldname } == $modellvar)" + lineDelimiter
97 + "\t$i = 0;" + lineDelimiter + "?>";
98 junit.framework.Assert.assertEquals(formatedString, testResult);
101 public void testFormatter3() {
102 System.out.println("----- testFormatter1 -----");
103 String text = "<?php\n \r\n \r\n" + "function test()\n"
104 + "{echo 'Test!';}\n" + "?>";
106 String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
108 if (fInitialIndentation != null) {
109 indent = Strings.computeIndent(fInitialIndentation,
110 CodeFormatterPreferencePage.getTabSize());
112 String formatedString = fFormatter.format(text, indent, null,
114 String testResult = "<?php" + lineDelimiter + "function test() {"
115 + lineDelimiter + "\techo 'Test!';" + lineDelimiter + "}"
116 + lineDelimiter + "?>";
117 junit.framework.Assert.assertEquals(formatedString, testResult);
120 public void testArrayFormatting() throws Exception {
121 System.out.println("-----testArrayFormatting-----");
122 String text = "<?php\n"
123 + "$my_array = array('first' => 'ok','snd' => 'right','3th' => 'super');\n"
126 String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
128 if (fInitialIndentation != null) {
129 indent = Strings.computeIndent(fInitialIndentation,
130 CodeFormatterPreferencePage.getTabSize());
132 String formatedString = fFormatter.format(text, indent, null,
134 String testResult = "<?php" + lineDelimiter + "$my_array = array ("
135 + lineDelimiter + "\t'first' => 'ok'," + lineDelimiter
136 + "\t'snd' => 'right'," + lineDelimiter + "\t'3th' => 'super'"
137 + lineDelimiter + ");" + lineDelimiter + "?>";
138 junit.framework.Assert.assertEquals(formatedString, testResult);
141 public void testDoubleArrayFormatting() throws Exception {
142 System.out.println("-----testDoubleArrayFormatting-----");
143 String text = "<?php\n"
144 + "$my_array = array('first' => 'ok', 'snd' => array ('1' => '11','2' => '22'),'3th' => 'super');\n"
147 String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
149 if (fInitialIndentation != null) {
150 indent = Strings.computeIndent(fInitialIndentation,
151 CodeFormatterPreferencePage.getTabSize());
153 String formatedString = fFormatter.format(text, indent, null,
155 String testResult = "<?php" + lineDelimiter + "$my_array = array ("
156 + lineDelimiter + "\t'first' => 'ok'," + lineDelimiter
157 + "\t'snd' => array (" + lineDelimiter + "\t\t'1' => '11',"
158 + lineDelimiter + "\t\t'2' => '22'" + lineDelimiter + "\t),"
159 + lineDelimiter + "\t'3th' => 'super'" + lineDelimiter + ");"
160 + lineDelimiter + "?>";
161 junit.framework.Assert.assertEquals(formatedString, testResult);