1package pgtype_test
2
3import (
4	"reflect"
5	"testing"
6
7	"github.com/jackc/pgtype"
8)
9
10func TestParseUntypedTextArray(t *testing.T) {
11	tests := []struct {
12		source string
13		result pgtype.UntypedTextArray
14	}{
15		{
16			source: "{}",
17			result: pgtype.UntypedTextArray{
18				Elements:   nil,
19				Dimensions: nil,
20			},
21		},
22		{
23			source: "{1}",
24			result: pgtype.UntypedTextArray{
25				Elements:   []string{"1"},
26				Dimensions: []pgtype.ArrayDimension{{Length: 1, LowerBound: 1}},
27			},
28		},
29		{
30			source: "{a,b}",
31			result: pgtype.UntypedTextArray{
32				Elements:   []string{"a", "b"},
33				Dimensions: []pgtype.ArrayDimension{{Length: 2, LowerBound: 1}},
34			},
35		},
36		{
37			source: `{"NULL"}`,
38			result: pgtype.UntypedTextArray{
39				Elements:   []string{"NULL"},
40				Dimensions: []pgtype.ArrayDimension{{Length: 1, LowerBound: 1}},
41			},
42		},
43		{
44			source: `{""}`,
45			result: pgtype.UntypedTextArray{
46				Elements:   []string{""},
47				Dimensions: []pgtype.ArrayDimension{{Length: 1, LowerBound: 1}},
48			},
49		},
50		{
51			source: `{"He said, \"Hello.\""}`,
52			result: pgtype.UntypedTextArray{
53				Elements:   []string{`He said, "Hello."`},
54				Dimensions: []pgtype.ArrayDimension{{Length: 1, LowerBound: 1}},
55			},
56		},
57		{
58			source: "{{a,b},{c,d},{e,f}}",
59			result: pgtype.UntypedTextArray{
60				Elements:   []string{"a", "b", "c", "d", "e", "f"},
61				Dimensions: []pgtype.ArrayDimension{{Length: 3, LowerBound: 1}, {Length: 2, LowerBound: 1}},
62			},
63		},
64		{
65			source: "{{{a,b},{c,d},{e,f}},{{a,b},{c,d},{e,f}}}",
66			result: pgtype.UntypedTextArray{
67				Elements: []string{"a", "b", "c", "d", "e", "f", "a", "b", "c", "d", "e", "f"},
68				Dimensions: []pgtype.ArrayDimension{
69					{Length: 2, LowerBound: 1},
70					{Length: 3, LowerBound: 1},
71					{Length: 2, LowerBound: 1},
72				},
73			},
74		},
75		{
76			source: "[4:4]={1}",
77			result: pgtype.UntypedTextArray{
78				Elements:   []string{"1"},
79				Dimensions: []pgtype.ArrayDimension{{Length: 1, LowerBound: 4}},
80			},
81		},
82		{
83			source: "[4:5][2:3]={{a,b},{c,d}}",
84			result: pgtype.UntypedTextArray{
85				Elements: []string{"a", "b", "c", "d"},
86				Dimensions: []pgtype.ArrayDimension{
87					{Length: 2, LowerBound: 4},
88					{Length: 2, LowerBound: 2},
89				},
90			},
91		},
92	}
93
94	for i, tt := range tests {
95		r, err := pgtype.ParseUntypedTextArray(tt.source)
96		if err != nil {
97			t.Errorf("%d: %v", i, err)
98			continue
99		}
100
101		if !reflect.DeepEqual(*r, tt.result) {
102			t.Errorf("%d: expected %+v to be parsed to %+v, but it was %+v", i, tt.source, tt.result, *r)
103		}
104	}
105}
106