pqarrays

Paddy 2016-02-25 Parent:bfe2a4af6bdf Child:9a415db0346a

1:ce9c92fc81ab Go to Latest

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.

History
1 package pqarrays
3 import (
4 "testing"
5 )
7 func strPtr(in string) *string {
8 return &in
9 }
11 var parseTestInputs = map[string][]*string{
12 `{}`: []*string{},
13 `{lions}`: []*string{strPtr("lions")},
14 `{lions,tigers}`: []*string{strPtr("lions"), strPtr("tigers")},
15 `{lions,tigers,NULL}`: []*string{strPtr("lions"), strPtr("tigers"), nil},
16 `{lions,tigers,bears}`: []*string{strPtr("lions"), strPtr("tigers"), strPtr("bears")},
17 `{lions,tigers,bears,"oh my!"}`: []*string{strPtr("lions"), strPtr("tigers"), strPtr("bears"), strPtr("oh my!")},
18 }
20 func TestParseInputsTable(t *testing.T) {
21 for input, expected := range parseTestInputs {
22 l := lex(input)
23 output, err := parse(l)
24 if err != nil {
25 t.Fatalf(err.Error())
26 }
27 t.Logf("`%s`: %#+v\n", input, output)
28 if len(output) != len(expected) {
29 t.Fatalf("Expected %d items in array, got %d\n", len(expected), len(output))
30 }
31 for pos, item := range output {
32 if item == nil && expected[pos] != nil {
33 t.Errorf("Expected %d to be %s, got nil instead.", pos, *expected[pos])
34 } else if item != nil && expected[pos] == nil {
35 t.Errorf("Expected %d to be nil, got %s instead.", pos, *item)
36 } else if item != nil && expected[pos] != nil {
37 continue
38 } else if item == nil && expected[pos] == nil {
39 continue
40 } else if *item != *expected[pos] {
41 t.Errorf("Expected %d to be %s, got %s instead.", pos, *expected[pos], *item)
42 }
43 }
44 }
45 }