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.5 $
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, CodeFormatterPreferencePage.getTabSize());
51 String formatedString = fFormatter.format(text, indent, null, lineDelimiter);
52 String testResult = "<?php" + lineDelimiter
53 + "header(\"Location: http://www.phpeclipse.de/eclipse/space/eclipse/faq/longlinetester\");" + lineDelimiter + "exit;"
54 + lineDelimiter + "?>";
55 junit.framework.Assert.assertEquals(formatedString, testResult);
58 public void testFormatter1() {
59 System.out.println("----- testFormatter1 -----");
60 String text = "<?php\n" + "function test()\n" + "{echo 'Test!';}\n" + "?>";
62 String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
64 if (fInitialIndentation != null) {
65 indent = Strings.computeIndent(fInitialIndentation, CodeFormatterPreferencePage.getTabSize());
67 String formatedString = fFormatter.format(text, indent, null, lineDelimiter);
68 String testResult = "<?php" + lineDelimiter + "function test() {" + lineDelimiter + "\techo 'Test!';" + lineDelimiter + "}"
69 + lineDelimiter + "?>";
70 junit.framework.Assert.assertEquals(formatedString, testResult);
73 public void testFormatter2() {
75 System.out.println("----- testFormatter2 -----");
76 String text = "<?php\n" + "if ( ${ $feldname }== $modellvar) $i=0;\n" + "?>";
78 String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
80 if (fInitialIndentation != null) {
81 indent = Strings.computeIndent(fInitialIndentation, CodeFormatterPreferencePage.getTabSize());
83 String formatedString = fFormatter.format(text, indent, null, lineDelimiter);
84 String testResult = "<?php" + lineDelimiter + "if (${ $feldname } == $modellvar)" + lineDelimiter + "\t$i = 0;"
85 + lineDelimiter + "?>";
86 junit.framework.Assert.assertEquals(formatedString, testResult);
89 public void testFormatter3() {
90 System.out.println("----- testFormatter1 -----");
91 String text = "<?php\n \r\n \r\n" + "function test()\n" + "{echo 'Test!';}\n" + "?>";
93 String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
95 if (fInitialIndentation != null) {
96 indent = Strings.computeIndent(fInitialIndentation, CodeFormatterPreferencePage.getTabSize());
98 String formatedString = fFormatter.format(text, indent, null, lineDelimiter);
99 String testResult = "<?php" + lineDelimiter + "function test() {" + lineDelimiter
100 + "\techo 'Test!';" + lineDelimiter + "}" + lineDelimiter + "?>";
101 junit.framework.Assert.assertEquals(formatedString, testResult);