|
| 1 | +import * as vscode from "vscode"; |
| 2 | +import * as assert from "assert"; |
| 3 | +import { WORKSPACE_PATH, examplesOutUri, openAndShowTextDocument } from "./test-utils"; |
| 4 | +import { isExecutableLanguageBlock, languageNameFromBlock } from "quarto-core"; |
| 5 | +import { MarkdownEngine } from "../markdown/engine"; |
| 6 | + |
| 7 | +suite("Code block detection", function () { |
| 8 | + const engine = new MarkdownEngine(); |
| 9 | + |
| 10 | + suiteSetup(async function () { |
| 11 | + await vscode.workspace.fs.delete(examplesOutUri(), { recursive: true }); |
| 12 | + await vscode.workspace.fs.copy(vscode.Uri.file(WORKSPACE_PATH), examplesOutUri()); |
| 13 | + }); |
| 14 | + |
| 15 | + // Test for issue #521 / PR #875: |
| 16 | + // Code blocks inside divs should be detected even when the closing ::: |
| 17 | + // doesn't have a preceding blank line. This real-world example has many |
| 18 | + // .notes divs which caused later code blocks to not be detected. |
| 19 | + test("Detects code blocks in document with many divs (issue #521)", async function () { |
| 20 | + const { doc } = await openAndShowTextDocument("div-code-blocks.qmd"); |
| 21 | + |
| 22 | + const tokens = engine.parse(doc); |
| 23 | + const executableBlocks = tokens.filter(isExecutableLanguageBlock); |
| 24 | + |
| 25 | + // Count R code blocks and math blocks separately |
| 26 | + const rBlocks = executableBlocks.filter(b => languageNameFromBlock(b) === "r"); |
| 27 | + const mathBlocks = executableBlocks.filter(b => languageNameFromBlock(b) === "tex"); |
| 28 | + |
| 29 | + // Should find all 3 R code blocks |
| 30 | + assert.strictEqual( |
| 31 | + rBlocks.length, |
| 32 | + 3, |
| 33 | + `Expected 3 R code blocks, found ${rBlocks.length}` |
| 34 | + ); |
| 35 | + |
| 36 | + // Should find all 3 math blocks (displayed as tex) |
| 37 | + assert.strictEqual( |
| 38 | + mathBlocks.length, |
| 39 | + 3, |
| 40 | + `Expected 3 math blocks, found ${mathBlocks.length}` |
| 41 | + ); |
| 42 | + |
| 43 | + // Total should be 6 executable blocks (3 R + 3 math) |
| 44 | + assert.strictEqual( |
| 45 | + executableBlocks.length, |
| 46 | + 6, |
| 47 | + `Expected 6 executable blocks total, found ${executableBlocks.length}` |
| 48 | + ); |
| 49 | + }); |
| 50 | + |
| 51 | + test("Detects code block in simple document", async function () { |
| 52 | + const { doc } = await openAndShowTextDocument("hello.qmd"); |
| 53 | + |
| 54 | + const tokens = engine.parse(doc); |
| 55 | + const executableBlocks = tokens.filter(isExecutableLanguageBlock); |
| 56 | + |
| 57 | + assert.strictEqual( |
| 58 | + executableBlocks.length, |
| 59 | + 1, |
| 60 | + `Expected 1 executable block in hello.qmd, found ${executableBlocks.length}` |
| 61 | + ); |
| 62 | + |
| 63 | + const language = languageNameFromBlock(executableBlocks[0]); |
| 64 | + assert.strictEqual(language, "python", `Expected python, got ${language}`); |
| 65 | + }); |
| 66 | +}); |
0 commit comments