|
| 1 | +import { mkdirSync, rmSync, writeFileSync } from 'node:fs'; |
| 2 | +import { tmpdir } from 'node:os'; |
| 3 | +import { join } from 'node:path'; |
| 4 | +import styles from 'ansi-styles'; |
| 5 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
| 6 | +import { AnsiStyle, DEFAULT_THEME } from './AnsiStyle'; |
| 7 | +import { readSourceSnippet } from './SourceFileReader'; |
| 8 | + |
| 9 | +describe('readSourceSnippet highlightPhp', () => { |
| 10 | + const style = new AnsiStyle(DEFAULT_THEME); |
| 11 | + const dir = join(tmpdir(), 'source-reader-test'); |
| 12 | + const file = join(dir, 'multiply.php'); |
| 13 | + |
| 14 | + beforeAll(() => { |
| 15 | + mkdirSync(dir, { recursive: true }); |
| 16 | + writeFileSync(file, ['<?php', '$x = $a', ' * $b;', ''].join('\n')); |
| 17 | + }); |
| 18 | + |
| 19 | + afterAll(() => { |
| 20 | + rmSync(dir, { recursive: true, force: true }); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should not treat multiplication line as doc-comment', () => { |
| 24 | + const lines = readSourceSnippet(file, 3, style); |
| 25 | + |
| 26 | + expect(lines).toBeDefined(); |
| 27 | + // Line 3 is " * $b;" — the * should NOT be styled as a comment |
| 28 | + const line3 = lines?.find((l) => l.includes('$b')); |
| 29 | + expect(line3).toBeDefined(); |
| 30 | + // $b should be styled as a variable (bold) |
| 31 | + expect(line3).toContain(`${styles.bold.open}$b${styles.bold.close}`); |
| 32 | + // The content after delimiter should NOT start with italic (comment style) |
| 33 | + const content = line3?.split('▕')[1] ?? ''; |
| 34 | + expect(content).not.toContain(styles.italic.open); |
| 35 | + }); |
| 36 | +}); |
0 commit comments