From 2eda50a592736320aa9fb101f579975f608e9721 Mon Sep 17 00:00:00 2001 From: Nick Dumas Date: Fri, 23 Jun 2023 22:34:36 -0400 Subject: [PATCH] expanding test case coverage --- wikilink_test.go | 90 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 30 deletions(-) diff --git a/wikilink_test.go b/wikilink_test.go index 18d63ce..0e02d00 100644 --- a/wikilink_test.go +++ b/wikilink_test.go @@ -6,40 +6,70 @@ import ( "code.ndumas.com/ndumas/wikilink-parser" ) -func Test_Extract_Link(t *testing.T) { +func Test_Wikilink_Parsing(t *testing.T) { t.Parallel() tcs := []struct { - name string - in string - expected string + name string + in string + link, alias, fragment string }{ - {name: "wikilink", in: "[[wikilink]]", expected: "wikilink"}, - {name: "wikilink|display name", in: "[[wikilink|display name]]", expected: "wikilink"}, - {name: "wikilink|display name|second pipe", in: "[[wikilink|display name|second pipe]]", expected: "wikilink"}, - {name: "wikilink with numeric alias|420|second pipe", in: "[[wikilink|420|second pipe]]", expected: "wikilink with numeric aliases"}, - {name: "wikilink with spaces in filename", in: "[[wikilink spaces]]", expected: "wikilink"}, - {name: "#heading", in: "[[#heading]]", expected: ""}, - {name: "wikilink#heading", in: "[[wikilink#heading]]", expected: "wikilink"}, - {name: "wikilink#heading|display name", in: "[[wikilink#heading|display name]]", expected: "wikilink"}, - {name: "wikilink#heading|display name", in: "[[wikilink#heading|display name]]", expected: "wikilink"}, - {name: "wikilink with numeric aliases#heading|420|display name", in: "[[wikilink#heading|420|second pipe]]", expected: "wikilink"}, - {name: "^blockRef", in: "[[^blockRef]]", expected: ""}, - {name: "wikilink^blockRef", in: "[[wikilink^blockRef]]", expected: "wikilink"}, - {name: "wikilink^blockRef|display name", in: "[[wikilink#^blockRef|display name]]", expected: "wikilink"}, - {name: "wikilink^blockRef|display name|second pipe", in: "[[wikilink#^blockRef|display name|second pipe]]", expected: "wikilink"}, - {name: "wikilink with numeric aliases^blockRef|420|second pipe", in: "[[wikilink#^blockRef|420|second pipe]]", expected: "wikilink"}, + {name: "wikilink", in: "[[wikilink]]", link: "wikilink"}, + {name: "wikilink|display name", in: "[[wikilink|display name]]", link: "wikilink", alias: "display name"}, + {name: "wikilink|display name|second pipe", in: "[[wikilink|display name|second pipe]]", link: "wikilink", alias: "display name|second pipe"}, + {name: "wikilink with numeric alias|420|second pipe", in: "[[wikilink|420|second pipe]]", link: "wikilink with numeric aliases", alias: "420|second pipe"}, + {name: "wikilink with spaces in filename", in: "[[wikilink spaces]]", link: "wikilink spaces"}, + {name: "#heading", in: "[[#heading]]", link: "", fragment: "heading"}, + {name: "wikilink#heading", in: "[[wikilink#heading]]", link: "wikilink", fragment: "heading"}, + {name: "wikilink#heading|display name", in: "[[wikilink#heading|display name]]", link: "wikilink", alias: "display name", fragment: "heading"}, + {name: "wikilink#heading|display name|second pipe", in: "[[wikilink#heading|display name|second pipe]]", link: "wikilink", alias: "display name|second pipe", fragment: "heading"}, + {name: "wikilink with numeric aliases#heading|420|display name", in: "[[wikilink#heading|420|second pipe]]", link: "wikilink", alias: "420|second pipe", fragment: "heading"}, + {name: "^blockRef", in: "[[^blockRef]]", link: "", fragment: "blockRef"}, + {name: "wikilink^blockRef", in: "[[wikilink^blockRef]]", link: "wikilink", fragment: "blockRef"}, + {name: "wikilink^blockRef|display name", in: "[[wikilink#^blockRef|display name]]", link: "wikilink", alias: "display name", fragment: "blockRef"}, + {name: "wikilink^blockRef|display name|second pipe", in: "[[wikilink#^blockRef|display name|second pipe]]", link: "wikilink", alias: "display name|second pipe", fragment: "blockRef"}, + {name: "wikilink with numeric aliases^blockRef|420|second pipe", in: "[[wikilink#^blockRef|420|second pipe]]", link: "wikilink", alias: "420|second pipe", fragment: "blockRef"}, } - for _, tc := range tcs { - tc := tc - t.Run(tc.name, func(t *testing.T) { - t.Parallel() - out := wikilink.Extract(tc.in) - if out.Link != tc.expected { - t.Logf("got %#v\n", out) - t.Fail() - } - }) - } + t.Run("link", func(t *testing.T) { + for _, tc := range tcs { + tc := tc + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + out := wikilink.Extract(tc.in) + if out.Link != tc.link { + t.Logf("got %#v\n", out) + t.Fail() + } + }) + } + }) + + t.Run("alias", func(t *testing.T) { + for _, tc := range tcs { + tc := tc + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + out := wikilink.Extract(tc.in) + if out.Alias != tc.alias { + t.Logf("got %#v\n", out) + t.Fail() + } + }) + } + }) + + t.Run("fragment", func(t *testing.T) { + for _, tc := range tcs { + tc := tc + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + out := wikilink.Extract(tc.in) + if out.Fragment != tc.fragment { + t.Logf("got %#v\n", out) + t.Fail() + } + }) + } + }) }