/**
 * Copyright (C) 2015 - present by OpenGamma Inc. and the OpenGamma group of companies
 * 
 * Please see distribution for license.
 */
package com.opengamma.strata.examples.regression;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.testng.Assert;

import com.opengamma.strata.collect.Messages;

/**
 * Utility class for trade report regression tests.
 */
public final class TradeReportRegressionTestUtils {
  
  /**
   * Restricted constructor.
   */
  private TradeReportRegressionTestUtils() {
  }
  
  public static void assertAsciiTableEquals(String actual, String expected) {
    List<String> actualLines = toLines(actual);
    List<String> expectedLines = toLines(expected);

    int maxLines = Math.max(actualLines.size(), expectedLines.size());
    
    for (int i = 0; i < maxLines; i++) {
      if (i >= actualLines.size()) {
        String expectedLine = expectedLines.get(i);
        Assert.fail(Messages.format("No more results but expected:\n{}", expectedLine));
      }
      if (i >= expectedLines.size()) {
        String actualLine = actualLines.get(i);
        Assert.fail(Messages.format("Expected end of results but got:\n{}", actualLine));
      }
      String actualLine = actualLines.get(i);
      String expectedLine = expectedLines.get(i);
      if (!actualLine.equals(expectedLine)) {
        if (isDataRow(expectedLine) && isDataRow(actualLine)) {
          List<String> actualCells = toCells(actualLine);
          List<String> expectedCells = toCells(expectedLine);
          Assert.assertEquals(actualCells, expectedCells, "Mismatch at line " + i);
        } else {
          Assert.fail(Messages.format(
              "Mismatch at line {}:\n" +
              "Expected:\n" +
              "{}\n" +
              "Got:\n" +
              "{}", i, expectedLine, actualLine));
        }
      }
    }
  }
  
  private static List<String> toLines(String asciiTable) {
    return Arrays.asList(asciiTable.split(System.lineSeparator())).stream()
        .filter(line -> StringUtils.isNotBlank(line))
        .collect(Collectors.toList());
  }
  
  private static boolean isDataRow(String asciiLine) {
    return asciiLine.contains("|");
  }
  
  private static List<String> toCells(String asciiLine) {
    return Arrays.asList(asciiLine.split("\\|"));
  }
  
}

@@ -499,19 +499,17 @@
 Utils %7B%0A
-  
 %0A
+
   /**%0A  
@@ -590,19 +590,17 @@
 ) %7B%0A  %7D%0A
-  
 %0A
+
   public
@@ -845,13 +845,9 @@
 ));%0A
-    
 %0A
+
     
@@ -1775,16 +1775,20 @@
         
+    
 %22Expecte
@@ -1791,24 +1791,28 @@
 ected:%5Cn%22 +%0A
+    
             
@@ -1836,16 +1836,20 @@
         
+    
 %22Got:%5Cn%22
@@ -1847,24 +1847,28 @@
  %22Got:%5Cn%22 +%0A
+    
             
@@ -1930,27 +1930,25 @@
 %7D%0A    %7D%0A  %7D%0A
-  
 %0A
+
   private st
@@ -2040,30 +2040,17 @@
 lit(
-System.lineSeparator()
+%22%5C%5Cr?%5C%5Cn%22
 )).s
@@ -2150,27 +2150,25 @@
 ist());%0A  %7D%0A
-  
 %0A
+
   private st
@@ -2250,19 +2250,17 @@
 %22);%0A  %7D%0A
-  
 %0A
+
   privat
@@ -2363,13 +2363,11 @@
 ));%0A  %7D%0A
-  
 %0A%7D%0A

Fix test to work on either line ending