-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Laurent RENARD
authored and
Laurent RENARD
committed
Oct 18, 2019
1 parent
cf87e18
commit 68ae33c
Showing
9 changed files
with
298 additions
and
266 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
})); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.