From 82dd27bd0612f9625043d406b363d41f074e4405 Mon Sep 17 00:00:00 2001 From: Nick Dumas Date: Mon, 3 Jul 2023 09:50:03 -0400 Subject: [PATCH] More tests --- cmd/demo/main.go | 2 +- lexer.go | 2 +- lexer_test.go | 36 +++++++++++++++++++++++++++++++----- states.go | 1 + 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/cmd/demo/main.go b/cmd/demo/main.go index 1e4f13f..7a7e74d 100644 --- a/cmd/demo/main.go +++ b/cmd/demo/main.go @@ -7,7 +7,7 @@ import ( ) func main() { - l := wikilink.Lex("debugLexer", `[[#heading]]`) + l := wikilink.Lex("debugLexer", `this is a [[wikilink]]`) for _, item := range l.Items { item := item log.Printf("%#+v\n", item) diff --git a/lexer.go b/lexer.go index a97bd4b..53c1244 100644 --- a/lexer.go +++ b/lexer.go @@ -150,7 +150,7 @@ func (l *Lexer) emit(t LexemeType) { L := l.L.Named("emit").With( zap.String("item", i.String()), ) - L.Info("emitting lexeme") + L.Debug("emitting lexeme") l.Items = append(l.Items, i) l.SetStart(l.GetPos()) /* original concurrent implementation diff --git a/lexer_test.go b/lexer_test.go index 8dc33cb..e6c8f35 100644 --- a/lexer_test.go +++ b/lexer_test.go @@ -209,16 +209,34 @@ var testCases = []tc{ }, } +func Test_ObsidianWikilinks_LinksStartOfInput(t *testing.T) { + for _, tc := range testCases { + mut, test := mutateTestCase( + tc, + "", + " test data please ignore", + + []wikilink.Lexeme{ + {Typ: wikilink.LexText, Val: ""}, + }, + []wikilink.Lexeme{ + {Typ: wikilink.LexText, Val: " test data please ignore"}, + }, + ) + t.Run(mut.name, test) + } +} + func Test_ObsidianWikilinks_LinksEndOfInput(t *testing.T) { for _, tc := range testCases { - mut, test := mutateTestCase(tc, + mut, test := mutateTestCase( + tc, "this is a ", "", []wikilink.Lexeme{ {Typ: wikilink.LexText, Val: "this is a "}, }, nil, - "", ) t.Run(mut.name, test) } @@ -228,7 +246,15 @@ func Test_ObsidianWikilinks_Basic(t *testing.T) { // t.Parallel() for _, tc := range testCases { - mut, test := mutateTestCase(tc, "", "", nil, nil, "") + mut, test := mutateTestCase( + tc, + "", + "", + []wikilink.Lexeme{ + {Typ: wikilink.LexText, Val: ""}, + }, + nil, + ) t.Run(mut.name, test) } } @@ -239,8 +265,7 @@ type tc struct { expected []wikilink.Lexeme } -func mutateTestCase(tc tc, prefix, suffix string, expectedPrefix, expectedSuffix []wikilink.Lexeme, name string) (tc, func(t *testing.T)) { - tc.name = tc.name + "/" + name +func mutateTestCase(tc tc, prefix, suffix string, expectedPrefix, expectedSuffix []wikilink.Lexeme) (tc, func(t *testing.T)) { tc.in = prefix + tc.in tc.in = tc.in + suffix if expectedPrefix != nil { @@ -256,6 +281,7 @@ func mutateTestCase(tc tc, prefix, suffix string, expectedPrefix, expectedSuffix 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.Logf("expected items: %#v\n", tc.expected) t.Logf("raw items: %#v\n", l.Items) t.Fail() diff --git a/states.go b/states.go index 37bf875..7ad8451 100644 --- a/states.go +++ b/states.go @@ -87,6 +87,7 @@ func lexText(l *Lexer) stateFn { for { if isOpenLink(l.input[l.GetPos():]) { L.Debug("found openLink") + l.emit(LexText) return lexOpenLink } r := l.next()