|
| 1 | +import { describe, it, expect, beforeAll } from 'vitest'; |
| 2 | +import { Code } from '../src/code'; |
| 3 | +import { setWasmBasePath } from '../src/utils'; |
| 4 | +import * as path from 'path'; |
| 5 | + |
| 6 | +describe('Syntax Highlighting Tests', () => { |
| 7 | + beforeAll(() => { |
| 8 | + // Set path for local wasm binaries |
| 9 | + setWasmBasePath(path.resolve(__dirname, '../wasm') + '/'); |
| 10 | + }); |
| 11 | + |
| 12 | + describe('Basic Highlighting', () => { |
| 13 | + it('should correctly highlight basic Python structures', async () => { |
| 14 | + const pythonCode = `# This is a comment |
| 15 | +def my_func(x): |
| 16 | + return x + 42 |
| 17 | +`; |
| 18 | + const code = new Code(pythonCode, 'test.py', 'python'); |
| 19 | + await code.init(); |
| 20 | + |
| 21 | + // Line 0: comment |
| 22 | + const nodesLine0 = code.getLineNodes(0); |
| 23 | + expect(nodesLine0.some(n => n.name === 'comment' && n.text === '# This is a comment')).toBe(true); |
| 24 | + |
| 25 | + // Line 1: def my_func(x): |
| 26 | + const nodesLine1 = code.getLineNodes(1); |
| 27 | + expect(nodesLine1.some(n => n.name === 'keyword' && n.text === 'def')).toBe(true); |
| 28 | + expect(nodesLine1.some(n => n.name === 'function' && n.text === 'my_func')).toBe(true); |
| 29 | + expect(nodesLine1.some(n => n.name === 'variable.parameter' && n.text === 'x')).toBe(true); |
| 30 | + |
| 31 | + // Line 2: return x + 42 |
| 32 | + const nodesLine2 = code.getLineNodes(2); |
| 33 | + expect(nodesLine2.some(n => n.name === 'keyword' && n.text === 'return')).toBe(true); |
| 34 | + expect(nodesLine2.some(n => n.name === 'constant' && n.text === '42')).toBe(true); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should correctly highlight basic JavaScript structures', async () => { |
| 38 | + const jsCode = `// Comment |
| 39 | +const val = "hello"; |
| 40 | +if (val === "hello") { |
| 41 | + console.log(123); |
| 42 | +} |
| 43 | +`; |
| 44 | + const code = new Code(jsCode, 'test.js', 'javascript'); |
| 45 | + await code.init(); |
| 46 | + |
| 47 | + // Line 0: comment |
| 48 | + const nodesLine0 = code.getLineNodes(0); |
| 49 | + expect(nodesLine0.some(n => n.name === 'comment' && n.text === '// Comment')).toBe(true); |
| 50 | + |
| 51 | + // Line 1: const val = "hello"; |
| 52 | + const nodesLine1 = code.getLineNodes(1); |
| 53 | + expect(nodesLine1.some(n => n.name === 'keyword' && n.text === 'const')).toBe(true); |
| 54 | + expect(nodesLine1.some(n => n.name === 'string' && n.text === '"hello"')).toBe(true); |
| 55 | + |
| 56 | + // Line 2: if (val === "hello") { |
| 57 | + const nodesLine2 = code.getLineNodes(2); |
| 58 | + expect(nodesLine2.some(n => n.name === 'keyword' && n.text === 'if')).toBe(true); |
| 59 | + expect(nodesLine2.some(n => n.name === 'punctuation.bracket' && n.text === '{')).toBe(true); |
| 60 | + |
| 61 | + // Line 3: console.log(123); |
| 62 | + const nodesLine3 = code.getLineNodes(3); |
| 63 | + expect(nodesLine3.some(n => n.name === 'variable.builtin' && n.text === 'console')).toBe(true); |
| 64 | + expect(nodesLine3.some(n => n.name === 'number' && n.text === '123')).toBe(true); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('Syntax Error Recovery Highlighting', () => { |
| 69 | + it('should correctly highlight code inside and after syntax errors in Python', async () => { |
| 70 | + const pythonCode = `print("hi") |
| 71 | +print("hi" |
| 72 | +print("hi") |
| 73 | +print("hi") |
| 74 | +`; |
| 75 | + const code = new Code(pythonCode, 'test.py', 'python'); |
| 76 | + await code.init(); |
| 77 | + |
| 78 | + // Line 0: print("hi") - normal highlight |
| 79 | + const nodesLine0 = code.getLineNodes(0); |
| 80 | + expect(nodesLine0.some(n => n.name === 'function.builtin' && n.text === 'print')).toBe(true); |
| 81 | + expect(nodesLine0.some(n => n.name === 'string' && n.text === '"hi"')).toBe(true); |
| 82 | + |
| 83 | + // Line 2 (which is print("hi") after the missing paren on line 1) |
| 84 | + const nodesLine2 = code.getLineNodes(2); |
| 85 | + expect(nodesLine2.some(n => n.name === 'function.builtin' && n.text === 'print')).toBe(true); |
| 86 | + expect(nodesLine2.some(n => n.name === 'string' && n.text === '"hi"')).toBe(true); |
| 87 | + // Ensure the entire line is not grouped under a single 'error' token |
| 88 | + const errorNodes = nodesLine2.filter(n => n.name === 'error'); |
| 89 | + if (errorNodes.length > 0) { |
| 90 | + expect(errorNodes.some(n => n.text === 'print("hi")')).toBe(false); |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + it('should correctly highlight code inside and after syntax errors in JavaScript', async () => { |
| 95 | + const jsCode = `console.log("hi"); |
| 96 | +console.log("hi" |
| 97 | +console.log("hi"); |
| 98 | +`; |
| 99 | + const code = new Code(jsCode, 'test.js', 'javascript'); |
| 100 | + await code.init(); |
| 101 | + |
| 102 | + // Line 2 (console.log("hi"); after the missing paren on line 1) |
| 103 | + const nodesLine2 = code.getLineNodes(2); |
| 104 | + expect(nodesLine2.some(n => n.name === 'string' && n.text === '"hi"')).toBe(true); |
| 105 | + const errorNodes = nodesLine2.filter(n => n.name === 'error'); |
| 106 | + if (errorNodes.length > 0) { |
| 107 | + expect(errorNodes.some(n => n.text.includes('console.log'))).toBe(false); |
| 108 | + } |
| 109 | + }); |
| 110 | + }); |
| 111 | +}); |
0 commit comments