Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More tests #27

Merged
merged 11 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/main/java/org/cqfn/astranaut/core/base/DraftNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 == '\"') {
Expand All @@ -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();
Expand All @@ -204,7 +208,6 @@ private static String parseData(final CharacterIterator iterator) {
private static List<Node> parseChildrenList(
final CharacterIterator iterator,
final Map<String, Set<Node>> nodes) {
assert iterator.current() == '(';
final List<Node> children = new LinkedList<>();
char next;
do {
Expand All @@ -214,7 +217,12 @@ private static List<Node> parseChildrenList(
children.add(child);
}
next = iterator.current();
assert next == ')' || next == ',' || next == ' ';
if (next != ')' && next != ',' && next != ' ') {
do {
next = iterator.next();
} while (next != ')' && next != CharacterIterator.DONE);
break;
}
} while (next != ')');
iterator.next();
return children;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

/**
* Exception "The file extension must be .png or .svg".
*
* @since 1.0.2
*/
public final class WrongFileExtension extends CoreException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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());
}
}
223 changes: 67 additions & 156 deletions src/test/java/org/cqfn/astranaut/core/base/BaseInterfaceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,184 +103,95 @@ void testChildrenList() {
}

void testListModifiersByOne(final List<Node> 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<Node> list) {
final Collection<Node> collection = Arrays.asList(
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<Node> list) {
ListIterator<Node> cursor = list.listIterator();
final ListIterator<Node> 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<Node> 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<Node> 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<Node> list) {
final List<Node> 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<Node> list) {
Expand Down
Loading
Loading