Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kniazkov committed Aug 30, 2024
1 parent 4e3d151 commit 383a1f3
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,11 @@ public Node convert(final Factory factory, final ActionList actions,
*/
private Node convertHole(final Factory factory, final ActionList actions,
final Map<Node, Integer> holes) {
assert this.number != null;
Node original = DummyNode.INSTANCE;
if (this.prototype != null) {
if (this.prototype != null && this.number != null) {
original = this.prototype.convert(factory, actions, holes);
holes.put(original, this.number);
}
holes.put(original, this.number);
return original;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@
*/
package org.cqfn.astranaut.core.utils;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.cqfn.astranaut.core.base.Builder;
import org.cqfn.astranaut.core.base.DefaultFactory;
import org.cqfn.astranaut.core.base.DummyNode;
import org.cqfn.astranaut.core.base.EmptyTree;
import org.cqfn.astranaut.core.base.Factory;
import org.cqfn.astranaut.core.base.Fragment;
import org.cqfn.astranaut.core.base.Node;
import org.cqfn.astranaut.core.base.Tree;
import org.cqfn.astranaut.core.base.Type;
Expand All @@ -41,6 +46,7 @@
* Test for {@link JsonDeserializer} class.
* @since 1.0.2
*/
@SuppressWarnings("PMD.TooManyMethods")
class JsonDeserializerTest {
/**
* The "Addition" type.
Expand Down Expand Up @@ -152,6 +158,66 @@ void deserializeWithNullFactory() {
Assertions.assertSame(EmptyTree.INSTANCE, result);
}

@Test
void deserializeWithFactoryThatProducesNullBuilder() {
final JsonDeserializer deserializer = new JsonDeserializer(
JsonDeserializerTest.SMALL_TREE,
language -> new Factory() {
@Override
public Type getType(final String name) {
return null;
}

@Override
public Builder createBuilder(final String name) {
return null;
}
}
);
final Tree result = deserializer.convert();
Assertions.assertSame(DummyNode.INSTANCE, result.getRoot());
}

@Test
void deserializeWithFactoryThatProducesInvalidBuilder() {
final JsonDeserializer deserializer = new JsonDeserializer(
JsonDeserializerTest.SMALL_TREE,
language -> new Factory() {
@Override
public Type getType(final String name) {
return null;
}

@Override
public Builder createBuilder(final String name) {
return new InvalidBuilder();
}
}
);
final Tree result = deserializer.convert();
Assertions.assertSame(DummyNode.INSTANCE, result.getRoot());
}

@Test
void deserializeInvalidHoleWithoutNumber() {
final JsonDeserializer deserializer = new JsonDeserializer(
"{ \"root\": { \"type\": \"Hole\", \"prototype\": { \"type\": \"Node\" } } }",
language -> DefaultFactory.EMPTY
);
final Tree result = deserializer.convert();
Assertions.assertSame(DummyNode.INSTANCE, result.getRoot());
}

@Test
void deserializeInvalidHoleWithoutPrototype() {
final JsonDeserializer deserializer = new JsonDeserializer(
"{ \"root\": { \"type\": \"Hole\", \"number\": 19 } } }",
language -> DefaultFactory.EMPTY
);
final Tree result = deserializer.convert();
Assertions.assertSame(DummyNode.INSTANCE, result.getRoot());
}

/**
* Returns content of the specified file.
* @param name The name of the file
Expand All @@ -163,4 +229,35 @@ private String getFileContent(final String name) {
Assertions.assertFalse(source.isEmpty());
return source;
}

/**
* Invalid builder for test purposes.
* @since 2.0.0
*/
private static final class InvalidBuilder implements Builder {
@Override
public void setFragment(final Fragment fragment) {
this.getClass();
}

@Override
public boolean setData(final String str) {
return true;
}

@Override
public boolean setChildrenList(final List<Node> list) {
return true;
}

@Override
public boolean isValid() {
return false;
}

@Override
public Node createNode() {
throw new UnsupportedOperationException();
}
}
}

0 comments on commit 383a1f3

Please sign in to comment.