/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.facebook.presto.sql.testing;

import com.facebook.presto.sql.parser.ParsingException;
import com.facebook.presto.sql.parser.SqlParser;
import com.facebook.presto.sql.tree.DefaultTraversalVisitor;
import com.facebook.presto.sql.tree.Node;
import com.facebook.presto.sql.tree.Statement;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;

import javax.annotation.Nullable;

import java.util.List;

import static com.facebook.presto.sql.SqlFormatter.formatSql;
import static java.lang.String.format;

public final class TreeAssertions
{
    private TreeAssertions() {}

    public static void assertFormattedSql(SqlParser sqlParser, Node expected)
    {
        String formatted = formatSql(expected);

        // verify round-trip of formatting already-formatted SQL
        Statement actual = parseFormatted(sqlParser, formatted, expected);
        assertEquals(formatSql(actual), formatted);

        // compare parsed tree with parsed tree of formatted SQL
        if (!actual.equals(expected)) {
            // simplify finding the non-equal part of the tree
            assertListEquals(linearizeTree(actual), linearizeTree(expected));
        }
        assertEquals(actual, expected);
    }

    private static Statement parseFormatted(SqlParser sqlParser, String sql, Node tree)
    {
        try {
            return sqlParser.createStatement(sql);
        }
        catch (ParsingException e) {
            throw new AssertionError(format(
                    "failed to parse formatted SQL: %s\nerror: %s\ntree: %s",
                    sql, e.getMessage(), tree));
        }
    }

    private static List<Node> linearizeTree(Node tree)
    {
        final ImmutableList.Builder<Node> nodes = ImmutableList.builder();
        new DefaultTraversalVisitor<Node, Void>()
        {
            @Override
            public Node process(Node node, @Nullable Void context)
            {
                Node result = super.process(node, context);
                nodes.add(node);
                return result;
            }
        }.process(tree, null);
        return nodes.build();
    }

    private static <T> void assertListEquals(List<T> actual, List<T> expected)
    {
        if (actual.size() != expected.size()) {
            Joiner joiner = Joiner.on("\n    ");
            throw new AssertionError(format(
                    "Lists not equal%nActual [%s]:%n    %s%nExpected [%s]:%n    %s%n",
                    actual.size(), joiner.join(actual),
                    expected.size(), joiner.join(expected)));
        }
        assertEquals(actual, expected);
    }

    private static <T> void assertEquals(T actual, T expected)
    {
        if (!actual.equals(expected)) {
            throw new AssertionError(format("expected [%s] but found [%s]", expected, actual));
        }
    }
}

@@ -2847,128 +2847,487 @@
     
-Joiner joiner = Joiner.on(%22%5Cn    %22);%0A            throw new AssertionError(format(%0A                    %22Lists not equal%25n
+throw new AssertionError(format(%22Lists not equal in size%25n%25s%22, formatLists(actual, expected)));%0A        %7D%0A        if (!actual.equals(expected)) %7B%0A            throw new AssertionError(format(%22Lists not equal at index %25s%25n%25s%22,%0A                    differingIndex(actual, expected), formatLists(actual, expected)));%0A        %7D%0A    %7D%0A%0A    private static %3CT%3E String formatLists(List%3CT%3E actual, List%3CT%3E expected)%0A    %7B%0A        Joiner joiner = Joiner.on(%22%5Cn    %22);%0A        return format(%22
 Actu
@@ -3371,20 +3371,16 @@
  %25s%25n%22,%0A
-    
         
@@ -3439,20 +3439,16 @@
         
-    
 expected
@@ -3482,58 +3482,284 @@
 ed))
-)
 ;%0A    
+%7D%0A%0A
     
-%7D%0A        assertEquals(actual, expected
+private static %3CT%3E int differingIndex(List%3CT%3E actual, List%3CT%3E expected)%0A    %7B%0A        for (int i = 0; i %3C actual.size(); i++) %7B%0A            if (!actual.get(i).equals(expected.get(i))) %7B%0A                return i;%0A            %7D%0A        %7D%0A        return actual.size(
 );%0A 

Improve TreeAssertions for differing lists