java/ch/wlkl/wsh/Test.java
package ch.wlkl.wsh;
import static ch.wlkl.wsh.Strings.fill;
import static ch.wlkl.wsh.Strings.xQuote;
import java.io.File;
import java.util.ArrayList;
/**
* @author Keller
* test Infrasturcture and all tests for package shBuf
*
*/
public class Test extends Top {
public static final String tempPath(String src) {
return (tempDir + "/" + src).replaceAll("\\\\", File.separator).replaceAll("/", File.separator);
}
public static final String mTit = "--- ";
public static final String mErr = "*** ";
public final static String tempDir = "tmpelieli";
char state = 'o';
int errors = 0;
ArrayList<String> output = new ArrayList<String>();
final ArrayList<String> compare = new ArrayList<String>();
int totalErrs = 0;
int totalFirst = -1;
final ArrayList<String> totalNames = new ArrayList<String>();
public Test() {
}
public void begin(String name, String txt) {
totalNames.add(name);
output.clear();
compare.clear();
state = 'o';
errors = 0;
out("--- --- test begin " + name + (txt == null ? "" : " " + txt));
}
public void compare(String... c) {
int oldSize = compare.size();
Ut.addAll(compare, c);
if (oldSize == 0 && state == 'o') {
ArrayList<String> old = output;
output = new ArrayList<String>();
state = 'c';
for (String o : old)
out(o);
}
}
public void end(String txt) {
String r = "";
out(mTit + mTit + "test end " + totalNames.get(totalNames.size() - 1) + " " + (txt == null ? "" : txt));
if (output.size() < compare.size()) {
err("more compare lines (" + compare.size() + ") than output (" + output.size() +")");
for (int ix = compare.size(); ix < (compare.size() < output.size() + 5 ? compare.size(): output.size() + 5); ix++)
say(" " + ix +": " + compare.get(ix));
}
for (String s: output)
r = r + ", " + xQuote(s);
say("output(" + (r.equals("") ? "" : r.substring(2)) + ");");
r = "";
for (String s: compare)
r = r + ", " + xQuote(s);
say("compare(" + (r.equals("") ? "" : r.substring(2)) + ");");
say(mTit + (errors == 0 ? "---" : "***") + " test end " + totalNames.get(totalNames.size() - 1) + ": " + errors + " errors");
total(errors);
}
public void out(String msg) {
int d, ix = output.size();
if (state != 's')
output.add(msg);
if (state != 'c') {
} else if (output.size() > compare.size()) {
if (output.size() == compare.size() + 1)
err("more new outputlines than " + compare.size() + " compare lines");
} else if (! msg.equals(compare.get(ix))) {
String cl = compare.get(ix);
for (d=0;d < msg.length() && d < cl.length() && msg.charAt(d) == cl.charAt(d) ;d++);
String e = mErr + "compare line " + ix + "(next) differs from output (overnext) at pos " + d;
if (d >= e.length()+2)
e += fill(" ", d-e.length() - 2) + " >*< ";
else if (d > 4)
e = e.substring(0, d-2) + " >*< " + e.substring(d-2);
say(e);
say(cl);
errors++;
}
if (state != 'o')
say(msg);
}
public void say(String msg) {
System.out.println(msg);
}
public void err(String msg) {
say(mErr + "error: " + msg);
errors++;
}
public String writeTestFiles(String... names) {
FileRW wf = new FileRW();
for (int ix=0; ix < names.length; ix++) {
wf.reset(tempPath(names[ix]));
wf.open("w");
Ut.write(wf, "test file " + ix + " of " + names.length, "name = " + names[ix], "]end of test file " + ix + ": " + names[ix]);
wf.close();
}
return tempDir;
}
public void removeTestFiles() {
removeRecursive(new File(tempDir));
}
public void removeRecursive(File f) {
if (f.isDirectory()) {
File [] li = f.listFiles();
for (int ix=0; ix < li.length; ix++)
removeRecursive(li[ix]);
}
if (! f.delete())
say("could not delete " + f);
}
public String timestamp(String...args) {
String r = new java.util.Date().toString();
if (args == null || args.length < 1) {
} else if ("fix".equals(args[0])) {
r = Strings.cut(r.replace(' ', '+'), 40, "+");
} else {
fail("timestamp("+args[0]+")");
}
return r;
}
public void total(int errs) {
totalErrs += errs;
if (totalFirst < 0 && errs > 0)
totalFirst = totalNames.size()-1;
}
public void total() {
say("--- --- " + totalNames.size() + " tests, with total " + totalErrs + " errors" + (totalFirst < 0 ? "" : " first in " + totalFirst + ": " + totalNames.get(totalFirst)));
}
public static void main(String... args) {
Test t = new Test();
t.compare("--- test begin test of Test 1", "eins", "zwei", "drei doch ziemlich langggggggggggggggggg...!", "--- test end test of Test 1 test of Test 1");
t.begin("test of Test 1", null);
t.out("eins");
t.out("zwei");
t.out("drei doch ziemlich langggggggggggggggggg...!");
t.end("test of Test 1");
t.total();
}
}