1 package net.sourceforge.phpeclipse.tests.parser;
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 **********************************************************************/
10 import net.sourceforge.phpdt.internal.compiler.parser.Parser;
11 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
13 import org.eclipse.core.runtime.CoreException;
14 import junit.framework.TestCase;
17 * Tests the php parser
19 public class PHPManualTestCase extends TestCase {
23 public PHPManualTestCase(String name) {
28 * Test the PHP Parser with different PHP snippets
30 public void testPHPParser() {
31 checkPHP("function foo()" +
\r "{" +
\r " echo \"In foo()<br>\\n\";" +
\r "}" +
\r "" +
\r "function bar($arg = '')" +
\r "{" +
\r " echo \"In bar(); argument was '$arg'.<br>\\n\";" +
\r "}" +
\r "" +
\r "// This is a wrapper function around echo" +
\r "function echoit($string)" +
\r "{" +
\r " echo $string;" +
\r "}" +
\r "" +
\r "$func = 'foo';" +
\r "$func(); // This calls foo()" +
\r "" +
\r "$func = 'bar';" +
\r "$func('test'); // This calls bar()" +
\r "" +
\r "$func = 'echoit';" +
\r "$func('test'); // This calls echoit()" +
\r "");
32 checkPHP("class Foo" +
\r "{" +
\r " function Vari()" +
\r " {" +
\r " $name = 'Bar';" +
\r " $this->$name(); // This calls the Bar() method\n" +
\r " }" +
\r " " +
\r " function Bar()" +
\r " {" +
\r " echo \"This is Bar\";" +
\r " }" +
\r "}" +
\r "" +
\r "$foo = new Foo();" +
\r "$funcname = \"Var\";" +
\r "$foo->$varname(); // This calls $foo->Var()\n" +
\r "");
33 checkPHP("function square ($num)" +
\r "{" +
\r " return $num * $num;" +
\r "}" +
\r "echo square (4); // outputs '16'." +
\r "");
34 checkPHP("function small_numbers()" +
\r "{" +
\r " return array (0, 1, 2);" +
\r "}" +
\r "list ($zero, $one, $two) = small_numbers();" +
\r "");
35 checkPHP("function &returns_reference()" +
\r "{" +
\r " return $someref;" +
\r "}" +
\r "" +
\r "$newref =& returns_reference();" +
\r " " +
\r "");
36 checkPHP("function add_some_extra(&$string)" +
\r "{" +
\r " $string .= 'and something extra.';" +
\r "}" +
\r "$str = 'This is a string, ';" +
\r "add_some_extra($str);" +
\r "echo $str; ");
37 checkPHP("function makecoffee ($type = \"cappuccino\")\n" +
\r "{\n" +
\r " return \"Making a cup of $type.\\n\";\n" +
\r "}" +
\r "echo makecoffee ();" +
\r "echo makecoffee (\"espresso\");" +
\r "");
38 checkPHP("$makefoo = true;" +
\r "" +
\r "/* We can't call foo() from here " +
\r " since it doesn't exist yet," +
\r " but we can call bar() */" +
\r "" +
\r "bar();" +
\r "" +
\r "if ($makefoo) {" +
\r " function foo ()" +
\r " {" +
\r " echo \"I don't exist until program execution reaches me.\\n\";" +
\r " }" +
\r "}" +
\r "" +
\r "/* Now we can safely call foo()" +
\r " since $makefoo evaluated to true */" +
\r "" +
\r "if ($makefoo) foo();" +
\r "" +
\r "function bar() " +
\r "{" +
\r " echo \"I exist immediately upon program start.\\n\";" +
\r "}" +
\r "" +
\r "");
40 "function foo() " +
\r "{" +
\r " function bar() " +
\r " {" +
\r " echo \"I don't exist until foo() is called.\\n\";" +
\r " }" +
\r "}" +
\r "" +
\r "/* We can't call bar() yet" +
\r " since it doesn't exist. */" +
\r "" +
\r "foo();\n" +
\r "" +
\r "/* Now we can call bar()," +
\r " foo()'s processesing has" +
\r " made it accessable. */" +
\r "" +
\r "bar();" +
\r "");
43 " echo \"This is a test\"; // This is a one-line c++ style comment\n"
44 + " /* This is a multi line comment\n"
45 + " yet another line of comment */\n"
46 + " echo \"This is yet another test\";\n"
47 + " echo \"One Final Test\"; # This is shell-style style comment \n");
49 "$bool = TRUE; // a boolean\n"
50 + "$str = \"foo\"; // a string\n"
51 + "$int = 12; // an integer\n"
53 + "echo gettype($bool); // prints out \"boolean\"\n"
54 + "echo gettype($str); // prints out \"string\"\n"
56 + "// If this is an integer, increment it by four\n"
57 + "if (is_int($int)) {\n"
61 + "// If $bool is a string, print it out\n"
62 + "// (does not print out anything)\n"
63 + "if (is_string($bool)) {\n"
64 + " echo \"String: $bool\";\n"
67 checkPHP("$foo = True; // assign the value TRUE to $foo");
70 "// == is an operator which test\n"
71 + "// equality and returns a boolean\n"
72 + "if ($action == \"show_version\") {\n"
73 + " echo \"The version is 1.23\";\n"
76 + "// this is not necessary...\n"
77 + "if ($show_separators == TRUE) {\n"
78 + " echo \"<hr>\\n\";\n"
81 + "// ...because you can simply type\n"
82 + "if ($show_separators) {\n"
83 + " echo \"<hr>\\n\";\n"
87 "echo gettype((bool) \"\"); // bool(false)\n"
88 + "echo gettype((bool) 1); // bool(true)\n"
89 + "echo gettype((bool) -2); // bool(true)\n"
90 + "echo gettype((bool) \"foo\"); // bool(true)\n"
91 + "echo gettype((bool) 2.3e5); // bool(true)\n"
92 + "echo gettype((bool) array(12)); // bool(true)\n"
93 + "echo gettype((bool) array()); // bool(false)\n");
96 "$a = 1234; # decimal number\n"
97 + "$a = -123; # a negative number\n"
98 + "$a = 0123; # octal number (equivalent to 83 decimal)\n"
99 + "$a = 0x1A; # hexadecimal number (equivalent to 26 decimal)\n");
102 "$large_number = 2147483647;\n"
103 + "var_dump($large_number);\n"
104 + "// output: int(2147483647)\n"
106 + "$large_number = 2147483648;\n"
107 + "var_dump($large_number);\n"
108 + "// output: float(2147483648)\n"
110 + "// this goes also for hexadecimal specified integers:\n"
111 + "var_dump( 0x80000000 );\n"
112 + "// output: float(2147483648)\n"
114 + "$million = 1000000;\n"
115 + "$large_number = 50000 * $million;\n"
116 + "var_dump($large_number);\n"
117 + "// output: float(50000000000)\n");
120 "var_dump(25/7); // float(3.5714285714286)\n"
121 + "var_dump((int) (25/7)); // int(3)\n"
122 + "var_dump(round(25/7)); // float(4)");
124 checkPHP("echo (int) ( (0.1+0.7) * 10 ); // echoes 7!");
126 checkPHP("$a = 1.234; " + "$b = 1.2e3; " + "$c = 7E-10;");
129 "echo 'this is a simple string';\n"
131 + "echo 'You can also have embedded newlines in \n"
132 + "strings this way as it is\n"
135 + "// Outputs: \"I'll be back\"\n"
136 + "echo 'Arnold once said: \"I\\'ll be back\"';\n"
138 + "// Outputs: You deleted C:\\*.*?\n"
139 + "echo 'You deleted C:\\\\*.*?';\n"
141 + "// Outputs: You deleted C:\\*.*?\n"
142 + "echo 'You deleted C:\\\\*.*?';\n"
144 + "// Outputs: This will not expand: \\n a newline\n"
145 + "echo 'This will not expand: \\n a newline';\n"
147 + "// Outputs: Variables do not $expand $either\n"
148 + "echo 'Variables do not $expand $either';\n");
152 + "Example of string\n"
153 + "spanning multiple lines\n"
154 + "using heredoc syntax.\n"
157 + "/* More complex example, with variables. */\n"
163 + " function foo()\n"
165 + " $this->foo = 'Foo';\n"
166 + " $this->bar = array('Bar1', 'Bar2', 'Bar3');\n"
170 + "$foo = new foo();\n"
171 + "$name = 'MyName';\n"
174 + "My name is \"$name\". I am printing some $foo->foo.\n"
175 + "Now, I am printing some {$foo->bar[1]}.\n"
176 + "This should print a capital 'A': \\x41\n"
179 checkPHP("echo \"This works: \" . $arr['foo'][3];");
181 checkPHP("echo \"\\$foo==$foo; type is \" . gettype ($foo) . \"<br />\\n\";");
184 "$arr = array(\"foo\" => \"bar\", 12 => true);\n"
186 + "echo $arr[\"foo\"]; // bar\n"
187 + "echo $arr[12]; // 1\n");
190 "// This array is the same as ...\n"
191 + "array(5 => 43, 32, 56, \"b\" => 12);\n"
193 + "// ...this array\n"
194 + "array(5 => 43, 6 => 32, 7 => 56, \"b\" => 12);\n");
197 "$arr = array(5 => 1, 12 => 2);\n"
199 + "$arr[] = 56; // This is the same as $arr[13] = 56;\n"
200 + " // at this point of the script\n"
202 + "$arr[\"x\"] = 42; // This adds a new element to\n"
203 + " // the array with key \"x\"\n"
205 + "unset($arr[5]); // This removes the element from the array\n"
207 + "unset($arr); // This deletes the whole array\n");
209 checkPHP("$foo[bar] = 'enemy';\n" + "echo $foo[bar];");
212 "$a = array( 'color' => 'red',\n"
213 + " 'taste' => 'sweet',\n"
214 + " 'shape' => 'round',\n"
215 + " 'name' => 'apple',\n"
216 + " 4 // key will be 0\n"
219 + "// is completely equivalent with\n"
220 + "$a['color'] = 'red';\n"
221 + "$a['taste'] = 'sweet';\n"
222 + "$a['shape'] = 'round';\n"
223 + "$a['name'] = 'apple';\n"
224 + "$a[] = 4; // key will be 0\n"
229 + "// will result in the array array(0 => 'a' , 1 => 'b' , 2 => 'c'),\n"
230 + "// or simply array('a', 'b', 'c')\n");
233 "foreach ($colors as $key => $color) {\n"
234 + " // won't work:\n"
235 + " //$color = strtoupper($color);\n"
238 + " $colors[$key] = strtoupper($color);\n"
240 + "print_r($colors);\n");
243 "$fruits = array ( \"fruits\" => array ( \"a\" => \"orange\",\n"
244 + " \"b\" => \"banana\",\n"
245 + " \"c\" => \"apple\"\n"
247 + " \"numbers\" => array ( 1,\n"
254 + " \"holes\" => array ( \"first\",\n"
255 + " 5 => \"second\",\n"
260 + "// Some examples to address values in the array above \n"
261 + "echo $fruits[\"holes\"][5]; // prints \"second\"\n"
262 + "echo $fruits[\"fruits\"][\"a\"]; // prints \"orange\"\n"
263 + "unset($fruits[\"holes\"][0]); // remove \"first\"\n"
265 + "// Create a new multi-dimensional array\n"
266 + "$juices[\"apple\"][\"green\"] = \"good\"; \n");
268 checkPHP("$arr3 = &$arr1;");
273 + " function do_foo()\n"
275 + " echo \"Doing foo.\"; \n"
279 + "$bar = new foo;\n"
280 + "$bar->do_foo();\n");
283 "$obj = (object) 'ciao';\n" +
284 "echo $obj->scalar; // outputs 'ciao'");
286 checkPHP("$var = NULL;");
288 checkPHP("$var = \"Bob\";\n" +
289 "$Var = \"Joe\";\n" +
290 "echo \"$var, $Var\"; // outputs \"Bob, Joe\"\n" +
292 // "$4site = 'not yet'; // invalid; starts with a number\n" +
293 "$_4site = 'not yet'; // valid; starts with an underscore\n" +
294 "$täyte = 'mansikka'; \n");
303 private void checkPHP(String strEval) {
306 System.out.println("\n------------------------------------");
307 System.out.println(strEval);
309 parser.phpParserTester(strEval, 1);
310 } catch (CoreException e) {
314 private void checkHTML(String strEval) {
317 System.out.println("\n------------------------------------");
318 System.out.println(strEval);
320 parser.parse(strEval);
321 } catch (CoreException e) {
326 * The JUnit setup method
328 protected void setUp() {
329 parser = new Parser(null);