b9e7618b3765fe741b2ed72f0b513bb67f63d566
[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  * @author Stefan Langer
21  * @version $Revision: 1.2 $
22  */
23 public class PHPFormatterTest extends TestCase {
24   private CodeFormatter fFormatter;
25   private IDocument fDocument;
26   private String fInitialIndentation;
27
28   /* (non-Javadoc)
29    * @see junit.framework.TestCase#setUp()
30    */
31   protected void setUp() throws Exception {
32     Map options = JavaCore.getOptions();
33     fFormatter = new CodeFormatter(options);
34     fDocument = new DummyDocument();
35   }
36
37   public void testFormatter1() {
38     System.out.println("----- testFormatter1 -----");
39
40     String text = "<?php\n" + "function test()\n" + "{echo 'Test!';}\n" + "?>";
41     fDocument.set(text);
42
43     String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
44
45     int indent = 0;
46     if (fInitialIndentation != null) {
47       indent = Strings.computeIndent(fInitialIndentation, CodeFormatterPreferencePage.getTabSize());
48     }
49     String formatedString = fFormatter.format(text, indent, null, lineDelimiter);
50
51     String testResult = "<?php function test() {" + lineDelimiter + "\techo 'Test!';" + lineDelimiter + "}" + lineDelimiter + "?>";
52
53     junit.framework.Assert.assertEquals(formatedString, testResult);
54   }
55
56   public void testFormatter2() {
57     //  bug 741752
58     System.out.println("----- testFormatter2 -----");
59     String text = "<?php\n" + "if ( ${ $feldname }== $modellvar) $i=0;\n" + "?>";
60     fDocument.set(text);
61
62     String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument);
63
64     int indent = 0;
65     if (fInitialIndentation != null) {
66       indent = Strings.computeIndent(fInitialIndentation, CodeFormatterPreferencePage.getTabSize());
67     }
68     String formatedString = fFormatter.format(text, indent, null, lineDelimiter);
69
70     String testResult = "<?php " + "if (${ $feldname } == $modellvar)" + lineDelimiter + "\t$i = 0;" + lineDelimiter + "?>";
71
72     junit.framework.Assert.assertEquals(formatedString, testResult);
73   }
74 }