Skip to content

Commit

Permalink
Trim whitespace for tokenizer, plus tests (#38)
Browse files Browse the repository at this point in the history
* Trim whitespace for tokenizer, plus tests

* Address review comments
  • Loading branch information
jaredoconnell authored May 10, 2024
1 parent 559a902 commit 670af8e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
10 changes: 10 additions & 0 deletions expression_evaluate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ var testData = map[string]struct {
false,
"Hello world!",
},
"with-whitespace": {
map[string]any{
"message": "Hello world!",
},
nil,
" $.message \n",
false,
false,
"Hello world!",
},
"sub1map": {
map[string]any{
"message": "Hello world!",
Expand Down
3 changes: 2 additions & 1 deletion internal/ast/tokenizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ var tokenPatterns = []tokenPattern{
// initTokenizer initializes the tokenizer struct with the given expression.
func initTokenizer(expression string, sourceName string) *tokenizer {
var t tokenizer
t.reader = strings.NewReader(expression)
// Need to trim the whitespace first since that can cause unexpected blank tokens.
t.reader = strings.NewReader(strings.TrimSpace(expression))
t.s.Init(t.reader)
t.s.Filename = sourceName
return &t
Expand Down
3 changes: 2 additions & 1 deletion internal/ast/tokenizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
var filename = "example.go"

func TestTokenizer(t *testing.T) {
input := `$.steps.read_kubeconfig.output["success"].credentials[f(1,2)]`
// Include trailing whitespace to ensure that it has no ill effects.
input := `$.steps.read_kubeconfig.output["success"].credentials[f(1,2)] `
tokenizer := initTokenizer(input, filename)
expectedValue := []TokenValue{
{"$", RootAccessToken, filename, 1, 1},
Expand Down

0 comments on commit 670af8e

Please sign in to comment.