pqarrays

Paddy 2015-04-19 Child:ce9c92fc81ab

0:bfe2a4af6bdf Go to Latest

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.

History
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/parser_test.go	Sun Apr 19 23:47:36 2015 -0400
     1.3 @@ -0,0 +1,44 @@
     1.4 +package pqarrays
     1.5 +
     1.6 +import (
     1.7 +	"testing"
     1.8 +)
     1.9 +
    1.10 +func strPtr(in string) *string {
    1.11 +	return &in
    1.12 +}
    1.13 +
    1.14 +var parseTestInputs = map[string][]*string{
    1.15 +	`{lions}`:                       []*string{strPtr("lions")},
    1.16 +	`{lions,tigers}`:                []*string{strPtr("lions"), strPtr("tigers")},
    1.17 +	`{lions,tigers,NULL}`:           []*string{strPtr("lions"), strPtr("tigers"), nil},
    1.18 +	`{lions,tigers,bears}`:          []*string{strPtr("lions"), strPtr("tigers"), strPtr("bears")},
    1.19 +	`{lions,tigers,bears,"oh my!"}`: []*string{strPtr("lions"), strPtr("tigers"), strPtr("bears"), strPtr("oh my!")},
    1.20 +}
    1.21 +
    1.22 +func TestParseInputsTable(t *testing.T) {
    1.23 +	for input, expected := range parseTestInputs {
    1.24 +		l := lex(input)
    1.25 +		output, err := parse(l)
    1.26 +		if err != nil {
    1.27 +			t.Fatalf(err.Error())
    1.28 +		}
    1.29 +		t.Logf("`%s`: %#+v\n", input, output)
    1.30 +		if len(output) != len(expected) {
    1.31 +			t.Fatalf("Expected %d items in array, got %d\n", len(expected), len(output))
    1.32 +		}
    1.33 +		for pos, item := range output {
    1.34 +			if item == nil && expected[pos] != nil {
    1.35 +				t.Errorf("Expected %d to be %s, got nil instead.", pos, *expected[pos])
    1.36 +			} else if item != nil && expected[pos] == nil {
    1.37 +				t.Errorf("Expected %d to be nil, got %s instead.", pos, *item)
    1.38 +			} else if item != nil && expected[pos] != nil {
    1.39 +				continue
    1.40 +			} else if item == nil && expected[pos] == nil {
    1.41 +				continue
    1.42 +			} else if *item != *expected[pos] {
    1.43 +				t.Errorf("Expected %d to be %s, got %s instead.", pos, *expected[pos], *item)
    1.44 +			}
    1.45 +		}
    1.46 +	}
    1.47 +}