java/ch/wlkl/env/IOCompare.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ch.wlkl.env;

import static ch.wlkl.env.Ut.xQuote;
import java.util.ArrayList;

/**
 *
 * @author walter
 */
public class IOCompare extends IOTest {

    String[] comTx = null;
    ArrayList<String> newTx = new ArrayList();
    int cIx = -1;
    boolean noComp = false;

    public void begin(String nm, String... c) {
        begin(nm);
        comTx = c;
        cm1 = c == null || c.length == 0 ? -9 : 0 ;
    }
    
    
    public void outln(String s) {
        if (cm1 < 0) {
            super.outln(s);
            return;
        }
        int cx = -2, cy;
        for (cy = -1; cy > cx; ) {
            cx = cy + 1;
            cy = s.indexOf('\n', cx);
            String one = cy >= cx ? s.substring(cx, cy) : s.substring(cx);
            String old;
            int ix = newTx.size();
            newTx.add(one);
            if (comTx == null)
                out("null");
            if (ix == comTx.length) {
                errNC("more new lines");
            }
            super.outln(one);
            if (ix < comTx.length && !one.equals(old = comTx[ix])) {
                int dx = 0, dE = Integer.min(one.length(), old.length());
                for (; dx < dE && one.charAt(dx) == old.charAt(dx); dx++) {
                }
                errNC("new line above not equal old line below. First diff at " + dx, dx);
                outFi(comTx[ix]);
            }
        }
    }

    // error no compare: avoid ix is moved and all following lines get compare errors
    public void errNC(String s, int cx) {
        int cy = cx - 2;
        s = "### error " + s + " #########";
        if (cy >= s.length()) {
            s = s + Ut.spaces(cy - s.length()) + " >!< ";
        } else if (cy >= 9) {
            s = s.substring(0, cy) + " >!< " + s.substring(cy);
        }
        outFi(s);
        cm1++;
    }

    public void errNC(String s) {
        errNC(s, 0);
    }

    public void end() {
        if (cm1 >= 0 && comTx.length != newTx.size()) {
            errNC((newTx.size() > comTx.length ? "more" : "less") + " new lines " + newTx.size() + " than old " +  comTx.length);
        }
        comTx = null;
        end1();
        if (cm1 > 0) {
            outFi("*** new output ***");
            outFi("t.begin(" + xQuote(name));
            for (String l : newTx) {
                outFi("  , " + xQuote(l));
            }
            outFi("  );");
        }
        end2();
        newTx.clear();
    }
}