request 1152810, implementing formatting of array initializers
[phpeclipse.git] / net.sourceforge.phpeclipse.tests / src / net / sourceforge / phpeclipse / tests / parser / PHPManualTestCase.java
index b460407..5d2c0b7 100644 (file)
 package net.sourceforge.phpeclipse.tests.parser;
-/**********************************************************************
-Copyright (c) 2002 Klaus Hartlage - www.eclipseproject.de
-All rights reserved. This program and the accompanying materials
-are made available under the terms of the Common Public License v1.0
-which accompanies this distribution, and is available at
-http://www.eclipse.org/legal/cpl-v10.html
-**********************************************************************/
-
-import net.sourceforge.phpdt.internal.compiler.parser.Parser;
+/*******************************************************************************
+ * Copyright (c) 2002 www.phpeclipse.de All rights
+ * reserved. This program and the accompanying materials are made available
+ * under the terms of the Common Public License v1.0 which accompanies this
+ * distribution, and is available at http://www.eclipse.org/legal/cpl-v10.html
+ ******************************************************************************/
+import net.sourceforge.phpdt.core.tests.util.AbstractCompilerTest;
 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
-
-import org.eclipse.core.runtime.CoreException;
-import junit.framework.TestCase;
-
 /**
- *  Tests the php parser
+ * Tests the php parser
  */
