Skip to content

Commit

Permalink
Testing on syntax tree obtained from real code using JavaParser
Browse files Browse the repository at this point in the history
  • Loading branch information
kniazkov committed Nov 27, 2024
1 parent 1cae215 commit b7e05c8
Show file tree
Hide file tree
Showing 5 changed files with 32,900 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public DiffTreeBuilder(final Tree before) {

/**
* Builds a difference tree based on the original tree and the tree after changes.
* @param after Root node of tree before the changes
* @param after Root node of tree after the changes
* @param mapper A mapper used for node mappings
* @return Result of operation, {@code true} if difference tree was built
*/
Expand All @@ -95,6 +95,16 @@ public boolean build(final Node after, final Mapper mapper) {
return result;
}

/**
* Builds a difference tree based on the original tree and the tree after changes.
* @param after Syntax tree after the changes
* @param mapper A mapper used for node mappings
* @return Result of operation, {@code true} if difference tree was built
*/
public boolean build(final Tree after, final Mapper mapper) {
return this.build(after.getRoot(), mapper);
}

/**
* Adds an action to the difference tree that inserts a node after another node.
* If no other node is specified, inserts at the beginning of the children's list.
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/cqfn/astranaut/core/base/DiffNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ public boolean insertNodeAfter(final Node node, final Node after) {
final ListIterator<DiffTreeItem> iterator = this.children.listIterator();
while (iterator.hasNext()) {
final Node child = iterator.next();
if (child instanceof DiffNode
&& ((DiffNode) child).getPrototype() == after) {
if (child instanceof DiffNode && ((DiffNode) child).getPrototype() == after
|| child instanceof Insert && ((Insert) child).getAfter() == after) {
iterator.add(new Insert(node));
result = true;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,15 @@
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.cqfn.astranaut.core.algorithms.DiffTreeBuilder;
import org.cqfn.astranaut.core.base.DefaultFactory;
import org.cqfn.astranaut.core.base.DiffTree;
import org.cqfn.astranaut.core.base.DraftNode;
import org.cqfn.astranaut.core.base.Insertion;
import org.cqfn.astranaut.core.base.Node;
import org.cqfn.astranaut.core.base.Tree;
import org.cqfn.astranaut.core.utils.FilesReader;
import org.cqfn.astranaut.core.utils.JsonDeserializer;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand All @@ -41,6 +47,11 @@
*/
@SuppressWarnings("PMD.TooManyMethods")
class TopDownMapperTest {
/**
* The folder with test resources.
*/
private static final String TESTS_PATH = "src/test/resources/heavy/";

@Test
void testIdenticalTrees() {
final String description = "A(B(C, D))";
Expand Down Expand Up @@ -426,4 +437,46 @@ void thirdTestComplexChanges() {
final Map<Node, Node> replaced = mapping.getReplaced();
Assertions.assertEquals(1, replaced.size());
}

@Test
void firstTestOnRealSyntaxTree() {
final Tree first = this.readSyntaxTreeFormFile("real_tree_from_java_parser_1.json");
final Tree second = this.readSyntaxTreeFormFile("real_tree_from_java_parser_2.json");
final Mapper mapper = TopDownMapper.INSTANCE;
final DiffTreeBuilder builder = new DiffTreeBuilder(first);
final boolean building = builder.build(second, mapper);
Assertions.assertTrue(building);
final DiffTree diff = builder.getDiffTree();
final Tree before = diff.getBefore();
Assertions.assertTrue(before.deepCompare(first));
final Tree after = diff.getAfter();
Assertions.assertTrue(after.deepCompare(second));
}

/**
* Returns content of the specified file.
* @param name The name of the file
* @return The file content
*/
private String getFileContent(final String name) {
final String file = TopDownMapperTest.TESTS_PATH.concat(name);
final String source = new FilesReader(file).readAsStringNoExcept();
Assertions.assertFalse(source.isEmpty());
return source;
}

/**
* Reads syntax tree from a JSON file.
* @param name The name of the file
* @return Syntax tree
*/
private Tree readSyntaxTreeFormFile(final String name) {
final JsonDeserializer deserializer = new JsonDeserializer(
this.getFileContent(name),
language -> DefaultFactory.EMPTY
);
final Tree tree = deserializer.convert();
Assertions.assertEquals("CompilationUnit", tree.getRoot().getTypeName());
return tree;
}
}
Loading

0 comments on commit b7e05c8

Please sign in to comment.