UNPKG

frankie

Version:

A snailish mocha reporter

130 lines (100 loc) 2.87 kB
var Base = require('mocha').reporters.Base; var charm = require('./charm-wrapper'); var art = require('./art-loader'); class Frankie extends Base { constructor(runner) { super(runner); this.tests = 0; this.passes = 0; this.fails = 0; this.testHistory = []; this.snailHeight = art.snail1.height - 1; this.sunHeight = art.sun.height - 1; this.sunVerticalOffset = 5; this.minWidth = Math.floor(Base.window.width * 0.4); this.maxWidth = Math.floor(Base.window.width * 0.6); this.width = this.minWidth; this.snailState = 1; runner.on('start', this.onStart.bind(this)); runner.on('pass', this.onPass.bind(this)); runner.on('fail', this.onFail.bind(this)); runner.on('end', this.onEnd.bind(this)); } onStart() { Base.cursor.hide(); charm.clearVerticalSpace(this.snailHeight + this.sunVerticalOffset); charm.down(this.sunVerticalOffset); } onPass() { this.recordTest(true); this.draw(); } onFail() { this.recordTest(false); this.draw(); } onEnd() { Base.cursor.show(); charm.down(this.snailHeight); this.epilogue(); } recordTest(pass) { this.tests += 1; // increment `passes` or `failures` if (pass) this.passes += 1; else this.fails += 1; // add result to `testHistory` this.testHistory.push(pass); if (this.testHistory.length === this.width) { this.width = this.minWidth + Math.floor(Math.random() * (this.maxWidth - this.minWidth)); } else if (this.testHistory.length > this.width) { this.testHistory.shift(); if (this.tests % 3 === 2) this.testHistory.shift(); } // transition snail state (roughly) every 10 tests if (this.tests % 10 === 9) { this.transitionSnailState(); } } transitionSnailState() { if (this.snailState === 1 || this.snailState === 3) { this.snailState = 2; } else if (this.snailState === 2) { this.snailState = Math.random() < .5 ? 1 : 3; } } drawTrail() { const length = this.testHistory.length; let state; charm.down(this.snailHeight - 1); for (let i = 0; i < length; i++) { state = this.testHistory[i]; charm .foreground(state ? 'green' : 'red') .write(art.trail.art); } return charm .up(this.snailHeight - 1) .left(art.trail.width * length); } drawSnail(offset) { const snail = 'snail' + this.snailState; return charm .foreground('black') .writeOffset(offset, art[snail].art); } drawSun() { const sun = this.tests % 10 > 7 ? 'sun2' : 'sun'; charm .up(this.sunVerticalOffset + 1) .foreground('yellow') .drawArt(art[sun].art); } draw() { this.drawSnail(this.testHistory.length); this.drawSun(); this.drawTrail(); } } module.exports = Frankie;