Skip to content
This repository has been archived by the owner on Aug 22, 2022. It is now read-only.

Commit

Permalink
use es module in codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzofox3 committed Nov 1, 2020
1 parent 5b22db9 commit 5384b57
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 81 deletions.
12 changes: 6 additions & 6 deletions src/output_stream.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
const {EOL} = require('os');
import {EOL} from 'os';

const delegate = exports.delegate = (...methods) => target => {
export const delegate = (...methods) => target => {
const output = {};

for (const m of methods) {
output[m] = (...args) => target[m](...args);
}

return output;
};

const delegateTTY = delegate('write', 'clearLine', 'cursorTo', 'moveCursor', 'end');

exports.output = stream => {
export const output = stream => {
return Object.assign(delegateTTY(stream), {
writeLine(message = '', padding = 0) {
this.write(' '.repeat(padding) + message + EOL);
Expand All @@ -22,4 +22,4 @@ exports.output = stream => {
this.writeLine(message, padding);
}
});
};
};
48 changes: 24 additions & 24 deletions src/reporters/default_reporter.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const {EOL} = require('os');
const {output} = require('../output_stream.js');
const {paint} = require('./theme.js');
const {testFile} = require('./test_file.js');
const {getDiagnosticReporter} = require('./diagnostic.js');
import {EOL} from 'os';
import {output} from '../output_stream.js';
import {paint} from './theme.js';
import {testFile} from './test_file.js';
import {getDiagnosticReporter} from './diagnostic.js';

const printHeader = (message, out) => {
const header = message.toUpperCase();
Expand All @@ -13,12 +13,12 @@ const printFailures = (tests, out) => {
const failing = tests
.filter(t => t.failureList.length)
.reduce((acc, curr) => acc.concat([...curr]), []);

if (failing.length === 0) {
out.writeLine('N/A', 2);
return;
}

failing.forEach((failure, index) => {
const data = failure.data;
const [file, ...testPath] = failure.path;
Expand All @@ -27,50 +27,50 @@ const printFailures = (tests, out) => {
out.writeLine(`${paint.adornment('at')} ${paint.stackTrace(data.at)}`, 4);
getDiagnosticReporter(data).report(out);
out.writeLine(out.adornment('_'.repeat(out.width)));

});
};

const printFooter = (tests, out) => {
const skipped = tests.reduce((acc, curr) => acc + curr.skip, 0);
const failure = tests.reduce((acc, curr) => acc + curr.failure, 0);
const success = tests.reduce((acc, curr) => acc + curr.success, 0);

out.writeLine(paint.summaryPass(success), 1);
out.writeLine(paint.summarySkip(skipped), 1);
out.writeLine(paint.summaryFail(failure), 1);

out.writeLine(`${EOL}`);
};

const isAssertionResult = result => 'operator' in result;

exports.defaultReporter = (theme = paint, stream = process.stdout) => {

export default (theme = paint, stream = process.stdout) => {
const out = Object.assign(output(stream), theme, {
width: stream.columns || 80
});

return async stream => {
const tests = [];
let testLines = 0;
let pass = true;

printHeader('tests files', out);
out.writeLine();

for await (const message of stream) {
const current = tests[tests.length - 1];
const {data, offset, type} = message;

if (type === 'BAIL_OUT') {
throw data;
}

if (type === 'TEST_END' && offset > 0) {
current.goOut();
}

if (type === `TEST_START`) {
if (offset === 0) {
testLines++;
Expand All @@ -81,7 +81,7 @@ exports.defaultReporter = (theme = paint, stream = process.stdout) => {
current.goIn(data.description);
}
}

if (type === `ASSERTION`) {
if (isAssertionResult(data) || data.skip) {
pass = pass && data.pass;
Expand All @@ -98,16 +98,16 @@ exports.defaultReporter = (theme = paint, stream = process.stdout) => {
tests[tests.length - 1].writeLine();
}
}

}

printHeader('failures', out);
printFailures(tests, out);

printHeader('summary', out);

out.writeLine();

printFooter(tests, out);
};
};
50 changes: 25 additions & 25 deletions src/reporters/diagnostic.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,79 @@
const {diffChars, diffJson} = require('diff');
const {EOL} = require('os');
import {diffChars, diffJson} from 'diff';
import {EOL} from 'os';

const valToTypeString = exports.valToTypeString = val => {
export const valToTypeString = val => {
const type = typeof val;
switch (type) {
case 'object': {
if (val === null) {
return null;
}

if (Array.isArray(val)) {
return 'array';
}

return 'object';
}
default:
return type;
}
};

const truthyDiagnostic = exports.truthyDiagnostic = diag => ({
export const truthyDiagnostic = diag => ({
report(out) {
let val = diag.actual;

if (val === '') {
val = '""';
}

if (val === undefined) {
val = 'undefined';
}

out.writeBlock(`expected a ${out.operator('TRUTHY')} value but got ${out.error(val)}`, 4);
}
});

const falsyDiagnostic = exports.falsyDiagnostic = diag => ({
export const falsyDiagnostic = diag => ({
report(out) {
out.writeBlock(`expected a ${out.operator('FALSY')} value but got ${out.error(JSON.stringify(diag.actual))}`, 4);
}
});

const notEqualDiagnostic = exports.notEqualDiagnostic = () => ({
export const notEqualDiagnostic = () => ({
report(out) {
out.writeBlock(`expected values ${out.operator('NOT TO BE EQUIVALENT')} but they are`, 4);
}
});

const unknownOperatorDiagnostic = exports.unknownOperatorDiagnostic = diag => ({
export const unknownOperatorDiagnostic = diag => ({
report(out) {
out.writeBlock(`(unknown operator: ${diag.operator})`, 4);
}
});

const isDiagnostic = exports.isDiagnostic = () => ({
export const isDiagnostic = () => ({
report(out) {
out.writeBlock(`expected values to point the ${out.operator('SAME REFERENCE')} but they don't`, 4);
}
});

const isNotDiagnostic = exports.isNotDiagnostic = () => ({
export const isNotDiagnostic = () => ({
report(out) {
out.writeBlock(`expected values to point ${out.operator('DIFFERENT REFERENCES')} but they point the same`, 4);
}
});

const countPadding = exports.countPadding = string => {
export const countPadding = string => {
let counter = 0;
let i = 0;

if (typeof string !== 'string') {
return 0;
}

for (i; i < string.length; i++) {
if (string[i] !== ' ') {
return counter;
Expand All @@ -97,7 +97,7 @@ const writeStringDifference = (out, actual, expected) => {
.filter(diff => !diff.removed)
.map(d => d.added ? out.diffAdd(d.value) : out.diffSame(d.value))
.join('');

out.writeBlock(`expected ${out.emphasis('string')} to be ${out.operator(expected)} but got the following differences:`, 4);
out.writeBlock(`${out.error('-')} ${toRemove}`, 4);
out.writeLine(`${out.success('+')} ${toAdd}`, 4);
Expand All @@ -116,7 +116,7 @@ const expandNewLines = exports.expandNewLines = (val, curr) => {
value: p.trim(),
padding: countPadding(p)
}));

val.push(...flatten);
return val;
};
Expand All @@ -135,18 +135,18 @@ const writeObjectLikeDifference = type => (out, actual, expected) => {
const lineDiffs = diff
.reduce(expandNewLines, [])
.map(printDiffLines);

out.writeBlock(`expected ${out.emphasis(type)} to be ${out.operator('EQUIVALENT')} but got the following differences:`, 4);
out.writeLine();
for (const l of lineDiffs) {
out.writeLine(l, 2);
}

};
const writeObjectDifference = writeObjectLikeDifference('objects');
const writeArrayDifference = writeObjectLikeDifference('arrays');

const equalDiagnostic = exports.equalDiagnostic = diag => {
export const equalDiagnostic = diag => {
const {actual, expected} = diag;
const expectedType = valToTypeString(expected);
const actualType = valToTypeString(actual);
Expand Down Expand Up @@ -176,12 +176,12 @@ const equalDiagnostic = exports.equalDiagnostic = diag => {
out.writeBlock(`expected ${out.emphasis(expectedType)} to be ${out.operator('EQUIVALENT')} but they are not`, 4);
}
}

}
};
};

exports.getDiagnosticReporter = diag => {
export const getDiagnosticReporter = diag => {
switch (diag.operator) {
case 'ok':
return truthyDiagnostic(diag);
Expand All @@ -198,4 +198,4 @@ exports.getDiagnosticReporter = diag => {
default:
return unknownOperatorDiagnostic(diag);
}
};
};
6 changes: 3 additions & 3 deletions src/reporters/log_reporter.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const {EOL} = require('os');
import {EOL} from 'os';

exports.logReporter = (out = process.stdout) => async stream => {
export const logReporter = (out = process.stdout) => async stream => {
for await (const message of stream) {
if (message.type === 'BAIL_OUT') {
throw message.data;
}
out.write(`${JSON.stringify(message)}${EOL}`);
}
};
};
14 changes: 7 additions & 7 deletions src/reporters/test_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ const TestFilePrototype = {
writeLine() {
this.out.clearLine(0);
this.out.cursorTo(0);

const statusSymbol = (this.failure > 0 ? ' ✖' : (this.skip > 0 ? ' ⚠' : ' ✔'));
const style = (this.failure > 0 ? 'failureBadge' : (this.skip > 0 ? 'skipBadge' : 'successBadge'));

let summaryString = `${this.success}/${this.total} `;

summaryString = `${statusSymbol}${summaryString.padStart(8)}`;

this.out.writeLine(`${this.out[style](summaryString)} ${this.out.path(this.file)}`, 1);
},
goIn(path) {
Expand All @@ -36,13 +36,13 @@ const TestFilePrototype = {
}
};

exports.testFile = (file, out) => {
export const testFile = (file, out) => {
let success = 0;
let failure = 0;
let skip = 0;
const path = [file];
const failureList = [];

return Object.create(TestFilePrototype, {
file: {
value: file
Expand Down Expand Up @@ -82,4 +82,4 @@ exports.testFile = (file, out) => {
path: {value: path},
failureList: {value: failureList}
});
};
};
Loading

0 comments on commit 5384b57

Please sign in to comment.