Skip to content

Commit

Permalink
test(api-markdown-documenter): Add multi-line fenced code block tests (
Browse files Browse the repository at this point in the history
…#22679)

Adds some missing test coverage around fenced code blocks. All of our
existing test cases were single-line.
  • Loading branch information
Josmithr authored Sep 30, 2024
1 parent 33ea229 commit dc893fe
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
*/

import { h } from "hastscript";
import { FencedCodeBlockNode, PlainTextNode } from "../../documentation-domain/index.js";
import {
FencedCodeBlockNode,
LineBreakNode,
PlainTextNode,
} from "../../documentation-domain/index.js";
import { assertTransformation } from "./Utilities.js";

const brElement = h("br");

describe("FencedCodeBlock HTML rendering tests", () => {
it("Simple FencedCodeBlock", () => {
const input = new FencedCodeBlockNode(
Expand All @@ -19,4 +25,28 @@ describe("FencedCodeBlock HTML rendering tests", () => {

assertTransformation(input, expected);
});

it("Multi-line FencedCodeBlock", () => {
const input = new FencedCodeBlockNode(
[
new PlainTextNode('const foo = "Hello world!'),
LineBreakNode.Singleton,
new PlainTextNode("console.log(foo);"),
LineBreakNode.Singleton,
new PlainTextNode("return foo;"),
],
"typescript",
);

// Note: HTML <code> elements don't support a language specification like Markdown fenced code blocks do.
const expected = h("code", [
{ type: "text", value: 'const foo = "Hello world!' },
brElement,
{ type: "text", value: "console.log(foo);" },
brElement,
{ type: "text", value: "return foo;" },
]);

assertTransformation(input, expected);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

import { expect } from "chai";

import { FencedCodeBlockNode, PlainTextNode } from "../../../documentation-domain/index.js";
import {
FencedCodeBlockNode,
LineBreakNode,
PlainTextNode,
} from "../../../documentation-domain/index.js";
import { testRender } from "./Utilities.js";

describe("FencedCodeBlock HTML rendering tests", () => {
Expand All @@ -20,4 +24,31 @@ describe("FencedCodeBlock HTML rendering tests", () => {

expect(result).to.equal(expected);
});

it("Multi-line FencedCodeBlock", () => {
const input = new FencedCodeBlockNode(
[
new PlainTextNode('const foo = "Hello world!"'),
LineBreakNode.Singleton,
new PlainTextNode("console.log(foo);"),
LineBreakNode.Singleton,
new PlainTextNode("return foo;"),
],
"typescript",
);
const result = testRender(input);

const expected = [
"<code>",
" const foo = &quot;Hello world!&quot;",
" <br>",
" console.log(foo);",
" <br>",
" return foo;",
"</code>",
"",
].join("\n");

expect(result).to.equal(expected);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,99 @@

import { expect } from "chai";

import { FencedCodeBlockNode, PlainTextNode } from "../../../documentation-domain/index.js";
import {
FencedCodeBlockNode,
LineBreakNode,
PlainTextNode,
} from "../../../documentation-domain/index.js";
import { testRender } from "./Utilities.js";

describe("FencedCodeBlock Markdown rendering tests", () => {
it("Simple FencedCodeBlock (standard context)", () => {
const input = new FencedCodeBlockNode(
[new PlainTextNode("console.log('hello world');")],
"typescript",
);
const result = testRender(input);

const expected = ["", "```typescript", "console.log('hello world');", "```", "", ""].join(
"\n",
);

expect(result).to.equal(expected);
describe("Standard context", () => {
it("Simple FencedCodeBlock", () => {
const input = new FencedCodeBlockNode(
[new PlainTextNode("console.log('hello world');")],
"typescript",
);
const result = testRender(input);

const expected = [
"",
"```typescript",
"console.log('hello world');",
"```",
"",
"",
].join("\n");

expect(result).to.equal(expected);
});

it("Multi-line FencedCodeBlock", () => {
const input = new FencedCodeBlockNode(
[
new PlainTextNode('const foo = "Hello world!"'),
LineBreakNode.Singleton,
new PlainTextNode("console.log(foo);"),
LineBreakNode.Singleton,
new PlainTextNode("return foo;"),
],
"typescript",
);
const result = testRender(input);

const expected = [
"",
"```typescript",
'const foo = "Hello world!"',
"console.log(foo);",
"return foo;",
"```",
"",
"",
].join("\n");

expect(result).to.equal(expected);
});
});

it("Simple FencedCodeBlock (table context)", () => {
const input = new FencedCodeBlockNode(
[new PlainTextNode("console.log('hello world');")],
"typescript",
);
const result = testRender(input, { insideTable: true });
describe("Table context", () => {
it("Simple FencedCodeBlock", () => {
const input = new FencedCodeBlockNode(
[new PlainTextNode("console.log('hello world');")],
"typescript",
);
const result = testRender(input, { insideTable: true });

const expected = ["<code>", "console.log('hello world');", "</code>"].join("");

expect(result).to.equal(expected);
});

it("Multi-line FencedCodeBlock", () => {
const input = new FencedCodeBlockNode(
[
new PlainTextNode('const foo = "Hello world!"'),
LineBreakNode.Singleton,
new PlainTextNode("console.log(foo);"),
LineBreakNode.Singleton,
new PlainTextNode("return foo;"),
],
"typescript",
);
const result = testRender(input, { insideTable: true });

const expected = ["<code>", "console.log('hello world');", "</code>"].join("");
const expected = [
"<code>",
"const foo = &quot;Hello world!&quot;",
"<br>",
"console.log(foo);",
"<br>",
"return foo;",
"</code>",
].join("");

expect(result).to.equal(expected);
expect(result).to.equal(expected);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ <h3 id="testnamespace-example1">
<p>
</p>
<code>
const foo = bar;
const foo: Foo = {
<br>
bar: &quot;Hello world!&quot;;
<br>
baz = 42;
<br>
};
</code>
</p>
</section>
Expand All @@ -58,7 +64,13 @@ <h3 id="testnamespace-example2">
<p>
</p>
<code>
const bar = foo
const foo = {
<br>
bar: &quot;Hello world!&quot;;
<br>
baz = 42;
<br>
};
</code>
</p>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2990,7 +2990,13 @@ <h4 id="testnamespace-example1">
<p>
</p>
<code>
const foo = bar;
const foo: Foo = {
<br>
bar: &quot;Hello world!&quot;;
<br>
baz = 42;
<br>
};
</code>
</p>
</section>
Expand All @@ -3002,7 +3008,13 @@ <h4 id="testnamespace-example2">
<p>
</p>
<code>
const bar = foo
const foo = {
<br>
bar: &quot;Hello world!&quot;;
<br>
baz = 42;
<br>
};
</code>
</p>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ <h4 id="testnamespace-example1">
<p>
</p>
<code>
const foo = bar;
const foo: Foo = {
<br>
bar: &quot;Hello world!&quot;;
<br>
baz = 42;
<br>
};
</code>
</p>
</section>
Expand All @@ -53,7 +59,13 @@ <h4 id="testnamespace-example2">
<p>
</p>
<code>
const bar = foo
const foo = {
<br>
bar: &quot;Hello world!&quot;;
<br>
baz = 42;
<br>
};
</code>
</p>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@ Here are some remarks about the namespace
### Example: TypeScript Example {#testnamespace-example1}
```typescript
const foo = bar;
const foo: Foo = {
bar: "Hello world!";
baz = 42;
};
```

### Example: JavaScript Example {#testnamespace-example2}

```javascript
const bar = foo
const foo = {
bar: "Hello world!";
baz = 42;
};
```

## Interfaces
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1056,13 +1056,19 @@ Here are some remarks about the namespace
#### Example: TypeScript Example {#testnamespace-example1}

```typescript
const foo = bar;
const foo: Foo = {
bar: "Hello world!";
baz = 42;
};
```

#### Example: JavaScript Example {#testnamespace-example2}

```javascript
const bar = foo
const foo = {
bar: "Hello world!";
baz = 42;
};
```

### Classes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@ Here are some remarks about the namespace
#### Example: TypeScript Example {#testnamespace-example1}
```typescript
const foo = bar;
const foo: Foo = {
bar: "Hello world!";
baz = 42;
};
```

#### Example: JavaScript Example {#testnamespace-example2}

```javascript
const bar = foo
const foo = {
bar: "Hello world!";
baz = 42;
};
```

### Classes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1855,7 +1855,7 @@
{
"kind": "Namespace",
"canonicalReference": "test-suite-a!TestNamespace:namespace",
"docComment": "/**\n * Test Namespace\n *\n * @remarks\n *\n * Here are some remarks about the namespace\n *\n * @example TypeScript Example\n * ```typescript\n * const foo = bar;\n * ```\n *\n * @example JavaScript Example\n *\n * ```javascript\n * const bar = foo\n * ```\n *\n * @public\n */\n",
"docComment": "/**\n * Test Namespace\n *\n * @remarks\n *\n * Here are some remarks about the namespace\n *\n * @example TypeScript Example\n * ```typescript\n * const foo: Foo = {\n * \tbar: \"Hello world!\";\n * \tbaz = 42;\n * };\n * ```\n *\n * @example JavaScript Example\n * ```javascript\n * const foo = {\n * \tbar: \"Hello world!\";\n * \tbaz = 42;\n * };\n * ```\n *\n * @public\n */\n",
"excerptTokens": [
{
"kind": "Content",
Expand Down

0 comments on commit dc893fe

Please sign in to comment.