tests.uts 1.1 KB
Newer Older
1
import { Matchers,NumberMatchers } from './Matchers.uts'
Y
yurj26 已提交
2 3 4

export const describes = new Map<string, Result>()

5
export class Result implements IJsonStringify {
Y
yurj26 已提交
6 7 8
    total = 0
    passed: string[] = []
    failed: string[] = []
杜庆泉's avatar
杜庆泉 已提交
9 10 11 12 13 14 15 16 17
	
	override toJSON():any|null{
		let jsonRet = {
			'total': this.total,
			'passed': JSON.stringify(this.passed),
			'failed': JSON.stringify(this.failed),
		}
		return jsonRet
	}
Y
yurj26 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
}

let result: Result = new Result()

export function describe(name: string, fn: () => void): Result {
    result = new Result()
    describes.set(name, result)
    fn()
    return result
}

export function test(name: string, fn: () => void) {
    try {
        fn()
        // console.log('push....',name)
        result.passed.push(name)
        // console.log('push....',result.passed.length)
    } catch (e) {
36
        result.failed.push(`${name}:\n${e.message}`)
Y
yurj26 已提交
37 38 39 40 41 42 43
    }
    result.total++
}

export function expect<T>(value: T): Matchers<T> {
    return new Matchers(value);
}
44 45 46 47

export function expectNumber(value: number): NumberMatchers {
    return new NumberMatchers(value);
}