-
-
Notifications
You must be signed in to change notification settings - Fork 269
/
parse-directive.ts
49 lines (44 loc) · 1.26 KB
/
parse-directive.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { OPEN_BRACE_EOL } from './brace-patterns.js'
/**
* Directive types supported by this library.
*
* `'time'` is not officially in the specification, but node-tap has
* supported it for a long time, and uses it to report how long
* subtests take to complete.
*/
export type Directive = 'todo' | 'skip' | 'time'
/**
* Parse a "directive", the bit that follows the `#` character
* on a TestPoint line.
*/
export const parseDirective = (
line: string,
): [Directive, any] | false => {
if (!line.trim()) return false
line = line
.replace(OPEN_BRACE_EOL, '')
.trim()
.replace(/^duration_ms ([0-9.]+)$/, 'time=$1ms')
const time = line.match(
/^time=((?:[1-9][0-9]*|0)(?:\.[0-9]+)?)(ms|s)$/i,
)
const t = time?.[1]
const s = time?.[2]
if (typeof t === 'string') {
let n = +t
if (s === 's') {
// JS does weird things with floats. Round it off a bit.
n *= 1000000
n = Math.round(n)
n /= 1000
}
return ['time', n]
}
const ts = line.match(/^(todo|skip)(?:\S*)\b(.*)$/i)
const type = ts?.[1]?.toLowerCase()
const msg = ts?.[2]?.trim()
if (!type) return false
// we know at this point it must be either 'todo' or 'skip',
// in unknown upper/lower case
return [type as Directive, msg || true]
}