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;
import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
import org.eclipse.core.runtime.CoreException;
import junit.framework.TestCase;
/**
* Tests the php parser
*/
public class PHPManualTestCase extends TestCase {
Parser parser;
public PHPManualTestCase(String name) {
super(name);
}
/**
* Test the PHP Parser with different PHP snippets
*/
public void testPHPParser() {
checkPHP("function foo()" +
"{" +
" echo \"In foo()
\\n\";" +
"}" +
"" +
"function bar($arg = '')" +
"{" +
" echo \"In bar(); argument was '$arg'.
\\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"
+ " /* 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"
+ ""
+ "// If this is an integer, increment it by four\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");
checkPHP("$foo = True; // assign the value TRUE to $foo");
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 \"