*** empty log message ***
[phpeclipse.git] / net.sourceforge.phpeclipse.tests / src / test / PHPParserTestCase2.java
1 package test;
2
3 /**********************************************************************
4  Copyright (c) 2002 Klaus Hartlage - www.eclipseproject.de
5  All rights reserved. This program and the accompanying materials
6  are made available under the terms of the Common Public License v1.0
7  which accompanies this distribution, and is available at
8  http://www.eclipse.org/legal/cpl-v10.html
9  **********************************************************************/
10
11
12 import junit.framework.TestCase;
13
14 import java.io.File;
15 import java.io.FileNotFoundException;
16
17 import net.sourceforge.phpdt.internal.corext.Assert;
18
19
20 /**
21  *  Tests the php parser
22  */
23 public class PHPParserTestCase2 extends TestCase {
24
25   PHPParser parser;
26
27   public PHPParserTestCase2(String name) {
28     super(name);
29   }
30
31   /**
32    *  Test the PHP Parser with different PHP snippets
33    */
34   public void testPHPParser() {
35     checkPHP("$tata;", true);
36     checkPHP("$tata = &new Tutu;", true);
37     checkPHP("$tata = $bobo;", true);
38     checkPHP("function test() {$id = $this->gogo($titi); echo $id;}", true);
39     checkPHP("function test() { $tata = $b; echo $b;}", true);
40     checkPHP("function test() { echo $b;}", true);
41     //checkHTML(new File("class.adm_gestuser.php"));
42     checkHTML("<?php $szOpenImg   = \"/$location[Treeview]/images/Open.gif\"; ?>", true);
43     checkHTML("<?php function f($a,$b) {" +
44               "echo $a; } ?>", true);
45     checkHTML("<?php ec ho 'coucou'; ?>" +
46               "\n dfgdfgfdfg" +
47               "\n" +
48               "\n" +
49               "<?php" +
50               "\necho ' caca';" +
51               "\n?>" +
52               "dfgdfg" +
53               "\ndsfgdf", false);
54     checkHTML("<?php echo 'coucou'; ?>" +
55               "\n dfgdfgfdfg" +
56               "\n" +
57               "\n" +
58               "<?php" +
59               "\necho ' caca';" +
60               "\n?>", true);
61     checkHTML("<html>sdfsdf  <?php phpinfo(); ?>", true);
62     checkHTML("\n\n\n\n  <?php print \"Hello world\"?> ", true);
63     checkHTML("<?php phpinfo()?>", true);
64     checkHTML("<?php phpinfo(); ?> foo <?php phpinfo(); ?>", true);
65     //   checkHTML(" <?php //this is a line comment ?>",true);
66 //todo : fix this    checkHTML("<?php mysql_query(\"CREATE DATABASE $DB_TABLE\" ) or print 'Error creating database<br>'; ?>",true);
67     checkHTML("<?php function func($a) {$v1 = $v23;}; ?>", true);
68     checkHTML("<?php $a = !$b; ?>", true);
69     checkHTML("<?php $a = @@!!@@@!@coucou(2); ?>", true);
70     checkHTML("<?php $aname= $out['Name'][$z] = $remote[$j]['Name']; ?>", true);
71     checkHTML("<?php +$b; ?>", true);
72     checkHTML("<?php $a = -$b; ?>", true);
73
74     checkPHP("$a = array();", true);
75     checkPHP("'caca';", true);
76     checkPHP("if $cac a) echo 'coucou';", false);
77     checkPHP("$oka dd = 'a'.$i;$val = $$add;", false);
78     checkPHP("($a==\"b\") || (c($this->x)==\"d\");", true);
79     checkPHP("(substr($this->file, 0, 2) == \"MM\");", true);
80     checkPHP("(substr($this->file, 0, 2) == \"MM\") || substr($this->file, 0, 2) == \"II\";", true);
81     checkPHP("return (substr($this->file, 0, 2) == \"MM\") || substr($this->file, 0, 2) == \"II\";", true);
82     checkPHP("$this->highlightfile->linkscripts{$category};", true);
83     checkPHP("$code = call_user_method($this->highlightfile->linkscripts{$category}, $this->highlightfile, $oldword, $this->output_module);", true);
84     checkPHP("$this->startmap[$startcurrtag]();", true);
85     checkPHP("new $this->startmap[$startcurrtag]();", true);
86     checkPHP("$this->highlightfile = new $this->startmap[$startcurrtag]();", true);
87     checkPHP("echo \"Test\", \"me\";", true);
88     checkPHP("print (\"Test me\");", true);
89 //    checkPHP("$s = <<<HEREDOC \n dskjfhskj\n \n\nHEREDOC;");
90 //    checkPHP("$a == 0 ? print \"true\" : print \"false\";");
91     checkPHP("if(!$result = mysql_query($sql)) return(array());", true);
92     checkPHP("class test { " +
93              "  var $t;" +
94              "  var $tutu,$toto;" +
95              "  var $a = 2;" +
96              "function &fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null) {  }" +
97              "}", true);
98     checkPHP("call_user_method_array($function_name[1], ${$objectname}, $arguments);", true);
99     checkPHP("@$connect_function($dbhost, $user, $pw);", true);
100     checkPHP("$conn = @$connect_function($dbhost, $user, $pw);", true);
101     checkPHP("global ${$objectname}; ", true);
102     checkPHP("class DB_mssql extends DB_common { var $connection; var $phptype, $dbsyntax; }  ", true);
103     checkPHP("unset($this->blockvariables[$block][$varname]);", true);
104     checkPHP("new IT_Error(\"The block '$block' was not found in the template.\", __FILE__, __LINE__);", true);
105     checkPHP("for ($i=156, $j=0; $i<512; $i++, $j++) $v_checksum += ord(substr($v_binary_data_last,$j,1));", true);
106     checkPHP("define('MAIL_MIME_CRLF', $crlf, true);", false);
107     checkPHP("static $last_run = 0;", true);
108     checkPHP("unset($headers['Subject']);", true);
109     checkPHP("switch($func) {\n case \"f0\":\n case \"f1\":\n f1();\n break; \n case \"tt\": \n default: \n f0(); \n break;\n }", true);
110     checkPHP("function validateAndParseResponse($code, &$arguments) { }", true);
111     checkPHP("$options = Console_Getopt::getopt($argv, \"h?v:e:p:d:\");", true);
112     checkPHP("$this->container = new $container_class($container_options);", true);
113     checkPHP("class Cmd extends PEAR { var $arrSetting     = array(); }", true);
114     checkPHP("class Cmd extends PEAR { var $arrSetting     = array(), $i=10; }", true);
115     checkPHP("if (isset($test)) { } elseif (isset($lang)) { }", true);
116     checkPHP("require_once(\"mainfile.php\");  ", true);
117     checkPHP("if (eregi(\"footer.php\",$PHP_SELF)) {\n" + "Header(\"Location: index.php\");\n" + "die();\n" + "}\n", true);
118     checkPHP("while (eregi(\"footer.php\",$PHP_SELF)) {\n" + "Header(\"Location: index.php\");\n" + "die();\n" + "}\n", true);
119     checkPHP("while (eregi(\"footer.php\",$PHP_SELF)) :\n" + "Header(\"Location: index.php\");\n" + "die();\n" + "endwhile;\n", true);
120     checkPHP("$tipath = \"images/topics/\";", true);
121     checkPHP("$reasons = array(\"1\", \"2\",\"test\");", true);
122     checkPHP("if ($home == 1) { message_box(); blocks(Center);}", true);
123     checkPHP("$bresult = sql_query(\"select * from \".$functionName.\"_banner WHERE type='0' AND active='1'\", $dbi);", true);
124     checkPHP("switch($func) {\n case \"f1\":\n f1();\n break; \n default: \n f0(); \n break;\n }", true);
125     checkPHP("list ($catid) = sql_fetch_row($result, $dbi);", true);
126     checkPHP("if (!$name) { \n }", true);
127     checkPHP("mt_srand((double)microtime()*1000000);", true);
128     checkPHP("\"\\\"\";", true);
129     checkPHP("$v->read();", true);
130     checkPHP("$alttext = ereg_replace(\"\\\"\", \"\", $alttext);", true);
131     checkPHP("$message .= \"\"._THISISAUTOMATED.\"\\n\\n\";", true);
132     checkPHP("if (!empty($pass) AND $pass==$passwd) { }", true);
133     checkPHP("$AllowableHTML = array(\"b\"=>1,\n \"i\"=>1);", true);
134     checkPHP("if ($term{0}!=$firstChar) {}", true);
135     checkPHP(
136         "echo \"<center><b>\"._NOADMINYET.\"</b></center><br><br>\"\n"
137         + ".\"<form action=\\\"admin.php\\\" method=\\\"post\\\">\"\n"
138         + ".\"<tr><td><b>\"._NICKNAME.\":</b></td><td><input type=\\\"text\\\" name=\\\"name\\\" size=\\\"30\\\" maxlength=\\\"25\\\"></td></tr>\"\n"
139         + ";", true);
140     checkPHP("/* \n overLib is from Eric Bosrup (http://www.bosrup.com/web/overlib/) \n */;", true);
141     checkPHP("if ($arrAtchCookie[1]==0 && $IdAtchPostId!=null){  } ", true);
142     checkPHP("$arrAtchCookie[1] -= filesize(realpath($AtchTempDir).\"/\".$xattachlist)/ 1024; ", true);
143     checkPHP(
144         "if (!isset($message)){ \n"
145         + "$message = $myrow[post_text];\n"
146         + "$message = eregi_replace(\"\\[addsig]\", \"\\n-----------------\\n\" .    $myrow[user_sig], $message); \n"
147         + "$message = str_replace(\"<BR>\", \"\\n\", $message); \n"
148         + "$message = str_replace(\"<br>\", \"\\n\", $message); \n } ", true);
149     checkPHP("do {$array[] = array(\"$myrow[uid]\" => \"$myrow[uname]\"); } while($myrow = mysql_fetch_array($result));", true);
150     checkPHP("$ol = new Overlib();", true);
151     checkPHP("$risultato = mysql_query($sql) or\n    die(mysql_error());", true);
152   }
153
154   private void checkPHP(String strEval, boolean good) {
155     ParseException ex = null;
156     try {
157       System.out.println("strEval = " + strEval);
158       PHPParser.phpParserTester(strEval);
159     } catch (ParseException e) {
160       ex = e;
161     }
162     if (good) {
163       try {
164         Assert.isTrue(ex == null);
165       } catch (RuntimeException e) {
166         ex.printStackTrace();
167         throw e;
168       }
169     } else {
170       Assert.isNotNull(ex);
171     }
172   }
173
174   private void checkHTML(String strEval, boolean good) {
175     ParseException ex = null;
176     try {
177       System.out.println("strEval = " + strEval);
178       PHPParser.htmlParserTester(strEval);
179     } catch (ParseException e) {
180       ex = e;
181     }
182     if (good) {
183       try {
184         Assert.isTrue(ex == null);
185       } catch (RuntimeException e) {
186         ex.printStackTrace();
187         throw e;
188       }
189     } else {
190       Assert.isNotNull(ex);
191     }
192   }
193
194   private void checkHTML(File strEval, boolean good) {
195     ParseException ex = null;
196     try {
197       System.out.println("strEval = " + strEval.toString());
198       PHPParser.htmlParserTester(strEval);
199     } catch (FileNotFoundException e) {
200       e.printStackTrace();
201     } catch (ParseException e) {
202       ex = e;
203     }
204     if (good) {
205       try {
206         Assert.isTrue(ex == null);
207       } catch (RuntimeException e) {
208         ex.printStackTrace();
209         throw e;
210       }
211     } else {
212       Assert.isNotNull(ex);
213     }
214   }
215
216   /**
217    *  The JUnit setup method
218    */
219   protected void setUp() {
220     parser = (test.PHPParser) PHPParserManager.getParser();
221   }
222
223 }