Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kniazkov committed Aug 23, 2024
1 parent 5d9b4db commit 849c0c3
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 25 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ SOFTWARE.
</parent>
<groupId>org.cqfn</groupId>
<artifactId>astranaut-core</artifactId>
<version>1.0-SNAPSHOT</version>
<version>2.0.0</version>
<packaging>jar</packaging>
<developers>
<developer>
Expand Down Expand Up @@ -134,7 +134,7 @@ SOFTWARE.
<dependency>
<groupId>com.kniazkov</groupId>
<artifactId>json</artifactId>
<version>1.0</version>
<version>1.1</version>
</dependency>
<dependency>
<groupId>guru.nidi</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,20 @@ public class LabeledTreeBuilder {
*/
private final Node root;

/**
* Constructor.
* @param root Root node of the original not labeled tree
*/
public LabeledTreeBuilder(final Node root) {
this.root = root;
}

/**
* Constructor.
* @param tree Original not labeled tree
*/
public LabeledTreeBuilder(final Tree tree) {
this.root = tree.getRoot();
this(tree.getRoot());
}

/**
Expand Down Expand Up @@ -126,7 +134,7 @@ public Node getChild(final int index) {

@Override
public String toString() {
return Node.toString(this);
return this.original.toString();
}

@Override
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/org/cqfn/astranaut/core/base/DummyNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ public final class DummyNode implements Node {
private DummyNode() {
}

@Override
public Fragment getFragment() {
return EmptyFragment.INSTANCE;
}

@Override
public Type getType() {
return DummyNode.TYPE;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cqfn/astranaut/core/base/Replace.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public boolean setChildrenList(final List<Node> list) {

@Override
public boolean isValid() {
return this.before != null && this.after != null;
return this.before != null;
}

@Override
Expand Down
1 change: 1 addition & 0 deletions src/test/java/org/cqfn/astranaut/core/base/ActionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ void testReplaceAction() {
Assertions.assertEquals(DummyNode.INSTANCE, created);
Assertions.assertTrue(builder.setChildrenList(Arrays.asList(before, after)));
Assertions.assertFalse(builder.setChildrenList(Collections.singletonList(before)));
Assertions.assertTrue(builder.isValid());
created = builder.createNode();
Assertions.assertEquals(ActionTest.REPLACE_TYPE, created.getTypeName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import org.cqfn.astranaut.core.algorithms.LabeledTreeBuilder;
import org.cqfn.astranaut.core.example.green.Addition;
import org.cqfn.astranaut.core.example.green.IntegerLiteral;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -281,7 +282,14 @@ void testDeepCompare() {
final Node node = DraftNode.create(description);
Assertions.assertTrue(node.deepCompare(node));
Assertions.assertTrue(node.deepCompare(DraftNode.create(description)));
Assertions.assertFalse(node.deepCompare(DraftNode.create("X(Q,V,E,R<\"data\",T(Y))")));
Assertions.assertFalse(node.deepCompare(DraftNode.create("X(Q,W,E,R<\"test\",T(Y))")));
Assertions.assertFalse(node.deepCompare(DraftNode.create("X(Q,W,E,R,T(Y))")));
final Node labeled = new LabeledTreeBuilder(node)
.build(Collections.singleton(node), "color", "red")
.getRoot();
Assertions.assertEquals(labeled.toString(), node.toString());
Assertions.assertFalse(node.deepCompare(labeled));
}

@Test
Expand Down
26 changes: 14 additions & 12 deletions src/test/java/org/cqfn/astranaut/core/base/PatternNodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package org.cqfn.astranaut.core.base;

import java.util.Collections;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand All @@ -33,28 +34,29 @@
class PatternNodeTest {
@Test
void testBaseInterface() {
final Source source = new TestSource();
final Source source = (start, end) -> null;
final Position begin = new DefaultPosition(source, 1, 1);
final Position end = new DefaultPosition(source, 1, 100);
final Fragment fragment = new DefaultFragment(begin, end);
final Node child = DraftNode.create("A");
final Node alien = DraftNode.create("B");
final DraftNode.Constructor ctor = new DraftNode.Constructor();
ctor.setFragment(fragment);
ctor.setName("X");
ctor.setData("test");
ctor.setChildrenList(Collections.singletonList(child));
final Node node = ctor.createNode();
final PatternNode pattern = new PatternNode(new DiffNode(node));
Assertions.assertEquals(100, pattern.getFragment().getEnd().getColumn());
Assertions.assertEquals("X<\"test\">", pattern.toString());
}

/**
* Source implementation for test purposes.
* @since 1.1.5
*/
private static final class TestSource implements Source {
@Override
public String getFragmentAsString(final Position start, final Position end) {
return "";
Assertions.assertEquals("X<\"test\">(A)", pattern.toString());
boolean oops = false;
try {
pattern.getType().createBuilder();
} catch (final UnsupportedOperationException ignored) {
oops = true;
}
Assertions.assertTrue(oops);
Assertions.assertFalse(pattern.makeHole(alien, 0));
Assertions.assertTrue(pattern.makeHole(child, 0));
}
}
6 changes: 4 additions & 2 deletions src/test/java/org/cqfn/astranaut/core/base/PositionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ void testBaseInterface() {
final Position first = new DefaultPosition(source, 1, 1);
final Position second = new DefaultPosition(source, 1, 2);
Assertions.assertTrue(second.compareTo(first) > 0);
final Position third = new DefaultPosition(another, 1, 1);
final Position third = new DefaultPosition(source, 2, 1);
Assertions.assertTrue(third.compareTo(first) > 0);
final Position alien = new DefaultPosition(another, 1, 1);
boolean oops = false;
try {
first.compareTo(third);
first.compareTo(alien);
} catch (final IllegalArgumentException ignored) {
oops = true;
}
Expand Down
21 changes: 20 additions & 1 deletion src/test/java/org/cqfn/astranaut/core/base/TreeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@
*/
package org.cqfn.astranaut.core.base;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import org.cqfn.astranaut.core.utils.MapUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* This test suite tests the {@link Tree} class.
*
* @since 2.0.0
*/
class TreeTest {
Expand All @@ -50,6 +53,22 @@ void testBaseInterface() {
Assertions.assertEquals(language, tree.getLanguage());
}

@Test
void testCreatingDraft() {
final String description = "A(B, C, D)";
final Set<String> expected = new TreeSet<>(Arrays.asList("A", "B", "C", "D"));
final Tree first = Tree.createDraft(description);
Assertions.assertEquals(description, first.toString());
final Map<String, Set<Node>> nodes = new TreeMap<>();
final Tree second = Tree.createDraft(description, nodes);
Assertions.assertEquals(description, second.toString());
for (final Map.Entry<String, Set<Node>> entry : nodes.entrySet()) {
Assertions.assertTrue(expected.remove(entry.getKey()));
Assertions.assertEquals(1, entry.getValue().size());
}
Assertions.assertTrue(expected.isEmpty());
}

/**
* Node implementation for testing purposes.
*
Expand Down

0 comments on commit 849c0c3

Please sign in to comment.