Created
February 1, 2017 03:20
-
-
Save thatalextaylor/3856c09a2e655e6359f478457b537225 to your computer and use it in GitHub Desktop.
CSV Hamcrest Matcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//package name | |
import org.apache.commons.csv.CSVFormat; | |
import org.apache.commons.csv.CSVParser; | |
import org.apache.commons.csv.CSVRecord; | |
import org.hamcrest.Description; | |
import org.hamcrest.Matcher; | |
import org.hamcrest.TypeSafeMatcher; | |
import java.io.IOException; | |
import java.io.StringReader; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class CSVMatcher { | |
public Matcher<? super String> equalToCsv(String expectedCsv, CSVFormat format) { | |
return new TypeSafeMatcher<String>() { | |
private List<String> actual = new ArrayList<>(); | |
private List<String> expected = new ArrayList<>(); | |
@Override | |
protected boolean matchesSafely(String actualCsv) { | |
boolean passed = true; | |
try { | |
CSVParser parser = new CSVParser(new StringReader(actualCsv), format); | |
List<CSVRecord> actualRecords = parser.getRecords(); | |
parser = new CSVParser(new StringReader(expectedCsv), format); | |
List<CSVRecord> expectedRecords = parser.getRecords(); | |
if (actualRecords.size() != expectedRecords.size()) { | |
actual.add(String.format("row count (%d)", actualRecords.size())); | |
expected.add(String.format("row count (%d)", expectedRecords.size())); | |
passed = false; | |
} | |
for (int i = 0; i < actualRecords.size(); i++) { | |
CSVRecord actualRecord = actualRecords.get(i); | |
if (i >= expectedRecords.size()) | |
break; | |
CSVRecord expectedRecord = expectedRecords.get(i); | |
if (actualRecord.size() != expectedRecord.size()) { | |
actual.add(String.format("column count (%d) on row %d", actualRecords.size(), i)); | |
expected.add(String.format("column count (%d) on row %d", expectedRecords.size(), i)); | |
passed = false; | |
} else { | |
for (int j = 0; j < actualRecord.size(); j++) { | |
String actualCell = actualRecord.get(j); | |
if (j >= expectedRecord.size()) | |
break; | |
String expectedCell = expectedRecord.get(j); | |
//Remove the 'trim' calls for exact matches | |
if (!actualCell.trim().equals(expectedCell.trim())) { | |
actual.add(String.format("value '%s' on row %d, column %d", actualCell, i, j)); | |
expected.add(String.format("value '%s' on row %d, column %d", expectedCell, i, j)); | |
passed = false; | |
} | |
} | |
} | |
} | |
} catch (IOException e) { | |
actual.add(e.toString()); | |
return false; | |
} | |
return passed; | |
} | |
@Override | |
public void describeTo(Description description) { | |
description.appendText(String.format("\"%s\"", String.join(" | ", expected))); | |
} | |
@Override | |
protected void describeMismatchSafely(String item, Description mismatchDescription) { | |
description.appendText(String.format("\"%s\"", String.join(" | ", actual))); | |
} | |
}; | |
} | |
} | |
//assertThat(csvDataAsString, equalToCsv(expectedCsv, CSVFormat.EXCEL)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment