Skip to content

Commit

Permalink
Support the matching of statements in double brace initializers
Browse files Browse the repository at this point in the history
extracted out of anonymous initializer
mockito/mockito@331ff01
src/test/java/org/mockito/internal/matchers/text/ValuePrinterTest.java
src/test/java/org/mockito/internal/stubbing/defaultanswers/ReturnsSmartNullsTest.java
src/test/java/org/mockito/internal/util/PlatformTest.java
  • Loading branch information
tsantalis committed Jan 18, 2025
1 parent 0ef71f2 commit 4ba4e4d
Showing 1 changed file with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -422,12 +422,20 @@ else if(statement1.getString().equals(list2.get(i))) {
}
List<AnonymousClassDeclarationObject> anonymous1 = body1.getAllAnonymousClassDeclarations();
List<AnonymousClassDeclarationObject> nestedAnonymous1 = new ArrayList<AnonymousClassDeclarationObject>();
int doubleBraceInitializations1 = 0;
for(AnonymousClassDeclarationObject anonymous : anonymous1) {
if(anonymous.toString().startsWith("{\n{")) {
doubleBraceInitializations1++;
}
nestedAnonymous1.addAll(anonymous.getAnonymousClassDeclarationsRecursively());
}
List<AnonymousClassDeclarationObject> anonymous2 = body2.getAllAnonymousClassDeclarations();
List<AnonymousClassDeclarationObject> nestedAnonymous2 = new ArrayList<AnonymousClassDeclarationObject>();
int doubleBraceInitializations2 = 0;
for(AnonymousClassDeclarationObject anonymous : anonymous2) {
if(anonymous.toString().startsWith("{\n{")) {
doubleBraceInitializations2++;
}
nestedAnonymous2.addAll(anonymous.getAnonymousClassDeclarationsRecursively());
}
List<LambdaExpressionObject> lambdas1 = body1.getAllLambdas();
Expand Down Expand Up @@ -539,6 +547,20 @@ else if(operation1.hasTestAnnotation() && operation2.hasTestAnnotation() && asse
lambdas2.size() == lambdas1.size() + assertThrows2 || assertThrows1 == 0)) {
handleAssertThrowsLambda(leaves1, leaves2, innerNodes2, lambdas2, operation2);
}
else if(doubleBraceInitializations1 > 0 && doubleBraceInitializations2 == 0) {
for(AbstractCodeFragment leaf1 : new ArrayList<>(leaves1)) {
if(leaf1.getAnonymousClassDeclarations().size() > 0) {
expandAnonymousAndLambdas(leaf1, leaves1, innerNodes1, new LinkedHashSet<>(), new LinkedHashSet<>(), anonymousClassList1(), codeFragmentOperationMap1, operation1, true);
}
}
}
else if(doubleBraceInitializations1 == 0 && doubleBraceInitializations2 > 0) {
for(AbstractCodeFragment leaf2 : new ArrayList<>(leaves2)) {
if(leaf2.getAnonymousClassDeclarations().size() > 0) {
expandAnonymousAndLambdas(leaf2, leaves2, innerNodes2, new LinkedHashSet<>(), new LinkedHashSet<>(), anonymousClassList2(), codeFragmentOperationMap2, operation2, true);
}
}
}
Set<AbstractCodeFragment> streamAPIStatements1 = statementsWithStreamAPICalls(leaves1);
Set<AbstractCodeFragment> streamAPIStatements2 = statementsWithStreamAPICalls(leaves2);
if(streamAPIStatements1.size() == 0 && streamAPIStatements2.size() > 0) {
Expand Down Expand Up @@ -3042,6 +3064,27 @@ private void expandAnonymousAndLambdas(AbstractCodeFragment fragment, List<Abstr
}
}
}
for(UMLInitializer initializer : anonymous.getInitializers()) {
List<AbstractCodeFragment> anonymousClassLeaves = initializer.getBody().getCompositeStatement().getLeaves();
for(AbstractCodeFragment anonymousLeaf : anonymousClassLeaves) {
if(!leaves.contains(anonymousLeaf)) {
leaves.add(anonymousLeaf);
addedLeaves.add(anonymousLeaf);
map.put(anonymousLeaf, initializer);
}
}
List<CompositeStatementObject> anonymousClassInnerNodes = initializer.getBody().getCompositeStatement().getInnerNodes();
for(CompositeStatementObject anonymousInnerNode : anonymousClassInnerNodes) {
if(excludeRootBlock && anonymousInnerNode.equals(initializer.getBody().getCompositeStatement())) {
continue;
}
if(!innerNodes.contains(anonymousInnerNode)) {
innerNodes.add(anonymousInnerNode);
addedInnerNodes.add(anonymousInnerNode);
map.put(anonymousInnerNode, initializer);
}
}
}
}
}
}
Expand Down

0 comments on commit 4ba4e4d

Please sign in to comment.