aboutsummaryrefslogtreecommitdiff
path: root/tests/drivers/sourceprovider_test.js
blob: f9551da0f1fa604a908db987ee69b55738994e50 (plain) (blame)
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
// verify the extension's virtual source provider and its language-server request.
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const { createSourceProvider } = require('../../editors/vscode/client/sourceprovider.js');

async function main() {
    const calls = [];
    const runtime = fs.readFileSync(path.resolve(__dirname, '..', '..', 'src', 'runtime', 'rt.zc'), 'utf8');
    const provider = createSourceProvider(async (method, params) => {
        calls.push({ method, params });
        return runtime;
    }, () => true);
    assert.strictEqual(await provider.provideTextDocumentContent({ path: '/rt.zc' }), runtime);
    assert.deepStrictEqual(calls, [{ method: 'zc/source', params: { path: '/rt.zc' } }]);

    const missing = createSourceProvider(async () => null, () => true);
    assert.strictEqual(await missing.provideTextDocumentContent({ path: '/missing.zc' }), '// no such built-in source\n');
    const failed = createSourceProvider(async () => { throw new Error('closed'); }, () => true);
    assert.strictEqual(await failed.provideTextDocumentContent({ path: '/rt.zc' }), '// the language server could not give this source\n');

    let readyChecks = 0, delays = 0;
    const starting = createSourceProvider(async () => runtime, () => ++readyChecks > 2, async () => { delays++; });
    assert.strictEqual(await starting.provideTextDocumentContent({ path: '/rt.zc' }), runtime);
    assert.strictEqual(delays, 2);
    const stopped = createSourceProvider(async () => runtime, () => false, async () => {});
    assert.strictEqual(await stopped.provideTextDocumentContent({ path: '/rt.zc' }), '// the language server is not running\n');
    console.log('ok   virtual source provider waits for the server and returns embedded source text');
}

main().catch(error => { console.error(error); process.exitCode = 1; });