added new test cases
[phpeclipse.git] / net.sourceforge.phpeclipse.tests / src / net / sourceforge / phpeclipse / tests / parser / PHPManualTestCase.java
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 **********************************************************************/
9
10 import net.sourceforge.phpdt.internal.compiler.parser.Parser;
11 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
12
13 import org.eclipse.core.runtime.CoreException;
14 import junit.framework.TestCase;
15
16 /**
17  *  Tests the php parser
18  */
19 public class PHPManualTestCase extends TestCase {
20
21   Parser parser;
22
23   public PHPManualTestCase(String name) {
24     super(name);
25   }
26
27   /**
28    *  Test the PHP Parser with different PHP snippets
29    */
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           "");
39     checkPHP(
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    "");
41     // Bugs item #690938
42     checkPHP(
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");
48     checkPHP(
49       "$bool = TRUE;   // a boolean\n"
50         + "$str  = \"foo\";  // a string\n"
51         + "$int  = 12;     // an integer\n"
52         + "\n"
53         + "echo gettype($bool); // prints out \"boolean\"\n"
54         + "echo gettype($str);  // prints out \"string\"\n"
55         + ""
56         + "// If this is an integer, increment it by four\n"
57         + "if (is_int($int)) {\n"
58         + "    $int += 4;\n"
59         + "}\n"
60         + "\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"
65         + "}\n");
66
67     checkPHP("$foo = True; // assign the value TRUE to $foo");
68
69     checkPHP(
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"
74         + "}\n"
75         + "\n"
76         + "// this is not necessary...\n"
77         + "if ($show_separators == TRUE) {\n"
78         + "    echo \"<hr>\\n\";\n"
79         + "}\n"
80         + "\n"
81         + "// ...because you can simply type\n"
82         + "if ($show_separators) {\n"
83         + "    echo \"<hr>\\n\";\n"
84         + "}");
85
86     checkPHP(
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");
94
95     checkPHP(
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");
100
101     checkPHP(
102       "$large_number =  2147483647;\n"
103         + "var_dump($large_number);\n"
104         + "// output: int(2147483647)\n"
105         + "\n"
106         + "$large_number =  2147483648;\n"
107         + "var_dump($large_number);\n"
108         + "// output: float(2147483648)\n"
109         + ""
110         + "// this goes also for hexadecimal specified integers:\n"
111         + "var_dump( 0x80000000 );\n"
112         + "// output: float(2147483648)\n"
113         + "\n"
114         + "$million = 1000000;\n"
115         + "$large_number =  50000 * $million;\n"
116         + "var_dump($large_number);\n"
117         + "// output: float(50000000000)\n");
118
119     checkPHP(
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)");
123
124     checkPHP("echo (int) ( (0.1+0.7) * 10 ); // echoes 7!");
125
126     checkPHP("$a = 1.234; " + "$b = 1.2e3; " + "$c = 7E-10;");
127
128     checkPHP(
129       "echo 'this is a simple string';\n"
130         + "\n"
131         + "echo 'You can also have embedded newlines in \n"
132         + "strings this way as it is\n"
133         + "okay to do';\n"
134         + "\n"
135         + "// Outputs: \"I'll be back\"\n"
136         + "echo 'Arnold once said: \"I\\'ll be back\"';\n"
137         + "\n"
138         + "// Outputs: You deleted C:\\*.*?\n"
139         + "echo 'You deleted C:\\\\*.*?';\n"
140         + "\n"
141         + "// Outputs: You deleted C:\\*.*?\n"
142         + "echo 'You deleted C:\\\\*.*?';\n"
143         + "\n"
144         + "// Outputs: This will not expand: \\n a newline\n"
145         + "echo 'This will not expand: \\n a newline';\n"
146         + "\n"
147         + "// Outputs: Variables do not $expand $either\n"
148         + "echo 'Variables do not $expand $either';\n");
149
150     checkPHP(
151       "$str = <<<EOD\n"
152         + "Example of string\n"
153         + "spanning multiple lines\n"
154         + "using heredoc syntax.\n"
155         + "EOD;\n"
156         + "\n"
157         + "/* More complex example, with variables. */\n"
158         + "class foo\n"
159         + "{\n"
160         + "    var $foo;\n"
161         + "    var $bar;\n"
162         + "\n"
163         + "    function foo()\n"
164         + "    {\n"
165         + "        $this->foo = 'Foo';\n"
166         + "        $this->bar = array('Bar1', 'Bar2', 'Bar3');\n"
167         + "    }\n"
168         + "}\n"
169         + "\n"
170         + "$foo = new foo();\n"
171         + "$name = 'MyName';\n"
172         + "\n"
173         + "echo <<<EOT\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"
177         + "EOT;\n");
178
179     checkPHP("echo \"This works: \" . $arr['foo'][3];");
180
181     checkPHP("echo \"\\$foo==$foo; type is \" . gettype ($foo) . \"<br />\\n\";");
182
183     checkPHP(
184       "$arr = array(\"foo\" => \"bar\", 12 => true);\n"
185         + "\n"
186         + "echo $arr[\"foo\"]; // bar\n"
187         + "echo $arr[12];    // 1\n");
188
189     checkPHP(
190       "// This array is the same as ...\n"
191         + "array(5 => 43, 32, 56, \"b\" => 12);\n"
192         + "\n"
193         + "// ...this array\n"
194         + "array(5 => 43, 6 => 32, 7 => 56, \"b\" => 12);\n");
195
196     checkPHP(
197       "$arr = array(5 => 1, 12 => 2);\n"
198         + "\n"
199         + "$arr[] = 56;    // This is the same as $arr[13] = 56;\n"
200         + "                // at this point of the script\n"
201         + "\n"
202         + "$arr[\"x\"] = 42; // This adds a new element to\n"
203         + "                // the array with key \"x\"\n"
204         + "                \n"
205         + "unset($arr[5]); // This removes the element from the array\n"
206         + "\n"
207         + "unset($arr);    // This deletes the whole array\n");
208
209     checkPHP("$foo[bar] = 'enemy';\n" + "echo $foo[bar];");
210
211     checkPHP(
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"
217         + "          );\n"
218         + "\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"
225         + "\n"
226         + "$b[] = 'a';\n"
227         + "$b[] = 'b';\n"
228         + "$b[] = 'c';\n"
229         + "// will result in the array array(0 => 'a' , 1 => 'b' , 2 => 'c'),\n"
230         + "// or simply array('a', 'b', 'c')\n");
231
232     checkPHP(
233       "foreach ($colors as $key => $color) {\n"
234         + "    // won't work:\n"
235         + "    //$color = strtoupper($color);\n"
236         + "    \n"
237         + "    // works:\n"
238         + "    $colors[$key] = strtoupper($color);\n"
239         + "}\n"
240         + "print_r($colors);\n");
241
242     checkPHP(
243       "$fruits = array ( \"fruits\"  => array ( \"a\" => \"orange\",\n"
244         + "                                       \"b\" => \"banana\",\n"
245         + "                                       \"c\" => \"apple\"\n"
246         + "                                     ),\n"
247         + "                  \"numbers\" => array ( 1,\n"
248         + "                                       2,\n"
249         + "                                       3,\n"
250         + "                                       4,\n"
251         + "                                       5,\n"
252         + "                                       6,\n"
253         + "                                     ),\n"
254         + "                  \"holes\"   => array (      \"first\",\n"
255         + "                                       5 => \"second\",\n"
256         + "                                            \"third\"\n"
257         + "                                     )\n"
258         + "                );\n"
259         + "\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"
264         + "\n"
265         + "// Create a new multi-dimensional array\n"
266         + "$juices[\"apple\"][\"green\"] = \"good\"; \n");
267
268     checkPHP("$arr3 = &$arr1;");
269
270     checkPHP(
271       "class foo\n"
272         + "{\n"
273         + "    function do_foo()\n"
274         + "    {\n"
275         + "        echo \"Doing foo.\"; \n"
276         + "    }\n"
277         + "}\n"
278         + "\n"
279         + "$bar = new foo;\n"
280         + "$bar->do_foo();\n");
281
282     checkPHP(
283       "$obj = (object) 'ciao';\n" + 
284       "echo $obj->scalar;  // outputs 'ciao'");
285
286     checkPHP("$var = NULL;");
287
288     checkPHP("$var = \"Bob\";\n" +
289         "$Var = \"Joe\";\n" +
290         "echo \"$var, $Var\";      // outputs \"Bob, Joe\"\n" +
291         "\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");
295
296     checkPHP("");
297
298     checkPHP("");
299
300     checkPHP("");
301   }
302
303   private void checkPHP(String strEval) {
304     try {
305       if (Scanner.DEBUG) {
306         System.out.println("\n------------------------------------");
307         System.out.println(strEval);
308       }
309       parser.phpParserTester(strEval, 1);
310     } catch (CoreException e) {
311     }
312   }
313
314   private void checkHTML(String strEval) {
315     try {
316       if (Scanner.DEBUG) {
317         System.out.println("\n------------------------------------");
318         System.out.println(strEval);
319       }
320       parser.parse(strEval);
321     } catch (CoreException e) {
322     }
323   }
324
325   /**
326    *  The JUnit setup method
327    */
328   protected void setUp() {
329     parser = new Parser(null);
330   }
331
332 }