2 * Created on 28.04.2003
5 package net.sourceforge.phpeclipse.phpeditor.php.test;
7 import junit.framework.TestCase;
8 import net.sourceforge.phpdt.core.JavaCore;
9 import net.sourceforge.phpdt.internal.corext.codemanipulation.StubUtility;
10 import net.sourceforge.phpdt.internal.corext.util.Strings;
11 import net.sourceforge.phpdt.internal.formatter.CodeFormatter;
12 import net.sourceforge.phpdt.internal.ui.preferences.CodeFormatterPreferencePage;
13 import org.eclipse.jface.text.IDocument;
15 * Testcase for the PHP CodeFormatter
17 * @author Stefan Langer
18 * @version $Revision: 1.3 $
20 public class PHPFormatterTest extends TestCase {
21 private CodeFormatter fFormatter;
22 private IDocument fDocument;
23 private String fInitialIndentation;
27 * @see junit.framework.TestCase#setUp()
29 protected void setUp() throws Exception {
30 Map options = JavaCore.getOptions();
31 fFormatter = new CodeFormatter(options);
32 fDocument = new DummyDocument();
34 public void testFormatter0() {
35 System.out.println("----- testFormatter0 -----");
36 String text = "<?php\nheader(\"Location: http://www.phpeclipse.de/eclipse/space/eclipse/faq/longlinetester\"); exit; ?>";
38 String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
40 if (fInitialIndentation != null) {
41 indent = Strings.computeIndent(fInitialIndentation,
42 CodeFormatterPreferencePage.getTabSize());
44 String formatedString = fFormatter
45 .format(text, indent, null, lineDelimiter);
46 String testResult = "<?php "
47 + lineDelimiter + "header(\"Location: http://www.phpeclipse.de/eclipse/space/eclipse/faq/longlinetester\");" + lineDelimiter
48 + "exit;" + lineDelimiter + "?>";
49 junit.framework.Assert.assertEquals(formatedString, testResult);
51 public void testFormatter1() {
52 System.out.println("----- testFormatter1 -----");
53 String text = "<?php\n" + "function test()\n" + "{echo 'Test!';}\n" + "?>";
55 String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
57 if (fInitialIndentation != null) {
58 indent = Strings.computeIndent(fInitialIndentation,
59 CodeFormatterPreferencePage.getTabSize());
61 String formatedString = fFormatter
62 .format(text, indent, null, lineDelimiter);
63 String testResult = "<?php "+ lineDelimiter + "function test() {" + lineDelimiter
64 + "\techo 'Test!';" + lineDelimiter + "}" + lineDelimiter + "?>";
65 junit.framework.Assert.assertEquals(formatedString, testResult);
67 public void testFormatter2() {
69 System.out.println("----- testFormatter2 -----");
70 String text = "<?php\n" + "if ( ${ $feldname }== $modellvar) $i=0;\n"
73 String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
75 if (fInitialIndentation != null) {
76 indent = Strings.computeIndent(fInitialIndentation,
77 CodeFormatterPreferencePage.getTabSize());
79 String formatedString = fFormatter
80 .format(text, indent, null, lineDelimiter);
81 String testResult = "<?php " + lineDelimiter + "if (${ $feldname } == $modellvar)"
82 + lineDelimiter + "\t$i = 0;" + lineDelimiter + "?>";
83 junit.framework.Assert.assertEquals(formatedString, testResult);
85 public void testFormatter3() {
86 System.out.println("----- testFormatter1 -----");
87 String text = "<?php\n \r\n \r\n" + "function test()\n"
88 + "{echo 'Test!';}\n" + "?>";
90 String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
92 if (fInitialIndentation != null) {
93 indent = Strings.computeIndent(fInitialIndentation,
94 CodeFormatterPreferencePage.getTabSize());
96 String formatedString = fFormatter
97 .format(text, indent, null, lineDelimiter);
98 String testResult = "<?php " + lineDelimiter + " " + lineDelimiter + " "
99 + lineDelimiter + "function test() {" + lineDelimiter
100 + "\techo 'Test!';" + lineDelimiter + "}" + lineDelimiter + "?>";
101 junit.framework.Assert.assertEquals(formatedString, testResult);