All files / tap-bark/src/output output.ts

100% Statements 47/47
100% Branches 2/2
100% Functions 9/9
100% Lines 45/45
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108      1x   1x           83x 83x     1x   53x 53x     1x   13x     13x     13x     13x     13x     1x   155x     155x     155x     155x     155x     1x   167x     167x   167x   167x   167x   167x 1661x     167x 1679x       167x     167x     167x     37x 37x   37x 37x 37x 37x   37x 36x 88x 88x         1x 6x     1x  
import { IStream } from "../stream/stream.i";
import { IOutputProvider } from "../output-provider/output-provider.i";
import { IResults } from "../results.i";
import { ResultType } from "../result-type";
 
export class Output {
 
    private _stream: IStream;
    private _outputProvider: IOutputProvider;
 
    constructor(stream: IStream, outputProvider: IOutputProvider) {
        this._stream = stream;
        this._outputProvider = outputProvider;
    }
 
    public setup(): void {
        // set up the two empty lines to hold fixture info and test case info
        this._stream.writeLine("");
        this._stream.writeLine("");
    }
 
    public setFixtureName(name: string): void {
        // move up two rows
        this._stream.moveCursor(0, -3);
 
        // clear the line
        this._stream.clearLine();
 
        // write the new name
        this._stream.write(name);
 
        // move down two rows
        this._stream.moveCursor(0, 3);
 
        // set the cursor to 0 x (all the way left), we don't want to move it up or down
        this._stream.cursorTo(0, undefined);
    }
 
    public setTestName(name: string): void {
        // move up one rows
        this._stream.moveCursor(0, -2);
 
        // clear the line
        this._stream.clearLine();
 
        // write the new name
        this._stream.write(name);
 
        // move down one rows
        this._stream.moveCursor(0, 2);
 
        // set the cursor to 0 x (all the way left), we don't want to move it up or down
        this._stream.cursorTo(0, undefined);
    }
 
    public setProgress(current: number, total: number): void {
        // move up three rows
        this._stream.moveCursor(0, -1);
 
        // clear the line
        this._stream.clearLine();
 
        let progressBar = "";
 
        const progressResolution = 20;
 
        const progressGap = Math.round(current / total * progressResolution);
 
        while (progressBar.length < progressGap) {
           progressBar += "=";
        }
 
        while (progressBar.length < progressResolution) {
           progressBar += " ";
        }
 
        // write the new name
        this._stream.write("|" + progressBar + "|");
 
        // move down three rows
        this._stream.moveCursor(0, 1);
 
        // set the cursor to 0 x (all the way left), we don't want to move it up or down
        this._stream.cursorTo(0, undefined);
    }
 
    public outputResults(results: IResults): void {
        let total = results.pass + results.fail + results.ignore;
 
        this._stream.writeLine("");
        this._stream.writeLine(this._outputProvider.getResultMessage(ResultType.PASS, results.pass, total));
        this._stream.writeLine(this._outputProvider.getResultMessage(ResultType.FAIL, results.fail, total));
        this._stream.writeLine(this._outputProvider.getResultMessage(ResultType.IGNORE, results.ignore, total));
 
        if (results.failures) {
            results.failures.forEach(f => {
                this._stream.writeLine("");
                this._stream.writeLine(this._outputProvider.getFailureMessage(f));
            });
        }
    }
 
    public getStream(): IStream {
        return this._stream;
    }
 
}