Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurent RENARD authored and Laurent RENARD committed Oct 18, 2019
1 parent cf87e18 commit 68ae33c
Show file tree
Hide file tree
Showing 9 changed files with 298 additions and 266 deletions.
5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
},
"license": "MIT",
"dependencies": {
"@lorenzofox3/for-await": "^0.2.0",
"fast-deep-equal": "^2.0.1"
},
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion src/assertion.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {defaultTestOptions, noop, tester} from './test';
import {tester} from './test';
import {Assert, AssertionFunction, AssertionResult, Operator, Test} from './interfaces';
import equal from 'fast-deep-equal';
import {defaultTestOptions, noop} from './commons';

export const isAssertionResult = (result: Test | AssertionResult): result is AssertionResult => {
return 'operator' in result;
Expand Down
82 changes: 82 additions & 0 deletions src/commons.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import {assertionMessage, bailout, endTestMessage, startTestMessage} from './protocol';
import {counter, delegateToCounter} from './counter';
import {Message} from './interfaces';

export const defaultTestOptions = Object.freeze({
offset: 0,
skip: false,
runOnly: false
});

export const noop = () => {
};

export const TesterPrototype = {
[Symbol.asyncIterator]: async function* () {
await this.routine;
for (const assertion of this.assertions) {
if (assertion[Symbol.asyncIterator]) {
// Sub test
yield startTestMessage({description: assertion.description}, this.offset);
yield* assertion;
if (assertion.error !== null) {
// Bubble up the error and return
this.error = assertion.error;
this.pass = false;
return;
}
}
yield assertionMessage(assertion, this.offset);
this.pass = this.pass && assertion.pass;
this.counter.update(assertion);
}

return this.error !== null ?
yield bailout(this.error, this.offset) :
yield endTestMessage(this, this.offset);
}
};

export const testerLikeProvider = (BaseProto = TesterPrototype) => (assertions: Array<any>, routine: Promise<any>, offset: number) => {
const testCounter = counter();
const withTestCounter = delegateToCounter(testCounter);
let pass = true;

return withTestCounter(Object.create(BaseProto, {
routine: {
value: routine
},
assertions: {
value: assertions
},
offset: {
value: offset
},
counter: {
value: testCounter
},
length: {
get() {
return assertions.length;
}
},
pass: {
enumerable: true,
get() {
return pass;
},
set(val) {
pass = val;
}
}
}));

};

export const testerFactory = testerLikeProvider();

export const map = fn => async function* (stream: AsyncIterable<Message<any>>) {
for await (const m of stream) {
yield fn(m);
}
};
59 changes: 17 additions & 42 deletions src/harness.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,33 @@
import {assert} from './assertion';
import {assertionMessage, endTestMessage, startTestMessage} from './protocol';
import {mochaTapLike, tapeTapLike} from './reporter';
import {counter, delegateToCounter} from './counter';
import {Test, TestHarness, TestHarnessConfiguration} from './interfaces';
import {TestHarness, TestHarnessConfiguration} from './interfaces';
import {testerLikeProvider, TesterPrototype} from './commons';

export const harnessFactory = ({runOnly = false, indent = false}: TestHarnessConfiguration = {
runOnly: false,
indent: false
}): TestHarness => {
const tests: Test[] = [];
const testCounter = counter();
const withTestCounter = delegateToCounter(testCounter);
const tests = [];
const rootOffset = 0;
const collect = item => tests.push(item);
const api = assert(collect, rootOffset, runOnly);
let error = null;

let pass = true;
let id = 0;
const factory = testerLikeProvider(Object.assign(api, TesterPrototype, {
report: async function (reporter) {
const rep = reporter || (indent ? mochaTapLike : tapeTapLike);
return rep(this);
}
}));

const instance = Object.create(api, {
length: {
get() {
return tests.length;
}
},
pass: {
return Object.defineProperties(factory(tests, Promise.resolve(), rootOffset), {
error: {
get() {
return pass;
return error;
},
set(val) {
error = val;
}
}
});

return withTestCounter(Object.assign(instance, {
[Symbol.asyncIterator]: async function* () {
for (const t of tests) {
t.id = ++id;
if (t[Symbol.asyncIterator]) {
// Sub test
yield startTestMessage({description: t.description}, rootOffset);
yield* t;
if (t.error !== null) {
pass = false;
return;
}
}
yield assertionMessage(t, rootOffset);
pass = pass && t.pass;
testCounter.update(t);
}
yield endTestMessage(this, 0);
},
report: async (reporter) => {
const rep = reporter || (indent ? mochaTapLike : tapeTapLike);
return rep(instance);
}
}));
};
};
14 changes: 7 additions & 7 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ export interface Counter {
count: number;
}

export interface Test extends AsyncIterable<Message<any>>, TestResult, Counter {
readonly routine: Promise<any>;
readonly length: number;
readonly error?: any;
}

export interface TestCounter extends Counter {
update(assertion: Test | AssertionResult): void;
}
Expand Down Expand Up @@ -153,10 +159,4 @@ export type AssertionMessage = Message<Test | AssertionResult>;

export type TestEndMessage = Message<Test>

export type BailoutMessage = Message<Error>;

export interface Test extends AsyncIterable<Message<any>>, TestResult, Counter {
readonly routine: Promise<any>;
readonly length: number;
readonly error?: any;
}
export type BailoutMessage = Message<Error>;
Loading

0 comments on commit 68ae33c

Please sign in to comment.