|
| 1 | +import { test, describe } from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { detectFormat, detectPy } from "../src/detect.js"; |
| 4 | + |
| 5 | +// --------------------------------------------------------------------------- |
| 6 | +// Python scripts |
| 7 | +// --------------------------------------------------------------------------- |
| 8 | + |
| 9 | +describe("detectPy", () => { |
| 10 | + test("`# %%` delimiter → percent", () => { |
| 11 | + assert.equal(detectPy("# %%\nx = 1\n# %%\ny = 2"), "percent"); |
| 12 | + }); |
| 13 | + |
| 14 | + test("`#%%` without space → percent", () => { |
| 15 | + assert.equal(detectPy("#%%\nx = 1"), "percent"); |
| 16 | + }); |
| 17 | + |
| 18 | + test("two 20-hash runs → sphinx-gallery", () => { |
| 19 | + const text = "####################\nx = 1\n####################\ny = 2"; |
| 20 | + assert.equal(detectPy(text), "sphinx-gallery"); |
| 21 | + }); |
| 22 | + |
| 23 | + test("`# %%` wins over hash runs (percent precedence)", () => { |
| 24 | + const text = "####################\n# %%\nx = 1\n####################"; |
| 25 | + assert.equal(detectPy(text), "percent"); |
| 26 | + }); |
| 27 | + |
| 28 | + test("leading module docstring, no delimiters → sphinx-gallery", () => { |
| 29 | + assert.equal(detectPy('"""Module doc."""\nimport os\nx = 1'), "sphinx-gallery"); |
| 30 | + }); |
| 31 | + |
| 32 | + test("r-prefixed docstring → sphinx-gallery", () => { |
| 33 | + assert.equal(detectPy('r"""\nTitle\n=====\n"""\nx = 1'), "sphinx-gallery"); |
| 34 | + }); |
| 35 | + |
| 36 | + test("plain script, no docstring, no delimiters → percent", () => { |
| 37 | + assert.equal(detectPy("import os\nx = 1\nprint(x)"), "percent"); |
| 38 | + }); |
| 39 | + |
| 40 | + test("`# %%` inside a docstring is not counted", () => { |
| 41 | + // The only `# %%` lives inside the docstring; file should fall back to the |
| 42 | + // leading-docstring rule → sphinx-gallery, not percent. |
| 43 | + const text = '"""\nExample:\n# %%\n"""\nimport os'; |
| 44 | + assert.equal(detectPy(text), "sphinx-gallery"); |
| 45 | + }); |
| 46 | + |
| 47 | + test("a single hash run is not enough for sphinx", () => { |
| 48 | + assert.equal(detectPy("####################\nimport os\nx = 1"), "percent"); |
| 49 | + }); |
| 50 | + |
| 51 | + test("leading docstring + `# %%` cell separators → sphinx-gallery", () => { |
| 52 | + const text = '"""\nLeading docstring\n"""\n# %%\nx = 1\n# %%\ny = 2'; |
| 53 | + assert.equal(detectPy(text), "sphinx-gallery"); |
| 54 | + }); |
| 55 | + |
| 56 | + test("leading docstring + `# %% [markdown]` → percent", () => { |
| 57 | + const text = '"""\nLeading docstring\n"""\n# %% [markdown]\n# Some text\n# %%\nx = 1'; |
| 58 | + assert.equal(detectPy(text), "percent"); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +// --------------------------------------------------------------------------- |
| 63 | +// Extension dispatch |
| 64 | +// --------------------------------------------------------------------------- |
| 65 | + |
| 66 | +describe("detectFormat", () => { |
| 67 | + test(".md with {code-cell} → myst", () => { |
| 68 | + assert.equal(detectFormat("```{code-cell}\nx = 1\n```", ".md"), "myst"); |
| 69 | + }); |
| 70 | + |
| 71 | + test(".md with +++ → myst", () => { |
| 72 | + assert.equal(detectFormat("First.\n+++\nSecond.", ".md"), "myst"); |
| 73 | + }); |
| 74 | + |
| 75 | + test(".md plain prose → classic", () => { |
| 76 | + assert.equal(detectFormat("# Title\n\n```python\nx = 1\n```", ".md"), "classic"); |
| 77 | + }); |
| 78 | + |
| 79 | + test("extension without a leading dot is accepted", () => { |
| 80 | + assert.equal(detectFormat("# %%\nx = 1", "py"), "percent"); |
| 81 | + }); |
| 82 | + |
| 83 | + test(".py routes to detectPy", () => { |
| 84 | + assert.equal(detectFormat("# %%\nx = 1", ".py"), "percent"); |
| 85 | + }); |
| 86 | +}); |
0 commit comments