-public class PHPManualTestCase extends TestCase {
-
-  Parser parser;
-
+public class PHPManualTestCase extends AbstractCompilerTest {
+//  Parser parser;
   public PHPManualTestCase(String name) {
     super(name);
   }
-
   /**
-   *  Test the PHP Parser with different PHP snippets
+   * Test the PHP Parser with different PHP snippets
    */
   public void testPHPParser() {
+       checkPHP("self::$instance = new Singleton();");
+    checkPHP("if ((eregi(\"<[^>]*script*\\\"?[^>]*>\", $secvalue)) ||\r\n" +
+               "       (eregi(\"<[^>]*object*\\\"?[^>]*>\", $secvalue)) ||\r\n" +
+               "       (eregi(\"<[^>]*iframe*\\\"?[^>]*>\", $secvalue)) ||\r\n" +
+               "       (eregi(\"<[^>]*applet*\\\"?[^>]*>\", $secvalue)) ||\r\n" +
+               "       (eregi(\"<[^>]*meta*\\\"?[^>]*>\", $secvalue)) ||\r\n" +
+               "       (eregi(\"<[^>]*style*\\\"?[^>]*>\", $secvalue)) ||\r\n" +
+               "       (eregi(\"<[^>]*form*\\\"?[^>]*>\", $secvalue)) ||\r\n" +
+               "       (eregi(\"\\([^>]*\\\"?[^)]*\\)\", $secvalue)) ||\r\n" +
+               "       (eregi(\"\\\"\", $secvalue))) {\r\n" +
+               "   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" +
+               "    }");
+    checkPHP("function foo()" + "{" + "    echo \"In foo()<br>\\n\";" + "}"
+        + "" + "function bar($arg = '')" + "{"
+        + "    echo \"In bar(); argument was '$arg'.<br>\\n\";" + "}" + ""
+        + "// This is a wrapper function around echo"
+        + "function echoit($string)" + "{" + "    echo $string;" + "}" + ""
+        + "$func = 'foo';" + "$func();        // This calls foo()" + ""
+        + "$func = 'bar';" + "$func('test');  // This calls bar()" + ""
+        + "$func = 'echoit';" + "$func('test');  // This calls echoit()" + "");
+    checkPHP("class Foo" + "{" + "    function Vari()" + "    {"
+        + "        $name = 'Bar';"
+        + "        $this->$name(); // This calls the Bar() method\n" + "    }"
+        + "    " + "    function Bar()" + "    {"
+        + "        echo \"This is Bar\";" + "    }" + "}" + ""
+        + "$foo = new Foo();" + "$funcname = \"Var\";"
+        + "$foo->$varname();   // This calls $foo->Var()\n" + "");
+    checkPHP("function square ($num)" + "{" + "    return $num * $num;" + "}"
+        + "echo square (4);   // outputs '16'." + "");
+    checkPHP("function small_numbers()" + "{" + "    return array (0, 1, 2);"
+        + "}" + "list ($zero, $one, $two) = small_numbers();" + "");
+    checkPHP("function &returns_reference()" + "{" + "    return $someref;"
+        + "}" + "" + "$newref =& returns_reference();" + " " + "");
+    checkPHP("function add_some_extra(&$string)" + "{"
+        + "    $string .= 'and something extra.';" + "}"
+        + "$str = 'This is a string, ';" + "add_some_extra($str);"
+        + "echo $str;    ");
+    checkPHP("function makecoffee ($type = \"cappuccino\")\n" + "{\n"
+        + "    return \"Making a cup of $type.\\n\";\n" + "}"
+        + "echo makecoffee ();" + "echo makecoffee (\"espresso\");" + "");
+    checkPHP("$makefoo = true;" + "" + "/* We can't call foo() from here "
+        + "   since it doesn't exist yet," + "   but we can call bar() */" + ""
+        + "bar();" + "" + "if ($makefoo) {" + "  function foo ()" + "  {"
+        + "    echo \"I don't exist until program execution reaches me.\\n\";"
+        + "  }" + "}" + "" + "/* Now we can safely call foo()"
+        + "   since $makefoo evaluated to true */" + ""
+        + "if ($makefoo) foo();" + "" + "function bar() " + "{"
+        + "  echo \"I exist immediately upon program start.\\n\";" + "}" + ""
+        + "");
+    checkPHP("function foo() " + "{" + "  function bar() " + "  {"
+        + "    echo \"I don't exist until foo() is called.\\n\";" + "  }" + "}"
+        + "" + "/* We can't call bar() yet" + "   since it doesn't exist. */"
+        + "" + "foo();\n" + "" + "/* Now we can call bar(),"
+        + "   foo()'s processesing has" + "   made it accessable. */" + ""
+        + "bar();" + "");
     // Bugs item #690938
-    checkPHP(
-      "    echo \"This is a test\"; // This is a one-line c++ style comment\n"
+    checkPHP("    echo \"This is a test\"; // This is a one-line c++ style comment\n"
         + "    /* This is a multi line comment\n"
         + "       yet another line of comment */\n"
         + "    echo \"This is yet another test\";\n"
         + "    echo \"One Final Test\"; # This is shell-style style comment \n");
-    checkPHP(
-      "$bool = TRUE;   // a boolean\n"
-        + "$str  = \"foo\";  // a string\n"
-        + "$int  = 12;     // an integer\n"
-        + "\n"
-        + "echo gettype($bool); // prints out \"boolean\"\n"
-        + "echo gettype($str);  // prints out \"string\"\n"
-        + ""
+    checkPHP("$bool = TRUE;   // a boolean\n"
+        + "$str  = \"foo\";  // a string\n" + "$int  = 12;     // an integer\n"
+        + "\n" + "echo gettype($bool); // prints out \"boolean\"\n"
+        + "echo gettype($str);  // prints out \"string\"\n" + ""
         + "// If this is an integer, increment it by four\n"
-        + "if (is_int($int)) {\n"
-        + "    $int += 4;\n"
-        + "}\n"
-        + "\n"
+        + "if (is_int($int)) {\n" + "    $int += 4;\n" + "}\n" + "\n"
         + "// If $bool is a string, print it out\n"
-        + "// (does not print out anything)\n"
-        + "if (is_string($bool)) {\n"
-        + "    echo \"String: $bool\";\n"
-        + "}\n");
-
+        + "// (does not print out anything)\n" + "if (is_string($bool)) {\n"
+        + "    echo \"String: $bool\";\n" + "}\n");
     checkPHP("$foo = True; // assign the value TRUE to $foo");
-
-    checkPHP(
-      "// == is an operator which test\n"
+    checkPHP("// == is an operator which test\n"
         + "// equality and returns a boolean\n"
         + "if ($action == \"show_version\") {\n"
-        + "    echo \"The version is 1.23\";\n"
-        + "}\n"
-        + "\n"
-        + "// this is not necessary...\n"
-        + "if ($show_separators == TRUE) {\n"
-        + "    echo \"<hr>\\n\";\n"
-        + "}\n"
-        + "\n"
-        + "// ...because you can simply type\n"
-        + "if ($show_separators) {\n"
-        + "    echo \"<hr>\\n\";\n"
-        + "}");
-
-    checkPHP(
-      "echo gettype((bool) \"\");        // bool(false)\n"
-        + "echo gettype((bool) 1);         // bool(true)\n"
+        + "    echo \"The version is 1.23\";\n" + "}\n" + "\n"
+        + "// this is not necessary...\n" + "if ($show_separators == TRUE) {\n"
+        + "    echo \"<hr>\\n\";\n" + "}\n" + "\n"
+        + "// ...because you can simply type\n" + "if ($show_separators) {\n"
+        + "    echo \"<hr>\\n\";\n" + "}");
+    checkPHP(  // "echo gettype((bool) \"\");        // bool(false)\n"
+        "echo gettype((bool) 1);         // bool(true)\n"
         + "echo gettype((bool) -2);        // bool(true)\n"
         + "echo gettype((bool) \"foo\");     // bool(true)\n"
         + "echo gettype((bool) 2.3e5);     // bool(true)\n"
         + "echo gettype((bool) array(12)); // bool(true)\n"
         + "echo gettype((bool) array());   // bool(false)\n");
-
-    checkPHP(
-      "$a = 1234; # decimal number\n"
+    checkPHP("$a = 1234; # decimal number\n"
         + "$a = -123; # a negative number\n"
         + "$a = 0123; # octal number (equivalent to 83 decimal)\n"
         + "$a = 0x1A; # hexadecimal number (equivalent to 26 decimal)\n");
-
-    checkPHP(
-      "$large_number =  2147483647;\n"
-        + "var_dump($large_number);\n"
-        + "// output: int(2147483647)\n"
-        + "\n"
-        + "$large_number =  2147483648;\n"
-        + "var_dump($large_number);\n"
-        + "// output: float(2147483648)\n"
-        + ""
+    checkPHP("$large_number =  2147483647;\n" + "var_dump($large_number);\n"
+        + "// output: int(2147483647)\n" + "\n"
+        + "$large_number =  2147483648;\n" + "var_dump($large_number);\n"
+        + "// output: float(2147483648)\n" + ""
         + "// this goes also for hexadecimal specified integers:\n"
-        + "var_dump( 0x80000000 );\n"
-        + "// output: float(2147483648)\n"
-        + "\n"
-        + "$million = 1000000;\n"
-        + "$large_number =  50000 * $million;\n"
-        + "var_dump($large_number);\n"
-        + "// output: float(50000000000)\n");
-
-    checkPHP(
-      "var_dump(25/7);         // float(3.5714285714286)\n"
+        + "var_dump( 0x80000000 );\n" + "// output: float(2147483648)\n" + "\n"
+        + "$million = 1000000;\n" + "$large_number =  50000 * $million;\n"
+        + "var_dump($large_number);\n" + "// output: float(50000000000)\n");
+    checkPHP("var_dump(25/7);         // float(3.5714285714286)\n"
         + "var_dump((int) (25/7)); // int(3)\n"
         + "var_dump(round(25/7));  // float(4)");
-
     checkPHP("echo (int) ( (0.1+0.7) * 10 ); // echoes 7!");
-
     checkPHP("$a = 1.234; " + "$b = 1.2e3; " + "$c = 7E-10;");
-
-    checkPHP(
-      "echo 'this is a simple string';\n"
-        + "\n"
+    checkPHP("echo 'this is a simple string';\n" + "\n"
         + "echo 'You can also have embedded newlines in \n"
-        + "strings this way as it is\n"
-        + "okay to do';\n"
-        + "\n"
+        + "strings this way as it is\n" + "okay to do';\n" + "\n"
         + "// Outputs: \"I'll be back\"\n"
-        + "echo 'Arnold once said: \"I\\'ll be back\"';\n"
-        + "\n"
+        + "echo 'Arnold once said: \"I\\'ll be back\"';\n" + "\n"
         + "// Outputs: You deleted C:\\*.*?\n"
-        + "echo 'You deleted C:\\\\*.*?';\n"
-        + "\n"
+        + "echo 'You deleted C:\\\\*.*?';\n" + "\n"
         + "// Outputs: You deleted C:\\*.*?\n"
-        + "echo 'You deleted C:\\\\*.*?';\n"
-        + "\n"
+        + "echo 'You deleted C:\\\\*.*?';\n" + "\n"
         + "// Outputs: This will not expand: \\n a newline\n"
-        + "echo 'This will not expand: \\n a newline';\n"
-        + "\n"
+        + "echo 'This will not expand: \\n a newline';\n" + "\n"
         + "// Outputs: Variables do not $expand $either\n"
         + "echo 'Variables do not $expand $either';\n");
 
-    checkPHP(
-      "$str = <<<EOD\n"
-        + "Example of string\n"
-        + "spanning multiple lines\n"
-        + "using heredoc syntax.\n"
-        + "EOD;\n"
-        + "\n"
-        + "/* More complex example, with variables. */\n"
-        + "class foo\n"
-        + "{\n"
-        + "    var $foo;\n"
-        + "    var $bar;\n"
-        + "\n"
-        + "    function foo()\n"
-        + "    {\n"
-        + "        $this->foo = 'Foo';\n"
-        + "        $this->bar = array('Bar1', 'Bar2', 'Bar3');\n"
-        + "    }\n"
-        + "}\n"
-        + "\n"
-        + "$foo = new foo();\n"
-        + "$name = 'MyName';\n"
-        + "\n"
-        + "echo <<<EOT\n"
-        + "My name is \"$name\". I am printing some $foo->foo.\n"
-        + "Now, I am printing some {$foo->bar[1]}.\n"
-        + "This should print a capital 'A': \\x41\n"
-        + "EOT;\n");
-
     checkPHP("echo \"This works: \" . $arr['foo'][3];");
-
     checkPHP("echo \"\\$foo==$foo; type is \" . gettype ($foo) . \"<br />\\n\";");
-
-    checkPHP(
-      "$arr = array(\"foo\" => \"bar\", 12 => true);\n"
-        + "\n"
-        + "echo $arr[\"foo\"]; // bar\n"
-        + "echo $arr[12];    // 1\n");
-
-    checkPHP(
-      "// This array is the same as ...\n"
-        + "array(5 => 43, 32, 56, \"b\" => 12);\n"
-        + "\n"
+    checkPHP("$arr = array(\"foo\" => \"bar\", 12 => true);\n" + "\n"
+        + "echo $arr[\"foo\"]; // bar\n" + "echo $arr[12];    // 1\n");
+    checkPHP("// This array is the same as ...\n"
+        + "array(5 => 43, 32, 56, \"b\" => 12);\n" + "\n"
         + "// ...this array\n"
         + "array(5 => 43, 6 => 32, 7 => 56, \"b\" => 12);\n");
-
-    checkPHP(
-      "$arr = array(5 => 1, 12 => 2);\n"
-        + "\n"
+    checkPHP("$arr = array(5 => 1, 12 => 2);\n" + "\n"
         + "$arr[] = 56;    // This is the same as $arr[13] = 56;\n"
-        + "                // at this point of the script\n"
-        + "\n"
+        + "                // at this point of the script\n" + "\n"
         + "$arr[\"x\"] = 42; // This adds a new element to\n"
         + "                // the array with key \"x\"\n"
         + "                \n"
-        + "unset($arr[5]); // This removes the element from the array\n"
-        + "\n"
+        + "unset($arr[5]); // This removes the element from the array\n" + "\n"
         + "unset($arr);    // This deletes the whole array\n");
-
     checkPHP("$foo[bar] = 'enemy';\n" + "echo $foo[bar];");
-
-    checkPHP(
-      "$a = array( 'color' => 'red',\n"
+    checkPHP("$a = array( 'color' => 'red',\n"
         + "            'taste' => 'sweet',\n"
         + "            'shape' => 'round',\n"
         + "            'name'  => 'apple',\n"
@@ -218,19 +172,11 @@ public class PHPManualTestCase extends TestCase {
         + "$b[] = 'c';\n"
         + "// will result in the array array(0 => 'a' , 1 => 'b' , 2 => 'c'),\n"
         + "// or simply array('a', 'b', 'c')\n");
-
-    checkPHP(
-      "foreach ($colors as $key => $color) {\n"
-        + "    // won't work:\n"
-        + "    //$color = strtoupper($color);\n"
-        + "    \n"
-        + "    // works:\n"
-        + "    $colors[$key] = strtoupper($color);\n"
-        + "}\n"
+    checkPHP("foreach ($colors as $key => $color) {\n" + "    // won't work:\n"
+        + "    //$color = strtoupper($color);\n" + "    \n" + "    // works:\n"
+        + "    $colors[$key] = strtoupper($color);\n" + "}\n"
         + "print_r($colors);\n");
-
-    checkPHP(
-      "$fruits = array ( \"fruits\"  => array ( \"a\" => \"orange\",\n"
+    checkPHP("$fruits = array ( \"fruits\"  => array ( \"a\" => \"orange\",\n"
         + "                                       \"b\" => \"banana\",\n"
         + "                                       \"c\" => \"apple\"\n"
         + "                                     ),\n"
@@ -244,79 +190,50 @@ public class PHPManualTestCase extends TestCase {
         + "                  \"holes\"   => array (      \"first\",\n"
         + "                                       5 => \"second\",\n"
         + "                                            \"third\"\n"
-        + "                                     )\n"
-        + "                );\n"
-        + "\n"
-        + "// Some examples to address values in the array above \n"
+        + "                                     )\n" + "                );\n"
+        + "\n" + "// Some examples to address values in the array above \n"
         + "echo $fruits[\"holes\"][5];    // prints \"second\"\n"
         + "echo $fruits[\"fruits\"][\"a\"]; // prints \"orange\"\n"
-        + "unset($fruits[\"holes\"][0]);  // remove \"first\"\n"
-        + "\n"
+        + "unset($fruits[\"holes\"][0]);  // remove \"first\"\n" + "\n"
         + "// Create a new multi-dimensional array\n"
         + "$juices[\"apple\"][\"green\"] = \"good\"; \n");
-
     checkPHP("$arr3 = &$arr1;");
-
-    checkPHP(
-      "class foo\n"
-        + "{\n"
-        + "    function do_foo()\n"
-        + "    {\n"
-        + "        echo \"Doing foo.\"; \n"
-        + "    }\n"
-        + "}\n"
-        + "\n"
-        + "$bar = new foo;\n"
-        + "$bar->do_foo();\n");
-
-    checkPHP(
-      "$obj = (object) 'ciao';\n" + 
-      "echo $obj->scalar;  // outputs 'ciao'");
-
+    checkPHP("class foo\n" + "{\n" + "    function do_foo()\n" + "    {\n"
+        + "        echo \"Doing foo.\"; \n" + "    }\n" + "}\n" + "\n"
+        + "$bar = new foo;\n" + "$bar->do_foo();\n");
+    checkPHP("$obj = (object) 'ciao';\n"
+        + "echo $obj->scalar;  // outputs 'ciao'");
     checkPHP("$var = NULL;");
-
-    checkPHP("$var = \"Bob\";\n" +
-       "$Var = \"Joe\";\n" +
-       "echo \"$var, $Var\";      // outputs \"Bob, Joe\"\n" +
-       "\n" +
-   //  "$4site = 'not yet';     // invalid; starts with a number\n" +
-       "$_4site = 'not yet';    // valid; starts with an underscore\n" +
-       "$täyte = 'mansikka';  \n");
-
-    checkPHP("");
+    checkPHP("$var = \"Bob\";\n" + "$Var = \"Joe\";\n"
+        + "echo \"$var, $Var\";      // outputs \"Bob, Joe\"\n" + "\n" +
+        //     "$4site = 'not yet'; // invalid; starts with a number\n" +
+        "$_4site = 'not yet';    // valid; starts with an underscore\n"
+        + "$täyte = 'mansikka';  \n");
 
     checkPHP("");
-
     checkPHP("");
   }
-
   private void checkPHP(String strEval) {
-    try {
-      if (Scanner.DEBUG) {
-        System.out.println("\n------------------------------------");
-        System.out.println(strEval);
-      }
-      parser.phpParserTester(strEval, 1);
-    } catch (CoreException e) {
-    }
-  }
-
-  private void checkHTML(String strEval) {
-    try {
-      if (Scanner.DEBUG) {
-        System.out.println("\n------------------------------------");
-        System.out.println(strEval);
-      }
-      parser.parse(strEval);
-    } catch (CoreException e) {
+    if (Scanner.DEBUG) {
+      System.out.println("\n------------------------------------");
+      System.out.println(strEval);
     }
+    checkParsePHP(
+        strEval.toCharArray(),
+               "");
+//    parser.phpParserTester(strEval, 1);
   }
-
+//  private void checkHTML(String strEval) {
+//    if (Scanner.DEBUG) {
+//      System.out.println("\n------------------------------------");
+//      System.out.println(strEval);
+//    }
+//    parser.parse(strEval);
+//  }
   /**
-   *  The JUnit setup method
+   * The JUnit setup method
    */
-  protected void setUp() {
-    parser = new Parser(null);
-  }
-
+//  protected void setUp() {
+//    parser = new Parser(null);
+//  }
 }