More testing

main
Nick Dumas 1 year ago
parent 24b7320e0f
commit 126dd495ba

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

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

Loading…
Cancel
Save