about summary refs log tree commit diff
path: root/src/tools/rust-analyzer/editors/code/tests/unit/index.ts
blob: bf74060c730117cb98a80927e3a635c885c627c9 (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import * as assert from "node:assert/strict";
import { readdir } from "fs/promises";
import * as path from "path";
import { pathToFileURL } from "url";

class Test {
    readonly name: string;
    readonly promise: Promise<void>;

    constructor(name: string, promise: Promise<void>) {
        this.name = name;
        this.promise = promise;
    }
}

class Suite {
    tests: Test[];

    constructor() {
        this.tests = [];
    }

    public addTest(name: string, f: () => Promise<void>): void {
        const test = new Test(name, f());
        this.tests.push(test);
    }

    public async run(): Promise<void> {
        let failed = 0;
        for (const test of this.tests) {
            try {
                await test.promise;
                ok(`  ✔ ${test.name}`);
            } catch (e) {
                assert.ok(e instanceof Error);
                error(`  ✖︎ ${test.name}\n  ${e.stack}`);
                failed += 1;
            }
        }
        if (failed) {
            const plural = failed > 1 ? "s" : "";
            throw new Error(`${failed} failed test${plural}`);
        }
    }
}

export class Context {
    public async suite(name: string, f: (ctx: Suite) => void): Promise<void> {
        const ctx = new Suite();
        f(ctx);
        try {
            ok(`⌛︎ ${name}`);
            await ctx.run();
            ok(`✔ ${name}`);
        } catch (e) {
            assert.ok(e instanceof Error);
            error(`✖︎ ${name}\n  ${e.stack}`);
            throw e;
        }
    }
}

export async function run(): Promise<void> {
    const context = new Context();

    const testFiles = (await readdir(path.resolve(__dirname))).filter((name) =>
        name.endsWith(".test.js"),
    );
    for (const testFile of testFiles) {
        try {
            const testModule = await import(pathToFileURL(path.resolve(__dirname, testFile)).href);
            await testModule.getTests(context);
        } catch (e) {
            error(`${e}`);
            throw e;
        }
    }
}

function ok(message: string): void {
    // eslint-disable-next-line no-console
    console.log(`\x1b[32m${message}\x1b[0m`);
}

function error(message: string): void {
    // eslint-disable-next-line no-console
    console.error(`\x1b[31m${message}\x1b[0m`);
}