idlize
83 строки · 2.2 Кб
1import { nativeModule } from "@koalaui/arkoala"2import { withStringResult } from "@koalaui/interop"3
4export const TEST_GROUP_LOG = 15export const CALL_GROUP_LOG = 26
7export let reportTestFailures: boolean = true8
9let failedTestsCount = 010
11export function setReportTestFailures(report: boolean) {12reportTestFailures = report13}
14
15export function checkTestFailures() {16if (reportTestFailures && failedTestsCount > 0) {17console.log(`failed tests: ${failedTestsCount}`)18process.exit(1)19}20}
21
22export function startNativeLog(group: number) {23nativeModule()._StartGroupedLog(group)24}
25
26export function stopNativeLog(group: number) {27nativeModule()._StopGroupedLog(group)28}
29
30export function getNativeLog(group: number = TEST_GROUP_LOG): string {31return withStringResult(nativeModule()._GetGroupedLog(group))!32}
33
34export function checkResult(name: string, test: () => void, expected: string) {35startNativeLog(TEST_GROUP_LOG)36test()37stopNativeLog(TEST_GROUP_LOG)38const out = getNativeLog()39// remove out comments like /* some text */40const actual = out.replace(/\s?\/\*.*?\*\//g, "");41if (reportTestFailures) {42if (actual != expected) {43failedTestsCount++44console.log(`TEST ${name} FAIL:\n EXPECTED "${expected}"\n ACTUAL "${actual}"`)45console.log(`output: ${out}`)46} else {47console.log(`TEST ${name} PASS`)48}49}50}
51
52export function assertTrue(name: string, value: boolean) {53if (!value) {54failedTestsCount++55console.log(`TEST ${name} FAIL:\n EXPECTED value is not true.`)56} else {57console.log(`TEST ${name} PASS`)58}59}
60
61export function assertEquals(name: string, expected: any, actual: any) {62if (expected != actual ) {63failedTestsCount++64console.log(`TEST ${name} FAIL:\n EXPECTED "${expected}"\n ACTUAL "${actual}"`)65} else {66console.log(`TEST ${name} PASS`)67}68}
69
70export function assertThrows(name: string, fn: () => void) {71let caught = false72try {73fn()74} catch (e) {75caught = true76}77if (!caught) {78failedTestsCount++79console.log(`TEST ${name} FAIL:\n No exception thrown`)80} else {81console.log(`TEST ${name} PASS`)82}83}