// go to definition and find references, in the document and across includes. // usage: node tests/drivers/nav_test.js path/to/zclsp (run from the repository root) // interpreted hosts (no jit) are far slower; ZC_TEST_TIMEOUT_SCALE stretches every deadline const TIMEOUT_SCALE = Number(process.env.ZC_TEST_TIMEOUT_SCALE) || 1; const assert = require('assert'); const cp = require('child_process'); const fs = require('fs'); const path = require('path'); const server = process.argv[2]; const root = path.resolve(__dirname, '..', '..'); const uri = 'file://' + path.join(root, 'tests', 'drivers', 'nav-fixture.zc'); const frame = m => { const b = Buffer.from(JSON.stringify(m)); return Buffer.concat([Buffer.from('Content-Length: ' + b.length + '\r\n\r\n'), b]); }; function request(text, needle, method, extra = {}) { const at = text.indexOf(needle); assert.ok(at >= 0, needle); const before = text.slice(0, at); const position = { line: before.split('\n').length - 1, character: before.length - before.lastIndexOf('\n') - 1 }; const input = Buffer.concat([ { jsonrpc: '2.0', id: 1, method: 'initialize', params: {} }, { jsonrpc: '2.0', method: 'textDocument/didOpen', params: { textDocument: { uri, version: 1, languageId: 'zc', text } } }, { jsonrpc: '2.0', id: 2, method, params: { textDocument: { uri }, position, ...extra } }, { jsonrpc: '2.0', method: 'exit' }].map(frame)); const r = cp.spawnSync(server, process.argv.slice(3), { input, cwd: root, timeout: 30000 * TIMEOUT_SCALE, maxBuffer: 8 << 20 }); assert.strictEqual(r.status, 0, String(r.stderr)); let d = r.stdout, init; while (d.length) { const e = d.indexOf('\r\n\r\n'), n = Number(/Content-Length:\s*(\d+)/i.exec(d.subarray(0, e).toString())[1]); const m = JSON.parse(d.subarray(e + 4, e + 4 + n)); d = d.subarray(e + 4 + n); if (m.id === 1) init = m.result.capabilities; if (m.id === 2) return { result: m.result, init }; } assert.fail('no reply'); } const lines = r => (r || []).map(l => l.range.start.line + ':' + l.range.start.character).sort((a, b) => a.localeCompare(b, undefined, { numeric: true })); const src = 'include "tests/drivers/nav-library.zc";\nstruct pt { int x; int y; };\nint twice(int v) { return v * 2; }\nfn main() {\n\tpt p;\n\tp.x = twice(2);\n\tlet n = p.x + twice(3);\n\tNavValue value;\n\treturn n + p.y + nav_helper(value.value);\n}\n'; const def = (needle, nth) => request(src, needle, 'textDocument/definition').result; assert.ok(request(src, 'twice(2', 'textDocument/definition').init.definitionProvider && request(src, 'twice(2', 'textDocument/definition').init.referencesProvider); assert.deepStrictEqual(lines(def('twice(2')), ['2:4'], 'a function goes to its definition'); assert.deepStrictEqual(lines(def('p.y')), ['4:4'], 'a local goes to its declaration'); assert.deepStrictEqual(lines(def('pt p')), ['1:7'], 'a struct goes to its declaration'); const inc = def('NavValue value'); assert.ok(inc && inc.length === 1 && inc[0].uri === 'file://' + path.join(root, 'tests', 'drivers', 'nav-library.zc'), 'a type from an include goes to the included file: ' + JSON.stringify(inc)); const includeText = fs.readFileSync(path.join(root, 'tests', 'drivers', 'nav-library.zc'), 'utf8').split('\n'); assert.ok(/NavValue/.test(includeText[inc[0].range.start.line].slice(inc[0].range.start.character, inc[0].range.end.character)), 'the range covers the name'); assert.strictEqual(request(src, 'return', 'textDocument/definition').result, null, 'a keyword has no definition'); const refs = (needle, includeDeclaration) => request(src, needle, 'textDocument/references', { context: { includeDeclaration } }).result; assert.deepStrictEqual(lines(refs('twice(2', true)), ['2:4', '5:7', '6:15'], 'references with the declaration'); assert.deepStrictEqual(lines(refs('twice(2', false)), ['5:7', '6:15'], 'references without the declaration'); assert.deepStrictEqual(lines(refs('n + p.y', true)), ['6:5', '8:8'], 'a local: declaration and uses'); const shared = refs('nav_helper(value', true); assert.ok(shared.some(loc => loc.uri === 'file://' + path.join(root, 'tests', 'drivers', 'nav-library.zc')), 'references include the declaration in another file'); assert.deepStrictEqual(lines(refs('y; };', true)).length >= 1, true); // built-ins: the image carries its sources. the runtime, the compiler and (for include std;) the standard library const std = 'include std, rt;\nfn main() {\n\trt.put_s("hi");\n\tlet n = std.Text.atoi("4");\n\tlet m = rt.s_to_n(b"1");\n\treturn n + m + std.Host.now();\n}\n'; const bdef = needle => request(std, needle, 'textDocument/definition').result; const source = name => request(std, 'main', 'zc/source', { path: name }).result; const slice = (text, loc) => { const l = text.split('\n')[loc.range.start.line]; return l.slice(loc.range.start.character, loc.range.end.character); }; let loc = bdef('put_s'); assert.ok(loc && loc.length === 1 && loc[0].uri === 'tinyvm:/rt.zc', 'a runtime function goes to the runtime source: ' + JSON.stringify(loc)); assert.strictEqual(slice(source('/rt.zc'), loc[0]), 'put_s'); assert.strictEqual(source('/rt.zc'), fs.readFileSync(path.join(root, 'src', 'runtime', 'rt.zc'), 'utf8'), 'the embedded runtime is the file'); loc = bdef('Text.atoi'); assert.ok(loc && loc[0].uri === 'tinyvm:/std.zc', 'a std type goes to the std source'); assert.strictEqual(slice(source('std.zc'), loc[0]), 'Text'); loc = bdef('atoi('); assert.strictEqual(loc[0].uri, 'tinyvm:/std.zc'); assert.strictEqual(slice(source('/std.zc'), loc[0]), 'atoi', 'a method goes to its own line'); loc = bdef('now('); assert.strictEqual(slice(source('/std.zc'), loc[0]), 'now'); assert.strictEqual(source('/std.zc'), fs.readFileSync(path.join(root, 'src', 'lib', 'std.zc'), 'utf8'), 'the embedded std is the file'); assert.strictEqual(source('/compiler.zc'), fs.readFileSync(path.join(root, 'src', 'compiler', 'compiler.zc'), 'utf8'), 'the embedded compiler is the file'); assert.strictEqual(source('/server.zc'), fs.readFileSync(path.join(root, 'src', 'lsp', 'server.zc'), 'utf8'), 'the embedded server is the file'); loc = bdef('s_to_n('); assert.ok(loc && loc.length === 1 && loc[0].uri === 'tinyvm:/rt.zc', 's_to_n opens the runtime source: ' + JSON.stringify(loc)); assert.strictEqual(slice(source('/rt.zc'), loc[0]), 's_to_n'); assert.strictEqual(source('/nope.zc'), null); console.log('ok definition and references across the document and includes');