From ebeea16b0db01161c84503b9d0e05ebf464111e6 Mon Sep 17 00:00:00 2001 From: Nick Dumas Date: Sun, 2 Jul 2023 10:42:39 -0400 Subject: [PATCH] Full test suite for Obsidian wikilinks --- lexer_test.go | 164 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 144 insertions(+), 20 deletions(-) diff --git a/lexer_test.go b/lexer_test.go index a0a7293..e28a6ea 100644 --- a/lexer_test.go +++ b/lexer_test.go @@ -6,7 +6,7 @@ import ( "code.ndumas.com/ndumas/wikilink-parser" ) -func Test_Lexer(t *testing.T) { +func Test_ObsidianWikilinks(t *testing.T) { // t.Parallel() tcs := []struct { name string @@ -14,7 +14,9 @@ func Test_Lexer(t *testing.T) { expected []wikilink.Lexeme }{ { - name: "wikilink", in: "[[wikilink]]", expected: []wikilink.Lexeme{ + name: "wikilink", + in: "[[wikilink]]", + expected: []wikilink.Lexeme{ {Typ: wikilink.LexOpenLink, Val: "[["}, {Typ: wikilink.LexIdent, Val: "wikilink"}, {Typ: wikilink.LexCloseLink, Val: "]]"}, @@ -22,7 +24,9 @@ func Test_Lexer(t *testing.T) { }, }, { - name: "wikilink|display name", in: "[[wikilink|display name]]", expected: []wikilink.Lexeme{ + name: "wikilink|display name", + in: "[[wikilink|display name]]", + expected: []wikilink.Lexeme{ {Typ: wikilink.LexOpenLink, Val: "[["}, {Typ: wikilink.LexIdent, Val: "wikilink"}, {Typ: wikilink.LexAlias, Val: "|"}, @@ -32,7 +36,9 @@ func Test_Lexer(t *testing.T) { }, }, { - name: "wikilink|display name|second pipe", in: "[[wikilink|display name|second pipe]]", expected: []wikilink.Lexeme{ + name: "wikilink|display name|second pipe", + in: "[[wikilink|display name|second pipe]]", + expected: []wikilink.Lexeme{ {Typ: wikilink.LexOpenLink, Val: "[["}, {Typ: wikilink.LexIdent, Val: "wikilink"}, {Typ: wikilink.LexAlias, Val: "|"}, @@ -44,7 +50,9 @@ func Test_Lexer(t *testing.T) { }, }, { - name: "wikilink with numeric alias|420|second pipe", in: "[[wikilink|420|second pipe]]", expected: []wikilink.Lexeme{ + name: "wikilink with numeric alias|420|second pipe", + in: "[[wikilink|420|second pipe]]", + expected: []wikilink.Lexeme{ {Typ: wikilink.LexOpenLink, Val: "[["}, {Typ: wikilink.LexIdent, Val: "wikilink"}, {Typ: wikilink.LexAlias, Val: "|"}, @@ -56,7 +64,9 @@ func Test_Lexer(t *testing.T) { }, }, { - name: "wikilink with spaces in filename", in: "[[wikilink spaces]]", expected: []wikilink.Lexeme{ + name: "wikilink with spaces in filename", + in: "[[wikilink spaces]]", + expected: []wikilink.Lexeme{ {Typ: wikilink.LexOpenLink, Val: "[["}, {Typ: wikilink.LexIdent, Val: "wikilink spaces"}, {Typ: wikilink.LexCloseLink, Val: "]]"}, @@ -64,7 +74,9 @@ func Test_Lexer(t *testing.T) { }, }, { - name: "#heading", in: "[[#heading]]", expected: []wikilink.Lexeme{ + name: "#heading", + in: "[[#heading]]", + expected: []wikilink.Lexeme{ {Typ: wikilink.LexOpenLink, Val: "[["}, {Typ: wikilink.LexIdent, Val: ""}, {Typ: wikilink.LexHeading, Val: "#"}, @@ -74,7 +86,9 @@ func Test_Lexer(t *testing.T) { }, }, { - name: "wikilink#heading", in: "[[wikilink#heading]]", expected: []wikilink.Lexeme{ + name: "wikilink#heading", + in: "[[wikilink#heading]]", + expected: []wikilink.Lexeme{ {Typ: wikilink.LexOpenLink, Val: "[["}, {Typ: wikilink.LexIdent, Val: "wikilink"}, {Typ: wikilink.LexHeading, Val: "#"}, @@ -83,28 +97,134 @@ func Test_Lexer(t *testing.T) { {Typ: wikilink.LexText, Val: ""}, }, }, - /* - {name: "", in: "", expected: []wikilink.ItemType{}}, - {name: "wikilink#heading|display name", in: "[[wikilink#heading|display name]]", expected: []wikilink.ItemType{}}, - {name: "wikilink#heading|display name|second pipe", in: "[[wikilink#heading|display name|second pipe]]", expected: []wikilink.ItemType{}}, - {name: "wikilink with numeric aliases#heading|420|display name", in: "[[wikilink#heading|420|second pipe]]", expected: []wikilink.ItemType{}}, - {name: "^blockRef", in: "[[^blockRef]]", expected: []wikilink.ItemType{}}, - {name: "wikilink^blockRef", in: "[[wikilink^blockRef]]", expected: []wikilink.ItemType{}}, - {name: "wikilink^blockRef|display name", in: "[[wikilink#^blockRef|display name]]", expected: []wikilink.ItemType{}}, - {name: "wikilink^blockRef|display name|second pipe", in: "[[wikilink#^blockRef|display name|second pipe]]", expected: []wikilink.ItemType{}}, - {name: "wikilink with numeric aliases^blockRef|420|second pipe", in: "[[wikilink#^blockRef|420|second pipe]]", expected: []wikilink.ItemType{}}, - */ + { + name: "wikilink#heading|display name", + in: "[[wikilink#heading|display name]]", + expected: []wikilink.Lexeme{ + {Typ: wikilink.LexOpenLink, Val: "[["}, + {Typ: wikilink.LexIdent, Val: "wikilink"}, + {Typ: wikilink.LexHeading, Val: "#"}, + {Typ: wikilink.LexIdent, Val: "heading"}, + {Typ: wikilink.LexAlias, Val: "|"}, + {Typ: wikilink.LexIdent, Val: "display name"}, + {Typ: wikilink.LexCloseLink, Val: "]]"}, + {Typ: wikilink.LexText, Val: ""}, + }, + }, + { + name: "wikilink#heading|display name|second pipe", + in: "[[wikilink#heading|display name|second pipe]]", + expected: []wikilink.Lexeme{ + {Typ: wikilink.LexOpenLink, Val: "[["}, + {Typ: wikilink.LexIdent, Val: "wikilink"}, + {Typ: wikilink.LexHeading, Val: "#"}, + {Typ: wikilink.LexIdent, Val: "heading"}, + {Typ: wikilink.LexAlias, Val: "|"}, + {Typ: wikilink.LexIdent, Val: "display name"}, + {Typ: wikilink.LexAlias, Val: "|"}, + {Typ: wikilink.LexIdent, Val: "second pipe"}, + {Typ: wikilink.LexCloseLink, Val: "]]"}, + {Typ: wikilink.LexText, Val: ""}, + }, + }, + { + name: "wikilink with numeric aliases#heading|420|display name", + in: "[[wikilink#heading|420|second pipe]]", + expected: []wikilink.Lexeme{ + {Typ: wikilink.LexOpenLink, Val: "[["}, + {Typ: wikilink.LexIdent, Val: "wikilink"}, + {Typ: wikilink.LexHeading, Val: "#"}, + {Typ: wikilink.LexIdent, Val: "heading"}, + {Typ: wikilink.LexAlias, Val: "|"}, + {Typ: wikilink.LexIdent, Val: "420"}, + {Typ: wikilink.LexAlias, Val: "|"}, + {Typ: wikilink.LexIdent, Val: "second pipe"}, + {Typ: wikilink.LexCloseLink, Val: "]]"}, + {Typ: wikilink.LexText, Val: ""}, + }, + }, + { + name: "#^blockRef", + in: "[[#^blockRef]]", + expected: []wikilink.Lexeme{ + {Typ: wikilink.LexOpenLink, Val: "[["}, + {Typ: wikilink.LexIdent, Val: ""}, + {Typ: wikilink.LexBlockRef, Val: "#^"}, + {Typ: wikilink.LexIdent, Val: "blockRef"}, + {Typ: wikilink.LexCloseLink, Val: "]]"}, + {Typ: wikilink.LexText, Val: ""}, + }, + }, + { + name: "wikilink#^blockRef", + in: "[[wikilink#^blockRef]]", + expected: []wikilink.Lexeme{ + {Typ: wikilink.LexOpenLink, Val: "[["}, + {Typ: wikilink.LexIdent, Val: "wikilink"}, + {Typ: wikilink.LexBlockRef, Val: "#^"}, + {Typ: wikilink.LexIdent, Val: "blockRef"}, + {Typ: wikilink.LexCloseLink, Val: "]]"}, + {Typ: wikilink.LexText, Val: ""}, + }, + }, + { + name: "wikilink#^blockRef|display name", + in: "[[wikilink#^blockRef|display name]]", + expected: []wikilink.Lexeme{ + {Typ: wikilink.LexOpenLink, Val: "[["}, + {Typ: wikilink.LexIdent, Val: "wikilink"}, + {Typ: wikilink.LexBlockRef, Val: "#^"}, + {Typ: wikilink.LexIdent, Val: "blockRef"}, + {Typ: wikilink.LexAlias, Val: "|"}, + {Typ: wikilink.LexIdent, Val: "display name"}, + {Typ: wikilink.LexCloseLink, Val: "]]"}, + {Typ: wikilink.LexText, Val: ""}, + }, + }, + { + name: "wikilink#^blockRef|display name|second pipe", + in: "[[wikilink#^blockRef|display name|second pipe]]", + expected: []wikilink.Lexeme{ + {Typ: wikilink.LexOpenLink, Val: "[["}, + {Typ: wikilink.LexIdent, Val: "wikilink"}, + {Typ: wikilink.LexBlockRef, Val: "#^"}, + {Typ: wikilink.LexIdent, Val: "blockRef"}, + {Typ: wikilink.LexAlias, Val: "|"}, + {Typ: wikilink.LexIdent, Val: "display name"}, + {Typ: wikilink.LexAlias, Val: "|"}, + {Typ: wikilink.LexIdent, Val: "second pipe"}, + {Typ: wikilink.LexCloseLink, Val: "]]"}, + {Typ: wikilink.LexText, Val: ""}, + }, + }, + { + name: "wikilink with numeric aliases#^blockRef|420|second pipe", + in: "[[wikilink#^blockRef|420|second pipe]]", + expected: []wikilink.Lexeme{ + {Typ: wikilink.LexOpenLink, Val: "[["}, + {Typ: wikilink.LexIdent, Val: "wikilink"}, + {Typ: wikilink.LexBlockRef, Val: "#^"}, + {Typ: wikilink.LexIdent, Val: "blockRef"}, + {Typ: wikilink.LexAlias, Val: "|"}, + {Typ: wikilink.LexIdent, Val: "420"}, + {Typ: wikilink.LexAlias, Val: "|"}, + {Typ: wikilink.LexIdent, Val: "second pipe"}, + {Typ: wikilink.LexCloseLink, Val: "]]"}, + {Typ: wikilink.LexText, Val: ""}, + }, + }, } for _, tc := range tcs { tc := tc t.Run(tc.name, func(t *testing.T) { - // t.Parallel() + 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 { @@ -112,11 +232,15 @@ func Test_Lexer(t *testing.T) { 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() + + return } } })