From 25f949fafda6ceb4e9759bd1d333b41992a17bbd Mon Sep 17 00:00:00 2001 From: Nick Dumas Date: Mon, 3 Jul 2023 09:07:33 -0400 Subject: [PATCH] refactoring tests to make this easier --- lexer_test.go | 60 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/lexer_test.go b/lexer_test.go index 0debba1..5734f1d 100644 --- a/lexer_test.go +++ b/lexer_test.go @@ -6,7 +6,7 @@ import ( "code.ndumas.com/ndumas/wikilink-parser" ) -func Test_ObsidianWikilinks_LinksIntext(t *testing.T) { +func Test_ObsidianWikilinks_LinksEndOfInput(t *testing.T) { tcs := []struct { name string in string @@ -14,7 +14,7 @@ func Test_ObsidianWikilinks_LinksIntext(t *testing.T) { }{ { name: "wikilink", - in: "this is a [[wikilink]]", + in: "[[wikilink]]", expected: []wikilink.Lexeme{ {Typ: wikilink.LexText, Val: "this is a "}, {Typ: wikilink.LexOpenLink, Val: "[["}, @@ -25,7 +25,7 @@ func Test_ObsidianWikilinks_LinksIntext(t *testing.T) { }, { name: "wikilink|display name", - in: "this is a [[wikilink|display name]]", + in: "is a [[wikilink|display name]]", expected: []wikilink.Lexeme{ {Typ: wikilink.LexText, Val: "this is a "}, {Typ: wikilink.LexOpenLink, Val: "[["}, @@ -457,33 +457,45 @@ func Test_ObsidianWikilinks_Basic(t *testing.T) { } for _, tc := range tcs { - tc := tc - t.Run(tc.name, func(t *testing.T) { - // t.Parallel() - l := wikilink.Lex("testLexer", tc.in) - defer l.L.Sync() - if len(tc.expected) != len(l.Items) { - t.Logf("expected %d tokens, got %d\n", len(tc.expected), len(l.Items)) + mut, test := mutateTestCase(tc, "", "") + t.Run(mut.name, test) + } +} + +type tc struct { + name string + in string + expected []wikilink.Lexeme +} + +func mutateTestCase(tc tc, prefix, suffix string) (tc, func(t *testing.T)) { + tc.in = prefix + tc.in + tc.in = tc.in + suffix + return tc, func(t *testing.T) { + // t.Parallel() + l := wikilink.Lex("testLexer", tc.in) + defer l.L.Sync() + if len(tc.expected) != len(l.Items) { + t.Logf("expected %d tokens, got %d\n", len(tc.expected), len(l.Items)) + t.Fail() + + return + } + for i, e := range tc.expected { + n := l.Items[i] + if e.Typ != n.Typ { + t.Logf("expected Type %s, received %s", e.Typ.String(), n.Typ.String()) t.Fail() return } - for i, e := range tc.expected { - n := l.Items[i] - if e.Typ != n.Typ { - t.Logf("expected Type %s, received %s", e.Typ.String(), n.Typ.String()) - t.Fail() - return - } - - if e.Val != n.Val { - t.Logf("expected Value %q, received %q", e.Val, n.Val) - t.Fail() + if e.Val != n.Val { + t.Logf("expected Value %q, received %q", e.Val, n.Val) + t.Fail() - return - } + return } - }) + } } }