Skip to content

Commit

Permalink
test(parse): add empty string cases
Browse files Browse the repository at this point in the history
Add cases to existing tests that correctly handle empty strings as
input. In this case, the lexer was setting its state function to nil
after returning an EOF. This resulted in a nil function call if the
parser calls `nextToken` repeatedly on the lexer. Instead of setting the
state function to `nil` after emitting an EOF, set the state function to
`lexLineStart`. This allows `nextToken` to be called on a lexer
repeatedly after it has already emitted an EOF. It will simply emit an
EOF token again.
  • Loading branch information
subpop committed Dec 14, 2023
1 parent fa0fcdb commit be0f378
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func lexLineStart(l *lexer) stateFunc {
switch {
case r == eof:
l.emit(tokenEOF)
return nil
return lexLineStart
case r == comment:
return lexComment
case r == numberSign:
Expand Down
10 changes: 10 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ func TestParseProp(t *testing.T) {
shouldError: true,
wantError: &unexpectedTokenErr{token{tokenError, `unexpected character: '\x00', an assignment must be followed by one or more alphanumeric characters`}},
},
{
description: "empty string",
input: "",
want: property{
key: "",
vals: map[string][]string{},
},
shouldError: true,
wantError: &unexpectedTokenErr{token{tokenEOF, ""}},
},
}

for _, test := range tests {
Expand Down

0 comments on commit be0f378

Please sign in to comment.