From 323e2b3f7817715c392cc848417c485bf5564bfd Mon Sep 17 00:00:00 2001 From: Manni Wood Date: Tue, 15 Nov 2016 22:22:57 -0500 Subject: [PATCH] Adds aclitem helper func tests --- aclitem_parse_test.go | 126 ++++++++++++++++++++++++++++++++++++++++++ values.go | 4 +- 2 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 aclitem_parse_test.go diff --git a/aclitem_parse_test.go b/aclitem_parse_test.go new file mode 100644 index 00000000..a0e5e858 --- /dev/null +++ b/aclitem_parse_test.go @@ -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) + } + } +} diff --git a/values.go b/values.go index d8ad8fb3..a209ec80 100644 --- a/values.go +++ b/values.go @@ -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 }