SyntacticEquivalence { public static boolean areEquivalent(List<? extends Tree> leftList, List<? extends Tree> rightList) { if (leftList.size() != rightList.size() || leftList.isEmpty()) { return false; } for (int i = 0; i < leftList.size(); i++) { Tree left = leftList.get(i); Tree right = rightList.get(i); if (!areEquivalent(left, right)) { return false; } } return true; } private  SyntacticEquivalence(); static boolean areEquivalent(List<? extends Tree> leftList, List<? extends Tree> rightList); static boolean areEquivalent(@Nullable Tree leftNode, @Nullable Tree rightNode); static ExpressionTree skipParentheses(ExpressionTree tree);  }
@Test public void test() throws Exception { Tree tree1 = parse("if a then end if;", Tree.Kind.IF_STATEMENT); Tree tree2 = parse("if a then end if;", Tree.Kind.IF_STATEMENT); Tree tree3 = parse("if b then end if;", Tree.Kind.IF_STATEMENT); Tree tree4 = parse("if b then leave aaa; end if;", Tree.Kind.IF_STATEMENT); Tree tree5 = parse("DECLARE a CHAR;", Tree.Kind.DECLARE_STATEMENT); assertThat(SyntacticEquivalence.areEquivalent(tree1, tree1)).isTrue(); assertThat(SyntacticEquivalence.areEquivalent(tree1, tree2)).isTrue(); assertThat(SyntacticEquivalence.areEquivalent(tree1, tree3)).isFalse(); assertThat(SyntacticEquivalence.areEquivalent(tree1, tree4)).isFalse(); assertThat(SyntacticEquivalence.areEquivalent(tree1, tree4)).isFalse(); assertThat(SyntacticEquivalence.areEquivalent(tree1, null)).isFalse(); assertThat(SyntacticEquivalence.areEquivalent(tree1, tree5)).isFalse(); }
@Test public void test_equivalence_for_tree_list() throws Exception { CallStatementTree tree1 = (CallStatementTree) parse("CALL f(a, b, c) ;", Tree.Kind.CALL_STATEMENT); CallStatementTree tree2 = (CallStatementTree) parse("CALL f(a, b, c) ;", Tree.Kind.CALL_STATEMENT); CallStatementTree tree3 = (CallStatementTree) parse("CALL f(a, b) ;", Tree.Kind.CALL_STATEMENT); CallStatementTree tree4 = (CallStatementTree) parse("CALL f(a, b, b) ;", Tree.Kind.CALL_STATEMENT); assertThat(SyntacticEquivalence.areEquivalent(tree1.parameterList(), tree1.parameterList())).isTrue(); assertThat(SyntacticEquivalence.areEquivalent(tree1.parameterList(), tree2.parameterList())).isTrue(); assertThat(SyntacticEquivalence.areEquivalent(tree1.parameterList(), tree3.parameterList())).isFalse(); assertThat(SyntacticEquivalence.areEquivalent(tree1.parameterList(), tree4.parameterList())).isFalse(); }
@Test public void test_equivalence_for_empty_tree_list() throws Exception { CallStatementTree tree1 = (CallStatementTree) parse("CALL f() ;", Tree.Kind.CALL_STATEMENT); CallStatementTree tree2 = (CallStatementTree) parse("CALL f() ;", Tree.Kind.CALL_STATEMENT); assertThat(SyntacticEquivalence.areEquivalent(tree1.parameterList(), tree1.parameterList())).isFalse(); assertThat(SyntacticEquivalence.areEquivalent(tree1.parameterList(), tree2.parameterList())).isFalse(); }
@Test public void test_equivalence_for_tokens() throws Exception { Tree tree1 = parse("IF TRUE THEN END IF;", Tree.Kind.IF_STATEMENT); Tree tree2 = parse("IF FALSE THEN END IF;", Tree.Kind.IF_STATEMENT); assertThat(SyntacticEquivalence.areEquivalent(tree1, tree1)).isTrue(); assertThat(SyntacticEquivalence.areEquivalent(tree1, tree2)).isFalse(); }