br.com.objectos.testing.LineAssert Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of assertion Show documentation
Show all versions of assertion Show documentation
objectos assertion library
/*
* Copyright 2015 Objectos, Fábrica de Software LTDA.
*
* 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 br.com.objectos.testing;
import static com.google.common.collect.Lists.newArrayList;
import java.util.Arrays;
import java.util.List;
import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import org.hamcrest.Description;
import org.hamcrest.SelfDescribing;
/**
* @author [email protected] (Marcio Endo)
*/
class LineAssert implements SelfDescribing {
public static final String sep = System.getProperty("line.separator");
private final List invalidList;
private LineAssert(List invalidList) {
this.invalidList = invalidList;
}
public static LineAssert get(List actualLines, List expectedLines) {
List invalidList = newArrayList();
for (int i = 0; i < expectedLines.size(); i++) {
String exp = expectedLines.get(i);
String res = "";
if (i < actualLines.size()) {
res = actualLines.get(i);
}
Line line = new Line(i, exp, res);
if (!line.valid()) {
invalidList.add(line);
}
}
return new LineAssert(invalidList);
}
@Override
public void describeTo(Description description) {
description.appendText("Found the following error(s):" + sep);
String text = Joiner.on(sep).join(invalidList);
description.appendText(text);
}
public boolean valid() {
return invalidList.isEmpty();
}
private static class Line {
private final int index;
private int errors;
private final char[] exp;
private final char[] out;
private final char[] top;
private final char[] bot;
public Line(int index, String res, String ans) {
this.index = index;
exp = ans.toCharArray();
out = Strings.nullToEmpty(res).toCharArray();
top = new char[exp.length + 1];
bot = new char[exp.length + 1];
Arrays.fill(top, ' ');
Arrays.fill(bot, ' ');
for (int i = 0; i < exp.length; i++) {
if (!isEqual(exp, out, i)) {
errors++;
top[i] = 'v';
bot[i] = '^';
}
}
if (out.length > exp.length) {
errors++;
top[exp.length] = 'v';
bot[exp.length] = '^';
}
}
public boolean valid() {
return errors == 0;
}
@Override
public String toString() {
return new StringBuilder()
.append(errors)
.append(" error(s) at line ")
.append(index + 1)
.append(":")
.append(sep)
.append(top)
.append(sep)
.append(out)
.append(sep)
.append(exp)
.append(sep)
.append(bot)
.append(sep)
.toString();
}
private boolean isEqual(char[] exp, char[] out, int i) {
boolean equal = false;
if (i < out.length) {
char expChar = exp[i];
char outChar = out[i];
equal = expChar == outChar;
}
return equal;
}
}
}