pqarrays
2015-04-19
Child:ce9c92fc81ab
pqarrays/parser_test.go
First pass implementation. Use a lexer to generate tokens out of the Array type responses that PostgreSQL will send. Write a parser for string[] array types. Create a StringArray type that fulfills the driver.Valuer and sql.Scanner interfaces using the parser and lexer.
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 `{lions}`: []*string{strPtr("lions")},
13 `{lions,tigers}`: []*string{strPtr("lions"), strPtr("tigers")},
14 `{lions,tigers,NULL}`: []*string{strPtr("lions"), strPtr("tigers"), nil},
15 `{lions,tigers,bears}`: []*string{strPtr("lions"), strPtr("tigers"), strPtr("bears")},
16 `{lions,tigers,bears,"oh my!"}`: []*string{strPtr("lions"), strPtr("tigers"), strPtr("bears"), strPtr("oh my!")},
17 }
19 func TestParseInputsTable(t *testing.T) {
20 for input, expected := range parseTestInputs {
21 l := lex(input)
22 output, err := parse(l)
23 if err != nil {
24 t.Fatalf(err.Error())
25 }
26 t.Logf("`%s`: %#+v\n", input, output)
27 if len(output) != len(expected) {
28 t.Fatalf("Expected %d items in array, got %d\n", len(expected), len(output))
29 }
30 for pos, item := range output {
31 if item == nil && expected[pos] != nil {
32 t.Errorf("Expected %d to be %s, got nil instead.", pos, *expected[pos])
33 } else if item != nil && expected[pos] == nil {
34 t.Errorf("Expected %d to be nil, got %s instead.", pos, *item)
35 } else if item != nil && expected[pos] != nil {
36 continue
37 } else if item == nil && expected[pos] == nil {
38 continue
39 } else if *item != *expected[pos] {
40 t.Errorf("Expected %d to be %s, got %s instead.", pos, *expected[pos], *item)
41 }
42 }
43 }
44 }