|
1 |
| -import type { Fixtures, Test } from './types' |
| 1 | +import type { TestContext } from './types' |
| 2 | + |
| 3 | +export interface FixtureItem { |
| 4 | + prop: string |
| 5 | + value: any |
| 6 | + index: number |
| 7 | + /** |
| 8 | + * Indicates whether the fixture is a function |
| 9 | + */ |
| 10 | + isFn: boolean |
| 11 | + /** |
| 12 | + * The dependencies(fixtures) of current fixture function. |
| 13 | + */ |
| 14 | + deps?: FixtureItem[] |
| 15 | +} |
| 16 | + |
| 17 | +export function mergeContextFixtures(fixtures: Record<string, any>, context: { fixtures?: FixtureItem[] } = {}) { |
| 18 | + const fixtureArray: FixtureItem[] = Object.entries(fixtures) |
| 19 | + .map(([prop, value], index) => { |
| 20 | + const isFn = typeof value === 'function' |
| 21 | + return { |
| 22 | + prop, |
| 23 | + value, |
| 24 | + index, |
| 25 | + isFn, |
| 26 | + } |
| 27 | + }) |
| 28 | + |
| 29 | + if (Array.isArray(context.fixtures)) |
| 30 | + context.fixtures = context.fixtures.concat(fixtureArray) |
| 31 | + else |
| 32 | + context.fixtures = fixtureArray |
| 33 | + |
| 34 | + // Update dependencies of fixture functions |
| 35 | + fixtureArray.forEach((fixture) => { |
| 36 | + if (fixture.isFn) { |
| 37 | + const usedProps = getUsedProps(fixture.value) |
| 38 | + if (usedProps.length) |
| 39 | + fixture.deps = context.fixtures!.filter(({ index, prop }) => index !== fixture.index && usedProps.includes(prop)) |
| 40 | + } |
| 41 | + }) |
2 | 42 |
|
3 |
| -export function withFixtures(fn: Function, fixtures: Fixtures<Record<string, any>>, context: Test<Record<string, any>>['context']) { |
4 |
| - const props = getUsedFixtureProps(fn, Object.keys(fixtures)) |
| 43 | + return context |
| 44 | +} |
5 | 45 |
|
6 |
| - if (props.length === 0) |
| 46 | +export function withFixtures(fn: Function, fixtures: FixtureItem[], context: TestContext & Record<string, any>) { |
| 47 | + if (!fixtures.length) |
7 | 48 | return () => fn(context)
|
8 | 49 |
|
| 50 | + const usedProps = getUsedProps(fn) |
| 51 | + if (!usedProps.length) |
| 52 | + return () => fn(context) |
| 53 | + |
| 54 | + const usedFixtures = fixtures.filter(({ prop }) => usedProps.includes(prop)) |
| 55 | + const pendingFixtures = resolveDeps(usedFixtures) |
9 | 56 | let cursor = 0
|
10 | 57 |
|
11 | 58 | async function use(fixtureValue: any) {
|
12 |
| - context[props[cursor++]] = fixtureValue |
13 |
| - |
14 |
| - if (cursor < props.length) |
| 59 | + const { prop } = pendingFixtures[cursor++] |
| 60 | + context[prop] = fixtureValue |
| 61 | + if (cursor < pendingFixtures.length) |
15 | 62 | await next()
|
16 | 63 | else await fn(context)
|
17 | 64 | }
|
18 | 65 |
|
19 | 66 | async function next() {
|
20 |
| - const fixtureValue = fixtures[props[cursor]] |
21 |
| - typeof fixtureValue === 'function' |
22 |
| - ? await fixtureValue(use) |
23 |
| - : await use(fixtureValue) |
| 67 | + const { value } = pendingFixtures[cursor] |
| 68 | + typeof value === 'function' ? await value(context, use) : await use(value) |
24 | 69 | }
|
25 | 70 |
|
26 | 71 | return () => next()
|
27 | 72 | }
|
28 | 73 |
|
29 |
| -function getUsedFixtureProps(fn: Function, fixtureProps: string[]) { |
30 |
| - if (!fixtureProps.length || !fn.length) |
31 |
| - return [] |
| 74 | +function resolveDeps(fixtures: FixtureItem[], depSet = new Set<FixtureItem>(), pendingFixtures: FixtureItem[] = []) { |
| 75 | + fixtures.forEach((fixture) => { |
| 76 | + if (pendingFixtures.includes(fixture)) |
| 77 | + return |
| 78 | + if (!fixture.isFn || !fixture.deps) { |
| 79 | + pendingFixtures.push(fixture) |
| 80 | + return |
| 81 | + } |
| 82 | + if (depSet.has(fixture)) |
| 83 | + throw new Error('circular fixture dependency') |
32 | 84 |
|
33 |
| - const paramsStr = fn.toString().match(/[^(]*\(([^)]*)/)![1] |
| 85 | + depSet.add(fixture) |
| 86 | + resolveDeps(fixture.deps, depSet, pendingFixtures) |
| 87 | + pendingFixtures.push(fixture) |
| 88 | + depSet.clear() |
| 89 | + }) |
34 | 90 |
|
35 |
| - if (paramsStr[0] === '{' && paramsStr.at(-1) === '}') { |
36 |
| - // ({...}) => {} |
37 |
| - const props = paramsStr.slice(1, -1).split(',') |
38 |
| - const filteredProps = [] |
| 91 | + return pendingFixtures |
| 92 | +} |
39 | 93 |
|
40 |
| - for (const prop of props) { |
41 |
| - if (!prop) |
42 |
| - continue |
| 94 | +function getUsedProps(fn: Function) { |
| 95 | + const match = fn.toString().match(/[^(]*\(([^)]*)/) |
| 96 | + if (!match) |
| 97 | + return [] |
43 | 98 |
|
44 |
| - let _prop = prop.trim() |
| 99 | + const args = splitByComma(match[1]) |
| 100 | + if (!args.length) |
| 101 | + return [] |
45 | 102 |
|
46 |
| - if (_prop.startsWith('...')) { |
47 |
| - // ({ a, b, ...rest }) => {} |
48 |
| - return fixtureProps |
49 |
| - } |
| 103 | + const first = args[0] |
| 104 | + if (!(first.startsWith('{') && first.endsWith('}'))) |
| 105 | + throw new Error('the first argument must use object destructuring pattern') |
50 | 106 |
|
51 |
| - const colonIndex = _prop.indexOf(':') |
52 |
| - if (colonIndex > 0) |
53 |
| - _prop = _prop.slice(0, colonIndex).trim() |
| 107 | + const _first = first.slice(1, -1).replace(/\s/g, '') |
| 108 | + const props = splitByComma(_first).map((prop) => { |
| 109 | + return prop.replace(/\:.*|\=.*/g, '') |
| 110 | + }) |
54 | 111 |
|
55 |
| - if (fixtureProps.includes(_prop)) |
56 |
| - filteredProps.push(_prop) |
57 |
| - } |
| 112 | + const last = props.at(-1) |
| 113 | + if (last && last.startsWith('...')) |
| 114 | + throw new Error('Rest parameters are not supported') |
58 | 115 |
|
59 |
| - // ({}) => {} |
60 |
| - // ({ a, b, c}) => {} |
61 |
| - return filteredProps |
62 |
| - } |
| 116 | + return props |
| 117 | +} |
63 | 118 |
|
64 |
| - // (ctx) => {} |
65 |
| - return fixtureProps |
| 119 | +function splitByComma(s: string) { |
| 120 | + const result = [] |
| 121 | + const stack = [] |
| 122 | + let start = 0 |
| 123 | + for (let i = 0; i < s.length; i++) { |
| 124 | + if (s[i] === '{' || s[i] === '[') { |
| 125 | + stack.push(s[i] === '{' ? '}' : ']') |
| 126 | + } |
| 127 | + else if (s[i] === stack[stack.length - 1]) { |
| 128 | + stack.pop() |
| 129 | + } |
| 130 | + else if (!stack.length && s[i] === ',') { |
| 131 | + const token = s.substring(start, i).trim() |
| 132 | + if (token) |
| 133 | + result.push(token) |
| 134 | + start = i + 1 |
| 135 | + } |
| 136 | + } |
| 137 | + const lastToken = s.substring(start).trim() |
| 138 | + if (lastToken) |
| 139 | + result.push(lastToken) |
| 140 | + return result |
66 | 141 | }
|
0 commit comments