2 * Created on 28.04.2003
5 package net.sourceforge.phpeclipse.phpeditor.php.test;
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;
16 import org.eclipse.jface.text.IDocument;
19 * Testcase for the PHP CodeFormatter
21 * @author Stefan Langer
22 * @version $Revision: 1.7 $
24 public class PHPFormatterTest extends TestCase {
25 private CodeFormatter fFormatter;
27 private IDocument fDocument;
29 private String fInitialIndentation;
34 * @see junit.framework.TestCase#setUp()
36 protected void setUp() throws Exception {
37 Map options = JavaCore.getOptions();
38 fFormatter = new CodeFormatter(options);
39 fDocument = new DummyDocument();
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; ?>";
46 String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
48 if (fInitialIndentation != null) {
49 indent = Strings.computeIndent(fInitialIndentation,
50 CodeFormatterPreferencePage.getTabSize());
52 String formatedString = fFormatter.format(text, indent, null,
54 String testResult = "<?php"
56 + "header(\"Location: http://www.phpeclipse.de/eclipse/space/eclipse/faq/longlinetester\");"
57 + lineDelimiter + "exit;" + lineDelimiter + "?>";
58 junit.framework.Assert.assertEquals(formatedString, testResult);
61 public void testFormatter1() {
62 System.out.println("----- testFormatter1 -----");
63 String text = "<?php\n" + "function test()\n" + "{echo 'Test!';}\n"
66 String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
68 if (fInitialIndentation != null) {
69 indent = Strings.computeIndent(fInitialIndentation,
70 CodeFormatterPreferencePage.getTabSize());
72 String formatedString = fFormatter.format(text, indent, null,
74 String testResult = "<?php" + lineDelimiter + "function test() {"
75 + lineDelimiter + "\techo 'Test!';" + lineDelimiter + "}"
76 + lineDelimiter + "?>";
77 junit.framework.Assert.assertEquals(formatedString, testResult);
80 public void testFormatter2() {
82 System.out.println("----- testFormatter2 -----");
83 String text = "<?php\n" + "if ( ${ $feldname }== $modellvar) $i=0;\n"
86 String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
88 if (fInitialIndentation != null) {
89 indent = Strings.computeIndent(fInitialIndentation,
90 CodeFormatterPreferencePage.getTabSize());
92 String formatedString = fFormatter.format(text, indent, null,
94 String testResult = "<?php" + lineDelimiter
95 + "if (${ $feldname } == $modellvar)" + lineDelimiter
96 + "\t$i = 0;" + lineDelimiter + "?>";
97 junit.framework.Assert.assertEquals(formatedString, testResult);
100 public void testFormatter3() {
101 System.out.println("----- testFormatter1 -----");
102 String text = "<?php\n \r\n \r\n" + "function test()\n"
103 + "{echo 'Test!';}\n" + "?>";
105 String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
107 if (fInitialIndentation != null) {
108 indent = Strings.computeIndent(fInitialIndentation,
109 CodeFormatterPreferencePage.getTabSize());
111 String formatedString = fFormatter.format(text, indent, null,
113 String testResult = "<?php" + lineDelimiter + "function test() {"
114 + lineDelimiter + "\techo 'Test!';" + lineDelimiter + "}"
115 + lineDelimiter + "?>";
116 junit.framework.Assert.assertEquals(formatedString, testResult);
119 public void testArrayFormatting() throws Exception {
120 System.out.println("-----testArrayFormatting-----");
121 String text = "<?php\n"
122 + "$my_array = array('first' => 'ok','snd' => 'right','3th' => 'super');\n"
125 String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
127 if (fInitialIndentation != null) {
128 indent = Strings.computeIndent(fInitialIndentation,
129 CodeFormatterPreferencePage.getTabSize());
131 String formatedString = fFormatter.format(text, indent, null,
133 String testResult = "<?php" + lineDelimiter + "$my_array = array ("
134 + lineDelimiter + "\t'first' => 'ok'," + lineDelimiter
135 + "\t'snd' => 'right'," + lineDelimiter + "\t'3th' => 'super'"
136 + lineDelimiter + ");" + lineDelimiter + "?>";
137 junit.framework.Assert.assertEquals(formatedString, testResult);
140 public void testDoubleArrayFormatting() throws Exception {
141 System.out.println("-----testDoubleArrayFormatting-----");
142 String text = "<?php\n"
143 + "$my_array = array('first' => 'ok', 'snd' => array ('1' => '11','2' => '22'),'3th' => 'super');\n"
146 String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
148 if (fInitialIndentation != null) {
149 indent = Strings.computeIndent(fInitialIndentation,
150 CodeFormatterPreferencePage.getTabSize());
152 String formatedString = fFormatter.format(text, indent, null,
154 String testResult = "<?php" + lineDelimiter + "$my_array = array ("
155 + lineDelimiter + "\t'first' => 'ok'," + lineDelimiter
156 + "\t'snd' => array (" + lineDelimiter + "\t\t'1' => '11',"
157 + lineDelimiter + "\t\t'2' => '22'" + lineDelimiter + "\t),"
158 + lineDelimiter + "\t'3th' => 'super'" + lineDelimiter + ");"
159 + lineDelimiter + "?>";
160 junit.framework.Assert.assertEquals(formatedString, testResult);