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 net.sourceforge.phpdt.core.tests.util.AbstractCompilerTest;
9 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
11 * Tests the php parser
13 public class PHPManualTestCase extends AbstractCompilerTest {
15 public PHPManualTestCase(String name) {
19 * Test the PHP Parser with different PHP snippets
21 public void testPHPParser() {
22 checkPHP("function foo()" + "{" + " echo \"In foo()<br>\\n\";" + "}"
23 + "" + "function bar($arg = '')" + "{"
24 + " echo \"In bar(); argument was '$arg'.<br>\\n\";" + "}" + ""
25 + "// This is a wrapper function around echo"
26 + "function echoit($string)" + "{" + " echo $string;" + "}" + ""
27 + "$func = 'foo';" + "$func(); // This calls foo()" + ""
28 + "$func = 'bar';" + "$func('test'); // This calls bar()" + ""
29 + "$func = 'echoit';" + "$func('test'); // This calls echoit()" + "");
30 checkPHP("class Foo" + "{" + " function Vari()" + " {"
32 + " $this->$name(); // This calls the Bar() method\n" + " }"
33 + " " + " function Bar()" + " {"
34 + " echo \"This is Bar\";" + " }" + "}" + ""
35 + "$foo = new Foo();" + "$funcname = \"Var\";"
36 + "$foo->$varname(); // This calls $foo->Var()\n" + "");
37 checkPHP("function square ($num)" + "{" + " return $num * $num;" + "}"
38 + "echo square (4); // outputs '16'." + "");
39 checkPHP("function small_numbers()" + "{" + " return array (0, 1, 2);"
40 + "}" + "list ($zero, $one, $two) = small_numbers();" + "");
41 checkPHP("function &returns_reference()" + "{" + " return $someref;"
42 + "}" + "" + "$newref =& returns_reference();" + " " + "");
43 checkPHP("function add_some_extra(&$string)" + "{"
44 + " $string .= 'and something extra.';" + "}"
45 + "$str = 'This is a string, ';" + "add_some_extra($str);"
47 checkPHP("function makecoffee ($type = \"cappuccino\")\n" + "{\n"
48 + " return \"Making a cup of $type.\\n\";\n" + "}"
49 + "echo makecoffee ();" + "echo makecoffee (\"espresso\");" + "");
50 checkPHP("$makefoo = true;" + "" + "/* We can't call foo() from here "
51 + " since it doesn't exist yet," + " but we can call bar() */" + ""
52 + "bar();" + "" + "if ($makefoo) {" + " function foo ()" + " {"
53 + " echo \"I don't exist until program execution reaches me.\\n\";"
54 + " }" + "}" + "" + "/* Now we can safely call foo()"
55 + " since $makefoo evaluated to true */" + ""
56 + "if ($makefoo) foo();" + "" + "function bar() " + "{"
57 + " echo \"I exist immediately upon program start.\\n\";" + "}" + ""
59 checkPHP("function foo() " + "{" + " function bar() " + " {"
60 + " echo \"I don't exist until foo() is called.\\n\";" + " }" + "}"
61 + "" + "/* We can't call bar() yet" + " since it doesn't exist. */"
62 + "" + "foo();\n" + "" + "/* Now we can call bar(),"
63 + " foo()'s processesing has" + " made it accessable. */" + ""
66 checkPHP(" echo \"This is a test\"; // This is a one-line c++ style comment\n"
67 + " /* This is a multi line comment\n"
68 + " yet another line of comment */\n"
69 + " echo \"This is yet another test\";\n"
70 + " echo \"One Final Test\"; # This is shell-style style comment \n");
71 checkPHP("$bool = TRUE; // a boolean\n"
72 + "$str = \"foo\"; // a string\n" + "$int = 12; // an integer\n"
73 + "\n" + "echo gettype($bool); // prints out \"boolean\"\n"
74 + "echo gettype($str); // prints out \"string\"\n" + ""
75 + "// If this is an integer, increment it by four\n"
76 + "if (is_int($int)) {\n" + " $int += 4;\n" + "}\n" + "\n"
77 + "// If $bool is a string, print it out\n"
78 + "// (does not print out anything)\n" + "if (is_string($bool)) {\n"
79 + " echo \"String: $bool\";\n" + "}\n");
80 checkPHP("$foo = True; // assign the value TRUE to $foo");
81 checkPHP("// == is an operator which test\n"
82 + "// equality and returns a boolean\n"
83 + "if ($action == \"show_version\") {\n"
84 + " echo \"The version is 1.23\";\n" + "}\n" + "\n"
85 + "// this is not necessary...\n" + "if ($show_separators == TRUE) {\n"
86 + " echo \"<hr>\\n\";\n" + "}\n" + "\n"
87 + "// ...because you can simply type\n" + "if ($show_separators) {\n"
88 + " echo \"<hr>\\n\";\n" + "}");
89 checkPHP( // "echo gettype((bool) \"\"); // bool(false)\n"
90 "echo gettype((bool) 1); // bool(true)\n"
91 + "echo gettype((bool) -2); // bool(true)\n"
92 + "echo gettype((bool) \"foo\"); // bool(true)\n"
93 + "echo gettype((bool) 2.3e5); // bool(true)\n"
94 + "echo gettype((bool) array(12)); // bool(true)\n"
95 + "echo gettype((bool) array()); // bool(false)\n");
96 checkPHP("$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");
100 checkPHP("$large_number = 2147483647;\n" + "var_dump($large_number);\n"
101 + "// output: int(2147483647)\n" + "\n"
102 + "$large_number = 2147483648;\n" + "var_dump($large_number);\n"
103 + "// output: float(2147483648)\n" + ""
104 + "// this goes also for hexadecimal specified integers:\n"
105 + "var_dump( 0x80000000 );\n" + "// output: float(2147483648)\n" + "\n"
106 + "$million = 1000000;\n" + "$large_number = 50000 * $million;\n"
107 + "var_dump($large_number);\n" + "// output: float(50000000000)\n");
108 checkPHP("var_dump(25/7); // float(3.5714285714286)\n"
109 + "var_dump((int) (25/7)); // int(3)\n"
110 + "var_dump(round(25/7)); // float(4)");
111 checkPHP("echo (int) ( (0.1+0.7) * 10 ); // echoes 7!");
112 checkPHP("$a = 1.234; " + "$b = 1.2e3; " + "$c = 7E-10;");
113 checkPHP("echo 'this is a simple string';\n" + "\n"
114 + "echo 'You can also have embedded newlines in \n"
115 + "strings this way as it is\n" + "okay to do';\n" + "\n"
116 + "// Outputs: \"I'll be back\"\n"
117 + "echo 'Arnold once said: \"I\\'ll be back\"';\n" + "\n"
118 + "// Outputs: You deleted C:\\*.*?\n"
119 + "echo 'You deleted C:\\\\*.*?';\n" + "\n"
120 + "// Outputs: You deleted C:\\*.*?\n"
121 + "echo 'You deleted C:\\\\*.*?';\n" + "\n"
122 + "// Outputs: This will not expand: \\n a newline\n"
123 + "echo 'This will not expand: \\n a newline';\n" + "\n"
124 + "// Outputs: Variables do not $expand $either\n"
125 + "echo 'Variables do not $expand $either';\n");
127 checkPHP("echo \"This works: \" . $arr['foo'][3];");
128 checkPHP("echo \"\\$foo==$foo; type is \" . gettype ($foo) . \"<br />\\n\";");
129 checkPHP("$arr = array(\"foo\" => \"bar\", 12 => true);\n" + "\n"
130 + "echo $arr[\"foo\"]; // bar\n" + "echo $arr[12]; // 1\n");
131 checkPHP("// This array is the same as ...\n"
132 + "array(5 => 43, 32, 56, \"b\" => 12);\n" + "\n"
133 + "// ...this array\n"
134 + "array(5 => 43, 6 => 32, 7 => 56, \"b\" => 12);\n");
135 checkPHP("$arr = array(5 => 1, 12 => 2);\n" + "\n"
136 + "$arr[] = 56; // This is the same as $arr[13] = 56;\n"
137 + " // at this point of the script\n" + "\n"
138 + "$arr[\"x\"] = 42; // This adds a new element to\n"
139 + " // the array with key \"x\"\n"
141 + "unset($arr[5]); // This removes the element from the array\n" + "\n"
142 + "unset($arr); // This deletes the whole array\n");
143 checkPHP("$foo[bar] = 'enemy';\n" + "echo $foo[bar];");
144 checkPHP("$a = array( 'color' => 'red',\n"
145 + " 'taste' => 'sweet',\n"
146 + " 'shape' => 'round',\n"
147 + " 'name' => 'apple',\n"
148 + " 4 // key will be 0\n"
151 + "// is completely equivalent with\n"
152 + "$a['color'] = 'red';\n"
153 + "$a['taste'] = 'sweet';\n"
154 + "$a['shape'] = 'round';\n"
155 + "$a['name'] = 'apple';\n"
156 + "$a[] = 4; // key will be 0\n"
161 + "// will result in the array array(0 => 'a' , 1 => 'b' , 2 => 'c'),\n"
162 + "// or simply array('a', 'b', 'c')\n");
163 checkPHP("foreach ($colors as $key => $color) {\n" + " // won't work:\n"
164 + " //$color = strtoupper($color);\n" + " \n" + " // works:\n"
165 + " $colors[$key] = strtoupper($color);\n" + "}\n"
166 + "print_r($colors);\n");
167 checkPHP("$fruits = array ( \"fruits\" => array ( \"a\" => \"orange\",\n"
168 + " \"b\" => \"banana\",\n"
169 + " \"c\" => \"apple\"\n"
171 + " \"numbers\" => array ( 1,\n"
178 + " \"holes\" => array ( \"first\",\n"
179 + " 5 => \"second\",\n"
182 + "\n" + "// Some examples to address values in the array above \n"
183 + "echo $fruits[\"holes\"][5]; // prints \"second\"\n"
184 + "echo $fruits[\"fruits\"][\"a\"]; // prints \"orange\"\n"
185 + "unset($fruits[\"holes\"][0]); // remove \"first\"\n" + "\n"
186 + "// Create a new multi-dimensional array\n"
187 + "$juices[\"apple\"][\"green\"] = \"good\"; \n");
188 checkPHP("$arr3 = &$arr1;");
189 checkPHP("class foo\n" + "{\n" + " function do_foo()\n" + " {\n"
190 + " echo \"Doing foo.\"; \n" + " }\n" + "}\n" + "\n"
191 + "$bar = new foo;\n" + "$bar->do_foo();\n");
192 checkPHP("$obj = (object) 'ciao';\n"
193 + "echo $obj->scalar; // outputs 'ciao'");
194 checkPHP("$var = NULL;");
195 checkPHP("$var = \"Bob\";\n" + "$Var = \"Joe\";\n"
196 + "echo \"$var, $Var\"; // outputs \"Bob, Joe\"\n" + "\n" +
197 // "$4site = 'not yet'; // invalid; starts with a number\n" +
198 "$_4site = 'not yet'; // valid; starts with an underscore\n"
199 + "$täyte = 'mansikka'; \n");
204 private void checkPHP(String strEval) {
206 System.out.println("\n------------------------------------");
207 System.out.println(strEval);
210 strEval.toCharArray(),
212 // parser.phpParserTester(strEval, 1);
214 // private void checkHTML(String strEval) {
215 // if (Scanner.DEBUG) {
216 // System.out.println("\n------------------------------------");
217 // System.out.println(strEval);
219 // parser.parse(strEval);
222 * The JUnit setup method
224 // protected void setUp() {
225 // parser = new Parser(null);