Skip to content

Commit

Permalink
Add lexer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Verseth committed Jul 2, 2023
1 parent 1da0983 commit 0a97b96
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/elk-language/go-prompt
go 1.14

require (
github.com/google/go-cmp v0.5.9 // indirect
github.com/mattn/go-colorable v0.1.7
github.com/mattn/go-runewidth v0.0.9
github.com/mattn/go-tty v0.0.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-colorable v0.1.7 h1:bQGKb3vps/j0E9GfJQ03JyhRuxsvdAanXlT9BTw3mdw=
github.com/mattn/go-colorable v0.1.7/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
Expand Down
118 changes: 118 additions & 0 deletions lexer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package prompt

import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)

func TestEagerLexerNext(t *testing.T) {
tests := map[string]struct {
lexer *EagerLexer
want Token
ok bool
}{
"return the first token when at the beginning": {
lexer: &EagerLexer{
tokens: []Token{
&SimpleToken{lexeme: "foo"},
&SimpleToken{lexeme: "bar"},
},
currentIndex: 0,
},
want: &SimpleToken{lexeme: "foo"},
ok: true,
},
"return the second token": {
lexer: &EagerLexer{
tokens: []Token{
&SimpleToken{lexeme: "foo"},
&SimpleToken{lexeme: "bar"},
&SimpleToken{lexeme: "baz"},
},
currentIndex: 1,
},
want: &SimpleToken{lexeme: "bar"},
ok: true,
},
"return false when at the end": {
lexer: &EagerLexer{
tokens: []Token{
&SimpleToken{lexeme: "foo"},
&SimpleToken{lexeme: "bar"},
&SimpleToken{lexeme: "baz"},
},
currentIndex: 3,
},
want: nil,
ok: false,
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got, ok := tc.lexer.Next()
opts := []cmp.Option{
cmp.AllowUnexported(SimpleToken{}, EagerLexer{}),
}
if diff := cmp.Diff(tc.want, got, opts...); diff != "" {
t.Fatalf(diff)
}
if diff := cmp.Diff(tc.ok, ok, opts...); diff != "" {
t.Fatalf(diff)
}
})
}
}

func charLex(s string) []Token {
var result []Token
for _, char := range s {
result = append(result, NewSimpleToken(0, string(char)))
}

return result
}

func TestEagerLexerInit(t *testing.T) {
tests := map[string]struct {
lexer *EagerLexer
input string
want *EagerLexer
}{
"reset the lexer's state": {
lexer: &EagerLexer{
lexFunc: charLex,
tokens: []Token{
&SimpleToken{lexeme: "foo"},
&SimpleToken{lexeme: "bar"},
},
currentIndex: 2,
},
input: "foo",
want: &EagerLexer{
lexFunc: charLex,
tokens: []Token{
&SimpleToken{lexeme: "f"},
&SimpleToken{lexeme: "o"},
&SimpleToken{lexeme: "o"},
},
currentIndex: 0,
},
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
tc.lexer.Init(tc.input)
opts := []cmp.Option{
cmp.AllowUnexported(SimpleToken{}, EagerLexer{}),
cmpopts.IgnoreFields(EagerLexer{}, "lexFunc"),
}
if diff := cmp.Diff(tc.want, tc.lexer, opts...); diff != "" {
t.Fatalf(diff)
}
})
}
}
2 changes: 0 additions & 2 deletions render.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package prompt

import (
"fmt"
"runtime"
"strings"

Expand Down Expand Up @@ -258,7 +257,6 @@ func (r *Render) lex(lexer Lexer, input string) {
a := strings.SplitAfter(s, token.Lexeme())
s = strings.TrimPrefix(s, a[0])

fmt.Printf("%#v\n", token)
r.out.SetColor(token.Color(), r.inputBGColor, false)
r.out.WriteStr(a[0])
}
Expand Down

0 comments on commit 0a97b96

Please sign in to comment.