package cn.edu.seu.ise.dca.app.php.parser.test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.regex.Pattern;

import org.junit.Test;

import cn.edu.seu.ise.dca.app.php.parser.PHPPattern;


public class PHPPatternTest {
	
	@Test
	public void testClassPattern() {
		Pattern pattern = Pattern.compile(PHPPattern.CLASS_DEF);
		assertTrue(pattern.matcher("class hello").find());
		assertTrue(pattern.matcher("class _hello {").find());
		assertTrue(pattern.matcher("class hello{").find());
		assertTrue(pattern.matcher("class hello {").find());
		assertTrue(pattern.matcher("abstract class hello {").find());
		assertTrue(pattern.matcher("class hello extends hello {").find());
		assertTrue(pattern.matcher("class hello extends hello implements h {").find());
		
		
		assertFalse(pattern.matcher("class hello extends hello {}").find());
		assertFalse(pattern.matcher("class hello { aaa; }").find());
		assertFalse(pattern.matcher("bstract class 0hello {").find());
		assertFalse(pattern.matcher("class 0hello {").find());
		assertFalse(pattern.matcher("function hello {").find());
		assertFalse(pattern.matcher("class hello {}").find());
		assertFalse(pattern.matcher("SELECT d.*, dc.download_category_class FROM #download as d").find());
		assertFalse(pattern.matcher("SELECT d.*, dc.download_category_class FROM ").find());
	}
	
	@Test
	public void testClassSingleLinePattern() {
		Pattern pattern = Pattern.compile(PHPPattern.CLASS_DEF_SINGLELINE);
		assertTrue(pattern.matcher("class hello extends hello implements h {}").find());
		assertTrue(pattern.matcher("abstract class hello  {}").find());
		assertFalse(pattern.matcher("abstract class hello  {").find());
	}
	
	@Test
	public void testFunctionPattern() {
		Pattern pattern = Pattern.compile(PHPPattern.FUNCTION_DEF);
		assertTrue(pattern.matcher("function hello()").find());
		assertTrue(pattern.matcher("function hello ()").find());
		assertTrue(pattern.matcher("function hello($var)").find());
		assertTrue(pattern.matcher("function hello($var, $ss)").find());
		assertTrue(pattern.matcher("function hello($var, $ss){").find());
		assertTrue(pattern.matcher("function hello($var, $ss) {").find());
		assertFalse(pattern.matcher("function hello").find());
		assertFalse(pattern.matcher("function hello {").find());
		assertTrue(pattern.matcher("public function __construct($address, $name=null)").find());
		
		
		//test cases from wikipedia
		assertTrue(pattern.matcher("function loadContent( $noredir = false )").find());
		assertTrue(pattern.matcher("function wfGetDB( $altuser = \"\", $altpassword = \"\" )").find());
		assertTrue(pattern.matcher("function _block($xbeg, $xlen, $ybeg, $ylen, &$edits) {").find());
		
		assertTrue(pattern.matcher("function GetWidth($aImg,$aUseTabs=false,$aTabExtraMargin=1.1) {").find());
		
		
		
	}
	
	@Test
	public void testFunctionSingleLinePattern() {
		Pattern pattern = Pattern.compile(PHPPattern.FUNCTION_DEF_SINGLELINE);
		assertFalse(pattern.matcher("function hello").find());
		assertFalse(pattern.matcher("function hello{").find());
		assertFalse(pattern.matcher("function hello {").find());
		assertFalse(pattern.matcher("function hello{}").find());
		assertTrue(pattern.matcher("function hello(){ aaa;}").find());
		assertTrue(pattern.matcher("abstract function hello (){}").find());
		assertTrue(pattern.matcher("static function hello (){}").find());
		assertTrue(pattern.matcher("abstract static function hello (){}").find());
		assertTrue(pattern.matcher("function getID() { global $wgTitle; return $wgTitle->getArticleID(); }").find());
	}
	
	@Test
	public void testAbstractFunction() {
		Pattern pattern = Pattern.compile(PHPPattern.ABSTRACT_FUCNTION_DEF);
		assertTrue(pattern.matcher("function hello();").find());
		assertTrue(pattern.matcher("function hello() ;").find());
		assertTrue(pattern.matcher("public function hello() ;").find());
		assertTrue(pattern.matcher("private function hello() ;").find());
		assertTrue(pattern.matcher("private function hello($var) ;").find());
		assertTrue(pattern.matcher("private abstract function hello() ;").find());
		assertTrue(pattern.matcher("private abstract static function hello() ;").find());
	}
}
