request 1152810, implementing formatting of array initializers
[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.6 $
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,
50                                         CodeFormatterPreferencePage.getTabSize());
51                 }
52                 String formatedString = fFormatter.format(text, indent, null,
53                                 lineDelimiter);
54                 String testResult = "<?php"
55                                 + lineDelimiter
56                                 + "header(\"Location: http://www.phpeclipse.de/eclipse/space/eclipse/faq/longlinetester\");"
57                                 + lineDelimiter + "exit;" + lineDelimiter + "?>";
58                 junit.framework.Assert.assertEquals(formatedString, testResult);
59         }
60
61         public void testFormatter1() {
62                 System.out.println("----- testFormatter1 -----");
63                 String text = "<?php\n" + "function test()\n" + "{echo 'Test!';}\n"
64                                 + "?>";
65                 fDocument.set(text);
66                 String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
67                 int indent = 0;
68                 if (fInitialIndentation != null) {
69                         indent = Strings.computeIndent(fInitialIndentation,
70                                         CodeFormatterPreferencePage.getTabSize());
71                 }
72                 String formatedString = fFormatter.format(text, indent, null,
73                                 lineDelimiter);
74                 String testResult = "<?php" + lineDelimiter + "function test() {"
75                                 + lineDelimiter + "\techo 'Test!';" + lineDelimiter + "}"
76                                 + lineDelimiter + "?>";
77                 junit.framework.Assert.assertEquals(formatedString, testResult);
78         }
79
80         public void testFormatter2() {
81                 // bug 741752
82                 System.out.println("----- testFormatter2 -----");
83                 String text = "<?php\n" + "if ( ${ $feldname }== $modellvar) $i=0;\n"
84                                 + "?>";
85                 fDocument.set(text);
86                 String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
87                 int indent = 0;
88                 if (fInitialIndentation != null) {
89                         indent = Strings.computeIndent(fInitialIndentation,
90                                         CodeFormatterPreferencePage.getTabSize());
91                 }
92                 String formatedString = fFormatter.format(text, indent, null,
93                                 lineDelimiter);
94                 String testResult = "<?php" + lineDelimiter
95                                 + "if (${ $feldname } == $modellvar)" + lineDelimiter
96                                 + "\t$i = 0;" + lineDelimiter + "?>";
97                 junit.framework.Assert.assertEquals(formatedString, testResult);
98         }
99
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" + "?>";
104                 fDocument.set(text);
105                 String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
106                 int indent = 0;
107                 if (fInitialIndentation != null) {
108                         indent = Strings.computeIndent(fInitialIndentation,
109                                         CodeFormatterPreferencePage.getTabSize());
110                 }
111                 String formatedString = fFormatter.format(text, indent, null,
112                                 lineDelimiter);
113                 String testResult = "<?php" + lineDelimiter + "function test() {"
114                                 + lineDelimiter + "\techo 'Test!';" + lineDelimiter + "}"
115                                 + lineDelimiter + "?>";
116                 junit.framework.Assert.assertEquals(formatedString, testResult);
117         }
118
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"
123                                 + "?>";
124                 fDocument.set(text);
125                 String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
126                 int indent = 0;
127                 if (fInitialIndentation != null) {
128                         indent = Strings.computeIndent(fInitialIndentation,
129                                         CodeFormatterPreferencePage.getTabSize());
130                 }
131                 String formatedString = fFormatter.format(text, indent, null,
132                                 lineDelimiter);
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);
138         }
139         
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"
144                                 + "?>";
145                 fDocument.set(text);
146                 String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
147                 int indent = 0;
148                 if (fInitialIndentation != null) {
149                         indent = Strings.computeIndent(fInitialIndentation,
150                                         CodeFormatterPreferencePage.getTabSize());
151                 }
152                 String formatedString = fFormatter.format(text, indent, null,
153                                 lineDelimiter);
154                 String testResult = "<?php" + lineDelimiter
155                         + "$my_array = array (" + lineDelimiter
156                         + "\t'first' => 'ok'," + lineDelimiter
157                         + "\t'snd' => array (" + lineDelimiter
158                         + "\t\t'1' => '11'," + lineDelimiter
159                         + "\t\t'2' => '22'" + lineDelimiter
160                         + "\t)," + lineDelimiter
161                         + "\t'3th' => 'super'" + lineDelimiter
162                         + ");" + lineDelimiter
163                         + "?>";
164                 junit.framework.Assert.assertEquals(formatedString, testResult);
165         }
166         
167         
168 }