aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnhgelus Morhtuuzh <william@herges.fr>2025-12-20 18:24:09 +0100
committerAnhgelus Morhtuuzh <william@herges.fr>2025-12-20 18:24:09 +0100
commit73c4a664b03f8afa8edb527fb6a8cc310bdb3380 (patch)
tree9ba2d2af2149024ea0dc05a88dfcecb351c90139
parent90246ee314cfbb6b6f0b43b51e392b6b5293b867 (diff)
test(markdown): use subtest to clean
-rw-r--r--markdown/ast_code_test.go33
-rw-r--r--markdown/ast_external_test.go34
-rw-r--r--markdown/ast_list_test.go17
-rw-r--r--markdown/ast_modifier_test.go19
-rw-r--r--markdown/ast_paragraph_test.go37
-rw-r--r--markdown/ast_quote_test.go22
-rw-r--r--markdown/ast_test.go29
7 files changed, 58 insertions, 133 deletions
diff --git a/markdown/ast_code_test.go b/markdown/ast_code_test.go
index b116544..f9c57f3 100644
--- a/markdown/ast_code_test.go
+++ b/markdown/ast_code_test.go
@@ -3,31 +3,10 @@ package markdown
import "testing"
func TestCode(t *testing.T) {
- got, err := Parse("`mono`", nil)
- if err != nil {
- t.Fatal(err)
- }
- if string(got) != `<p><code>mono</code></p>` {
- t.Errorf("invalid value, got %s", got)
- }
-
- got, err = Parse("bonjour `code` !", nil)
- if err != nil {
- t.Fatal(err)
- }
- if string(got) != `<p>bonjour <code>code</code> !</p>` {
- t.Errorf("invalid value, got %s", got)
- }
-
- got, err = Parse(
- "```\n"+"raw\nhehe"+"```",
- nil,
- )
- if err != nil {
- t.Fatal(err)
- }
- if string(got) != `<pre><code>raw
-hehe</code></pre>` {
- t.Errorf("invalid value, got %s", got)
- }
+ t.Run("code", func(t *testing.T) {
+ t.Run("mono", test("`mono`", `<p><code>mono</code></p>`))
+ t.Run("combo", test("bonjour `code` !", `<p>bonjour <code>code</code> !</p>`))
+ t.Run("mult-line", test("```\n"+"raw\nhehe"+"```", `<pre><code>raw
+hehe</code></pre>`))
+ })
}
diff --git a/markdown/ast_external_test.go b/markdown/ast_external_test.go
index c31c233..ec9dd29 100644
--- a/markdown/ast_external_test.go
+++ b/markdown/ast_external_test.go
@@ -3,35 +3,19 @@ package markdown
import "testing"
func TestExternal(t *testing.T) {
- got, err := Parse("[content](href)", nil)
- if err != nil {
- t.Fatal(err)
- }
- if string(got) != `<p><a href="href">content</a></p>` {
- t.Errorf("invalid value, got %s", got)
- }
-
- got, err = Parse("![image alt](image src)", nil)
- if err != nil {
- t.Fatal(err)
- }
- if string(got) != `<figure><img alt="image alt" src="image src" /></figure>` {
- t.Errorf("invalid value, got %s", got)
- }
-
- got, err = Parse(`
+ t.Run("link", func(t *testing.T) {
+ t.Run("simple", test("[content](href)", `<p><a href="href">content</a></p>`))
+ t.Run("combo", test("Hey, [link](href)", `<p>Hey, <a href="href">link</a></p>`))
+ })
+ t.Run("image", func(t *testing.T) {
+ t.Run("simple", test("![image alt](image src)", `<figure><img alt="image alt" src="image src" /></figure>`))
+ t.Run("combo", test(`
Avant la source
![image alt](image src)
source 1
source 2
Hors de la source
-`, nil)
- if err != nil {
- t.Fatal(err)
- }
- if string(got) != `<p>Avant la source</p><figure><img alt="image alt" src="image src" /><figcaption>source 1 source 2</figcaption></figure><p>Hors de la source</p>` {
- t.Errorf("invalid value, got %s", got)
- }
-
+`, `<p>Avant la source</p><figure><img alt="image alt" src="image src" /><figcaption>source 1 source 2</figcaption></figure><p>Hors de la source</p>`))
+ })
}
diff --git a/markdown/ast_list_test.go b/markdown/ast_list_test.go
index 210a83a..6ecfc25 100644
--- a/markdown/ast_list_test.go
+++ b/markdown/ast_list_test.go
@@ -27,18 +27,7 @@ var expected = `
`
func TestList(t *testing.T) {
- lxs := lex(rw, new(Option))
- tree, err := ast(lxs)
- if err != nil {
- t.Fatal(err)
- }
- got, err := tree.Eval(nil)
- if err != nil {
- t.Fatal(err)
- }
- exp := strings.ReplaceAll(expected, "\n", "")
- if string(got) != exp {
- t.Errorf("invalid value, got %s", got)
- t.Logf("expected %s", exp)
- }
+ t.Run("lists", func(t *testing.T) {
+ t.Run("combo", test(rw, strings.ReplaceAll(expected, "\n", "")))
+ })
}
diff --git a/markdown/ast_modifier_test.go b/markdown/ast_modifier_test.go
index 3689657..acb9f40 100644
--- a/markdown/ast_modifier_test.go
+++ b/markdown/ast_modifier_test.go
@@ -3,20 +3,7 @@ package markdown
import "testing"
func TestModifier(t *testing.T) {
- content := `
-**bo*n*soir**, ça ***va* bien** ?
-`
- lxs := lex(content, new(Option))
- tree, err := ast(lxs)
- if err != nil {
- t.Fatal(err)
- }
- c, err := tree.Eval(nil)
- if err != nil {
- t.Fatal(err)
- }
- if c != "<p><b>bo<em>n</em>soir</b>, ça <b><em>va</em> bien</b> ?</p>" {
- t.Errorf("failed, got %s", c)
- t.Logf("lxs: %s\ntree: %s", lxs, tree)
- }
+ t.Run("modifiers", func(t *testing.T) {
+ t.Run("combo", test(`**bo*n*soir**, ça ***va* bien** ?`, `<p><b>bo<em>n</em>soir</b>, ça <b><em>va</em> bien</b> ?</p>`))
+ })
}
diff --git a/markdown/ast_paragraph_test.go b/markdown/ast_paragraph_test.go
index 2014ab9..ab3ab6c 100644
--- a/markdown/ast_paragraph_test.go
+++ b/markdown/ast_paragraph_test.go
@@ -3,31 +3,14 @@ package markdown
import "testing"
func TestParagraph(t *testing.T) {
- c, err := Parse("bonsoir", nil)
- if err != nil {
- t.Fatal(err)
- }
- if c != "<p>bonsoir</p>" {
- t.Errorf("failed, got %s", c)
- }
-}
-
-func TestParagraph_Replacer(t *testing.T) {
- opt := &Option{
- Replaces: map[rune]string{'~': "&thinsp;"},
- }
- c, err := Parse("bonsoir", opt)
- if err != nil {
- t.Fatal(err)
- }
- if c != "<p>bonsoir</p>" {
- t.Errorf("failed, got %s", c)
- }
- c, err = Parse("bonsoir~!", opt)
- if err != nil {
- t.Fatal(err)
- }
- if c != "<p>bonsoir&thinsp;!</p>" {
- t.Errorf("failed, got %s", c)
- }
+ t.Run("paragraph", func(t *testing.T) {
+ t.Run("simple", test("bonsoir", `<p>bonsoir</p>`))
+ })
+ t.Run("replacer", func(t *testing.T) {
+ opt := &Option{
+ Replaces: map[rune]string{'~': "&thinsp;"},
+ }
+ t.Run("empty", testWithOptions(opt, "bonsoir", `<p>bonsoir</p>`))
+ t.Run("simple", testWithOptions(opt, "bonsoir~!", `<p>bonsoir&thinsp;!</p>`))
+ })
}
diff --git a/markdown/ast_quote_test.go b/markdown/ast_quote_test.go
index 50ff23a..203d4fc 100644
--- a/markdown/ast_quote_test.go
+++ b/markdown/ast_quote_test.go
@@ -3,21 +3,13 @@ package markdown
import "testing"
func TestQuote(t *testing.T) {
- content := `
+ t.Run("quote", func(t *testing.T) {
+ t.Run("simple", test(`
+> Bonsoir
+`, `<div class="quote"><blockquote>Bonsoir</blockquote></div>`))
+ t.Run("source", test(`
> Bonsoir, je suis un **code**
avec une source
-`
- lxs := lex(content, new(Option))
- tree, err := ast(lxs)
- if err != nil {
- t.Fatal(err)
- }
- c, err := tree.Eval(nil)
- if err != nil {
- t.Fatal(err)
- }
- if c != `<div class="quote"><blockquote>Bonsoir, je suis un <b>code</b></blockquote><p>avec une source</p></div>` {
- t.Errorf("failed, got %s", c)
- t.Logf("lxs: %s\ntree: %s", lxs, tree)
- }
+`, `<div class="quote"><blockquote>Bonsoir, je suis un <b>code</b></blockquote><p>avec une source</p></div>`))
+ })
}
diff --git a/markdown/ast_test.go b/markdown/ast_test.go
index 8f701f4..ed2ef87 100644
--- a/markdown/ast_test.go
+++ b/markdown/ast_test.go
@@ -45,14 +45,25 @@ var parsed = `
</figure>
`
-func TestAst(t *testing.T) {
- res, err := Parse(raw, nil)
- if err != nil {
- t.Fatal(err)
- }
- wanted := strings.ReplaceAll(parsed, "\n", "")
- if string(res) != wanted {
- t.Errorf("invalid string, got\n%s", res)
- t.Logf("wanted\n%s", wanted)
+func test(input, expected string) func(*testing.T) {
+ return testWithOptions(nil, input, expected)
+}
+
+func testWithOptions(opt *Option, input, expected string) func(*testing.T) {
+ return func(t *testing.T) {
+ t.Parallel()
+ got, err := Parse(input, opt)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if string(got) != expected {
+ t.Errorf("invalid value, got %s", got)
+ }
}
}
+
+func TestAst(t *testing.T) {
+ t.Run("ast", func(t *testing.T) {
+ t.Run("complete", test(raw, strings.ReplaceAll(parsed, "\n", "")))
+ })
+}