Skip to content

Commit

Permalink
Added and tested the main 'build()' method for difference tree builder
Browse files Browse the repository at this point in the history
  • Loading branch information
kniazkov committed Feb 15, 2024
1 parent 9c51dbd commit e213ef5
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.util.Map;
import org.cqfn.astranaut.core.DifferenceNode;
import org.cqfn.astranaut.core.Node;
import org.cqfn.astranaut.core.algorithms.mapping.Mapper;
import org.cqfn.astranaut.core.algorithms.mapping.Mapping;

/**
* Builder of difference syntax tree, that is, one that stores changes between two trees.
Expand All @@ -48,13 +50,28 @@ public final class DifferenceTreeBuilder {

/**
* Constructor.
* @param prototype Root node of an 'ordinary', non-difference original tree before the changes
* @param before Root node of an 'ordinary', non-difference original tree before the changes
*/
public DifferenceTreeBuilder(final Node prototype) {
this.root = new DifferenceNode(prototype);
public DifferenceTreeBuilder(final Node before) {
this.root = new DifferenceNode(before);
this.parents = DifferenceTreeBuilder.buildParentsMap(this.root);
}

/**
* Builds a difference tree based on the original tree and the tree after changes.
* @param after Root node of tree before the changes
* @param mapper A mapper used for node mappings
* @return Result of operation, {@code true} if difference tree was built
*/
public boolean build(final Node after, final Mapper mapper) {
final Mapping mapping = mapper.map(this.root.getPrototype(), after);
boolean result = true;
for (final Node deleted : mapping.getDeleted()) {
result = result & this.deleteNode(deleted);
}
return result;
}

/**
* Returns root of resulting difference tree.
* @return Root node of difference tree
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2024 Ivan Kniazkov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cqfn.astranaut.core.algorithms;

import org.cqfn.astranaut.core.DifferenceNode;
import org.cqfn.astranaut.core.Node;
import org.cqfn.astranaut.core.algorithms.mapping.BottomUpMapper;
import org.cqfn.astranaut.core.example.LittleTrees;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* Testing {@link DifferenceTreeBuilder} class.
*
* @since 1.1.0
*/
class DifferenceTreeBuilderTest {
/**
* Testing the construction of a difference tree with a deleted node.
*/
@Test
void testTreeWithDeletedNode() {
final Node before = LittleTrees.createStatementListWithThreeChildren();
final Node after = LittleTrees.createStatementListWithTwoChildren();
final DifferenceTreeBuilder builder = new DifferenceTreeBuilder(before);
final boolean result = builder.build(after, new BottomUpMapper());
Assertions.assertTrue(result);
final DifferenceNode diff = builder.getRoot();
final Node expected = LittleTrees.createTreeWithDeleteAction();
Assertions.assertTrue(expected.deepCompare(diff));
Assertions.assertTrue(before.deepCompare(diff.getBefore()));
Assertions.assertTrue(after.deepCompare(diff.getAfter()));
}
}

0 comments on commit e213ef5

Please sign in to comment.