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.
wikilinks-parser/lexer_test.go

70 lines
2.9 KiB
Go

package wikilink_test
import (
"testing"
"code.ndumas.com/ndumas/wikilink-parser"
)
func Test_Lexer(t *testing.T) {
// t.Parallel()
tcs := []struct {
name string
in string
expected []wikilink.Item
}{
{
name: "wikilink", in: "[[wikilink]]", expected: []wikilink.Item{
{Typ: wikilink.ItemOpenLink, Val: "[["},
{Typ: wikilink.ItemText, Val: "wikilink"},
{Typ: wikilink.ItemCloseLink, Val: "]]"},
},
},
{
name: "wikilink|display name", in: "[[wikilink|display name]]", expected: []wikilink.Item{
{Typ: wikilink.ItemOpenLink, Val: "[["},
{Typ: wikilink.ItemText, Val: "wikilink"},
{Typ: wikilink.ItemAlias, Val: "|"},
{Typ: wikilink.ItemText, Val: "display name"},
{Typ: wikilink.ItemCloseLink, Val: "]]"},
},
},
// {name: "", in: "", expected: []wikilink.ItemType{wikilink.ItemOpenLink, wikilink.ItemText, wikilink.ItemAlias, wikilink.ItemText, wikilink.ItemCloseLink}},
/*
{name: "wikilink|display name|second pipe", in: "[[wikilink|display name|second pipe]]", expected: []wikilink.ItemType{}},
{name: "wikilink with numeric alias|420|second pipe", in: "[[wikilink|420|second pipe]]", expected: []wikilink.ItemType{}},
{name: "wikilink with spaces in filename", in: "[[wikilink spaces]]", expected: []wikilink.ItemType{}},
{name: "#heading", in: "[[#heading]]", expected: []wikilink.ItemType{}},
{name: "wikilink#heading", in: "[[wikilink#heading]]", 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{}},
*/
}
for _, tc := range tcs {
tc := tc
t.Run(tc.name, func(t *testing.T) {
// t.Parallel()
l := wikilink.Lex("testLexer", tc.in)
for _, e := range tc.expected {
n := l.NextItem()
if e.Typ != n.Typ {
t.Logf("expected Type %s, received %s\n with raw value %q", e.String(), n, n.Val)
t.Fail()
}
if e.Val != n.Val {
t.Logf("expected Value %s, received %s\n with raw value %q", e.String(), n, n.Val)
t.Fail()
}
}
})
}
}