Rewritten Parser/Scanner to package net.sourceforge.phpdt.internal.compiler.parser
[phpeclipse.git] / net.sourceforge.phpeclipse / src / junit / sourceforge / phpeclipse / PHPParserTestCase.java
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 **********************************************************************/
9
10 import net.sourceforge.phpdt.internal.compiler.parser.Parser;
11
12 import org.eclipse.core.runtime.CoreException;
13 import junit.framework.TestCase;
14
15 /**
16  *  Tests the php parser
17  */
18 public class PHPParserTestCase extends TestCase {
19
20         Parser parser;
21
22         public PHPParserTestCase(String name) {
23                 super(name);
24         }
25
26         /**
27          *  Test the PHP Parser with different PHP snippets
28          */
29         public void testPHPParser() {
30
31     checkPHP("foreach ($HTTP_GET_VARS as $secvalue) { }");
32                 checkPHP("\"\\\"\";");
33                 checkPHP("\"\\[addsig]\"");
34                 checkPHP("$v->read();");
35                 checkPHP("$add = 'a'.$i;$val = $$add;");
36                 checkPHP("($a==\"b\") || (c($this->x)==\"d\");");
37                 checkPHP("(substr($this->file, 0, 2) == \"MM\");");
38                 checkPHP("(substr($this->file, 0, 2) == \"MM\") || substr($this->file, 0, 2) == \"II\";");
39                 checkPHP("return (substr($this->file, 0, 2) == \"MM\") || substr($this->file, 0, 2) == \"II\";");
40                 checkPHP("$this->highlightfile->linkscripts{$category}");
41                 checkPHP("$code = call_user_method($this->highlightfile->linkscripts{$category}, $this->highlightfile, $oldword, $this->output_module)");
42                 checkPHP("$this->startmap[$startcurrtag]();");
43                 checkPHP("new $this->startmap[$startcurrtag]();");
44                 checkPHP("$this->highlightfile = new $this->startmap[$startcurrtag]();");
45                 checkPHP("echo \"Test\", \"me\";");
46                 checkPHP("print (\"Test me\");");
47                 checkPHP("$s = <<<HEREDOC \n dskjfhskj\n \n\nHEREDOC;");
48                 checkPHP("$a == 0 ? print \"true\" : print \"false\";");
49                 checkPHP("if(!$result = mysql_query($sql)) return(array());");
50                 checkPHP("class test { function &fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null) \n{ \n } \n }");
51                 checkPHP("call_user_method_array($function_name[1], ${$objectname}, $arguments);");
52                 checkPHP("@$connect_function($dbhost, $user, $pw);");
53                 checkPHP("$conn = @$connect_function($dbhost, $user, $pw);");
54                 checkPHP("global ${$objectname}; ");
55                 checkPHP("class DB_mssql extends DB_common { var $connection; var $phptype, $dbsyntax; }  ");
56                 checkPHP("unset($this->blockvariables[$block][$varname]);");
57                 checkPHP("new IT_Error(\"The block '$block' was not found in the template.\", __FILE__, __LINE__);");
58                 checkPHP("for ($i=156, $j=0; $i<512; $i++, $j++) $v_checksum += ord(substr($v_binary_data_last,$j,1));");
59                 checkPHP("define('MAIL_MIME_CRLF', $crlf, true);");
60                 checkPHP("static $last_run = 0;");
61                 checkPHP("unset($headers['Subject']);");
62                 checkPHP("switch($func) {\n case \"f0\":\n case \"f1\":\n f1();\n break; \n case \"tt\": \n default: \n f0(); \n break;\n }");
63                 checkPHP("function validateAndParseResponse($code, &$arguments) { }");
64                 checkPHP("$options = Console_Getopt::getopt($argv, \"h?v:e:p:d:\");");
65                 checkPHP("$this->container = new $container_class($container_options);");
66                 checkPHP("class Cmd extends PEAR { var $arrSetting     = array(); }");
67                 checkPHP("class Cmd extends PEAR { var $arrSetting     = array(), $i=10; }");
68                 checkPHP("if (isset($test)) { } elseif (isset($lang)) { }");
69                 checkPHP("require_once(\"mainfile.php\");  ");
70                 checkPHP(
71                         "if (eregi(\"footer.php\",$PHP_SELF)) {\n"
72                                 + "Header(\"Location: index.php\");\n"
73                                 + "die();\n"
74                                 + "}\n");
75                 checkPHP(
76                         "while (eregi(\"footer.php\",$PHP_SELF)) {\n"
77                                 + "Header(\"Location: index.php\");\n"
78                                 + "die();\n"
79                                 + "}\n");
80                 checkPHP(
81                         "while (eregi(\"footer.php\",$PHP_SELF)) :\n"
82                                 + "Header(\"Location: index.php\");\n"
83                                 + "die();\n"
84                                 + "endwhile;\n");
85                 checkPHP("$tipath = \"images/topics/\";");
86                 checkPHP("$reasons = array(\"1\", \"2\",\"test\");");
87                 checkPHP("if ($home == 1) { message_box(); blocks(Center);}");
88                 checkPHP("$bresult = sql_query(\"select * from \".$prefix.\"_banner WHERE type='0' AND active='1'\", $dbi);");
89                 checkPHP("switch($func) {\n case \"f1\":\n f1();\n break; \n default: \n f0(); \n break;\n }");
90                 checkPHP("list ($catid) = sql_fetch_row($result, $dbi);");
91                 checkPHP("if (!$name) { \n }");
92                 checkPHP("mt_srand((double)microtime()*1000000);");
93                 checkPHP("$alttext = ereg_replace(\"\\\"\", \"\", $alttext);");
94                 checkPHP("$message .= \"\"._THISISAUTOMATED.\"\\n\\n\";");
95                 checkPHP("if (!empty($pass) AND $pass==$passwd) { }");
96                 checkPHP("$AllowableHTML = array(\"b\"=>1,\n \"i\"=>1);");
97                 checkPHP("if ($term{0}!=$firstChar) {}");
98                 checkPHP(
99                         "echo \"<center><b>\"._NOADMINYET.\"</b></center><br><br>\"\n"
100                                 + ".\"<form action=\\\"admin.php\\\" method=\\\"post\\\">\"\n"
101                                 + ".\"<tr><td><b>\"._NICKNAME.\":</b></td><td><input type=\\\"text\\\" name=\\\"name\\\" size=\\\"30\\\" maxlength=\\\"25\\\"></td></tr>\"\n"
102                                 + ";");
103                 checkPHP("/* \n overLib is from Eric Bosrup (http://www.bosrup.com/web/overlib/) \n */");
104                 checkPHP("if ($arrAtchCookie[1]==0 && $IdAtchPostId!=null){  } ");
105                 checkPHP("$arrAtchCookie[1] -= filesize(realpath($AtchTempDir).\"/\".$xattachlist)/ 1024; ");
106                 checkPHP(
107                         "if (!isset($message)){ \n"
108                                 + "$message = $myrow[post_text];\n"
109                                 + "$message = eregi_replace(\"\\[addsig]\", \"\\n-----------------\\n\" .    $myrow[user_sig], $message); \n"
110                                 + "$message = str_replace(\"<BR>\", \"\\n\", $message); \n"
111                                 + "$message = str_replace(\"<br>\", \"\\n\", $message); \n } ");
112                 checkPHP("do {$array[] = array(\"$myrow[uid]\" => \"$myrow[uname]\"); } while($myrow = mysql_fetch_array($result));");
113                 checkPHP("$ol = new Overlib();");
114                 checkPHP("$risultato = mysql_query($sql) or\n    die(mysql_error());");
115
116     
117                 checkHTML("\n\n\n\n  <?php print \"Hello world\" ?>");
118                 checkHTML("<?php phpinfo(); ?>");
119                 checkHTML("<?php phpinfo()?>");
120                 checkHTML("<?php phpinfo(); ?> foo <?php phpinfo(); ?>");
121                 checkHTML(" <?php //this is a line comment ?>");
122     checkHTML("<?php echo $module_name ?>");
123         }
124
125         private void checkPHP(String strEval) {
126                 try {
127                         if (Parser.DEBUG) {
128                 System.out.println("\n------------------------------------");
129                                 System.out.println(strEval);
130                         }
131                         parser.phpParserTester(strEval, 1);
132                 } catch (CoreException e) {
133                 }
134         }
135
136         private void checkHTML(String strEval) {
137                 try {
138                         if (Parser.DEBUG) {
139                                 System.out.println("\n------------------------------------");
140                                 System.out.println(strEval);
141                         }
142                         parser.parse(strEval);
143                 } catch (CoreException e) {
144                 }
145         }
146
147         /**
148          *  The JUnit setup method
149          */
150         protected void setUp() {
151                 parser = new Parser(null);
152         }
153
154 }