From 6bebe7e60078fe738aec839b5e1e91a006bd02f6 Mon Sep 17 00:00:00 2001 From: Ivan Kniazkov Date: Wed, 4 Sep 2024 12:41:33 +0300 Subject: [PATCH] More tests --- .../core/algorithms/conversion/Adapter.java | 18 +- .../cqfn/astranaut/core/base/DraftNode.java | 17 ++ .../algorithms/conversion/AdapterTest.java | 162 ++++++++++-------- 3 files changed, 111 insertions(+), 86 deletions(-) diff --git a/src/main/java/org/cqfn/astranaut/core/algorithms/conversion/Adapter.java b/src/main/java/org/cqfn/astranaut/core/algorithms/conversion/Adapter.java index a5ed013..8dfed57 100644 --- a/src/main/java/org/cqfn/astranaut/core/algorithms/conversion/Adapter.java +++ b/src/main/java/org/cqfn/astranaut/core/algorithms/conversion/Adapter.java @@ -32,7 +32,6 @@ /** * Tree converter built on a set of rules described in DSL. - * * @since 1.0 */ public class Adapter { @@ -62,10 +61,10 @@ public Adapter(final List converters, final Factory factory) { * @return A converted tree or empty tree if the conversion is impossible */ public Node convert(final Node root) { - final MutableNode convertible = new MutableNode(root); - Node result = convertible; + final MutableNode mutable = new MutableNode(root); + Node result = mutable; final List nodes = new ArrayList<>(0); - NodeListBuilder.buildNodeList(convertible, nodes); + NodeListBuilder.buildNodeList(mutable, nodes); for (final MutableNode original : nodes) { boolean converted = false; for (final Converter converter : this.converters) { @@ -92,10 +91,10 @@ public Node convert(final Node root) { */ public Node partialConvert(final int variant, final Node root) { int conversions = 0; - final MutableNode convertible = new MutableNode(root); - Node result = convertible; + final MutableNode mutable = new MutableNode(root); + Node result = mutable; final List nodes = new ArrayList<>(0); - NodeListBuilder.buildNodeList(convertible, nodes); + NodeListBuilder.buildNodeList(mutable, nodes); for (final MutableNode original : nodes) { final Converter converter = this.converters.get(0); final Node transformed = converter.convert(original, this.factory); @@ -118,9 +117,9 @@ public Node partialConvert(final int variant, final Node root) { */ public int calculateConversions(final Node root) { int conversions = 0; - final MutableNode convertible = new MutableNode(root); + final MutableNode mutable = new MutableNode(root); final List nodes = new ArrayList<>(0); - NodeListBuilder.buildNodeList(convertible, nodes); + NodeListBuilder.buildNodeList(mutable, nodes); for (final MutableNode original : nodes) { final Converter converter = this.converters.get(0); final Node transformed = converter.convert(original, this.factory); @@ -154,7 +153,6 @@ private static Node replace(final MutableNode original, * Creates a list from nodes. * The list is sorted in descending order of nodes depth in the tree. * Leaf nodes are at the beginning of the list, and the last element is the root. - * * @since 0.2.2 */ private static class NodeListBuilder { diff --git a/src/main/java/org/cqfn/astranaut/core/base/DraftNode.java b/src/main/java/org/cqfn/astranaut/core/base/DraftNode.java index 8756b83..d681300 100644 --- a/src/main/java/org/cqfn/astranaut/core/base/DraftNode.java +++ b/src/main/java/org/cqfn/astranaut/core/base/DraftNode.java @@ -26,6 +26,7 @@ import java.text.CharacterIterator; import java.text.StringCharacterIterator; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashSet; import java.util.LinkedList; import java.util.List; @@ -134,6 +135,22 @@ public static Node create(final String description, final Map> return DraftNode.create(iterator, nodes); } + /** + * Creates a node from the type name, data, and existing child nodes.. + * @param type The type name + * @param data The data (in a textual format) + * @param children The list of children + * @return A new node + */ + @SuppressWarnings("PMD.ProhibitPublicStaticMethods") + public static Node create(final String type, final String data, final Node... children) { + final DraftNode.Constructor ctor = new DraftNode.Constructor(); + ctor.setName(type); + ctor.setData(data); + ctor.setChildrenList(Arrays.asList(children)); + return ctor.createNode(); + } + /** * Creates a tree based on its description (recursive method). * @param iterator Iterator by description characters diff --git a/src/test/java/org/cqfn/astranaut/core/algorithms/conversion/AdapterTest.java b/src/test/java/org/cqfn/astranaut/core/algorithms/conversion/AdapterTest.java index 8181cc1..df8465b 100644 --- a/src/test/java/org/cqfn/astranaut/core/algorithms/conversion/AdapterTest.java +++ b/src/test/java/org/cqfn/astranaut/core/algorithms/conversion/AdapterTest.java @@ -24,8 +24,10 @@ package org.cqfn.astranaut.core.algorithms.conversion; import java.util.Arrays; +import java.util.Collections; import java.util.List; import org.cqfn.astranaut.core.base.Builder; +import org.cqfn.astranaut.core.base.DefaultFactory; import org.cqfn.astranaut.core.base.DraftNode; import org.cqfn.astranaut.core.base.DummyNode; import org.cqfn.astranaut.core.base.Factory; @@ -37,7 +39,6 @@ /** * Test for {@link Adapter} class. - * * @since 1.0 */ class AdapterTest { @@ -56,6 +57,11 @@ class AdapterTest { */ private static final String STR_ADDITION = "Addition"; + /** + * The 'Subtraction' string constant. + */ + private static final String STR_SUBTRACTION = "Subtraction"; + /** * The 'singleExpression' string constant. */ @@ -81,34 +87,31 @@ class AdapterTest { */ private static final String STR_NUM_LITERAL = "numericLiteral"; - /** - * Testing tree converter. - */ @Test void testTreeConverter() { - final Node original = this.createNode( + final Node original = DraftNode.create( AdapterTest.STR_SINGLE_EXPR, "", - this.createNode( + DraftNode.create( AdapterTest.STR_SINGLE_EXPR, "", - this.createNode( + DraftNode.create( AdapterTest.STR_IDENTIFIER, "", - this.createNode(AdapterTest.STR_LITERAL, "x") + DraftNode.create(AdapterTest.STR_LITERAL, "x") ) ), - this.createNode(AdapterTest.STR_LITERAL, "+"), - this.createNode( + DraftNode.create(AdapterTest.STR_LITERAL, "+"), + DraftNode.create( AdapterTest.STR_SINGLE_EXPR, "", - this.createNode( + DraftNode.create( AdapterTest.STR_LITERAL, "", - this.createNode( + DraftNode.create( AdapterTest.STR_NUM_LITERAL, "", - this.createNode(AdapterTest.STR_LITERAL, "0") + DraftNode.create(AdapterTest.STR_LITERAL, "0") ) ) ) @@ -135,55 +138,52 @@ void testTreeConverter() { Assertions.assertEquals(converted.getChild(1).getData(), "0"); } - /** - * Testing tree converter (complex case). - */ @Test void testTreeConverterComplexCase() { - final Node subtree = this.createNode( + final Node subtree = DraftNode.create( AdapterTest.STR_SINGLE_EXPR, "", - this.createNode( + DraftNode.create( AdapterTest.STR_SINGLE_EXPR, "", - this.createNode( + DraftNode.create( AdapterTest.STR_LITERAL, "", - this.createNode( + DraftNode.create( AdapterTest.STR_NUM_LITERAL, "", - this.createNode(AdapterTest.STR_LITERAL, "2") + DraftNode.create(AdapterTest.STR_LITERAL, "2") ) ) ), - this.createNode(AdapterTest.STR_LITERAL, "+"), - this.createNode( + DraftNode.create(AdapterTest.STR_LITERAL, "+"), + DraftNode.create( AdapterTest.STR_SINGLE_EXPR, "", - this.createNode( + DraftNode.create( AdapterTest.STR_LITERAL, "", - this.createNode( + DraftNode.create( AdapterTest.STR_NUM_LITERAL, "", - this.createNode(AdapterTest.STR_LITERAL, "3") + DraftNode.create(AdapterTest.STR_LITERAL, "3") ) ) ) ); - final Node original = this.createNode( + final Node original = DraftNode.create( AdapterTest.STR_SINGLE_EXPR, "", - this.createNode( + DraftNode.create( AdapterTest.STR_SINGLE_EXPR, "", - this.createNode( + DraftNode.create( AdapterTest.STR_IDENTIFIER, "", - this.createNode(AdapterTest.STR_LITERAL, "x") + DraftNode.create(AdapterTest.STR_LITERAL, "x") ) ), - this.createNode(AdapterTest.STR_LITERAL, "+"), + DraftNode.create(AdapterTest.STR_LITERAL, "+"), subtree ); final List converters = Arrays.asList( @@ -201,20 +201,17 @@ void testTreeConverterComplexCase() { Assertions.assertEquals(converted.getChild(1).getTypeName(), AdapterTest.STR_ADDITION); } - /** - * Test covering the variable converter. - */ @Test - void variableConverterTest() { + void testVariableConverter() { final Factory factory = GreenFactory.INSTANCE; final Converter converter = new VariableConverter(); - final Node original = this.createNode( + final Node original = DraftNode.create( AdapterTest.STR_SINGLE_EXPR, "", - this.createNode( + DraftNode.create( AdapterTest.STR_IDENTIFIER, "", - this.createNode( + DraftNode.create( AdapterTest.STR_LITERAL, "x" ) @@ -225,16 +222,16 @@ void variableConverterTest() { Assertions.assertEquals(converted.getChildCount(), 0); Assertions.assertEquals(converted.getData(), "x"); Assertions.assertTrue(converted.belongsToGroup(AdapterTest.STR_EXPRESSION)); - final Node bad = this.createNode( + final Node bad = DraftNode.create( AdapterTest.STR_SINGLE_EXPR, "", - this.createNode( + DraftNode.create( AdapterTest.STR_LITERAL, "", - this.createNode( + DraftNode.create( AdapterTest.STR_NUM_LITERAL, "", - this.createNode(AdapterTest.STR_LITERAL, "0") + DraftNode.create(AdapterTest.STR_LITERAL, "0") ) ) ); @@ -242,20 +239,17 @@ void variableConverterTest() { Assertions.assertEquals(empty, DummyNode.INSTANCE); } - /** - * Test covering the variable converter created as an example for generation. - */ @Test - void exampleVariableConverterTest() { + void testAnotherVariableConversion() { final Factory factory = GreenFactory.INSTANCE; final Converter converter = Rule0.INSTANCE; - final Node original = this.createNode( + final Node original = DraftNode.create( AdapterTest.STR_SINGLE_EXPR, "", - this.createNode( + DraftNode.create( AdapterTest.STR_IDENTIFIER, "", - this.createNode( + DraftNode.create( AdapterTest.STR_LITERAL, "Z" ) @@ -268,23 +262,20 @@ void exampleVariableConverterTest() { Assertions.assertTrue(converted.belongsToGroup(AdapterTest.STR_EXPRESSION)); } - /** - * Test covering the numeric literal converter. - */ @Test - void numericConverterTest() { + void testNumericConverter() { final Factory factory = GreenFactory.INSTANCE; final Converter converter = new NumericConverter(); - final Node original = this.createNode( + final Node original = DraftNode.create( AdapterTest.STR_SINGLE_EXPR, "", - this.createNode( + DraftNode.create( AdapterTest.STR_LITERAL, "", - this.createNode( + DraftNode.create( AdapterTest.STR_NUM_LITERAL, "", - this.createNode(AdapterTest.STR_LITERAL, "0") + DraftNode.create(AdapterTest.STR_LITERAL, "0") ) ) ); @@ -295,22 +286,19 @@ void numericConverterTest() { Assertions.assertTrue(converted.belongsToGroup(AdapterTest.STR_EXPRESSION)); } - /** - * Test covering the addition operators converter. - */ @Test - void additionConverterTest() { + void testAdditionConverter() { final Factory factory = GreenFactory.INSTANCE; final Converter converter = new AdditionConverter(); final Builder left = factory.createBuilder(AdapterTest.STR_VARIABLE); left.setData("x"); final Builder right = factory.createBuilder(AdapterTest.STR_INT_LITERAL); right.setData("0"); - final Node original = this.createNode( + final Node original = DraftNode.create( AdapterTest.STR_SINGLE_EXPR, "", left.createNode(), - this.createNode(AdapterTest.STR_LITERAL, "+"), + DraftNode.create(AdapterTest.STR_LITERAL, "+"), right.createNode() ); final Node converted = converter.convert(original, factory); @@ -319,19 +307,24 @@ void additionConverterTest() { Assertions.assertTrue(converted.belongsToGroup(AdapterTest.STR_EXPRESSION)); } - /** - * Creates node for test purposes. - * @param type The type name - * @param data The data (in a textual format) - * @param children The list of children - * @return A new node - */ - private Node createNode(final String type, final String data, final Node... children) { - final DraftNode.Constructor ctor = new DraftNode.Constructor(); - ctor.setName(type); - ctor.setData(data); - ctor.setChildrenList(Arrays.asList(children)); - return ctor.createNode(); + @Test + void testPartialConversion() { + final Factory factory = new DefaultFactory(Collections.emptyMap()); + final Converter converter = new AdditionToSubtractionConverter(); + final Adapter adapter = new Adapter(Collections.singletonList(converter), factory); + final Node original = DraftNode.create( + AdapterTest.STR_ADDITION, + "", + DraftNode.create( + AdapterTest.STR_ADDITION, + "", + DraftNode.create(AdapterTest.STR_NUM_LITERAL, "3"), + DraftNode.create(AdapterTest.STR_NUM_LITERAL, "2") + ), + DraftNode.create(AdapterTest.STR_NUM_LITERAL, "1") + ); + final int count = adapter.calculateConversions(original); + Assertions.assertEquals(2, count); } /** @@ -473,4 +466,21 @@ public Node convert(final Node root, final Factory factory) { return result; } } + + /** + * Converter that performs addition operation to subtraction operation. + * @since 2.0.0 + */ + private static class AdditionToSubtractionConverter implements Converter { + @Override + public Node convert(final Node root, final Factory factory) { + Node result = DummyNode.INSTANCE; + if (root.belongsToGroup(AdapterTest.STR_ADDITION) && root.getChildCount() == 2) { + final Builder builder = factory.createBuilder(AdapterTest.STR_SUBTRACTION); + builder.setChildrenList(root.getChildrenList()); + result = builder.createNode(); + } + return result; + } + } }