Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-11-22 09:54:24 +01:00

Reformatted into a table driven test

This commit is contained in:
Marco De Araujo 2024-11-05 16:25:59 -04:00
parent 5555d46c8a
commit 6608320da3

View file

@ -1358,112 +1358,90 @@ func TestCallout(t *testing.T) {
test(">\n0", "<blockquote>\n</blockquote>\n<p>0</p>")
}
var escapedMarkdownTests = map[string]struct {
input string
expected string
}{
"Escape heading level 1": {
input: "# Heading level 1",
expected: "\\# Heading level 1",
},
"Escape heading level 2": {
input: "## Heading level 2",
expected: "\\#\\# Heading level 2",
},
"Escape heading level 3": {
input: "### Heading level 3",
expected: "\\#\\#\\# Heading level 3",
},
"Escape bold text": {
input: "**bold text**",
expected: "\\*\\*bold text\\*\\*",
},
"Escape italic text": {
input: "*italic text*",
expected: "\\*italic text\\*",
},
"Escape italic text underline": {
input: "_italic text_",
expected: "\\_italic text\\_",
},
"Escape strikethrough": {
input: "~~strikethrough~~",
expected: "\\~\\~strikethrough\\~\\~",
},
"Escape Ordered list item": {
input: "1. Ordered list item\n2. Second ordered list item\n999999999999. 999999999999 ordered list item",
expected: "1\\. Ordered list item\n2\\. Second ordered list item\n999999999999\\. 999999999999 ordered list item",
},
"Escape Unordered list item": {
input: "- Unordered list\n + using plus",
expected: "\\- Unordered list\n \\+ using plus",
},
"Escape bullet list item": {
input: "* Bullet list item",
expected: "\\* Bullet list item",
},
"Escape table": {
input: "| Table | Example |\n|-|-|\n| Lorem | Ipsum |",
expected: "\\| Table \\| Example \\|\n\\|-\\|-\\|\n\\| Lorem \\| Ipsum \\|",
},
"Escape link": {
input: "[Link to Forgejo](https://forgejo.org/)",
expected: "\\[Link to Forgejo\\]\\(https://forgejo.org/\\)",
},
"Escape Alt text for an image": {
input: "![Alt text for an image](https://forgejo.org/_astro/mascot-dark.1omhhgvT_Zm0N2n.webp)",
expected: "\\!\\[Alt text for an image\\]\\(https://forgejo.org/\\_astro/mascot-dark.1omhhgvT\\_Zm0N2n.webp\\)",
},
"Escape URL if it has markdown character": {
input: "https://forgejo.org/_astro/mascot-dark.1omhhgvT_Zm0N2n.webp",
expected: "https://forgejo.org/\\_astro/mascot-dark.1omhhgvT\\_Zm0N2n.webp",
},
"Escape blockquote text": {
input: "> Blockquote text.",
expected: "\\> Blockquote text.",
},
"Escape inline code": {
input: "`Inline code`",
expected: "\\`Inline code\\`",
},
"Escape multiple code": {
input: "```\nCode block\nwith multiple lines\n```\n",
expected: "\\`\\`\\`\nCode block\nwith multiple lines\n\\`\\`\\`\n",
},
"Escape horizontal rule": {
input: "---",
expected: "\\---",
},
}
func TestEscapeMarkdownChar(t *testing.T) {
t.Run("Escape heading level 1", func(t *testing.T) {
input := "# Heading level 1"
escaped := markdown.Escape(input)
assert.Equal(t, "\\# Heading level 1", escaped)
})
t.Run("Escape heading level 2", func(t *testing.T) {
input := "## Heading level 2"
escaped := markdown.Escape(input)
assert.Equal(t, "\\#\\# Heading level 2", escaped)
})
t.Run("Escape heading level 3", func(t *testing.T) {
input := "### Heading level 3"
escaped := markdown.Escape(input)
assert.Equal(t, "\\#\\#\\# Heading level 3", escaped)
})
t.Run("Escape bold text", func(t *testing.T) {
input := "**bold text**"
escaped := markdown.Escape(input)
assert.Equal(t, "\\*\\*bold text\\*\\*", escaped)
})
t.Run("Escape italic text", func(t *testing.T) {
input := "*italic text*"
escaped := markdown.Escape(input)
assert.Equal(t, "\\*italic text\\*", escaped)
})
t.Run("Escape italic text underline", func(t *testing.T) {
input := "_italic text_"
escaped := markdown.Escape(input)
assert.Equal(t, "\\_italic text\\_", escaped)
})
t.Run("Escape strikethrough", func(t *testing.T) {
input := "~~strikethrough~~"
escaped := markdown.Escape(input)
assert.Equal(t, "\\~\\~strikethrough\\~\\~", escaped)
})
t.Run("Escape Ordered list item", func(t *testing.T) {
input := "1. Ordered list item\n2. Second ordered list item\n999999999999. 999999999999 ordered list item"
escaped := markdown.Escape(input)
assert.Equal(t, "1\\. Ordered list item\n2\\. Second ordered list item\n999999999999\\. 999999999999 ordered list item", escaped)
})
t.Run("Escape Unordered list item", func(t *testing.T) {
input := "- Unordered list\n + using plus"
escaped := markdown.Escape(input)
assert.Equal(t, "\\- Unordered list\n \\+ using plus", escaped)
})
t.Run("Escape bullet list item", func(t *testing.T) {
input := "* Bullet list item"
escaped := markdown.Escape(input)
assert.Equal(t, "\\* Bullet list item", escaped)
})
t.Run("Escape table", func(t *testing.T) {
input := "| Table | Example |\n|-|-|\n| Lorem | Ipsum |"
escaped := markdown.Escape(input)
assert.Equal(t, "\\| Table \\| Example \\|\n\\|-\\|-\\|\n\\| Lorem \\| Ipsum \\|", escaped)
})
t.Run("Escape link", func(t *testing.T) {
input := "[Link to Forgejo](https://forgejo.org/)"
escaped := markdown.Escape(input)
assert.Equal(t, "\\[Link to Forgejo\\]\\(https://forgejo.org/\\)", escaped)
})
t.Run("Escape Alt text for an image", func(t *testing.T) {
input := "![Alt text for an image](https://forgejo.org/_astro/mascot-dark.1omhhgvT_Zm0N2n.webp)"
escaped := markdown.Escape(input)
assert.Equal(t, "\\!\\[Alt text for an image\\]\\(https://forgejo.org/\\_astro/mascot-dark.1omhhgvT\\_Zm0N2n.webp\\)", escaped)
})
t.Run("Escape URL if it has markdown character", func(t *testing.T) {
input := "https://forgejo.org/_astro/mascot-dark.1omhhgvT_Zm0N2n.webp"
escaped := markdown.Escape(input)
assert.Equal(t, "https://forgejo.org/\\_astro/mascot-dark.1omhhgvT\\_Zm0N2n.webp", escaped)
})
t.Run("Escape blockquote text", func(t *testing.T) {
input := "> Blockquote text."
escaped := markdown.Escape(input)
assert.Equal(t, "\\> Blockquote text.", escaped)
})
t.Run("Escape inline code", func(t *testing.T) {
input := "`Inline code`"
escaped := markdown.Escape(input)
assert.Equal(t, "\\`Inline code\\`", escaped)
})
t.Run("Escape multiple code", func(t *testing.T) {
input := "```\nCode block\nwith multiple lines\n```\n"
escaped := markdown.Escape(input)
assert.Equal(t, "\\`\\`\\`\nCode block\nwith multiple lines\n\\`\\`\\`\n", escaped)
})
t.Run("Escape horizontal rule", func(t *testing.T) {
input := "---"
escaped := markdown.Escape(input)
assert.Equal(t, "\\---", escaped)
})
for name, test := range escapedMarkdownTests {
t.Run(name, func(t *testing.T) {
if got, expected := markdown.Escape(test.input), test.expected; got != expected {
t.Errorf("markdown.Escape(%q) return %q; expected %q", test.input, got, expected)
}
})
}
}