1 package net.sourceforge.phpeclipse.tests.parser;
2 /*******************************************************************************
3 * Copyright (c) 2002 www.phpeclipse.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("self::$instance = new Singleton();");
23 checkPHP("if ((eregi(\"<[^>]*script*\\\"?[^>]*>\", $secvalue)) ||\r\n" +
24 " (eregi(\"<[^>]*object*\\\"?[^>]*>\", $secvalue)) ||\r\n" +
25 " (eregi(\"<[^>]*iframe*\\\"?[^>]*>\", $secvalue)) ||\r\n" +
26 " (eregi(\"<[^>]*applet*\\\"?[^>]*>\", $secvalue)) ||\r\n" +
27 " (eregi(\"<[^>]*meta*\\\"?[^>]*>\", $secvalue)) ||\r\n" +
28 " (eregi(\"<[^>]*style*\\\"?[^>]*>\", $secvalue)) ||\r\n" +
29 " (eregi(\"<[^>]*form*\\\"?[^>]*>\", $secvalue)) ||\r\n" +
30 " (eregi(\"\\([^>]*\\\"?[^)]*\\)\", $secvalue)) ||\r\n" +
31 " (eregi(\"\\\"\", $secvalue))) {\r\n" +
32 " die (\"<center><img src=images/logo.gif><br><br><b>The html tags you attempted to use are not allowed</b><br><br>[ <a href=\\\"javascript:history.go(-1)\\\"><b>Go Back</b></a> ]\");\r\n" +
34 checkPHP("function foo()" + "{" + " echo \"In foo()<br>\\n\";" + "}"
35 + "" + "function bar($arg = '')" + "{"
36 + " echo \"In bar(); argument was '$arg'.<br>\\n\";" + "}" + ""
37 + "// This is a wrapper function around echo"
38 + "function echoit($string)" + "{" + " echo $string;" + "}" + ""
39 + "$func = 'foo';" + "$func(); // This calls foo()" + ""
40 + "$func = 'bar';" + "$func('test'); // This calls bar()" + ""
41 + "$func = 'echoit';" + "$func('test'); // This calls echoit()" + "");
42 checkPHP("class Foo" + "{" + " function Vari()" + " {"
44 + " $this->$name(); // This calls the Bar() method\n" + " }"
45 + " " + " function Bar()" + " {"
46 + " echo \"This is Bar\";" + " }" + "}" + ""
47 + "$foo = new Foo();" + "$funcname = \"Var\";"
48 + "$foo->$varname(); // This calls $foo->Var()\n" + "");
49 checkPHP("function square ($num)" + "{" + " return $num * $num;" + "}"
50 + "echo square (4); // outputs '16'." + "");
51 checkPHP("function small_numbers()" + "{" + " return array (0, 1, 2);"
52 + "}" + "list ($zero, $one, $two) = small_numbers();" + "");
53 checkPHP("function &returns_reference()" + "{" + " return $someref;"
54 + "}" + "" + "$newref =& returns_reference();" + " " + "");
55 checkPHP("function add_some_extra(&$string)" + "{"
56 + " $string .= 'and something extra.';" + "}"
57 + "$str = 'This is a string, ';" + "add_some_extra($str);"
59 checkPHP("function makecoffee ($type = \"cappuccino\")\n" + "{\n"
60 + " return \"Making a cup of $type.\\n\";\n" + "}"
61 + "echo makecoffee ();" + "echo makecoffee (\"espresso\");" + "");
62 checkPHP("$makefoo = true;" + "" + "/* We can't call foo() from here "
63 + " since it doesn't exist yet," + " but we can call bar() */" + ""
64 + "bar();" + "" + "if ($makefoo) {" + " function foo ()" + " {"
65 + " echo \"I don't exist until program execution reaches me.\\n\";"
66 + " }" + "}" + "" + "/* Now we can safely call foo()"
67 + " since $makefoo evaluated to true */" + ""
68 + "if ($makefoo) foo();" + "" + "function bar() " + "{"
69 + " echo \"I exist immediately upon program start.\\n\";" + "}" + ""
71 checkPHP("function foo() " + "{" + " function bar() " + " {"
72 + " echo \"I don't exist until foo() is called.\\n\";" + " }" + "}"
73 + "" + "/* We can't call bar() yet" + " since it doesn't exist. */"
74 + "" + "foo();\n" + "" + "/* Now we can call bar(),"
75 + " foo()'s processesing has" + " made it accessable. */" + ""
78 checkPHP(" echo \"This is a test\"; // This is a one-line c++ style comment\n"
79 + " /* This is a multi line comment\n"
80 + " yet another line of comment */\n"
81 + " echo \"This is yet another test\";\n"
82 + " echo \"One Final Test\"; # This is shell-style style comment \n");
83 checkPHP("$bool = TRUE; // a boolean\n"
84 + "$str = \"foo\"; // a string\n" + "$int = 12; // an integer\n"
85 + "\n" + "echo gettype($bool); // prints out \"boolean\"\n"
86 + "echo gettype($str); // prints out \"string\"\n" + ""
87 + "// If this is an integer, increment it by four\n"
88 + "if (is_int($int)) {\n" + " $int += 4;\n" + "}\n" + "\n"
89 + "// If $bool is a string, print it out\n"
90 + "// (does not print out anything)\n" + "if (is_string($bool)) {\n"
91 + " echo \"String: $bool\";\n" + "}\n");
92 checkPHP("$foo = True; // assign the value TRUE to $foo");
93 checkPHP("// == is an operator which test\n"
94 + "// equality and returns a boolean\n"
95 + "if ($action == \"show_version\") {\n"
96 + " echo \"The version is 1.23\";\n" + "}\n" + "\n"
97 + "// this is not necessary...\n" + "if ($show_separators == TRUE) {\n"
98 + " echo \"<hr>\\n\";\n" + "}\n" + "\n"
99 + "// ...because you can simply type\n" + "if ($show_separators) {\n"
100 + " echo \"<hr>\\n\";\n" + "}");
101 checkPHP( // "echo gettype((bool) \"\"); // bool(false)\n"
102 "echo gettype((bool) 1); // bool(true)\n"
103 + "echo gettype((bool) -2); // bool(true)\n"
104 + "echo gettype((bool) \"foo\"); // bool(true)\n"
105 + "echo gettype((bool) 2.3e5); // bool(true)\n"
106 + "echo gettype((bool) array(12)); // bool(true)\n"
107 + "echo gettype((bool) array()); // bool(false)\n");
108 checkPHP("$a = 1234; # decimal number\n"
109 + "$a = -123; # a negative number\n"
110 + "$a = 0123; # octal number (equivalent to 83 decimal)\n"
111 + "$a = 0x1A; # hexadecimal number (equivalent to 26 decimal)\n");
112 checkPHP("$large_number = 2147483647;\n" + "var_dump($large_number);\n"
113 + "// output: int(2147483647)\n" + "\n"
114 + "$large_number = 2147483648;\n" + "var_dump($large_number);\n"
115 + "// output: float(2147483648)\n" + ""
116 + "// this goes also for hexadecimal specified integers:\n"
117 + "var_dump( 0x80000000 );\n" + "// output: float(2147483648)\n" + "\n"
118 + "$million = 1000000;\n" + "$large_number = 50000 * $million;\n"
119 + "var_dump($large_number);\n" + "// output: float(50000000000)\n");
120 checkPHP("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)");
123 checkPHP("echo (int) ( (0.1+0.7) * 10 ); // echoes 7!");
124 checkPHP("$a = 1.234; " + "$b = 1.2e3; " + "$c = 7E-10;");
125 checkPHP("echo 'this is a simple string';\n" + "\n"
126 + "echo 'You can also have embedded newlines in \n"
127 + "strings this way as it is\n" + "okay to do';\n" + "\n"
128 + "// Outputs: \"I'll be back\"\n"
129 + "echo 'Arnold once said: \"I\\'ll be back\"';\n" + "\n"
130 + "// Outputs: You deleted C:\\*.*?\n"
131 + "echo 'You deleted C:\\\\*.*?';\n" + "\n"
132 + "// Outputs: You deleted C:\\*.*?\n"
133 + "echo 'You deleted C:\\\\*.*?';\n" + "\n"
134 + "// Outputs: This will not expand: \\n a newline\n"
135 + "echo 'This will not expand: \\n a newline';\n" + "\n"
136 + "// Outputs: Variables do not $expand $either\n"
137 + "echo 'Variables do not $expand $either';\n");
139 checkPHP("echo \"This works: \" . $arr['foo'][3];");
140 checkPHP("echo \"\\$foo==$foo; type is \" . gettype ($foo) . \"<br />\\n\";");
141 checkPHP("$arr = array(\"foo\" => \"bar\", 12 => true);\n" + "\n"
142 + "echo $arr[\"foo\"]; // bar\n" + "echo $arr[12]; // 1\n");
143 checkPHP("// This array is the same as ...\n"
144 + "array(5 => 43, 32, 56, \"b\" => 12);\n" + "\n"
145 + "// ...this array\n"
146 + "array(5 => 43, 6 => 32, 7 => 56, \"b\" => 12);\n");
147 checkPHP("$arr = array(5 => 1, 12 => 2);\n" + "\n"
148 + "$arr[] = 56; // This is the same as $arr[13] = 56;\n"
149 + " // at this point of the script\n" + "\n"
150 + "$arr[\"x\"] = 42; // This adds a new element to\n"
151 + " // the array with key \"x\"\n"
153 + "unset($arr[5]); // This removes the element from the array\n" + "\n"
154 + "unset($arr); // This deletes the whole array\n");
155 checkPHP("$foo[bar] = 'enemy';\n" + "echo $foo[bar];");
156 checkPHP("$a = array( 'color' => 'red',\n"
157 + " 'taste' => 'sweet',\n"
158 + " 'shape' => 'round',\n"
159 + " 'name' => 'apple',\n"
160 + " 4 // key will be 0\n"
163 + "// is completely equivalent with\n"
164 + "$a['color'] = 'red';\n"
165 + "$a['taste'] = 'sweet';\n"
166 + "$a['shape'] = 'round';\n"
167 + "$a['name'] = 'apple';\n"
168 + "$a[] = 4; // key will be 0\n"
173 + "// will result in the array array(0 => 'a' , 1 => 'b' , 2 => 'c'),\n"
174 + "// or simply array('a', 'b', 'c')\n");
175 checkPHP("foreach ($colors as $key => $color) {\n" + " // won't work:\n"
176 + " //$color = strtoupper($color);\n" + " \n" + " // works:\n"
177 + " $colors[$key] = strtoupper($color);\n" + "}\n"
178 + "print_r($colors);\n");
179 checkPHP("$fruits = array ( \"fruits\" => array ( \"a\" => \"orange\",\n"
180 + " \"b\" => \"banana\",\n"
181 + " \"c\" => \"apple\"\n"
183 + " \"numbers\" => array ( 1,\n"
190 + " \"holes\" => array ( \"first\",\n"
191 + " 5 => \"second\",\n"
194 + "\n" + "// Some examples to address values in the array above \n"
195 + "echo $fruits[\"holes\"][5]; // prints \"second\"\n"
196 + "echo $fruits[\"fruits\"][\"a\"]; // prints \"orange\"\n"
197 + "unset($fruits[\"holes\"][0]); // remove \"first\"\n" + "\n"
198 + "// Create a new multi-dimensional array\n"
199 + "$juices[\"apple\"][\"green\"] = \"good\"; \n");
200 checkPHP("$arr3 = &$arr1;");
201 checkPHP("class foo\n" + "{\n" + " function do_foo()\n" + " {\n"
202 + " echo \"Doing foo.\"; \n" + " }\n" + "}\n" + "\n"
203 + "$bar = new foo;\n" + "$bar->do_foo();\n");
204 checkPHP("$obj = (object) 'ciao';\n"
205 + "echo $obj->scalar; // outputs 'ciao'");
206 checkPHP("$var = NULL;");
207 checkPHP("$var = \"Bob\";\n" + "$Var = \"Joe\";\n"
208 + "echo \"$var, $Var\"; // outputs \"Bob, Joe\"\n" + "\n" +
209 // "$4site = 'not yet'; // invalid; starts with a number\n" +
210 "$_4site = 'not yet'; // valid; starts with an underscore\n"
211 + "$täyte = 'mansikka'; \n");
216 private void checkPHP(String strEval) {
218 System.out.println("\n------------------------------------");
219 System.out.println(strEval);
222 strEval.toCharArray(),
224 // parser.phpParserTester(strEval, 1);
226 // private void checkHTML(String strEval) {
227 // if (Scanner.DEBUG) {
228 // System.out.println("\n------------------------------------");
229 // System.out.println(strEval);
231 // parser.parse(strEval);
234 * The JUnit setup method
236 // protected void setUp() {
237 // parser = new Parser(null);