From 0a97b9627e914bc05bcb57b83e7bf0dbe29eb820 Mon Sep 17 00:00:00 2001 From: Mateusz Drewniak Date: Sun, 2 Jul 2023 09:50:27 +0200 Subject: [PATCH] Add lexer tests --- go.mod | 1 + go.sum | 2 + lexer_test.go | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++ render.go | 2 - 4 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 lexer_test.go diff --git a/go.mod b/go.mod index 3ee29a32..d559da46 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 768bf936..a0af5319 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/lexer_test.go b/lexer_test.go new file mode 100644 index 00000000..b4ccd567 --- /dev/null +++ b/lexer_test.go @@ -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) + } + }) + } +} diff --git a/render.go b/render.go index 57a5bc8d..e865d137 100644 --- a/render.go +++ b/render.go @@ -1,7 +1,6 @@ package prompt import ( - "fmt" "runtime" "strings" @@ -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]) }