1 package junit.sourceforge.phpeclipse;
 
   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 **********************************************************************/
 
  10 import net.sourceforge.phpdt.internal.compiler.parser.Parser;
 
  11 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
 
  13 import org.eclipse.core.runtime.CoreException;
 
  14 import junit.framework.TestCase;
 
  17  *  Tests the php parser
 
  19 public class PHPManualTestCase extends TestCase {
 
  23   public PHPManualTestCase(String name) {
 
  28    *  Test the PHP Parser with different PHP snippets
 
  30   public void testPHPParser() {
 
  33       "    echo \"This is a test\"; // This is a one-line c++ style comment\n"
 
  34         + "    /* This is a multi line comment\n"
 
  35         + "       yet another line of comment */\n"
 
  36         + "    echo \"This is yet another test\";\n"
 
  37         + "    echo \"One Final Test\"; # This is shell-style style comment \n");
 
  39       "$bool = TRUE;   // a boolean\n"
 
  40         + "$str  = \"foo\";  // a string\n"
 
  41         + "$int  = 12;     // an integer\n"
 
  43         + "echo gettype($bool); // prints out \"boolean\"\n"
 
  44         + "echo gettype($str);  // prints out \"string\"\n"
 
  46         + "// If this is an integer, increment it by four\n"
 
  47         + "if (is_int($int)) {\n"
 
  51         + "// If $bool is a string, print it out\n"
 
  52         + "// (does not print out anything)\n"
 
  53         + "if (is_string($bool)) {\n"
 
  54         + "    echo \"String: $bool\";\n"
 
  57     checkPHP("$foo = True; // assign the value TRUE to $foo");
 
  60       "// == is an operator which test\n"
 
  61         + "// equality and returns a boolean\n"
 
  62         + "if ($action == \"show_version\") {\n"
 
  63         + "    echo \"The version is 1.23\";\n"
 
  66         + "// this is not necessary...\n"
 
  67         + "if ($show_separators == TRUE) {\n"
 
  68         + "    echo \"<hr>\\n\";\n"
 
  71         + "// ...because you can simply type\n"
 
  72         + "if ($show_separators) {\n"
 
  73         + "    echo \"<hr>\\n\";\n"
 
  77       "echo gettype((bool) \"\");        // bool(false)\n"
 
  78         + "echo gettype((bool) 1);         // bool(true)\n"
 
  79         + "echo gettype((bool) -2);        // bool(true)\n"
 
  80         + "echo gettype((bool) \"foo\");     // bool(true)\n"
 
  81         + "echo gettype((bool) 2.3e5);     // bool(true)\n"
 
  82         + "echo gettype((bool) array(12)); // bool(true)\n"
 
  83         + "echo gettype((bool) array());   // bool(false)\n");
 
  86       "$a = 1234; # decimal number\n"
 
  87         + "$a = -123; # a negative number\n"
 
  88         + "$a = 0123; # octal number (equivalent to 83 decimal)\n"
 
  89         + "$a = 0x1A; # hexadecimal number (equivalent to 26 decimal)\n");
 
  92       "$large_number =  2147483647;\n"
 
  93         + "var_dump($large_number);\n"
 
  94         + "// output: int(2147483647)\n"
 
  96         + "$large_number =  2147483648;\n"
 
  97         + "var_dump($large_number);\n"
 
  98         + "// output: float(2147483648)\n"
 
 100         + "// this goes also for hexadecimal specified integers:\n"
 
 101         + "var_dump( 0x80000000 );\n"
 
 102         + "// output: float(2147483648)\n"
 
 104         + "$million = 1000000;\n"
 
 105         + "$large_number =  50000 * $million;\n"
 
 106         + "var_dump($large_number);\n"
 
 107         + "// output: float(50000000000)\n");
 
 110       "var_dump(25/7);         // float(3.5714285714286)\n"
 
 111         + "var_dump((int) (25/7)); // int(3)\n"
 
 112         + "var_dump(round(25/7));  // float(4)");
 
 114     checkPHP("echo (int) ( (0.1+0.7) * 10 ); // echoes 7!");
 
 116     checkPHP("$a = 1.234; " + "$b = 1.2e3; " + "$c = 7E-10;");
 
 119       "echo 'this is a simple string';\n"
 
 121         + "echo 'You can also have embedded newlines in \n"
 
 122         + "strings this way as it is\n"
 
 125         + "// Outputs: \"I'll be back\"\n"
 
 126         + "echo 'Arnold once said: \"I\\'ll be back\"';\n"
 
 128         + "// Outputs: You deleted C:\\*.*?\n"
 
 129         + "echo 'You deleted C:\\\\*.*?';\n"
 
 131         + "// Outputs: You deleted C:\\*.*?\n"
 
 132         + "echo 'You deleted C:\\\\*.*?';\n"
 
 134         + "// Outputs: This will not expand: \\n a newline\n"
 
 135         + "echo 'This will not expand: \\n a newline';\n"
 
 137         + "// Outputs: Variables do not $expand $either\n"
 
 138         + "echo 'Variables do not $expand $either';\n");
 
 142         + "Example of string\n"
 
 143         + "spanning multiple lines\n"
 
 144         + "using heredoc syntax.\n"
 
 147         + "/* More complex example, with variables. */\n"
 
 153         + "    function foo()\n"
 
 155         + "        $this->foo = 'Foo';\n"
 
 156         + "        $this->bar = array('Bar1', 'Bar2', 'Bar3');\n"
 
 160         + "$foo = new foo();\n"
 
 161         + "$name = 'MyName';\n"
 
 164         + "My name is \"$name\". I am printing some $foo->foo.\n"
 
 165         + "Now, I am printing some {$foo->bar[1]}.\n"
 
 166         + "This should print a capital 'A': \\x41\n"
 
 169     checkPHP("echo \"This works: \" . $arr['foo'][3];");
 
 171     checkPHP("echo \"\\$foo==$foo; type is \" . gettype ($foo) . \"<br />\\n\";");
 
 174       "$arr = array(\"foo\" => \"bar\", 12 => true);\n"
 
 176         + "echo $arr[\"foo\"]; // bar\n"
 
 177         + "echo $arr[12];    // 1\n");
 
 180       "// This array is the same as ...\n"
 
 181         + "array(5 => 43, 32, 56, \"b\" => 12);\n"
 
 183         + "// ...this array\n"
 
 184         + "array(5 => 43, 6 => 32, 7 => 56, \"b\" => 12);\n");
 
 187       "$arr = array(5 => 1, 12 => 2);\n"
 
 189         + "$arr[] = 56;    // This is the same as $arr[13] = 56;\n"
 
 190         + "                // at this point of the script\n"
 
 192         + "$arr[\"x\"] = 42; // This adds a new element to\n"
 
 193         + "                // the array with key \"x\"\n"
 
 195         + "unset($arr[5]); // This removes the element from the array\n"
 
 197         + "unset($arr);    // This deletes the whole array\n");
 
 199     checkPHP("$foo[bar] = 'enemy';\n" + "echo $foo[bar];");
 
 202       "$a = array( 'color' => 'red',\n"
 
 203         + "            'taste' => 'sweet',\n"
 
 204         + "            'shape' => 'round',\n"
 
 205         + "            'name'  => 'apple',\n"
 
 206         + "                       4        // key will be 0\n"
 
 209         + "// is completely equivalent with\n"
 
 210         + "$a['color'] = 'red';\n"
 
 211         + "$a['taste'] = 'sweet';\n"
 
 212         + "$a['shape'] = 'round';\n"
 
 213         + "$a['name']  = 'apple';\n"
 
 214         + "$a[]        = 4;        // key will be 0\n"
 
 219         + "// will result in the array array(0 => 'a' , 1 => 'b' , 2 => 'c'),\n"
 
 220         + "// or simply array('a', 'b', 'c')\n");
 
 223       "foreach ($colors as $key => $color) {\n"
 
 224         + "    // won't work:\n"
 
 225         + "    //$color = strtoupper($color);\n"
 
 228         + "    $colors[$key] = strtoupper($color);\n"
 
 230         + "print_r($colors);\n");
 
 233       "$fruits = array ( \"fruits\"  => array ( \"a\" => \"orange\",\n"
 
 234         + "                                       \"b\" => \"banana\",\n"
 
 235         + "                                       \"c\" => \"apple\"\n"
 
 237         + "                  \"numbers\" => array ( 1,\n"
 
 244         + "                  \"holes\"   => array (      \"first\",\n"
 
 245         + "                                       5 => \"second\",\n"
 
 250         + "// Some examples to address values in the array above \n"
 
 251         + "echo $fruits[\"holes\"][5];    // prints \"second\"\n"
 
 252         + "echo $fruits[\"fruits\"][\"a\"]; // prints \"orange\"\n"
 
 253         + "unset($fruits[\"holes\"][0]);  // remove \"first\"\n"
 
 255         + "// Create a new multi-dimensional array\n"
 
 256         + "$juices[\"apple\"][\"green\"] = \"good\"; \n");
 
 258     checkPHP("$arr3 = &$arr1;");
 
 263         + "    function do_foo()\n"
 
 265         + "        echo \"Doing foo.\"; \n"
 
 269         + "$bar = new foo;\n"
 
 270         + "$bar->do_foo();\n");
 
 273       "$obj = (object) 'ciao';\n" + 
 
 274       "echo $obj->scalar;  // outputs 'ciao'");
 
 276     checkPHP("$var = NULL;");
 
 278     checkPHP("$var = \"Bob\";\n" +
\r     "$Var = \"Joe\";\n" +
\r          "echo \"$var, $Var\";      // outputs \"Bob, Joe\"\n" +
\r        "\n" +
\r   //    "$4site = 'not yet';     // invalid; starts with a number\n" +
\r         "$_4site = 'not yet';    // valid; starts with an underscore\n" +
\r      "$täyte = 'mansikka';  \n");
 
 287   private void checkPHP(String strEval) {
 
 290         System.out.println("\n------------------------------------");
 
 291         System.out.println(strEval);
 
 293       parser.phpParserTester(strEval, 1);
 
 294     } catch (CoreException e) {
 
 298   private void checkHTML(String strEval) {
 
 301         System.out.println("\n------------------------------------");
 
 302         System.out.println(strEval);
 
 304       parser.parse(strEval);
 
 305     } catch (CoreException e) {
 
 310    *  The JUnit setup method
 
 312   protected void setUp() {
 
 313     parser = new Parser(null);