php formatter based on the JDT java formatter (very early version)
[phpeclipse.git] / net.sourceforge.phpeclipse / src / junit / sourceforge / phpeclipse / PHPManualTestCase.java
1 package junit.sourceforge.phpeclipse;
2 /**********************************************************************
3 Copyright (c) 2002 Klaus Hartlage - www.eclipseproject.de
4 All rights reserved. This program and the accompanying materials
5 are made available under the terms of the Common Public License v1.0
6 which accompanies this distribution, and is available at
7 http://www.eclipse.org/legal/cpl-v10.html
8 **********************************************************************/
9
10 import net.sourceforge.phpdt.internal.compiler.parser.Parser;
11 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
12
13 import org.eclipse.core.runtime.CoreException;
14 import junit.framework.TestCase;
15
16 /**
17  *  Tests the php parser
18  */
19 public class PHPManualTestCase extends TestCase {
20
21   Parser parser;
22
23   public PHPManualTestCase(String name) {
24     super(name);
25   }
26
27   /**
28    *  Test the PHP Parser with different PHP snippets
29    */
30   public void testPHPParser() {
31     // Bugs item #690938
32     checkPHP(
33       "    echo \"This is a test\"; // This is a one-line c++ style comment\n"
34         + "    /* This is a multi line comment\n"
35         + "       yet another line of comment */\n"
36         + "    echo \"This is yet another test\";\n"
37         + "    echo \"One Final Test\"; # This is shell-style style comment \n");
38     checkPHP(
39       "$bool = TRUE;   // a boolean\n"
40         + "$str  = \"foo\";  // a string\n"
41         + "$int  = 12;     // an integer\n"
42         + "\n"
43         + "echo gettype($bool); // prints out \"boolean\"\n"
44         + "echo gettype($str);  // prints out \"string\"\n"
45         + ""
46         + "// If this is an integer, increment it by four\n"
47         + "if (is_int($int)) {\n"
48         + "    $int += 4;\n"
49         + "}\n"
50         + "\n"
51         + "// If $bool is a string, print it out\n"
52         + "// (does not print out anything)\n"
53         + "if (is_string($bool)) {\n"
54         + "    echo \"String: $bool\";\n"
55         + "}\n");
56
57     checkPHP("$foo = True; // assign the value TRUE to $foo");
58
59     checkPHP(
60       "// == is an operator which test\n"
61         + "// equality and returns a boolean\n"
62         + "if ($action == \"show_version\") {\n"
63         + "    echo \"The version is 1.23\";\n"
64         + "}\n"
65         + "\n"
66         + "// this is not necessary...\n"
67         + "if ($show_separators == TRUE) {\n"
68         + "    echo \"<hr>\\n\";\n"
69         + "}\n"
70         + "\n"
71         + "// ...because you can simply type\n"
72         + "if ($show_separators) {\n"
73         + "    echo \"<hr>\\n\";\n"
74         + "}");
75
76     checkPHP(
77       "echo gettype((bool) \"\");        // bool(false)\n"
78         + "echo gettype((bool) 1);         // bool(true)\n"
79         + "echo gettype((bool) -2);        // bool(true)\n"
80         + "echo gettype((bool) \"foo\");     // bool(true)\n"
81         + "echo gettype((bool) 2.3e5);     // bool(true)\n"
82         + "echo gettype((bool) array(12)); // bool(true)\n"
83         + "echo gettype((bool) array());   // bool(false)\n");
84
85     checkPHP(
86       "$a = 1234; # decimal number\n"
87         + "$a = -123; # a negative number\n"
88         + "$a = 0123; # octal number (equivalent to 83 decimal)\n"
89         + "$a = 0x1A; # hexadecimal number (equivalent to 26 decimal)\n");
90
91     checkPHP(
92       "$large_number =  2147483647;\n"
93         + "var_dump($large_number);\n"
94         + "// output: int(2147483647)\n"
95         + "\n"
96         + "$large_number =  2147483648;\n"
97         + "var_dump($large_number);\n"
98         + "// output: float(2147483648)\n"
99         + ""
100         + "// this goes also for hexadecimal specified integers:\n"
101         + "var_dump( 0x80000000 );\n"
102         + "// output: float(2147483648)\n"
103         + "\n"
104         + "$million = 1000000;\n"
105         + "$large_number =  50000 * $million;\n"
106         + "var_dump($large_number);\n"
107         + "// output: float(50000000000)\n");
108
109     checkPHP(
110       "var_dump(25/7);         // float(3.5714285714286)\n"
111         + "var_dump((int) (25/7)); // int(3)\n"
112         + "var_dump(round(25/7));  // float(4)");
113
114     checkPHP("echo (int) ( (0.1+0.7) * 10 ); // echoes 7!");
115
116     checkPHP("$a = 1.234; " + "$b = 1.2e3; " + "$c = 7E-10;");
117
118     checkPHP(
119       "echo 'this is a simple string';\n"
120         + "\n"
121         + "echo 'You can also have embedded newlines in \n"
122         + "strings this way as it is\n"
123         + "okay to do';\n"
124         + "\n"
125         + "// Outputs: \"I'll be back\"\n"
126         + "echo 'Arnold once said: \"I\\'ll be back\"';\n"
127         + "\n"
128         + "// Outputs: You deleted C:\\*.*?\n"
129         + "echo 'You deleted C:\\\\*.*?';\n"
130         + "\n"
131         + "// Outputs: You deleted C:\\*.*?\n"
132         + "echo 'You deleted C:\\\\*.*?';\n"
133         + "\n"
134         + "// Outputs: This will not expand: \\n a newline\n"
135         + "echo 'This will not expand: \\n a newline';\n"
136         + "\n"
137         + "// Outputs: Variables do not $expand $either\n"
138         + "echo 'Variables do not $expand $either';\n");
139
140     checkPHP(
141       "$str = <<<EOD\n"
142         + "Example of string\n"
143         + "spanning multiple lines\n"
144         + "using heredoc syntax.\n"
145         + "EOD;\n"
146         + "\n"
147         + "/* More complex example, with variables. */\n"
148         + "class foo\n"
149         + "{\n"
150         + "    var $foo;\n"
151         + "    var $bar;\n"
152         + "\n"
153         + "    function foo()\n"
154         + "    {\n"
155         + "        $this->foo = 'Foo';\n"
156         + "        $this->bar = array('Bar1', 'Bar2', 'Bar3');\n"
157         + "    }\n"
158         + "}\n"
159         + "\n"
160         + "$foo = new foo();\n"
161         + "$name = 'MyName';\n"
162         + "\n"
163         + "echo <<<EOT\n"
164         + "My name is \"$name\". I am printing some $foo->foo.\n"
165         + "Now, I am printing some {$foo->bar[1]}.\n"
166         + "This should print a capital 'A': \\x41\n"
167         + "EOT;\n");
168
169     checkPHP("echo \"This works: \" . $arr['foo'][3];");
170
171     checkPHP("echo \"\\$foo==$foo; type is \" . gettype ($foo) . \"<br />\\n\";");
172
173     checkPHP(
174       "$arr = array(\"foo\" => \"bar\", 12 => true);\n"
175         + "\n"
176         + "echo $arr[\"foo\"]; // bar\n"
177         + "echo $arr[12];    // 1\n");
178
179     checkPHP(
180       "// This array is the same as ...\n"
181         + "array(5 => 43, 32, 56, \"b\" => 12);\n"
182         + "\n"
183         + "// ...this array\n"
184         + "array(5 => 43, 6 => 32, 7 => 56, \"b\" => 12);\n");
185
186     checkPHP(
187       "$arr = array(5 => 1, 12 => 2);\n"
188         + "\n"
189         + "$arr[] = 56;    // This is the same as $arr[13] = 56;\n"
190         + "                // at this point of the script\n"
191         + "\n"
192         + "$arr[\"x\"] = 42; // This adds a new element to\n"
193         + "                // the array with key \"x\"\n"
194         + "                \n"
195         + "unset($arr[5]); // This removes the element from the array\n"
196         + "\n"
197         + "unset($arr);    // This deletes the whole array\n");
198
199     checkPHP("$foo[bar] = 'enemy';\n" + "echo $foo[bar];");
200
201     checkPHP(
202       "$a = array( 'color' => 'red',\n"
203         + "            'taste' => 'sweet',\n"
204         + "            'shape' => 'round',\n"
205         + "            'name'  => 'apple',\n"
206         + "                       4        // key will be 0\n"
207         + "          );\n"
208         + "\n"
209         + "// is completely equivalent with\n"
210         + "$a['color'] = 'red';\n"
211         + "$a['taste'] = 'sweet';\n"
212         + "$a['shape'] = 'round';\n"
213         + "$a['name']  = 'apple';\n"
214         + "$a[]        = 4;        // key will be 0\n"
215         + "\n"
216         + "$b[] = 'a';\n"
217         + "$b[] = 'b';\n"
218         + "$b[] = 'c';\n"
219         + "// will result in the array array(0 => 'a' , 1 => 'b' , 2 => 'c'),\n"
220         + "// or simply array('a', 'b', 'c')\n");
221
222     checkPHP(
223       "foreach ($colors as $key => $color) {\n"
224         + "    // won't work:\n"
225         + "    //$color = strtoupper($color);\n"
226         + "    \n"
227         + "    // works:\n"
228         + "    $colors[$key] = strtoupper($color);\n"
229         + "}\n"
230         + "print_r($colors);\n");
231
232     checkPHP(
233       "$fruits = array ( \"fruits\"  => array ( \"a\" => \"orange\",\n"
234         + "                                       \"b\" => \"banana\",\n"
235         + "                                       \"c\" => \"apple\"\n"
236         + "                                     ),\n"
237         + "                  \"numbers\" => array ( 1,\n"
238         + "                                       2,\n"
239         + "                                       3,\n"
240         + "                                       4,\n"
241         + "                                       5,\n"
242         + "                                       6,\n"
243         + "                                     ),\n"
244         + "                  \"holes\"   => array (      \"first\",\n"
245         + "                                       5 => \"second\",\n"
246         + "                                            \"third\"\n"
247         + "                                     )\n"
248         + "                );\n"
249         + "\n"
250         + "// Some examples to address values in the array above \n"
251         + "echo $fruits[\"holes\"][5];    // prints \"second\"\n"
252         + "echo $fruits[\"fruits\"][\"a\"]; // prints \"orange\"\n"
253         + "unset($fruits[\"holes\"][0]);  // remove \"first\"\n"
254         + "\n"
255         + "// Create a new multi-dimensional array\n"
256         + "$juices[\"apple\"][\"green\"] = \"good\"; \n");
257
258     checkPHP("$arr3 = &$arr1;");
259
260     checkPHP(
261       "class foo\n"
262         + "{\n"
263         + "    function do_foo()\n"
264         + "    {\n"
265         + "        echo \"Doing foo.\"; \n"
266         + "    }\n"
267         + "}\n"
268         + "\n"
269         + "$bar = new foo;\n"
270         + "$bar->do_foo();\n");
271
272     checkPHP(
273       "$obj = (object) 'ciao';\n" + 
274       "echo $obj->scalar;  // outputs 'ciao'");
275
276     checkPHP("$var = NULL;");
277
278     checkPHP("$var = \"Bob\";\n" +\r     "$Var = \"Joe\";\n" +\r          "echo \"$var, $Var\";      // outputs \"Bob, Joe\"\n" +\r        "\n" +\r   //    "$4site = 'not yet';     // invalid; starts with a number\n" +\r         "$_4site = 'not yet';    // valid; starts with an underscore\n" +\r      "$täyte = 'mansikka';  \n");
279
280     checkPHP("");
281
282     checkPHP("");
283
284     checkPHP("");
285   }
286
287   private void checkPHP(String strEval) {
288     try {
289       if (Scanner.DEBUG) {
290         System.out.println("\n------------------------------------");
291         System.out.println(strEval);
292       }
293       parser.phpParserTester(strEval, 1);
294     } catch (CoreException e) {
295     }
296   }
297
298   private void checkHTML(String strEval) {
299     try {
300       if (Scanner.DEBUG) {
301         System.out.println("\n------------------------------------");
302         System.out.println(strEval);
303       }
304       parser.parse(strEval);
305     } catch (CoreException e) {
306     }
307   }
308
309   /**
310    *  The JUnit setup method
311    */
312   protected void setUp() {
313     parser = new Parser(null);
314   }
315
316 }