You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
308 lines
8.5 KiB
Go
308 lines
8.5 KiB
Go
package wikilink_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"code.ndumas.com/ndumas/wikilink-parser"
|
|
)
|
|
|
|
var testCases = []tc{
|
|
{
|
|
name: "wikilink",
|
|
in: "[[wikilink]]",
|
|
expected: []wikilink.Lexeme{
|
|
{Typ: wikilink.LexOpenLink, Val: "[["},
|
|
{Typ: wikilink.LexIdent, Val: "wikilink"},
|
|
{Typ: wikilink.LexCloseLink, Val: "]]"},
|
|
{Typ: wikilink.LexText, Val: ""},
|
|
},
|
|
},
|
|
{
|
|
name: "wikilink|display name",
|
|
in: "[[wikilink|display name]]",
|
|
expected: []wikilink.Lexeme{
|
|
{Typ: wikilink.LexOpenLink, Val: "[["},
|
|
{Typ: wikilink.LexIdent, Val: "wikilink"},
|
|
{Typ: wikilink.LexAlias, Val: "|"},
|
|
{Typ: wikilink.LexIdent, Val: "display name"},
|
|
{Typ: wikilink.LexCloseLink, Val: "]]"},
|
|
{Typ: wikilink.LexText, Val: ""},
|
|
},
|
|
},
|
|
{
|
|
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: "|"},
|
|
{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 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: "|"},
|
|
{Typ: wikilink.LexIdent, Val: "420"},
|
|
{Typ: wikilink.LexAlias, Val: "|"},
|
|
{Typ: wikilink.LexIdent, Val: "second pipe"},
|
|
{Typ: wikilink.LexCloseLink, Val: "]]"},
|
|
{Typ: wikilink.LexText, Val: ""},
|
|
},
|
|
},
|
|
{
|
|
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: "]]"},
|
|
{Typ: wikilink.LexText, Val: ""},
|
|
},
|
|
},
|
|
{
|
|
name: "#heading",
|
|
in: "[[#heading]]",
|
|
expected: []wikilink.Lexeme{
|
|
{Typ: wikilink.LexOpenLink, Val: "[["},
|
|
{Typ: wikilink.LexIdent, Val: ""},
|
|
{Typ: wikilink.LexHeading, Val: "#"},
|
|
{Typ: wikilink.LexIdent, Val: "heading"},
|
|
{Typ: wikilink.LexCloseLink, Val: "]]"},
|
|
{Typ: wikilink.LexText, Val: ""},
|
|
},
|
|
},
|
|
{
|
|
name: "wikilink#heading",
|
|
in: "[[wikilink#heading]]",
|
|
expected: []wikilink.Lexeme{
|
|
{Typ: wikilink.LexOpenLink, Val: "[["},
|
|
{Typ: wikilink.LexIdent, Val: "wikilink"},
|
|
{Typ: wikilink.LexHeading, Val: "#"},
|
|
{Typ: wikilink.LexIdent, Val: "heading"},
|
|
{Typ: wikilink.LexCloseLink, Val: "]]"},
|
|
{Typ: wikilink.LexText, Val: ""},
|
|
},
|
|
},
|
|
{
|
|
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: ""},
|
|
},
|
|
},
|
|
}
|
|
|
|
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,
|
|
"this is a ",
|
|
"",
|
|
[]wikilink.Lexeme{
|
|
{Typ: wikilink.LexText, Val: "this is a "},
|
|
},
|
|
nil,
|
|
)
|
|
t.Run(mut.name, test)
|
|
}
|
|
}
|
|
|
|
func Test_ObsidianWikilinks_Basic(t *testing.T) {
|
|
// t.Parallel()
|
|
|
|
for _, tc := range testCases {
|
|
mut, test := mutateTestCase(
|
|
tc,
|
|
"",
|
|
"",
|
|
[]wikilink.Lexeme{
|
|
{Typ: wikilink.LexText, Val: ""},
|
|
},
|
|
nil,
|
|
)
|
|
t.Run(mut.name, test)
|
|
}
|
|
}
|
|
|
|
type tc struct {
|
|
name string
|
|
in string
|
|
expected []wikilink.Lexeme
|
|
}
|
|
|
|
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 {
|
|
tc.expected = append(expectedPrefix, tc.expected...)
|
|
}
|
|
if expectedSuffix != nil {
|
|
tc.expected = append(tc.expected, expectedSuffix...)
|
|
}
|
|
|
|
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.Logf("expected items: %#v\n", tc.expected)
|
|
t.Logf("raw items: %#v\n", 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
|
|
}
|
|
|
|
if e.Val != n.Val {
|
|
t.Logf("expected Value %q, received %q", e.Val, n.Val)
|
|
t.Fail()
|
|
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|