Skip to content

Commit

Permalink
Big test for patcher
Browse files Browse the repository at this point in the history
  • Loading branch information
kniazkov committed Jun 14, 2024
1 parent fe944df commit 49b80ac
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,23 @@
*/
package org.cqfn.astranaut.core.algorithms.patching;

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.cqfn.astranaut.core.Builder;
import org.cqfn.astranaut.core.DraftNode;
import org.cqfn.astranaut.core.Insertion;
import org.cqfn.astranaut.core.Node;
import org.cqfn.astranaut.core.PatternNode;
import org.cqfn.astranaut.core.algorithms.DifferenceTreeBuilder;
import org.cqfn.astranaut.core.algorithms.PatternBuilder;
import org.cqfn.astranaut.core.example.green.Addition;
import org.cqfn.astranaut.core.example.green.ExpressionStatement;
import org.cqfn.astranaut.core.example.green.IntegerLiteral;
import org.cqfn.astranaut.core.example.green.SimpleAssignment;
import org.cqfn.astranaut.core.example.green.Variable;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -87,4 +96,74 @@ void patchPatternWithDeletion() {
final Node expected = DraftNode.createByDescription("X(Y,A(D),Z)");
Assertions.assertTrue(expected.deepCompare(result));
}

@Test
void patchPatternWithHole() {
Builder ctor = new Variable.Constructor();
ctor.setData("a");
Node first = ctor.createNode();
ctor = new IntegerLiteral.Constructor();
ctor.setData("1");
Node second = ctor.createNode();
ctor = new Addition.Constructor();
ctor.setChildrenList(Arrays.asList(first, second));
Node addition = ctor.createNode();
ctor = new Variable.Constructor();
ctor.setData("x");
Node variable = ctor.createNode();
ctor = new SimpleAssignment.Constructor();
ctor.setChildrenList(Arrays.asList(variable, addition));
Node assignment = ctor.createNode();
ctor = new ExpressionStatement.Constructor();
ctor.setChildrenList(Collections.singletonList(assignment));
final Node stmt = ctor.createNode();
final Patcher patcher = new DefaultPatcher();
final PatternNode pattern = PatcherTest.createPatternWithHole();
final Node patched = patcher.patch(stmt, pattern);
ctor = new Variable.Constructor();
ctor.setData("a");
first = ctor.createNode();
ctor = new IntegerLiteral.Constructor();
ctor.setData("2");
second = ctor.createNode();
ctor = new Addition.Constructor();
ctor.setChildrenList(Arrays.asList(first, second));
addition = ctor.createNode();
ctor = new Variable.Constructor();
ctor.setData("x");
variable = ctor.createNode();
ctor = new SimpleAssignment.Constructor();
ctor.setChildrenList(Arrays.asList(variable, addition));
assignment = ctor.createNode();
ctor = new ExpressionStatement.Constructor();
ctor.setChildrenList(Collections.singletonList(assignment));
final Node expected = ctor.createNode();
Assertions.assertTrue(expected.deepCompare(patched));
}

/**
* Creates pattern with a hole.
* @return A pattern
*/
private static PatternNode createPatternWithHole() {
Builder ctor = new Variable.Constructor();
ctor.setData("w");
final Node first = ctor.createNode();
ctor = new IntegerLiteral.Constructor();
ctor.setData("1");
final Node second = ctor.createNode();
ctor = new Addition.Constructor();
ctor.setChildrenList(Arrays.asList(first, second));
final Node addition = ctor.createNode();
ctor = new IntegerLiteral.Constructor();
ctor.setData("2");
final Node replacement = ctor.createNode();
final DifferenceTreeBuilder dtbld = new DifferenceTreeBuilder(addition);
dtbld.replaceNode(second, replacement);
final PatternBuilder pbld = new PatternBuilder(dtbld.getRoot());
pbld.makeHole(first, 0);
final PatternNode pattern = pbld.getRoot();
Assertions.assertNotNull(pattern);
return pattern;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.cqfn.astranaut.core.Builder;
import org.cqfn.astranaut.core.ChildDescriptor;
import org.cqfn.astranaut.core.ChildrenMapper;
Expand Down Expand Up @@ -159,6 +162,15 @@ private static class TypeImpl implements Type {
)
);

/**
* Properties.
*/
private static final Map<String, String> PROPERTIES = Stream.of(
new String[][] {
{"color", "green"},
{"language", "common"},
}).collect(Collectors.toMap(data -> data[0], data -> data[1]));

@Override
public String getName() {
return TypeImpl.NAME;
Expand All @@ -176,7 +188,7 @@ public List<String> getHierarchy() {

@Override
public String getProperty(final String name) {
return "";
return TypeImpl.PROPERTIES.getOrDefault(name, "");
}

@Override
Expand Down

0 comments on commit 49b80ac

Please sign in to comment.