markdownlint-rule-relative-.../test/utils.test.js

80 lines
2.5 KiB
JavaScript
Raw Normal View History

const { test } = require("node:test")
const assert = require("node:assert/strict")
const {
convertHeadingToHTMLFragment,
getMarkdownHeadings,
2024-01-12 00:43:45 +01:00
getMarkdownIdOrAnchorNameFragments,
isValidIntegerString,
getNumberOfLines,
getLineNumberStringFromFragment,
} = require("../src/utils.js")
test("utils", async (t) => {
await t.test("convertHeadingToHTMLFragment", async () => {
assert.strictEqual(
convertHeadingToHTMLFragment("Valid Fragments"),
"#valid-fragments",
)
assert.strictEqual(
convertHeadingToHTMLFragment("Valid Heading With Underscores _"),
"#valid-heading-with-underscores-_",
)
assert.strictEqual(
convertHeadingToHTMLFragment(
`Valid Heading With Quotes ' And Double Quotes "`,
),
"#valid-heading-with-quotes--and-double-quotes-",
)
assert.strictEqual(
convertHeadingToHTMLFragment("🚀 Valid Heading With Emoji"),
"#-valid-heading-with-emoji",
)
})
await t.test("getMarkdownHeadings", async () => {
assert.deepStrictEqual(
getMarkdownHeadings("# Hello\n\n## World\n\n## Hello, world!\n"),
["Hello", "World", "Hello, world!"],
)
})
2024-01-11 14:42:34 +01:00
2024-01-12 00:43:45 +01:00
await t.test("getMarkdownIdOrAnchorNameFragments", async () => {
2024-01-11 14:42:34 +01:00
assert.deepStrictEqual(
2024-01-12 00:43:45 +01:00
getMarkdownIdOrAnchorNameFragments(
2024-01-12 00:33:05 +01:00
'<a name="anchorName" id="anchorId">Link</a>',
),
2024-01-11 14:42:34 +01:00
["#anchorId"],
)
assert.deepStrictEqual(
2024-01-12 00:43:45 +01:00
getMarkdownIdOrAnchorNameFragments('<a name="anchorName">Link</a>'),
2024-01-11 14:42:34 +01:00
["#anchorName"],
)
2024-01-12 00:43:45 +01:00
assert.deepStrictEqual(
getMarkdownIdOrAnchorNameFragments("<a>Link</a>"),
[],
)
assert.deepStrictEqual(getMarkdownIdOrAnchorNameFragments("<a>"), [])
assert.deepStrictEqual(getMarkdownIdOrAnchorNameFragments("<a id=>"), [])
2024-01-11 14:42:34 +01:00
})
await t.test("isValidIntegerString", async () => {
assert.strictEqual(isValidIntegerString("1"), true)
assert.strictEqual(isValidIntegerString("45"), true)
assert.strictEqual(isValidIntegerString("1abc"), false)
assert.strictEqual(isValidIntegerString("1.0"), false)
})
await t.test("getNumberOfLines", async () => {
assert.strictEqual(getNumberOfLines(""), 1)
assert.strictEqual(getNumberOfLines("Hello"), 1)
assert.strictEqual(getNumberOfLines("Hello\nWorld"), 2)
assert.strictEqual(getNumberOfLines("Hello\nWorld\n"), 3)
assert.strictEqual(getNumberOfLines("Hello\nWorld\n\n"), 4)
})
await t.test("getLineNumberStringFromFragment", async () => {
assert.strictEqual(getLineNumberStringFromFragment("#L50"), "50")
})
})