Skip to content

Commit

Permalink
PHP8.3 - ASTMatcher tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zulus committed Nov 13, 2023
1 parent cafc3c9 commit e71a6c7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ public boolean match(ConstantDeclaration node, Object other) {

return (safeSubtreeListMatch(node.initializers(), o.initializers())
&& safeSubtreeMatch((Identifier) node.getConstantType(), (Identifier) o.getConstantType())
&& safeSubtreeListMatch(node.names(), o.names()));
&& safeEquals(node.getModifier(), o.getModifier()) && safeSubtreeListMatch(node.names(), o.names()));
}

public boolean match(ClassDeclaration node, Object other) {
Expand All @@ -377,7 +377,7 @@ public boolean match(ClassDeclaration node, Object other) {

return (safeEquals(node.getModifier(), o.getModifier())
&& safeSubtreeMatch(node.getSuperClass(), o.getSuperClass())
&& safeSubtreeMatch((TypeDeclaration) node, (TypeDeclaration) o));
&& match((TypeDeclaration) node, (TypeDeclaration) o));
}

private boolean match(TypeDeclaration node, Object other) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class ConstantDeclaration extends BodyDeclaration {
ConstantDeclaration.class, "initializers", Expression.class, //$NON-NLS-1$
CYCLE_RISK);
public static final ChildPropertyDescriptor CONSTANT_TYPE_PROPERTY = new ChildPropertyDescriptor(
FormalParameter.class, "constantType", Expression.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
ConstantDeclaration.class, "constantType", Expression.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$

public static final SimplePropertyDescriptor MODIFIER_PROPERTY = new SimplePropertyDescriptor(
ConstantDeclaration.class, "modifier", Integer.class, OPTIONAL); //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ public class ASTMatcherTests {
public static TestWatcher watcher = new TestSuiteWatcher();

public void performMatching(String matchingStr, String notMatchingStr) throws Exception {
performMatching(matchingStr, notMatchingStr, PHPVersion.PHP8_3);
}

public void performMatching(String matchingStr, String notMatchingStr, PHPVersion phpVersion) throws Exception {

ASTNode node = getAstNode(matchingStr);
ASTNode notMatchingNode = getAstNode(notMatchingStr);
ASTNode node = getAstNode(matchingStr, phpVersion);
ASTNode notMatchingNode = getAstNode(notMatchingStr, phpVersion);

assertTrue(node.subtreeMatch(new PHPASTMatcher(), node));
assertFalse(node.subtreeMatch(new PHPASTMatcher(), notMatchingNode));
Expand All @@ -58,11 +62,10 @@ public void performMatching(String matchingStr, String notMatchingStr) throws Ex
assertFalse(node.subtreeMatch(new PHPASTMatcher(), null));
}

private ASTNode getAstNode(String str) throws Exception {
private ASTNode getAstNode(String str, PHPVersion phpVersion) throws Exception {
StringReader reader = new StringReader(str);
Program program = ASTParser.newParser(reader, PHPVersion.PHP5,
ProjectOptions.isSupportingASPTags((IProject) null), ProjectOptions.useShortTags((IProject) null))
.createAST(new NullProgressMonitor());
Program program = ASTParser.newParser(reader, phpVersion, ProjectOptions.isSupportingASPTags((IProject) null),
ProjectOptions.useShortTags((IProject) null)).createAST(new NullProgressMonitor());
List<Statement> statements = program.statements();

assertNotNull(statements);
Expand Down Expand Up @@ -363,7 +366,7 @@ public void matchRefernceWithFunction() throws Exception {
public void matchRefernceInstanciation() throws Exception {
String matchingStr = "<?php $b = &new MyClass();?>";
String notMatchingStr = "<?php $b = &new MyClass2();?>";
performMatching(matchingStr, notMatchingStr);
performMatching(matchingStr, notMatchingStr, PHPVersion.PHP5_6);
}

@Test
Expand Down Expand Up @@ -468,7 +471,7 @@ public void matchMinusOperation() throws Exception {
public void matchAndOperation() throws Exception {
String matchingStr = "<?php foo() & $a->bar();?>";
String notMatchingStr = "<?php foo() & $b->bar();?>";
performMatching(matchingStr, notMatchingStr);
performMatching(matchingStr, notMatchingStr, PHPVersion.PHP5_6);
}

@Test
Expand Down Expand Up @@ -812,4 +815,25 @@ public void matchEval() throws Exception {
performMatching(matchingStr, notMatchingStr);
}

@Test
public void matchConstatantVisibility() throws Exception {
String matchingStr = "<?php class MyClass { private const STATUS = '1'; } ?> ";
String notMatchingStr = "<?php class MyClass { public const STATUS = '1'; } ?> ";
performMatching(matchingStr, notMatchingStr);
}

@Test
public void matchConstatantType() throws Exception {
String matchingStr = "<?php class MyClass { private const string STATUS = '1'; } ?> ";
String notMatchingStr = "<?php class MyClass { private const STATUS = '1'; } ?> ";
performMatching(matchingStr, notMatchingStr);
}

@Test
public void matchConstantAccess() throws Exception {
String matchingStr = "<?php echo MyClass::{$var} ?> ";
String notMatchingStr = "<?php echo MyClass::{$var2} ?> ";
performMatching(matchingStr, notMatchingStr);
}

}

0 comments on commit e71a6c7

Please sign in to comment.