def test_ternary(self, root_node): """ Another version of isset <?php $f ? $u[1] : $n; """ print_tree(root_node) tern = root_node.match("PHP/STATEMENT/EXPRESSION/OPERATOR3|?") self.assertContainsNode(tern, "EXPRESSION/INDEX/GLOBALVAR|u") self.assertContainsNode(tern, "GLOBALVAR|f")
def test_class_extends(self, root_node): """ A class with an extends keyword <?php class TestClass extends TestBaseClass{ $a; } """ print_tree(root_node) class_node = root_node["PHP"]["CLASS"] self.assertContainsNode(class_node, "EXTENDS")
def test_class_attributes(self, root_node): """ A class with some private attributes <?php class TestClass { private $a = 1; } """ print_tree(root_node) class_node = root_node["PHP"]["CLASS"] self.assertContainsNode(class_node, "BLOCK/STATEMENT/EXPRESSION/ASSIGNMENT/VAR")
def test_blank_lines(self, root_node): """ Php block with a blank line in it <?php echo("Before blank"); echo("After blank"); """ print_tree(root_node) php_node = root_node.get("PHP") self.assertEcho(php_node[0], "Before blank") self.assertEqual("NOOP", php_node[1].kind)
def test_new(self, root_node): """ Test creation of new options <?php $a = new B(); $c = new D("e", "f"); """ print_tree(root_node) statement_1 = root_node["PHP"][0] assignment = statement_1.get("EXPRESSION").get("ASSIGNMENT") new = assignment[0] self.assertEqual(new.kind, "NEW") call = new[0] self.assertEqual(call.kind, "CALL") self.assertEqual(call["CONSTANT"].value, "B")
def test_try_catch(self, root_node): """ Basic try catch <?php try { 1; } catch (Exception $e) { 0; } """ # print_tree(root_node) try_node = root_node.get("PHP")[0] print_tree(try_node) self.assertEqual(try_node.kind, "TRY") self.assertEqual(try_node[0].kind, "BLOCK") self.assertEqual(try_node[1].kind, "CATCH") self.assertContainsNode(try_node, "CATCH/AS/GLOBALVAR|e") self.assertContainsNode(try_node, "CATCH/BLOCK/STATEMENT/EXPRESSION/INT|0")
def test_comments(self, root_node): """Testing comments <?php // Out of band comment $a = 1; //In band comment /* Big groupy comment */ $a = 2; /* groupy comment on line */ $b=3; """ print_tree(root_node) php_node = root_node.get("PHP") comment_node = php_node[0]["COMMENTLINE"] self.assertEqual(comment_node.value, " Out of band comment") comment_node2 = php_node[1]["COMMENTLINE"] self.assertEqual(comment_node2.value, "In band comment") comment_node3 = php_node[2]["COMMENTBLOCK"] self.assertEqual(comment_node3.value, "Big groupy comment") comment_node4 = php_node[2][1] self.assertEqual(comment_node4.value, "") comment_node5 = php_node[4]["COMMENTBLOCK"] self.assertEqual(comment_node5.value, "groupy comment on line")
def test_class_with_method(self, root_node): """ A class with a static classmethod in it <?php class TestClass2 { static public function blah() { return "class,blah"; } private function baz() { return "object,baz"; } } """ print_tree(root_node) class_node = root_node.get("PHP")[0] self.assertEqual(class_node.kind, "CLASS") classmethod_node = class_node.get("BLOCK").get("CLASSMETHOD") self.assertEqual(classmethod_node.value, "blah") self.assertEqual(classmethod_node.get("VISIBILITY").value, "public") method_node = class_node.get("BLOCK").get("METHOD") self.assertEqual(method_node.value, "baz") self.assertEqual(method_node.get("VISIBILITY").value, "private")