From 73c4a664b03f8afa8edb527fb6a8cc310bdb3380 Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Sat, 20 Dec 2025 18:24:09 +0100 Subject: test(markdown): use subtest to clean --- markdown/ast_code_test.go | 33 ++++++--------------------------- markdown/ast_external_test.go | 34 +++++++++------------------------- markdown/ast_list_test.go | 17 +++-------------- markdown/ast_modifier_test.go | 19 +++---------------- markdown/ast_paragraph_test.go | 37 ++++++++++--------------------------- markdown/ast_quote_test.go | 22 +++++++--------------- markdown/ast_test.go | 29 ++++++++++++++++++++--------- 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) != `

mono

` { - t.Errorf("invalid value, got %s", got) - } - - got, err = Parse("bonjour `code` !", nil) - if err != nil { - t.Fatal(err) - } - if string(got) != `

bonjour code !

` { - t.Errorf("invalid value, got %s", got) - } - - got, err = Parse( - "```\n"+"raw\nhehe"+"```", - nil, - ) - if err != nil { - t.Fatal(err) - } - if string(got) != `
raw
-hehe
` { - t.Errorf("invalid value, got %s", got) - } + t.Run("code", func(t *testing.T) { + t.Run("mono", test("`mono`", `

mono

`)) + t.Run("combo", test("bonjour `code` !", `

bonjour code !

`)) + t.Run("mult-line", test("```\n"+"raw\nhehe"+"```", `
raw
+hehe
`)) + }) } 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) != `

content

` { - t.Errorf("invalid value, got %s", got) - } - - got, err = Parse("![image alt](image src)", nil) - if err != nil { - t.Fatal(err) - } - if string(got) != `
image alt
` { - t.Errorf("invalid value, got %s", got) - } - - got, err = Parse(` + t.Run("link", func(t *testing.T) { + t.Run("simple", test("[content](href)", `

content

`)) + t.Run("combo", test("Hey, [link](href)", `

Hey, link

`)) + }) + t.Run("image", func(t *testing.T) { + t.Run("simple", test("![image alt](image src)", `
image alt
`)) + 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) != `

Avant la source

image alt
source 1 source 2

Hors de la source

` { - t.Errorf("invalid value, got %s", got) - } - +`, `

Avant la source

image alt
source 1 source 2

Hors de la source

`)) + }) } 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 != "

bonsoir, ça va bien ?

" { - 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** ?`, `

bonsoir, ça va bien ?

`)) + }) } 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 != "

bonsoir

" { - t.Errorf("failed, got %s", c) - } -} - -func TestParagraph_Replacer(t *testing.T) { - opt := &Option{ - Replaces: map[rune]string{'~': " "}, - } - c, err := Parse("bonsoir", opt) - if err != nil { - t.Fatal(err) - } - if c != "

bonsoir

" { - t.Errorf("failed, got %s", c) - } - c, err = Parse("bonsoir~!", opt) - if err != nil { - t.Fatal(err) - } - if c != "

bonsoir !

" { - t.Errorf("failed, got %s", c) - } + t.Run("paragraph", func(t *testing.T) { + t.Run("simple", test("bonsoir", `

bonsoir

`)) + }) + t.Run("replacer", func(t *testing.T) { + opt := &Option{ + Replaces: map[rune]string{'~': " "}, + } + t.Run("empty", testWithOptions(opt, "bonsoir", `

bonsoir

`)) + t.Run("simple", testWithOptions(opt, "bonsoir~!", `

bonsoir !

`)) + }) } 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 +`, `
Bonsoir
`)) + 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 != `
Bonsoir, je suis un code

avec une source

` { - t.Errorf("failed, got %s", c) - t.Logf("lxs: %s\ntree: %s", lxs, tree) - } +`, `
Bonsoir, je suis un code

avec une source

`)) + }) } 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 = ` ` -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", ""))) + }) +} -- cgit v1.2.3