-
-
Notifications
You must be signed in to change notification settings - Fork 269
/
on-add.ts
32 lines (30 loc) · 931 Bytes
/
on-add.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { Base, TestBase } from '@tapjs/core'
import { DiagnosticData } from '@tapjs/error-serdes'
import { TestMap } from './test-map.js'
import { testNestedLocation } from './test-nested-location.js'
export const onAddFn = (
comment: (this: Base, ...args: any[]) => void,
diagsMap: TestMap<DiagnosticData[]>,
subsMap: TestMap<Base[]>,
) => {
const onAdd = (t: Base) => {
const tt = t as TestBase
const subs: Base[] = []
const diags: DiagnosticData[] = diagsMap.get(t) || []
subsMap.set(t, subs)
diagsMap.set(t, diags)
if (typeof tt.comment === 'function') {
tt.comment = comment
} else if (!tt.comment) {
t.parser.on('comment', message => {
message = message.replace(/^#/, '').trim()
diags.push({ ...testNestedLocation(t), message })
})
}
tt.on('subtestAdd', onAdd)
if (t.parent) {
subsMap.get(t.parent)?.push(t)
}
}
return onAdd
}