pqarrays
pqarrays/parser_test.go
Fix bug parsing empty arrays, make golint and go vet happy. Add comments to make golint happy. Also, because comments are a good thing to have. Turn += 1 and -= 1 into ++ and --, respectively, so golint will be happy. Fix an improperly formated errorf, where a rune was being treated as a string. Thanks, go vet! Fix whitespace parsing, returning the parse functions again instead of just skipping the one character. Now if we have more than one whitespace character in a row, they'll all be skipped. Add a parseStringOrNullOrEnd parse function that will be called after the tokenArrayStart character, to fix a bug where empty arrays were expecting a string or null and getting the array end character. This is only valid after tokenArrayStart, however; in other places where parseSeparatorOrDelim is used, it wouldn't be appropriate. Add a parser test for an empty array.
| paddy@0 | 1 package pqarrays |
| paddy@0 | 2 |
| paddy@0 | 3 import ( |
| paddy@0 | 4 "testing" |
| paddy@0 | 5 ) |
| paddy@0 | 6 |
| paddy@0 | 7 func strPtr(in string) *string { |
| paddy@0 | 8 return &in |
| paddy@0 | 9 } |
| paddy@0 | 10 |
| paddy@0 | 11 var parseTestInputs = map[string][]*string{ |
| paddy@1 | 12 `{}`: []*string{}, |
| paddy@0 | 13 `{lions}`: []*string{strPtr("lions")}, |
| paddy@0 | 14 `{lions,tigers}`: []*string{strPtr("lions"), strPtr("tigers")}, |
| paddy@0 | 15 `{lions,tigers,NULL}`: []*string{strPtr("lions"), strPtr("tigers"), nil}, |
| paddy@0 | 16 `{lions,tigers,bears}`: []*string{strPtr("lions"), strPtr("tigers"), strPtr("bears")}, |
| paddy@0 | 17 `{lions,tigers,bears,"oh my!"}`: []*string{strPtr("lions"), strPtr("tigers"), strPtr("bears"), strPtr("oh my!")}, |
| paddy@0 | 18 } |
| paddy@0 | 19 |
| paddy@0 | 20 func TestParseInputsTable(t *testing.T) { |
| paddy@0 | 21 for input, expected := range parseTestInputs { |
| paddy@0 | 22 l := lex(input) |
| paddy@0 | 23 output, err := parse(l) |
| paddy@0 | 24 if err != nil { |
| paddy@0 | 25 t.Fatalf(err.Error()) |
| paddy@0 | 26 } |
| paddy@0 | 27 t.Logf("`%s`: %#+v\n", input, output) |
| paddy@0 | 28 if len(output) != len(expected) { |
| paddy@0 | 29 t.Fatalf("Expected %d items in array, got %d\n", len(expected), len(output)) |
| paddy@0 | 30 } |
| paddy@0 | 31 for pos, item := range output { |
| paddy@0 | 32 if item == nil && expected[pos] != nil { |
| paddy@0 | 33 t.Errorf("Expected %d to be %s, got nil instead.", pos, *expected[pos]) |
| paddy@0 | 34 } else if item != nil && expected[pos] == nil { |
| paddy@0 | 35 t.Errorf("Expected %d to be nil, got %s instead.", pos, *item) |
| paddy@0 | 36 } else if item != nil && expected[pos] != nil { |
| paddy@0 | 37 continue |
| paddy@0 | 38 } else if item == nil && expected[pos] == nil { |
| paddy@0 | 39 continue |
| paddy@0 | 40 } else if *item != *expected[pos] { |
| paddy@0 | 41 t.Errorf("Expected %d to be %s, got %s instead.", pos, *expected[pos], *item) |
| paddy@0 | 42 } |
| paddy@0 | 43 } |
| paddy@0 | 44 } |
| paddy@0 | 45 } |