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);  }
@Test public void test() throws Exception { Tree tree1 = parse("if (a) ;", Tree.Kind.IF_STATEMENT); Tree tree2 = parse("if (a) ;", Tree.Kind.IF_STATEMENT); Tree tree3 = parse("if (b) ;", Tree.Kind.IF_STATEMENT); Tree tree4 = parse("if (b) break;", Tree.Kind.IF_STATEMENT); Tree tree5 = parse("var a;", Tree.Kind.VAR_DECLARATION); 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 { ArgumentListTree tree1 = parse("f(a, b, c) ;", Tree.Kind.ARGUMENT_LIST); ArgumentListTree tree2 = parse("f(a, b, c) ;", Tree.Kind.ARGUMENT_LIST); ArgumentListTree tree3 = parse("f(a, b) ;", Tree.Kind.ARGUMENT_LIST); ArgumentListTree tree4 = parse("f(a, b, b) ;", Tree.Kind.ARGUMENT_LIST); assertThat(SyntacticEquivalence.areEquivalent(tree1.arguments(), tree1.arguments())).isTrue(); assertThat(SyntacticEquivalence.areEquivalent(tree1.arguments(), tree2.arguments())).isTrue(); assertThat(SyntacticEquivalence.areEquivalent(tree1.arguments(), tree3.arguments())).isFalse(); assertThat(SyntacticEquivalence.areEquivalent(tree1.arguments(), tree4.arguments())).isFalse(); }
@Test public void test_equivalence_for_empty_tree_list() throws Exception { ArgumentListTree tree1 = parse("f() ;", Tree.Kind.ARGUMENT_LIST); ArgumentListTree tree2 = parse("f() ;", Tree.Kind.ARGUMENT_LIST); assertThat(SyntacticEquivalence.areEquivalent(tree1.arguments(), tree1.arguments())).isFalse(); assertThat(SyntacticEquivalence.areEquivalent(tree1.arguments(), tree2.arguments())).isFalse(); }
@Test public void test_equivalence_for_tokens() throws Exception { Tree tree1 = parse("true;", Tree.Kind.BOOLEAN_LITERAL); Tree tree2 = parse("false;", Tree.Kind.BOOLEAN_LITERAL); assertThat(SyntacticEquivalence.areEquivalent(tree1, tree1)).isTrue(); assertThat(SyntacticEquivalence.areEquivalent(tree1, tree2)).isFalse(); }