Skip to content

Commit

Permalink
+ test
Browse files Browse the repository at this point in the history
  • Loading branch information
kniazkov committed Feb 28, 2024
1 parent afe020b commit 7c07685
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/org/cqfn/astranaut/core/Replace.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public String getData() {

@Override
public int getChildCount() {
return 1;
return 2;
}

@Override
Expand Down
84 changes: 84 additions & 0 deletions src/test/java/org/cqfn/astranaut/core/ActionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ class ActionTest {
*/
private static final String INSERT_TYPE = "Insert";

/**
* The 'Replace' type.
*/
private static final String REPLACE_TYPE = "Replace";

/**
* The 'Delete' type.
*/
private static final String DELETE_TYPE = "Delete";

/**
* The 'color' property.
*/
Expand Down Expand Up @@ -86,4 +96,78 @@ void testInsertAction() {
created = builder.createNode();
Assertions.assertEquals(ActionTest.INSERT_TYPE, created.getTypeName());
}

/**
* Testing {@link Replace} action.
*/
@Test
void testReplaceAction() {
final Node before = LittleTrees.createVariable("x");
final Node after = LittleTrees.createIntegerLiteral(0);
final Action action = new Replace(before, after);
Assertions.assertEquals(EmptyFragment.INSTANCE, action.getFragment());
Assertions.assertEquals("", action.getData());
Assertions.assertEquals(2, action.getChildCount());
Assertions.assertEquals(before, action.getChild(0));
Assertions.assertEquals(after, action.getChild(1));
Assertions.assertNull(action.getChild(2));
final Type type = action.getType();
Assertions.assertEquals(ActionTest.REPLACE_TYPE, type.getName());
final List<ChildDescriptor> descriptors = type.getChildTypes();
Assertions.assertFalse(descriptors.isEmpty());
final List<String> hierarchy = type.getHierarchy();
Assertions.assertFalse(hierarchy.isEmpty());
Assertions.assertEquals(type.getName(), hierarchy.get(0));
Assertions.assertEquals(
ActionTest.EXPECTED_COLOR,
type.getProperty(ActionTest.COLOR_PROPERTY)
);
final Builder builder = type.createBuilder();
builder.setFragment(EmptyFragment.INSTANCE);
Assertions.assertTrue(builder.setData(""));
Assertions.assertFalse(builder.setData("it's a kind of magic"));
Assertions.assertFalse(builder.isValid());
Node created = builder.createNode();
Assertions.assertEquals(EmptyTree.INSTANCE, created);
Assertions.assertTrue(builder.setChildrenList(Arrays.asList(before, after)));
Assertions.assertFalse(builder.setChildrenList(Collections.singletonList(before)));
created = builder.createNode();
Assertions.assertEquals(ActionTest.REPLACE_TYPE, created.getTypeName());
}

/**
* Testing {@link Delete} action.
*/
@Test
void testDeleteAction() {
final Node deleted = LittleTrees.createReturnStatement(null);
final Action action = new Delete(deleted);
Assertions.assertEquals(EmptyFragment.INSTANCE, action.getFragment());
Assertions.assertEquals("", action.getData());
Assertions.assertEquals(1, action.getChildCount());
Assertions.assertEquals(deleted, action.getChild(0));
Assertions.assertNull(action.getChild(1));
final Type type = action.getType();
Assertions.assertEquals(ActionTest.DELETE_TYPE, type.getName());
final List<ChildDescriptor> descriptors = type.getChildTypes();
Assertions.assertFalse(descriptors.isEmpty());
final List<String> hierarchy = type.getHierarchy();
Assertions.assertFalse(hierarchy.isEmpty());
Assertions.assertEquals(type.getName(), hierarchy.get(0));
Assertions.assertEquals(
ActionTest.EXPECTED_COLOR,
type.getProperty(ActionTest.COLOR_PROPERTY)
);
final Builder builder = type.createBuilder();
builder.setFragment(EmptyFragment.INSTANCE);
Assertions.assertTrue(builder.setData(""));
Assertions.assertFalse(builder.setData("I hate syntax trees"));
Assertions.assertFalse(builder.isValid());
Node created = builder.createNode();
Assertions.assertEquals(EmptyTree.INSTANCE, created);
Assertions.assertTrue(builder.setChildrenList(Collections.singletonList(deleted)));
Assertions.assertFalse(builder.setChildrenList(Arrays.asList(deleted, deleted)));
created = builder.createNode();
Assertions.assertEquals(ActionTest.DELETE_TYPE, created.getTypeName());
}
}

0 comments on commit 7c07685

Please sign in to comment.