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