v1: Allow empty files to be imported (#3421)

* add tests for an empty file

* return nil from lexer.load when the file is empty
This commit is contained in:
Billie Cleek 2020-05-16 10:14:03 -07:00 committed by GitHub
parent 3b4f7a3c81
commit 240de5a5dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 13 deletions

View file

@ -48,6 +48,9 @@ func (l *lexer) load(input io.Reader) error {
// discard byte order mark, if present // discard byte order mark, if present
firstCh, _, err := l.reader.ReadRune() firstCh, _, err := l.reader.ReadRune()
if err != nil { if err != nil {
if err == io.EOF {
return nil
}
return err return err
} }
if firstCh != 0xFEFF { if firstCh != 0xFEFF {

View file

@ -23,22 +23,48 @@ import (
) )
func TestAllTokens(t *testing.T) { func TestAllTokens(t *testing.T) {
input := strings.NewReader("a b c\nd e") tests := []struct {
expected := []string{"a", "b", "c", "d", "e"} name string
tokens, err := allTokens(input) input string
expected []string
}{
{
name: "not-empty",
input: "a b c\nd e",
expected: []string{"a", "b", "c", "d", "e"},
}, {
name: "empty",
input: "",
}, {
name: "newline",
input: "\n",
}, {
name: "space",
input: " ",
}, {
name: "tab and newline",
input: "\t\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tokens, err := allTokens(strings.NewReader(tt.input))
if err != nil { if err != nil {
t.Fatalf("Expected no error, got %v", err) t.Fatalf("Expected no error, got %v", err)
} }
if len(tokens) != len(expected) { if len(tokens) != len(tt.expected) {
t.Fatalf("Expected %d tokens, got %d", len(expected), len(tokens)) t.Fatalf("Expected %d tokens, got %d", len(tt.expected), len(tokens))
} }
for i, val := range expected { for i, val := range tt.expected {
if tokens[i].Text != val { if tokens[i].Text != val {
t.Errorf("Token %d should be '%s' but was '%s'", i, val, tokens[i].Text) t.Errorf("Token %d should be '%s' but was '%s'", i, val, tokens[i].Text)
} }
} }
})
}
} }
func TestParseOneAndImport(t *testing.T) { func TestParseOneAndImport(t *testing.T) {