Moved the code in the "tests" plugin (plural) to the "test" fragment (singular)....
[phpeclipse.git] / net.sourceforge.phpeclipse.test / src / net / sourceforge / phpdt / internal / formatter / CodeFormatterTest.java
1 /*
2  * Created on 28.04.2003
3  *
4  */
5 package net.sourceforge.phpdt.internal.formatter;
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 import net.sourceforge.phpeclipse.phpeditor.php.test.DummyDocument;
16
17 import org.eclipse.jface.text.IDocument;
18
19 /**
20  * Testcase for the PHP CodeFormatter
21  * 
22  * @author Stefan Langer
23  * @version $Revision: 1.7 $
24  */
25 public class CodeFormatterTest extends TestCase {
26         private CodeFormatter fFormatter;
27
28         private IDocument fDocument;
29
30         private String fInitialIndentation;
31
32         /*
33          * (non-Javadoc)
34          * 
35          * @see junit.framework.TestCase#setUp()
36          */
37         protected void setUp() throws Exception {
38                 Map options = JavaCore.getOptions();
39                 fFormatter = new CodeFormatter(options);
40                 fDocument = new DummyDocument();
41         }
42
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; ?>";
46                 fDocument.set(text);
47                 String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
48                 int indent = 0;
49                 if (fInitialIndentation != null) {
50                         indent = Strings.computeIndent(fInitialIndentation,
51                                         CodeFormatterPreferencePage.getTabSize());
52                 }
53                 String formatedString = fFormatter.format(text, indent, null,
54                                 lineDelimiter);
55                 String testResult = "<?php"
56                                 + lineDelimiter
57                                 + "header(\"Location: http://www.phpeclipse.de/eclipse/space/eclipse/faq/longlinetester\");"
58                                 + lineDelimiter + "exit;" + lineDelimiter + "?>";
59                 junit.framework.Assert.assertEquals(formatedString, testResult);
60         }
61
62         public void testFormatter1() {
63                 System.out.println("----- testFormatter1 -----");
64                 String text = "<?php\n" + "function test()\n" + "{echo 'Test!';}\n"
65                                 + "?>";
66                 fDocument.set(text);
67                 String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
68                 int indent = 0;
69                 if (fInitialIndentation != null) {
70                         indent = Strings.computeIndent(fInitialIndentation,
71                                         CodeFormatterPreferencePage.getTabSize());
72                 }
73                 String formatedString = fFormatter.format(text, indent, null,
74                                 lineDelimiter);
75                 String testResult = "<?php" + lineDelimiter + "function test() {"
76                                 + lineDelimiter + "\techo 'Test!';" + lineDelimiter + "}"
77                                 + lineDelimiter + "?>";
78                 junit.framework.Assert.assertEquals(formatedString, testResult);
79         }
80
81         public void testFormatter2() {
82                 // bug 741752
83                 System.out.println("----- testFormatter2 -----");
84                 String text = "<?php\n" + "if ( ${ $feldname }== $modellvar) $i=0;\n"
85                                 + "?>";
86                 fDocument.set(text);
87                 String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
88                 int indent = 0;
89                 if (fInitialIndentation != null) {
90                         indent = Strings.computeIndent(fInitialIndentation,
91                                         CodeFormatterPreferencePage.getTabSize());
92                 }
93                 String formatedString = fFormatter.format(text, indent, null,
94                                 lineDelimiter);
95                 String testResult = "<?php" + lineDelimiter
96                                 + "if (${ $feldname } == $modellvar)" + lineDelimiter
97                                 + "\t$i = 0;" + lineDelimiter + "?>";
98                 junit.framework.Assert.assertEquals(formatedString, testResult);
99         }
100
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" + "?>";
105                 fDocument.set(text);
106                 String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
107                 int indent = 0;
108                 if (fInitialIndentation != null) {
109                         indent = Strings.computeIndent(fInitialIndentation,
110                                         CodeFormatterPreferencePage.getTabSize());
111                 }
112                 String formatedString = fFormatter.format(text, indent, null,
113                                 lineDelimiter);
114                 String testResult = "<?php" + lineDelimiter + "function test() {"
115                                 + lineDelimiter + "\techo 'Test!';" + lineDelimiter + "}"
116                                 + lineDelimiter + "?>";
117                 junit.framework.Assert.assertEquals(formatedString, testResult);
118         }
119
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"
124                                 + "?>";
125                 fDocument.set(text);
126                 String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
127                 int indent = 0;
128                 if (fInitialIndentation != null) {
129                         indent = Strings.computeIndent(fInitialIndentation,
130                                         CodeFormatterPreferencePage.getTabSize());
131                 }
132                 String formatedString = fFormatter.format(text, indent, null,
133                                 lineDelimiter);
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);
139         }
140
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"
145                                 + "?>";
146                 fDocument.set(text);
147                 String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
148                 int indent = 0;
149                 if (fInitialIndentation != null) {
150                         indent = Strings.computeIndent(fInitialIndentation,
151                                         CodeFormatterPreferencePage.getTabSize());
152                 }
153                 String formatedString = fFormatter.format(text, indent, null,
154                                 lineDelimiter);
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);
162         }
163
164 }