From 4ba4e4d527e1767c9d694db2764d24611a55c583 Mon Sep 17 00:00:00 2001 From: tsantalis Date: Sat, 18 Jan 2025 06:04:48 -0500 Subject: [PATCH] Support the matching of statements in double brace initializers extracted out of anonymous initializer https://github.com/mockito/mockito/commit/331ff01c5f840ff8a3c5b0859185ba78f9446cf0 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 --- .../decomposition/UMLOperationBodyMapper.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/main/java/gr/uom/java/xmi/decomposition/UMLOperationBodyMapper.java b/src/main/java/gr/uom/java/xmi/decomposition/UMLOperationBodyMapper.java index 8c1c7eb3a..a434d35c7 100644 --- a/src/main/java/gr/uom/java/xmi/decomposition/UMLOperationBodyMapper.java +++ b/src/main/java/gr/uom/java/xmi/decomposition/UMLOperationBodyMapper.java @@ -422,12 +422,20 @@ else if(statement1.getString().equals(list2.get(i))) { } List anonymous1 = body1.getAllAnonymousClassDeclarations(); List nestedAnonymous1 = new ArrayList(); + int doubleBraceInitializations1 = 0; for(AnonymousClassDeclarationObject anonymous : anonymous1) { + if(anonymous.toString().startsWith("{\n{")) { + doubleBraceInitializations1++; + } nestedAnonymous1.addAll(anonymous.getAnonymousClassDeclarationsRecursively()); } List anonymous2 = body2.getAllAnonymousClassDeclarations(); List nestedAnonymous2 = new ArrayList(); + int doubleBraceInitializations2 = 0; for(AnonymousClassDeclarationObject anonymous : anonymous2) { + if(anonymous.toString().startsWith("{\n{")) { + doubleBraceInitializations2++; + } nestedAnonymous2.addAll(anonymous.getAnonymousClassDeclarationsRecursively()); } List lambdas1 = body1.getAllLambdas(); @@ -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 streamAPIStatements1 = statementsWithStreamAPICalls(leaves1); Set streamAPIStatements2 = statementsWithStreamAPICalls(leaves2); if(streamAPIStatements1.size() == 0 && streamAPIStatements2.size() > 0) { @@ -3042,6 +3064,27 @@ private void expandAnonymousAndLambdas(AbstractCodeFragment fragment, List anonymousClassLeaves = initializer.getBody().getCompositeStatement().getLeaves(); + for(AbstractCodeFragment anonymousLeaf : anonymousClassLeaves) { + if(!leaves.contains(anonymousLeaf)) { + leaves.add(anonymousLeaf); + addedLeaves.add(anonymousLeaf); + map.put(anonymousLeaf, initializer); + } + } + List 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); + } + } + } } } }