1 package net.sourceforge.phpeclipse.tests.parser;
2 /*******************************************************************************
3 * Copyright (c) 2002 Klaus Hartlage - www.eclipseproject.de All rights
4 * reserved. This program and the accompanying materials are made available
5 * under the terms of the Common Public License v1.0 which accompanies this
6 * distribution, and is available at http://www.eclipse.org/legal/cpl-v10.html
7 ******************************************************************************/
8 import junit.framework.TestCase;
9 import net.sourceforge.phpdt.internal.compiler.parser.Parser;
10 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
12 * Tests the php parser
14 public class PHPManualTestCase extends TestCase {
16 public PHPManualTestCase(String name) {
20 * Test the PHP Parser with different PHP snippets
22 public void testPHPParser() {
23 checkPHP("function foo()" + "{" + " echo \"In foo()<br>\\n\";" + "}"
24 + "" + "function bar($arg = '')" + "{"
25 + " echo \"In bar(); argument was '$arg'.<br>\\n\";" + "}" + ""
26 + "// This is a wrapper function around echo"
27 + "function echoit($string)" + "{" + " echo $string;" + "}" + ""
28 + "$func = 'foo';" + "$func(); // This calls foo()" + ""
29 + "$func = 'bar';" + "$func('test'); // This calls bar()" + ""
30 + "$func = 'echoit';" + "$func('test'); // This calls echoit()" + "");
31 checkPHP("class Foo" + "{" + " function Vari()" + " {"
33 + " $this->$name(); // This calls the Bar() method\n" + " }"
34 + " " + " function Bar()" + " {"
35 + " echo \"This is Bar\";" + " }" + "}" + ""
36 + "$foo = new Foo();" + "$funcname = \"Var\";"
37 + "$foo->$varname(); // This calls $foo->Var()\n" + "");
38 checkPHP("function square ($num)" + "{" + " return $num * $num;" + "}"
39 + "echo square (4); // outputs '16'." + "");
40 checkPHP("function small_numbers()" + "{" + " return array (0, 1, 2);"
41 + "}" + "list ($zero, $one, $two) = small_numbers();" + "");
42 checkPHP("function &returns_reference()" + "{" + " return $someref;"
43 + "}" + "" + "$newref =& returns_reference();" + " " + "");
44 checkPHP("function add_some_extra(&$string)" + "{"
45 + " $string .= 'and something extra.';" + "}"
46 + "$str = 'This is a string, ';" + "add_some_extra($str);"
48 checkPHP("function makecoffee ($type = \"cappuccino\")\n" + "{\n"
49 + " return \"Making a cup of $type.\\n\";\n" + "}"
50 + "echo makecoffee ();" + "echo makecoffee (\"espresso\");" + "");
51 checkPHP("$makefoo = true;" + "" + "/* We can't call foo() from here "
52 + " since it doesn't exist yet," + " but we can call bar() */" + ""
53 + "bar();" + "" + "if ($makefoo) {" + " function foo ()" + " {"
54 + " echo \"I don't exist until program execution reaches me.\\n\";"
55 + " }" + "}" + "" + "/* Now we can safely call foo()"
56 + " since $makefoo evaluated to true */" + ""
57 + "if ($makefoo) foo();" + "" + "function bar() " + "{"
58 + " echo \"I exist immediately upon program start.\\n\";" + "}" + ""
60 checkPHP("function foo() " + "{" + " function bar() " + " {"
61 + " echo \"I don't exist until foo() is called.\\n\";" + " }" + "}"
62 + "" + "/* We can't call bar() yet" + " since it doesn't exist. */"
63 + "" + "foo();\n" + "" + "/* Now we can call bar(),"
64 + " foo()'s processesing has" + " made it accessable. */" + ""
67 checkPHP(" echo \"This is a test\"; // This is a one-line c++ style comment\n"
68 + " /* This is a multi line comment\n"
69 + " yet another line of comment */\n"
70 + " echo \"This is yet another test\";\n"
71 + " echo \"One Final Test\"; # This is shell-style style comment \n");
72 checkPHP("$bool = TRUE; // a boolean\n"
73 + "$str = \"foo\"; // a string\n" + "$int = 12; // an integer\n"
74 + "\n" + "echo gettype($bool); // prints out \"boolean\"\n"
75 + "echo gettype($str); // prints out \"string\"\n" + ""
76 + "// If this is an integer, increment it by four\n"
77 + "if (is_int($int)) {\n" + " $int += 4;\n" + "}\n" + "\n"
78 + "// If $bool is a string, print it out\n"
79 + "// (does not print out anything)\n" + "if (is_string($bool)) {\n"
80 + " echo \"String: $bool\";\n" + "}\n");
81 checkPHP("$foo = True; // assign the value TRUE to $foo");
82 checkPHP("// == is an operator which test\n"
83 + "// equality and returns a boolean\n"
84 + "if ($action == \"show_version\") {\n"
85 + " echo \"The version is 1.23\";\n" + "}\n" + "\n"
86 + "// this is not necessary...\n" + "if ($show_separators == TRUE) {\n"
87 + " echo \"<hr>\\n\";\n" + "}\n" + "\n"
88 + "// ...because you can simply type\n" + "if ($show_separators) {\n"
89 + " echo \"<hr>\\n\";\n" + "}");
90 checkPHP("echo gettype((bool) \"\"); // bool(false)\n"
91 + "echo gettype((bool) 1); // bool(true)\n"
92 + "echo gettype((bool) -2); // bool(true)\n"
93 + "echo gettype((bool) \"foo\"); // bool(true)\n"
94 + "echo gettype((bool) 2.3e5); // bool(true)\n"
95 + "echo gettype((bool) array(12)); // bool(true)\n"
96 + "echo gettype((bool) array()); // bool(false)\n");
97 checkPHP("$a = 1234; # decimal number\n"
98 + "$a = -123; # a negative number\n"
99 + "$a = 0123; # octal number (equivalent to 83 decimal)\n"
100 + "$a = 0x1A; # hexadecimal number (equivalent to 26 decimal)\n");
101 checkPHP("$large_number = 2147483647;\n" + "var_dump($large_number);\n"
102 + "// output: int(2147483647)\n" + "\n"
103 + "$large_number = 2147483648;\n" + "var_dump($large_number);\n"
104 + "// output: float(2147483648)\n" + ""
105 + "// this goes also for hexadecimal specified integers:\n"
106 + "var_dump( 0x80000000 );\n" + "// output: float(2147483648)\n" + "\n"
107 + "$million = 1000000;\n" + "$large_number = 50000 * $million;\n"
108 + "var_dump($large_number);\n" + "// output: float(50000000000)\n");
109 checkPHP("var_dump(25/7); // float(3.5714285714286)\n"
110 + "var_dump((int) (25/7)); // int(3)\n"
111 + "var_dump(round(25/7)); // float(4)");
112 checkPHP("echo (int) ( (0.1+0.7) * 10 ); // echoes 7!");
113 checkPHP("$a = 1.234; " + "$b = 1.2e3; " + "$c = 7E-10;");
114 checkPHP("echo 'this is a simple string';\n" + "\n"
115 + "echo 'You can also have embedded newlines in \n"
116 + "strings this way as it is\n" + "okay to do';\n" + "\n"
117 + "// Outputs: \"I'll be back\"\n"
118 + "echo 'Arnold once said: \"I\\'ll be back\"';\n" + "\n"
119 + "// Outputs: You deleted C:\\*.*?\n"
120 + "echo 'You deleted C:\\\\*.*?';\n" + "\n"
121 + "// Outputs: You deleted C:\\*.*?\n"
122 + "echo 'You deleted C:\\\\*.*?';\n" + "\n"
123 + "// Outputs: This will not expand: \\n a newline\n"
124 + "echo 'This will not expand: \\n a newline';\n" + "\n"
125 + "// Outputs: Variables do not $expand $either\n"
126 + "echo 'Variables do not $expand $either';\n");
127 checkPHP("$str = <<<EOD\n" + "Example of string\n"
128 + "spanning multiple lines\n" + "using heredoc syntax.\n" + "EOD;\n"
129 + "\n" + "/* More complex example, with variables. */\n"
130 + "class foo\n" + "{\n" + " var $foo;\n" + " var $bar;\n" + "\n"
131 + " function foo()\n" + " {\n" + " $this->foo = 'Foo';\n"
132 + " $this->bar = array('Bar1', 'Bar2', 'Bar3');\n" + " }\n"
133 + "}\n" + "\n" + "$foo = new foo();\n" + "$name = 'MyName';\n" + "\n"
135 + "My name is \"$name\". I am printing some $foo->foo.\n"
136 + "Now, I am printing some {$foo->bar[1]}.\n"
137 + "This should print a capital 'A': \\x41\n" + "EOT;\n");
138 checkPHP("echo \"This works: \" . $arr['foo'][3];");
139 checkPHP("echo \"\\$foo==$foo; type is \" . gettype ($foo) . \"<br />\\n\";");
140 checkPHP("$arr = array(\"foo\" => \"bar\", 12 => true);\n" + "\n"
141 + "echo $arr[\"foo\"]; // bar\n" + "echo $arr[12]; // 1\n");
142 checkPHP("// This array is the same as ...\n"
143 + "array(5 => 43, 32, 56, \"b\" => 12);\n" + "\n"
144 + "// ...this array\n"
145 + "array(5 => 43, 6 => 32, 7 => 56, \"b\" => 12);\n");
146 checkPHP("$arr = array(5 => 1, 12 => 2);\n" + "\n"
147 + "$arr[] = 56; // This is the same as $arr[13] = 56;\n"
148 + " // at this point of the script\n" + "\n"
149 + "$arr[\"x\"] = 42; // This adds a new element to\n"
150 + " // the array with key \"x\"\n"
152 + "unset($arr[5]); // This removes the element from the array\n" + "\n"
153 + "unset($arr); // This deletes the whole array\n");
154 checkPHP("$foo[bar] = 'enemy';\n" + "echo $foo[bar];");
155 checkPHP("$a = array( 'color' => 'red',\n"
156 + " 'taste' => 'sweet',\n"
157 + " 'shape' => 'round',\n"
158 + " 'name' => 'apple',\n"
159 + " 4 // key will be 0\n"
162 + "// is completely equivalent with\n"
163 + "$a['color'] = 'red';\n"
164 + "$a['taste'] = 'sweet';\n"
165 + "$a['shape'] = 'round';\n"
166 + "$a['name'] = 'apple';\n"
167 + "$a[] = 4; // key will be 0\n"
172 + "// will result in the array array(0 => 'a' , 1 => 'b' , 2 => 'c'),\n"
173 + "// or simply array('a', 'b', 'c')\n");
174 checkPHP("foreach ($colors as $key => $color) {\n" + " // won't work:\n"
175 + " //$color = strtoupper($color);\n" + " \n" + " // works:\n"
176 + " $colors[$key] = strtoupper($color);\n" + "}\n"
177 + "print_r($colors);\n");
178 checkPHP("$fruits = array ( \"fruits\" => array ( \"a\" => \"orange\",\n"
179 + " \"b\" => \"banana\",\n"
180 + " \"c\" => \"apple\"\n"
182 + " \"numbers\" => array ( 1,\n"
189 + " \"holes\" => array ( \"first\",\n"
190 + " 5 => \"second\",\n"
193 + "\n" + "// Some examples to address values in the array above \n"
194 + "echo $fruits[\"holes\"][5]; // prints \"second\"\n"
195 + "echo $fruits[\"fruits\"][\"a\"]; // prints \"orange\"\n"
196 + "unset($fruits[\"holes\"][0]); // remove \"first\"\n" + "\n"
197 + "// Create a new multi-dimensional array\n"
198 + "$juices[\"apple\"][\"green\"] = \"good\"; \n");
199 checkPHP("$arr3 = &$arr1;");
200 checkPHP("class foo\n" + "{\n" + " function do_foo()\n" + " {\n"
201 + " echo \"Doing foo.\"; \n" + " }\n" + "}\n" + "\n"
202 + "$bar = new foo;\n" + "$bar->do_foo();\n");
203 checkPHP("$obj = (object) 'ciao';\n"
204 + "echo $obj->scalar; // outputs 'ciao'");
205 checkPHP("$var = NULL;");
206 checkPHP("$var = \"Bob\";\n" + "$Var = \"Joe\";\n"
207 + "echo \"$var, $Var\"; // outputs \"Bob, Joe\"\n" + "\n" +
208 // "$4site = 'not yet'; // invalid; starts with a number\n" +
209 "$_4site = 'not yet'; // valid; starts with an underscore\n"
210 + "$täyte = 'mansikka'; \n");
215 private void checkPHP(String strEval) {
217 System.out.println("\n------------------------------------");
218 System.out.println(strEval);
220 parser.phpParserTester(strEval, 1);
222 private void checkHTML(String strEval) {
224 System.out.println("\n------------------------------------");
225 System.out.println(strEval);
227 parser.parse(strEval);
230 * The JUnit setup method
232 protected void setUp() {
233 parser = new Parser(null);