More testing

main
Nick Dumas 2 years ago
parent 24b7320e0f
commit 126dd495ba

@ -5,6 +5,7 @@ import (
"fmt"
"log"
"strings"
// "unicode"
"unicode/utf8"
)
@ -136,11 +137,17 @@ func lexFragment(l *Lexer) stateFn {
func lexAlias(l *Lexer) stateFn {
// l.accept
log.Println("entering lexAlias")
if strings.HasPrefix(l.input[l.pos:], CloseLink) {
return lexCloseLink
}
l.emit(ItemAlias)
return lexInsideLink
}
func lexInsideLink(l *Lexer) stateFn {
log.Println("entering lexInsideLink")
for {
if strings.HasPrefix(l.input[l.pos:], CloseLink) {
return lexCloseLink
@ -154,12 +161,15 @@ func lexInsideLink(l *Lexer) stateFn {
return l.errorf("unclosed link")
case r == '#':
l.emit(ItemText)
return lexFragment
case r == '|':
l.emit(ItemText)
return lexAlias
case l.peek() == ']':
l.emit(ItemText)
return lexCloseLink
}
}

@ -11,11 +11,26 @@ func Test_Lexer(t *testing.T) {
tcs := []struct {
name string
in string
expected []wikilink.ItemType
expected []wikilink.Item
}{
{name: "wikilink", in: "[[wikilink]]", expected: []wikilink.ItemType{wikilink.ItemOpenLink, wikilink.ItemText, wikilink.ItemCloseLink}},
{
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", in: "[[wikilink|display name]]", expected: []wikilink.ItemType{}},
{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{}},
@ -39,8 +54,13 @@ func Test_Lexer(t *testing.T) {
l := wikilink.Lex("testLexer", tc.in)
for _, e := range tc.expected {
n := l.NextItem()
if e != n.Typ {
t.Logf("expected %s, received %s\n with raw value %q", e, n, n.Val)
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()
}
}

Loading…
Cancel
Save