From f7f55b75aa0f568612d4f51420a3a090d101c95b Mon Sep 17 00:00:00 2001 From: Ivan Kniazkov Date: Sat, 31 Aug 2024 12:18:32 +0300 Subject: [PATCH 01/11] More tests --- .../core/base/BaseInterfaceTest.java | 33 +++---------------- 1 file changed, 5 insertions(+), 28 deletions(-) diff --git a/src/test/java/org/cqfn/astranaut/core/base/BaseInterfaceTest.java b/src/test/java/org/cqfn/astranaut/core/base/BaseInterfaceTest.java index 4e064cf2..7de3076b 100644 --- a/src/test/java/org/cqfn/astranaut/core/base/BaseInterfaceTest.java +++ b/src/test/java/org/cqfn/astranaut/core/base/BaseInterfaceTest.java @@ -253,34 +253,11 @@ void testSublist(final Node root, final List list) { final List sub = list.subList(1, 3); Assertions.assertEquals(2, sub.size()); Assertions.assertEquals(root.getChild(1), sub.get(0)); - boolean oops = false; - try { - sub.get(3); - } catch (final IndexOutOfBoundsException ex) { - oops = true; - } - Assertions.assertTrue(oops); - oops = false; - try { - list.subList(0, 4); - } catch (final IndexOutOfBoundsException ex) { - oops = true; - } - Assertions.assertTrue(oops); - oops = false; - try { - list.subList(2, 1); - } catch (final IndexOutOfBoundsException ex) { - oops = true; - } - Assertions.assertTrue(oops); - oops = false; - try { - sub.get(10); - } catch (final IndexOutOfBoundsException ex) { - oops = true; - } - Assertions.assertTrue(oops); + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> sub.get(-1)); + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> sub.get(3)); + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> list.subList(-1, 0)); + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> list.subList(0, 4)); + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> list.subList(2, 1)); } void testListOtherMethods(final Node root, final List list) { From f66bb5401fdc1e0a0b90337a0a2a566092e4262b Mon Sep 17 00:00:00 2001 From: Ivan Kniazkov Date: Mon, 2 Sep 2024 12:40:40 +0300 Subject: [PATCH 02/11] Test refactoring --- .../core/base/BaseInterfaceTest.java | 190 ++++++------------ 1 file changed, 62 insertions(+), 128 deletions(-) diff --git a/src/test/java/org/cqfn/astranaut/core/base/BaseInterfaceTest.java b/src/test/java/org/cqfn/astranaut/core/base/BaseInterfaceTest.java index 7de3076b..534afaa6 100644 --- a/src/test/java/org/cqfn/astranaut/core/base/BaseInterfaceTest.java +++ b/src/test/java/org/cqfn/astranaut/core/base/BaseInterfaceTest.java @@ -103,48 +103,30 @@ void testChildrenList() { } void testListModifiersByOne(final List list) { - boolean oops = false; - try { - list.add(DraftNode.create("H")); - } catch (final UnsupportedOperationException ex) { - oops = true; - } - Assertions.assertTrue(oops); - oops = false; - try { - list.remove(list.get(0)); - } catch (final UnsupportedOperationException ex) { - oops = true; - } - Assertions.assertTrue(oops); - oops = false; - try { - list.clear(); - } catch (final UnsupportedOperationException ex) { - oops = true; - } - Assertions.assertTrue(oops); - oops = false; - try { - list.set(1, DraftNode.create("K")); - } catch (final UnsupportedOperationException ex) { - oops = true; - } - Assertions.assertTrue(oops); - oops = false; - try { - list.add(1, DraftNode.create("L")); - } catch (final UnsupportedOperationException ex) { - oops = true; - } - Assertions.assertTrue(oops); - oops = false; - try { - list.remove(1); - } catch (final UnsupportedOperationException ex) { - oops = true; - } - Assertions.assertTrue(oops); + Assertions.assertThrows( + UnsupportedOperationException.class, + () -> list.add(DraftNode.create("H")) + ); + Assertions.assertThrows( + UnsupportedOperationException.class, + () -> list.remove(list.get(0)) + ); + Assertions.assertThrows( + UnsupportedOperationException.class, + () -> list.clear() + ); + Assertions.assertThrows( + UnsupportedOperationException.class, + () -> list.set(1, DraftNode.create("K")) + ); + Assertions.assertThrows( + UnsupportedOperationException.class, + () -> list.add(1, DraftNode.create("L")) + ); + Assertions.assertThrows( + UnsupportedOperationException.class, + () -> list.remove(1) + ); } void testListModifiersByCollection(final List list) { @@ -152,101 +134,53 @@ void testListModifiersByCollection(final List list) { DraftNode.create("I"), DraftNode.create("J") ); - boolean oops = false; - try { - list.addAll(collection); - } catch (final UnsupportedOperationException ex) { - oops = true; - } - Assertions.assertTrue(oops); - oops = false; - try { - list.addAll(1, collection); - } catch (final UnsupportedOperationException ex) { - oops = true; - } - Assertions.assertTrue(oops); - oops = false; - try { - list.removeAll(collection); - } catch (final UnsupportedOperationException ex) { - oops = true; - } - Assertions.assertTrue(oops); - oops = false; - try { - list.retainAll(collection); - } catch (final UnsupportedOperationException ex) { - oops = true; - } - Assertions.assertTrue(oops); + Assertions.assertThrows( + UnsupportedOperationException.class, + () -> list.addAll(collection) + ); + Assertions.assertThrows( + UnsupportedOperationException.class, + () -> list.addAll(1, collection) + ); + Assertions.assertThrows( + UnsupportedOperationException.class, + () -> list.removeAll(collection) + ); + Assertions.assertThrows( + UnsupportedOperationException.class, + () -> list.retainAll(collection) + ); } void testListIterator(final List list) { - ListIterator cursor = list.listIterator(); + final ListIterator first = list.listIterator(); final StringBuilder builder = new StringBuilder(); - while (cursor.hasNext()) { - builder.append(cursor.next().getTypeName()); - } - boolean oops = false; - try { - cursor.next(); - } catch (final NoSuchElementException ignored) { - oops = true; + while (first.hasNext()) { + builder.append(first.next().getTypeName()); } - Assertions.assertTrue(oops); + Assertions.assertThrows(NoSuchElementException.class, first::next); Assertions.assertEquals("DEF", builder.toString()); - cursor = list.listIterator(1); - oops = false; - try { - cursor.remove(); - } catch (final UnsupportedOperationException ex) { - oops = true; - } - Assertions.assertTrue(oops); - oops = false; - try { - cursor.set(DraftNode.create("M")); - } catch (final UnsupportedOperationException ex) { - oops = true; - } - Assertions.assertTrue(oops); - oops = false; - try { - cursor.add(DraftNode.create("N")); - } catch (final UnsupportedOperationException ex) { - oops = true; - } - Assertions.assertTrue(oops); - Assertions.assertEquals(1, cursor.nextIndex()); - Assertions.assertEquals(0, cursor.previousIndex()); - Assertions.assertTrue(cursor.hasPrevious()); - Assertions.assertEquals("D", cursor.previous().getTypeName()); - Assertions.assertFalse(cursor.hasPrevious()); - oops = false; - try { - cursor.previous(); - } catch (final NoSuchElementException ignored) { - oops = true; - } - Assertions.assertTrue(oops); + final ListIterator second = list.listIterator(1); + Assertions.assertThrows(UnsupportedOperationException.class, second::remove); + Assertions.assertThrows( + UnsupportedOperationException.class, + () -> second.set(DraftNode.create("M")) + ); + Assertions.assertThrows( + UnsupportedOperationException.class, + () -> second.add(DraftNode.create("N")) + ); + Assertions.assertEquals(1, second.nextIndex()); + Assertions.assertEquals(0, second.previousIndex()); + Assertions.assertTrue(second.hasPrevious()); + Assertions.assertEquals("D", second.previous().getTypeName()); + Assertions.assertFalse(second.hasPrevious()); + Assertions.assertThrows(NoSuchElementException.class, second::previous); } void testListIteratorExceptions(final List list) { - boolean oops = false; - try { - list.listIterator(-1); - } catch (final IndexOutOfBoundsException ignored) { - oops = true; - } - Assertions.assertTrue(oops); - oops = false; - try { - list.listIterator(10); - } catch (final IndexOutOfBoundsException ignored) { - oops = true; - } - Assertions.assertTrue(oops); + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> list.listIterator(-1)); + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> list.listIterator(10)); } void testSublist(final Node root, final List list) { From 3f00006fbad86fbf90b52f3df933089b5601b7bd Mon Sep 17 00:00:00 2001 From: Ivan Kniazkov Date: Mon, 2 Sep 2024 13:08:37 +0300 Subject: [PATCH 03/11] Test refactoring --- .../normalization/NormalizedNodeTest.java | 8 +-- .../normalization/PropertyTest.java | 16 ++--- .../astranaut/core/base/DraftNodeTest.java | 8 +-- .../astranaut/core/base/DummyNodeTest.java | 8 +-- .../cqfn/astranaut/core/base/HoleTest.java | 8 +-- .../astranaut/core/base/PatternNodeTest.java | 11 ++-- .../astranaut/core/base/PositionTest.java | 8 +-- .../astranaut/core/utils/FilesReaderTest.java | 26 +++----- .../astranaut/core/utils/ListUtilsTest.java | 8 +-- .../core/utils/TreeVisualizerTest.java | 63 ++++++++----------- 10 files changed, 51 insertions(+), 113 deletions(-) diff --git a/src/test/java/org/cqfn/astranaut/core/algorithms/normalization/NormalizedNodeTest.java b/src/test/java/org/cqfn/astranaut/core/algorithms/normalization/NormalizedNodeTest.java index 9e547ba4..4f9c3484 100644 --- a/src/test/java/org/cqfn/astranaut/core/algorithms/normalization/NormalizedNodeTest.java +++ b/src/test/java/org/cqfn/astranaut/core/algorithms/normalization/NormalizedNodeTest.java @@ -112,13 +112,7 @@ void testBuilder() { builder.setFragment(EmptyFragment.INSTANCE); Assertions.assertTrue(builder.setData("")); Assertions.assertFalse(builder.isValid()); - boolean oops = false; - try { - builder.createNode(); - } catch (final IllegalStateException ignored) { - oops = true; - } - Assertions.assertTrue(oops); + Assertions.assertThrows(IllegalStateException.class, builder::createNode); final Node left = LittleTrees.createIntegerLiteral(2); Assertions.assertFalse(builder.setChildrenList(Collections.emptyList())); Assertions.assertFalse(builder.setChildrenList(Collections.singletonList(left))); diff --git a/src/test/java/org/cqfn/astranaut/core/algorithms/normalization/PropertyTest.java b/src/test/java/org/cqfn/astranaut/core/algorithms/normalization/PropertyTest.java index b9b76dd2..a18114c5 100644 --- a/src/test/java/org/cqfn/astranaut/core/algorithms/normalization/PropertyTest.java +++ b/src/test/java/org/cqfn/astranaut/core/algorithms/normalization/PropertyTest.java @@ -41,17 +41,11 @@ void testBaseMethods() { final String name = "color"; final String before = "red"; final String after = "blue"; - Node property = new Property(name, before); + final Node property = new Property(name, before); Assertions.assertEquals(name, property.getTypeName()); Assertions.assertEquals(before, property.getData()); Assertions.assertEquals(0, property.getChildCount()); - boolean oops = false; - try { - property.getChild(0); - } catch (final IndexOutOfBoundsException ignored) { - oops = true; - } - Assertions.assertTrue(oops); + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> property.getChild(0)); final Builder builder = property.getType().createBuilder(); builder.setFragment(EmptyFragment.INSTANCE); Assertions.assertTrue(builder.setData(after)); @@ -60,8 +54,8 @@ void testBaseMethods() { ); Assertions.assertTrue(builder.setChildrenList(Collections.emptyList())); Assertions.assertTrue(builder.isValid()); - property = builder.createNode(); - Assertions.assertEquals(name, property.getTypeName()); - Assertions.assertEquals(after, property.getData()); + final Node created = builder.createNode(); + Assertions.assertEquals(name, created.getTypeName()); + Assertions.assertEquals(after, created.getData()); } } diff --git a/src/test/java/org/cqfn/astranaut/core/base/DraftNodeTest.java b/src/test/java/org/cqfn/astranaut/core/base/DraftNodeTest.java index 41f59611..5ff07dd8 100644 --- a/src/test/java/org/cqfn/astranaut/core/base/DraftNodeTest.java +++ b/src/test/java/org/cqfn/astranaut/core/base/DraftNodeTest.java @@ -125,13 +125,7 @@ void typeTest() { @Test void wrongConstructionTest() { final DraftNode.Constructor ctor = new DraftNode.Constructor(); - boolean oops = false; - try { - ctor.createNode(); - } catch (final IllegalStateException ignored) { - oops = true; - } - Assertions.assertTrue(oops); + Assertions.assertThrows(IllegalStateException.class, ctor::createNode); } /** diff --git a/src/test/java/org/cqfn/astranaut/core/base/DummyNodeTest.java b/src/test/java/org/cqfn/astranaut/core/base/DummyNodeTest.java index f6a68be1..ee434fba 100644 --- a/src/test/java/org/cqfn/astranaut/core/base/DummyNodeTest.java +++ b/src/test/java/org/cqfn/astranaut/core/base/DummyNodeTest.java @@ -38,13 +38,7 @@ void testBaseInterface() { Assertions.assertSame(EmptyFragment.INSTANCE, node.getFragment()); Assertions.assertEquals(0, node.getChildCount()); Assertions.assertEquals("", node.getData()); - boolean oops = false; - try { - node.getChild(0); - } catch (final IndexOutOfBoundsException ignored) { - oops = true; - } - Assertions.assertTrue(oops); + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> node.getChild(0)); Assertions.assertEquals("\u2205", node.toString()); final Type type = node.getType(); Assertions.assertEquals(0, type.getChildTypes().size()); diff --git a/src/test/java/org/cqfn/astranaut/core/base/HoleTest.java b/src/test/java/org/cqfn/astranaut/core/base/HoleTest.java index 867dff06..8b86ba79 100644 --- a/src/test/java/org/cqfn/astranaut/core/base/HoleTest.java +++ b/src/test/java/org/cqfn/astranaut/core/base/HoleTest.java @@ -54,12 +54,6 @@ void testBaseInterface() { Assertions.assertEquals(typename, type.getHierarchy().get(0)); Assertions.assertFalse(type.getProperties().containsKey("abracadabra")); Assertions.assertEquals(typename, node.getTypeName()); - boolean oops = false; - try { - type.createBuilder(); - } catch (final UnsupportedOperationException ignored) { - oops = true; - } - Assertions.assertTrue(oops); + Assertions.assertThrows(UnsupportedOperationException.class, type::createBuilder); } } diff --git a/src/test/java/org/cqfn/astranaut/core/base/PatternNodeTest.java b/src/test/java/org/cqfn/astranaut/core/base/PatternNodeTest.java index e1bb78c7..fcbd94ea 100644 --- a/src/test/java/org/cqfn/astranaut/core/base/PatternNodeTest.java +++ b/src/test/java/org/cqfn/astranaut/core/base/PatternNodeTest.java @@ -49,13 +49,10 @@ void testBaseInterface() { final PatternNode pattern = new PatternNode(new DiffNode(node)); Assertions.assertEquals(100, pattern.getFragment().getEnd().getColumn()); Assertions.assertEquals("X<\"test\">(A)", pattern.toString()); - boolean oops = false; - try { - pattern.getType().createBuilder(); - } catch (final UnsupportedOperationException ignored) { - oops = true; - } - Assertions.assertTrue(oops); + Assertions.assertThrows( + UnsupportedOperationException.class, + () -> pattern.getType().createBuilder() + ); Assertions.assertFalse(pattern.makeHole(alien, 0)); Assertions.assertTrue(pattern.makeHole(child, 0)); } diff --git a/src/test/java/org/cqfn/astranaut/core/base/PositionTest.java b/src/test/java/org/cqfn/astranaut/core/base/PositionTest.java index d202da77..8656b355 100644 --- a/src/test/java/org/cqfn/astranaut/core/base/PositionTest.java +++ b/src/test/java/org/cqfn/astranaut/core/base/PositionTest.java @@ -41,12 +41,6 @@ void testBaseInterface() { 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(alien); - } catch (final IllegalArgumentException ignored) { - oops = true; - } - Assertions.assertTrue(oops); + Assertions.assertThrows(IllegalArgumentException.class, () -> first.compareTo(alien)); } } diff --git a/src/test/java/org/cqfn/astranaut/core/utils/FilesReaderTest.java b/src/test/java/org/cqfn/astranaut/core/utils/FilesReaderTest.java index bb12f5c6..00075bb3 100644 --- a/src/test/java/org/cqfn/astranaut/core/utils/FilesReaderTest.java +++ b/src/test/java/org/cqfn/astranaut/core/utils/FilesReaderTest.java @@ -23,7 +23,6 @@ */ package org.cqfn.astranaut.core.utils; -import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import org.junit.jupiter.api.Assertions; @@ -43,14 +42,12 @@ void wroteAndReadFile(@TempDir final Path tmpdir) { Assertions.assertTrue(flag); Assertions.assertTrue(Files.exists(path)); final FilesReader reader = new FilesReader(path.toString()); - boolean oops = false; - try { - final String result = reader.readAsString(); - Assertions.assertEquals("test string", result); - } catch (final IOException ignored) { - oops = true; - } - Assertions.assertFalse(oops); + Assertions.assertDoesNotThrow( + () -> { + final String result = reader.readAsString(); + Assertions.assertEquals("test string", result); + } + ); } @Test @@ -63,13 +60,10 @@ void readWithoutException() { @Test void testCustomException() { final FilesReader reader = new FilesReader("path.to.another.file.that.does.not.exist"); - boolean oops = false; - try { - reader.readAsString(CustomException::new); - } catch (final CustomException ignored) { - oops = true; - } - Assertions.assertTrue(oops); + Assertions.assertThrows( + CustomException.class, + () -> reader.readAsString(CustomException::new) + ); } /** diff --git a/src/test/java/org/cqfn/astranaut/core/utils/ListUtilsTest.java b/src/test/java/org/cqfn/astranaut/core/utils/ListUtilsTest.java index 17dbe863..6ea0dc29 100644 --- a/src/test/java/org/cqfn/astranaut/core/utils/ListUtilsTest.java +++ b/src/test/java/org/cqfn/astranaut/core/utils/ListUtilsTest.java @@ -46,13 +46,7 @@ void testAddingItems() { final List result = list.make(); final List expected = Arrays.asList(1, 2); Assertions.assertEquals(expected, result); - boolean oops = false; - try { - result.add(3); - } catch (final UnsupportedOperationException exception) { - oops = true; - } - Assertions.assertTrue(oops); + Assertions.assertThrows(UnsupportedOperationException.class, () -> result.add(3)); } @Test diff --git a/src/test/java/org/cqfn/astranaut/core/utils/TreeVisualizerTest.java b/src/test/java/org/cqfn/astranaut/core/utils/TreeVisualizerTest.java index b2b7d9c3..110f21d8 100644 --- a/src/test/java/org/cqfn/astranaut/core/utils/TreeVisualizerTest.java +++ b/src/test/java/org/cqfn/astranaut/core/utils/TreeVisualizerTest.java @@ -35,7 +35,6 @@ import org.cqfn.astranaut.core.base.Node; import org.cqfn.astranaut.core.base.NodeAndType; import org.cqfn.astranaut.core.base.Tree; -import org.cqfn.astranaut.core.utils.visualizer.WrongFileExtension; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; @@ -55,13 +54,11 @@ void testSingleNodeVisualization(@TempDir final Path temp) { final Tree tree = Tree.createDraft("TestNode<\"value\">"); final TreeVisualizer visualizer = new TreeVisualizer(tree); final Path img = temp.resolve("node.png"); - boolean oops = false; - try { - visualizer.visualize(new File(img.toString())); - } catch (final WrongFileExtension | IOException exception) { - oops = true; - } - Assertions.assertFalse(oops); + Assertions.assertDoesNotThrow( + () -> { + visualizer.visualize(new File(img.toString())); + } + ); } /** @@ -73,13 +70,11 @@ void testNullNodeVisualization(@TempDir final Path temp) { final Tree tree = EmptyTree.INSTANCE; final TreeVisualizer visualizer = new TreeVisualizer(tree); final Path img = temp.resolve("null.png"); - boolean oops = false; - try { - visualizer.visualize(new File(img.toString())); - } catch (final WrongFileExtension | IOException exception) { - oops = true; - } - Assertions.assertFalse(oops); + Assertions.assertDoesNotThrow( + () -> { + visualizer.visualize(new File(img.toString())); + } + ); } /** @@ -94,13 +89,11 @@ void testNodeVisualizationWithEncoding(@TempDir final Path temp) { final Tree tree = new Tree(ctor.createNode()); final TreeVisualizer visualizer = new TreeVisualizer(tree); final Path img = temp.resolve("data.png"); - boolean oops = false; - try { - visualizer.visualize(new File(img.toString())); - } catch (final WrongFileExtension | IOException exception) { - oops = true; - } - Assertions.assertFalse(oops); + Assertions.assertDoesNotThrow( + () -> { + visualizer.visualize(new File(img.toString())); + } + ); } /** @@ -121,13 +114,11 @@ void testTreeVisualization(@TempDir final Path temp) { final Tree tree = new Tree(addition.createNode()); final TreeVisualizer visualizer = new TreeVisualizer(tree); final Path img = temp.resolve("tree.svg"); - boolean oops = false; - try { - visualizer.visualize(new File(img.toString())); - } catch (final WrongFileExtension | IOException exception) { - oops = true; - } - Assertions.assertFalse(oops); + Assertions.assertDoesNotThrow( + () -> { + visualizer.visualize(new File(img.toString())); + } + ); } /** @@ -149,8 +140,8 @@ void testWrongExtension(@TempDir final Path temp) { } catch (final IOException ignored) { } Assertions.assertTrue(oops); - oops = false; img = temp.resolve("node"); + oops = false; try { visualizer.visualize(new File(img.toString())); } catch (final CoreException | IOException ignored) { @@ -168,13 +159,11 @@ void testColoredNodeVisualization(@TempDir final Path temp) { final Tree tree = new Tree(new ColoredNode()); final TreeVisualizer visualizer = new TreeVisualizer(tree); final Path img = temp.resolve("node.svg"); - boolean oops = false; - try { - visualizer.visualize(new File(img.toString())); - } catch (final WrongFileExtension | IOException exception) { - oops = true; - } - Assertions.assertFalse(oops); + Assertions.assertDoesNotThrow( + () -> { + visualizer.visualize(new File(img.toString())); + } + ); final FilesReader reader = new FilesReader(img.toString()); final String content = reader.readAsStringNoExcept(); Assertions.assertTrue(content.contains("fill=\"yellow\"")); From 8003e28476de90dd0b59e996e652b00b7739be9e Mon Sep 17 00:00:00 2001 From: Ivan Kniazkov Date: Mon, 2 Sep 2024 14:35:38 +0300 Subject: [PATCH 04/11] More tests --- .../org/cqfn/astranaut/core/base/DraftNode.java | 17 ++++++++++++----- .../utils/visualizer/WrongFileExtension.java | 1 - .../cqfn/astranaut/core/base/DiffNodeTest.java | 2 +- .../cqfn/astranaut/core/base/DraftNodeTest.java | 13 ++++++++++++- 4 files changed, 25 insertions(+), 8 deletions(-) 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 bdc99252..2d21dae8 100644 --- a/src/main/java/org/cqfn/astranaut/core/base/DraftNode.java +++ b/src/main/java/org/cqfn/astranaut/core/base/DraftNode.java @@ -178,7 +178,6 @@ private static Node create( * @return Node data, extracted from description */ private static String parseData(final CharacterIterator iterator) { - assert iterator.current() == '<'; final StringBuilder data = new StringBuilder(); char symbol = iterator.next(); if (symbol == '\"') { @@ -188,8 +187,13 @@ private static String parseData(final CharacterIterator iterator) { symbol = iterator.next(); } symbol = iterator.next(); - } - if (symbol == '>') { + if (symbol == '>') { + iterator.next(); + } + } else { + do { + symbol = iterator.next(); + } while (symbol != '>' && symbol != CharacterIterator.DONE); iterator.next(); } return data.toString(); @@ -204,7 +208,6 @@ private static String parseData(final CharacterIterator iterator) { private static List parseChildrenList( final CharacterIterator iterator, final Map> nodes) { - assert iterator.current() == '('; final List children = new LinkedList<>(); char next; do { @@ -214,7 +217,11 @@ private static List parseChildrenList( children.add(child); } next = iterator.current(); - assert next == ')' || next == ',' || next == ' '; + if (next != ')' && next != ',' && next != ' ') { + do { + next = iterator.next(); + } while (next != ')' && next != CharacterIterator.DONE); + } } while (next != ')'); iterator.next(); return children; diff --git a/src/main/java/org/cqfn/astranaut/core/utils/visualizer/WrongFileExtension.java b/src/main/java/org/cqfn/astranaut/core/utils/visualizer/WrongFileExtension.java index 48f3e35f..4eaf5283 100644 --- a/src/main/java/org/cqfn/astranaut/core/utils/visualizer/WrongFileExtension.java +++ b/src/main/java/org/cqfn/astranaut/core/utils/visualizer/WrongFileExtension.java @@ -27,7 +27,6 @@ /** * Exception "The file extension must be .png or .svg". - * * @since 1.0.2 */ public final class WrongFileExtension extends CoreException { diff --git a/src/test/java/org/cqfn/astranaut/core/base/DiffNodeTest.java b/src/test/java/org/cqfn/astranaut/core/base/DiffNodeTest.java index 560e8bf7..ea0b7286 100644 --- a/src/test/java/org/cqfn/astranaut/core/base/DiffNodeTest.java +++ b/src/test/java/org/cqfn/astranaut/core/base/DiffNodeTest.java @@ -224,7 +224,7 @@ private String getFileContent(final String name) { source = new FilesReader(file).readAsString( (FilesReader.CustomExceptionCreator) () -> new CoreException() { - private static final long serialVersionUID = -8921455132545245481L; + private static final long serialVersionUID = -1; @Override public String getInitiator() { diff --git a/src/test/java/org/cqfn/astranaut/core/base/DraftNodeTest.java b/src/test/java/org/cqfn/astranaut/core/base/DraftNodeTest.java index 5ff07dd8..41254de0 100644 --- a/src/test/java/org/cqfn/astranaut/core/base/DraftNodeTest.java +++ b/src/test/java/org/cqfn/astranaut/core/base/DraftNodeTest.java @@ -34,7 +34,6 @@ /** * Tests covering {@link DraftNode} class. - * * @since 1.1.0 */ class DraftNodeTest { @@ -128,6 +127,18 @@ void wrongConstructionTest() { Assertions.assertThrows(IllegalStateException.class, ctor::createNode); } + @Test + void wrongChildrenListFormat() { + final Node node = DraftNode.create("A(B(C #D),E)"); + Assertions.assertEquals("A(B(C), E)", node.toString()); + } + + @Test + void wrongDataFormat() { + final Node node = DraftNode.create("A<$>(B)"); + Assertions.assertEquals("A(B)", node.toString()); + } + /** * Creates a tree based on the given description and then serializes it to obtain * its description. If the original description matches the generated one, the test From 9ec9af3d04c873f832347089cf5fa1a7e6330551 Mon Sep 17 00:00:00 2001 From: Ivan Kniazkov Date: Mon, 2 Sep 2024 14:44:07 +0300 Subject: [PATCH 05/11] More tests --- .../java/org/cqfn/astranaut/core/base/DraftNode.java | 1 + .../org/cqfn/astranaut/core/base/DraftNodeTest.java | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) 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 2d21dae8..8756b833 100644 --- a/src/main/java/org/cqfn/astranaut/core/base/DraftNode.java +++ b/src/main/java/org/cqfn/astranaut/core/base/DraftNode.java @@ -221,6 +221,7 @@ private static List parseChildrenList( do { next = iterator.next(); } while (next != ')' && next != CharacterIterator.DONE); + break; } } while (next != ')'); iterator.next(); diff --git a/src/test/java/org/cqfn/astranaut/core/base/DraftNodeTest.java b/src/test/java/org/cqfn/astranaut/core/base/DraftNodeTest.java index 41254de0..5dbdc993 100644 --- a/src/test/java/org/cqfn/astranaut/core/base/DraftNodeTest.java +++ b/src/test/java/org/cqfn/astranaut/core/base/DraftNodeTest.java @@ -129,14 +129,18 @@ void wrongConstructionTest() { @Test void wrongChildrenListFormat() { - final Node node = DraftNode.create("A(B(C #D),E)"); - Assertions.assertEquals("A(B(C), E)", node.toString()); + final Node first = DraftNode.create("A(B(C #D),E)"); + Assertions.assertEquals("A(B(C), E)", first.toString()); + final Node second = DraftNode.create("A(B"); + Assertions.assertEquals("A(B)", second.toString()); } @Test void wrongDataFormat() { - final Node node = DraftNode.create("A<$>(B)"); - Assertions.assertEquals("A(B)", node.toString()); + final Node first = DraftNode.create("A<$>(B)"); + Assertions.assertEquals("A(B)", first.toString()); + final Node second = DraftNode.create("A<$"); + Assertions.assertEquals("A", second.toString()); } /** From d47c659517eb65b0e54705332ef499137a128649 Mon Sep 17 00:00:00 2001 From: Ivan Kniazkov Date: Mon, 2 Sep 2024 14:48:48 +0300 Subject: [PATCH 06/11] More tests --- src/test/java/org/cqfn/astranaut/core/base/DraftNodeTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/cqfn/astranaut/core/base/DraftNodeTest.java b/src/test/java/org/cqfn/astranaut/core/base/DraftNodeTest.java index 5dbdc993..a046572f 100644 --- a/src/test/java/org/cqfn/astranaut/core/base/DraftNodeTest.java +++ b/src/test/java/org/cqfn/astranaut/core/base/DraftNodeTest.java @@ -137,9 +137,9 @@ void wrongChildrenListFormat() { @Test void wrongDataFormat() { - final Node first = DraftNode.create("A<$>(B)"); + final Node first = DraftNode.create("A<$ >(B)"); Assertions.assertEquals("A(B)", first.toString()); - final Node second = DraftNode.create("A<$"); + final Node second = DraftNode.create("A<$ "); Assertions.assertEquals("A", second.toString()); } From e7966a888e2eee7078b675f8c67dc735e20e1de4 Mon Sep 17 00:00:00 2001 From: Ivan Kniazkov Date: Mon, 2 Sep 2024 15:36:58 +0300 Subject: [PATCH 07/11] More tests --- .../astranaut/core/base/DiffNodeTest.java | 169 +++++++++++++++--- 1 file changed, 143 insertions(+), 26 deletions(-) diff --git a/src/test/java/org/cqfn/astranaut/core/base/DiffNodeTest.java b/src/test/java/org/cqfn/astranaut/core/base/DiffNodeTest.java index ea0b7286..ee9542cc 100644 --- a/src/test/java/org/cqfn/astranaut/core/base/DiffNodeTest.java +++ b/src/test/java/org/cqfn/astranaut/core/base/DiffNodeTest.java @@ -23,6 +23,7 @@ */ package org.cqfn.astranaut.core.base; +import java.util.List; import org.cqfn.astranaut.core.example.LittleTrees; import org.cqfn.astranaut.core.example.green.GreenFactory; import org.cqfn.astranaut.core.utils.FilesReader; @@ -211,6 +212,26 @@ void testDiffNodeAsString() { Assertions.assertEquals(description, root.toString()); } + @Test + void getBranchFromBadNode() { + Assertions.assertSame( + DummyNode.INSTANCE, + new DiffNode(new TestNode(TestCase.NULL_BUILDER)).getBefore() + ); + Assertions.assertSame( + DummyNode.INSTANCE, + new DiffNode(new TestNode(TestCase.BAD_DATA)).getBefore() + ); + Assertions.assertSame( + DummyNode.INSTANCE, + new DiffNode(new TestNode(TestCase.BAD_CHILDREN)).getBefore() + ); + Assertions.assertSame( + DummyNode.INSTANCE, + new DiffNode(new TestNode(TestCase.NULL_BUILDER)).getBefore() + ); + } + /** * Returns content of the specified file. * @param name The name of the file @@ -218,32 +239,8 @@ void testDiffNodeAsString() { */ private String getFileContent(final String name) { final String file = DiffNodeTest.TESTS_PATH.concat(name); - boolean oops = false; - String source = ""; - try { - source = new FilesReader(file).readAsString( - (FilesReader.CustomExceptionCreator) () - -> new CoreException() { - private static final long serialVersionUID = -1; - - @Override - public String getInitiator() { - return "DifferenceNodeTest"; - } - - @Override - public String getErrorMessage() { - return String.format( - "Could not read the file that contains source tree: %s", - file - ); - } - } - ); - } catch (final CoreException exception) { - oops = true; - } - Assertions.assertFalse(oops); + final String source = new FilesReader(file).readAsStringNoExcept(); + Assertions.assertFalse(source.isEmpty()); return source; } @@ -263,4 +260,124 @@ private Node loadTree(final String name) { Assertions.assertNotEquals(DummyNode.INSTANCE, root); return root; } + + /** + * List of test cases. + * @since 2.0.0 + */ + private enum TestCase { + /** + * Case: node does not have a builder. + */ + NULL_BUILDER, + + /** + * Case: builder is not accepting data. + */ + BAD_DATA, + + /** + * Case: builder won't accept children. + */ + BAD_CHILDREN, + + /** + * Case: builder is not valid. + */ + INVALID_BUILDER + } + + /** + * Custom node for test purposes. + * @since 2.0.0 + */ + private static final class TestNode extends NodeAndType { + /** + * Test case. + */ + private final TestCase test; + + /** + * Constructor. + * @param test Test case + */ + private TestNode(final TestCase test) { + this.test = test; + } + + @Override + public String getData() { + return ""; + } + + @Override + public int getChildCount() { + return 0; + } + + @Override + public Node getChild(final int index) { + throw new IndexOutOfBoundsException(); + } + + @Override + public String getName() { + return "Test"; + } + + @Override + public Builder createBuilder() { + final Builder builder; + if (this.test == TestCase.NULL_BUILDER) { + builder = null; + } else { + builder = new TestBuilder(this.test); + } + return builder; + } + } + + /** + * Custom builder for test purposes. + * @since 2.0.0 + */ + private static final class TestBuilder implements Builder { + /** + * Test case. + */ + private final TestCase test; + + /** + * Constructor. + * @param test Test case + */ + private TestBuilder(final TestCase test) { + this.test = test; + } + + @Override + public void setFragment(final Fragment fragment) { + this.getClass(); + } + + @Override + public boolean setData(final String str) { + return this.test != TestCase.BAD_DATA; + } + + @Override + public boolean setChildrenList(final List list) { + return this.test != TestCase.BAD_CHILDREN; + } + + @Override + public boolean isValid() { + return this.test != TestCase.INVALID_BUILDER; + } + + @Override + public Node createNode() { + throw new UnsupportedOperationException(); + } + } } From 4e2b1ab17a6a9116e9a1d78c306ce5563848e6af Mon Sep 17 00:00:00 2001 From: Ivan Kniazkov Date: Mon, 2 Sep 2024 15:48:27 +0300 Subject: [PATCH 08/11] More tests --- .../org/cqfn/astranaut/core/base/DiffNodeTest.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/cqfn/astranaut/core/base/DiffNodeTest.java b/src/test/java/org/cqfn/astranaut/core/base/DiffNodeTest.java index ee9542cc..ce234186 100644 --- a/src/test/java/org/cqfn/astranaut/core/base/DiffNodeTest.java +++ b/src/test/java/org/cqfn/astranaut/core/base/DiffNodeTest.java @@ -24,6 +24,7 @@ package org.cqfn.astranaut.core.base; import java.util.List; +import jdk.internal.org.jline.utils.DiffHelper; import org.cqfn.astranaut.core.example.LittleTrees; import org.cqfn.astranaut.core.example.green.GreenFactory; import org.cqfn.astranaut.core.utils.FilesReader; @@ -228,10 +229,18 @@ void getBranchFromBadNode() { ); Assertions.assertSame( DummyNode.INSTANCE, - new DiffNode(new TestNode(TestCase.NULL_BUILDER)).getBefore() + new DiffNode(new TestNode(TestCase.INVALID_BUILDER)).getBefore() ); } + @Test + void deleteNonExistingNode() { + final DiffNode diff = new DiffNode(DraftNode.create("X")); + Assertions.assertFalse(diff.deleteNode(-1)); + Assertions.assertFalse(diff.deleteNode(1)); + Assertions.assertFalse(diff.deleteNode(DummyNode.INSTANCE)); + } + /** * Returns content of the specified file. * @param name The name of the file From 46100d4de08402c46ad2ff88085359faf73f5d0f Mon Sep 17 00:00:00 2001 From: Ivan Kniazkov Date: Mon, 2 Sep 2024 15:55:11 +0300 Subject: [PATCH 09/11] More tests --- src/test/java/org/cqfn/astranaut/core/base/DiffNodeTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/cqfn/astranaut/core/base/DiffNodeTest.java b/src/test/java/org/cqfn/astranaut/core/base/DiffNodeTest.java index ce234186..fb05d095 100644 --- a/src/test/java/org/cqfn/astranaut/core/base/DiffNodeTest.java +++ b/src/test/java/org/cqfn/astranaut/core/base/DiffNodeTest.java @@ -24,7 +24,6 @@ package org.cqfn.astranaut.core.base; import java.util.List; -import jdk.internal.org.jline.utils.DiffHelper; import org.cqfn.astranaut.core.example.LittleTrees; import org.cqfn.astranaut.core.example.green.GreenFactory; import org.cqfn.astranaut.core.utils.FilesReader; @@ -37,6 +36,7 @@ * * @since 1.1.0 */ +@SuppressWarnings("PMD.TooManyMethods") class DiffNodeTest { /** * The folder with test resources. From 3c2639e3dfa07ec6b873926a99d08a6bbbc37f7e Mon Sep 17 00:00:00 2001 From: Ivan Kniazkov Date: Mon, 2 Sep 2024 16:07:40 +0300 Subject: [PATCH 10/11] More tests --- .../astranaut/core/base/DiffNodeTest.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/test/java/org/cqfn/astranaut/core/base/DiffNodeTest.java b/src/test/java/org/cqfn/astranaut/core/base/DiffNodeTest.java index fb05d095..85321c71 100644 --- a/src/test/java/org/cqfn/astranaut/core/base/DiffNodeTest.java +++ b/src/test/java/org/cqfn/astranaut/core/base/DiffNodeTest.java @@ -233,6 +233,21 @@ void getBranchFromBadNode() { ); } + @Test + void replaceNonExistingNode() { + final DiffNode diff = new DiffNode(DraftNode.create("X")); + Assertions.assertFalse(diff.replaceNode(-1, DummyNode.INSTANCE)); + Assertions.assertFalse(diff.replaceNode(1, DummyNode.INSTANCE)); + Assertions.assertFalse(diff.replaceNode(DummyNode.INSTANCE, DummyNode.INSTANCE)); + } + + @Test + void replaceAlreadyReplacedNode() { + final DiffNode diff = new DiffNode(DraftNode.create("X(A)")); + Assertions.assertTrue(diff.replaceNode(0, DraftNode.create("B"))); + Assertions.assertFalse(diff.replaceNode(0, DraftNode.create("C"))); + } + @Test void deleteNonExistingNode() { final DiffNode diff = new DiffNode(DraftNode.create("X")); @@ -241,6 +256,13 @@ void deleteNonExistingNode() { Assertions.assertFalse(diff.deleteNode(DummyNode.INSTANCE)); } + @Test + void deleteAlreadyDeletedNode() { + final DiffNode diff = new DiffNode(DraftNode.create("X(A)")); + Assertions.assertTrue(diff.deleteNode(0)); + Assertions.assertFalse(diff.deleteNode(0)); + } + /** * Returns content of the specified file. * @param name The name of the file From 49dcf15ac15bd0b4f0b66c4657409416c081c548 Mon Sep 17 00:00:00 2001 From: Ivan Kniazkov Date: Mon, 2 Sep 2024 16:28:22 +0300 Subject: [PATCH 11/11] More tests --- .../astranaut/core/base/DiffNodeTest.java | 52 +++++++++---------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/src/test/java/org/cqfn/astranaut/core/base/DiffNodeTest.java b/src/test/java/org/cqfn/astranaut/core/base/DiffNodeTest.java index 85321c71..46881a87 100644 --- a/src/test/java/org/cqfn/astranaut/core/base/DiffNodeTest.java +++ b/src/test/java/org/cqfn/astranaut/core/base/DiffNodeTest.java @@ -24,6 +24,9 @@ package org.cqfn.astranaut.core.base; import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; import org.cqfn.astranaut.core.example.LittleTrees; import org.cqfn.astranaut.core.example.green.GreenFactory; import org.cqfn.astranaut.core.utils.FilesReader; @@ -78,9 +81,17 @@ class DiffNodeTest { */ private static final String TREE_WITH_DELETE = "tree_containing_delete_action.json"; - /** - * Testing {@link DiffNode#getBefore()} method with inserted node. - */ + @Test + void testBaseInterface() { + final Node original = DraftNode.create("A<\"test\">(B,C,D,E,F)"); + final DiffNode diff = new DiffNode(original); + Assertions.assertSame(diff.getPrototype(), original); + Assertions.assertSame(diff.getFragment(), original.getFragment()); + Assertions.assertEquals(diff.getData(), original.getData()); + Assertions.assertEquals(diff.getChildCount(), original.getChildCount()); + Assertions.assertThrows(UnsupportedOperationException.class, diff::createBuilder); + } + @Test void testInsertGetBefore() { final Node root = this.loadTree(DiffNodeTest.TREE_WITH_INSERT); @@ -92,9 +103,6 @@ void testInsertGetBefore() { Assertions.assertTrue(expected.deepCompare(actual)); } - /** - * Testing {@link DiffNode#getAfter()} method with inserted node. - */ @Test void testInsertGetAfter() { final Node root = this.loadTree(DiffNodeTest.TREE_WITH_INSERT); @@ -106,9 +114,6 @@ void testInsertGetAfter() { Assertions.assertTrue(expected.deepCompare(actual)); } - /** - * Testing tree loading / composing with replaced node. - */ @Test void testReplace() { final Node root = this.loadTree(DiffNodeTest.TREE_WITH_REPLACE); @@ -126,9 +131,6 @@ void testReplace() { ); } - /** - * Testing {@link DiffNode#getBefore()} method with deleted node. - */ @Test void testDeleteGetBefore() { final Node root = this.loadTree(DiffNodeTest.TREE_WITH_DELETE); @@ -140,9 +142,6 @@ void testDeleteGetBefore() { Assertions.assertTrue(expected.deepCompare(actual)); } - /** - * Testing {@link DiffNode#getAfter()} method with deleted node. - */ @Test void testDeleteGetAfter() { final Node root = this.loadTree(DiffNodeTest.TREE_WITH_DELETE); @@ -154,9 +153,6 @@ void testDeleteGetAfter() { Assertions.assertTrue(expected.deepCompare(actual)); } - /** - * Tests the case where a node is inserted at the start position of the child list. - */ @Test void testInsertNodeFirst() { final Node first = LittleTrees.createReturnStatement(null); @@ -175,9 +171,17 @@ void testInsertNodeFirst() { Assertions.assertTrue(after.deepCompare(diff.getAfter())); } - /** - * Tests the case where an attempt to insert a node fails. - */ + @Test + void insertAndDelete() { + final Map> nodes = new TreeMap<>(); + final DiffNode diff = new DiffNode(DraftNode.create("A(B,C,D)", nodes)); + Assertions.assertTrue(diff.deleteNode(nodes.get("B").iterator().next())); + Assertions.assertTrue( + diff.insertNodeAfter(DraftNode.create("E"), nodes.get("C").iterator().next()) + ); + Assertions.assertEquals("A(C, E, D)", diff.getAfter().toString()); + } + @Test void testInsertNodeFails() { final DiffNode diff = new DiffNode( @@ -192,9 +196,6 @@ void testInsertNodeFails() { Assertions.assertFalse(result); } - /** - * Tests {@link DiffNode#getParent()} method. - */ @Test void testParentReference() { final DiffNode root = new DiffNode(DraftNode.create("A(B,C)")); @@ -203,9 +204,6 @@ void testParentReference() { Assertions.assertSame(((DiffNode) child).getParent(), root); } - /** - * Tests {@link DiffNode#getParent()} method. - */ @Test void testDiffNodeAsString() { final String description = "X(Y, Z)";