Adds aclitem helper func tests

pull/210/head
Manni Wood 2016-11-15 22:22:57 -05:00
parent 7b3488b088
commit 323e2b3f78
2 changed files with 128 additions and 2 deletions

126
aclitem_parse_test.go Normal file
View File

@ -0,0 +1,126 @@
package pgx
import (
"reflect"
"testing"
)
func TestEscapeAclItem(t *testing.T) {
tests := []struct {
input string
expected string
}{
{
"foo",
"foo",
},
{
`foo, "\}`,
`foo\, \"\\\}`,
},
}
for i, tt := range tests {
actual, err := escapeAclItem(tt.input)
if err != nil {
t.Errorf("%d. Unexpected error %v", i, err)
}
if actual != tt.expected {
t.Errorf("%d.\nexpected: %s,\nactual: %s", i, tt.expected, actual)
}
}
}
func TestParseAclItemArray(t *testing.T) {
tests := []struct {
input string
expected []string
errMsg string
}{
{
"",
[]string{},
"",
},
{
"one",
[]string{"one"},
"",
},
{
`"one"`,
[]string{"one"},
"",
},
{
"one,two,three",
[]string{"one", "two", "three"},
"",
},
{
`"one","two","three"`,
[]string{"one", "two", "three"},
"",
},
{
`"one",two,"three"`,
[]string{"one", "two", "three"},
"",
},
{
`one,two,"three"`,
[]string{"one", "two", "three"},
"",
},
{
`"one","two",three`,
[]string{"one", "two", "three"},
"",
},
{
`"one","t w o",three`,
[]string{"one", "t w o", "three"},
"",
},
{
`"one","t, w o\"\}\\",three`,
[]string{"one", `t, w o"}\`, "three"},
"",
},
{
`"one","two",three"`,
[]string{"one", "two", `three"`},
"",
},
{
`"one","two,"three"`,
nil,
"unexpected rune after quoted value",
},
{
`"one","two","three`,
nil,
"unexpected end of quoted value",
},
}
for i, tt := range tests {
actual, err := parseAclItemArray(tt.input)
if err != nil {
if tt.errMsg == "" {
t.Errorf("%d. Unexpected error %v", i, err)
} else if err.Error() != tt.errMsg {
t.Errorf("%d. Expected error %v did not match actual error %v", i, tt.errMsg, err.Error())
}
} else if tt.errMsg != "" {
t.Errorf("%d. Expected error not returned: \"%v\"", i, tt.errMsg)
}
if !reflect.DeepEqual(actual, tt.expected) {
t.Errorf("%d. Expected %v did not match actual %v", i, tt.expected, actual)
}
}
}

View File

@ -3000,7 +3000,7 @@ func decodeTextArray(vr *ValueReader) []string {
return a
}
func EscapeAclItem(acl string) (string, error) {
func escapeAclItem(acl string) (string, error) {
var buf bytes.Buffer
r := strings.NewReader(acl)
for {
@ -3030,7 +3030,7 @@ func encodeAclItemSlice(w *WriteBuf, oid Oid, aclitems []AclItem) error {
var escaped string
var err error
for i := range strs {
escaped, err = EscapeAclItem(string(aclitems[i]))
escaped, err = escapeAclItem(string(aclitems[i]))
if err != nil {
return err
}