2016-04-25 03:43:09 +02:00
|
|
|
package strmangle
|
2016-03-02 04:11:47 +01:00
|
|
|
|
|
|
|
import (
|
2016-06-20 03:45:33 +02:00
|
|
|
"strings"
|
2016-03-02 04:11:47 +01:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2016-08-02 05:25:02 +02:00
|
|
|
func TestIdentQuote(t *testing.T) {
|
2016-08-01 09:41:52 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
In string
|
|
|
|
Out string
|
|
|
|
}{
|
|
|
|
{In: `thing`, Out: `"thing"`},
|
2016-08-02 04:55:40 +02:00
|
|
|
{In: `null`, Out: `null`},
|
|
|
|
{In: `"thing"`, Out: `"thing"`},
|
2016-08-01 09:41:52 +02:00
|
|
|
{In: `*`, Out: `*`},
|
2016-08-02 05:25:02 +02:00
|
|
|
{In: ``, Out: ``},
|
2016-08-02 04:55:40 +02:00
|
|
|
{In: `thing.thing`, Out: `"thing"."thing"`},
|
|
|
|
{In: `"thing"."thing"`, Out: `"thing"."thing"`},
|
|
|
|
{In: `thing.thing.thing.thing`, Out: `"thing"."thing"."thing"."thing"`},
|
|
|
|
{In: `thing."thing".thing."thing"`, Out: `"thing"."thing"."thing"."thing"`},
|
2016-08-02 05:25:02 +02:00
|
|
|
{In: `count(*) as ab, thing as bd`, Out: `count(*) as ab, thing as bd`},
|
2016-08-04 04:17:29 +02:00
|
|
|
{In: `hello.*`, Out: `"hello".*`},
|
|
|
|
{In: `hello.there.*`, Out: `"hello"."there".*`},
|
|
|
|
{In: `"hello".there.*`, Out: `"hello"."there".*`},
|
|
|
|
{In: `hello."there".*`, Out: `"hello"."there".*`},
|
2016-08-01 09:41:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2016-08-02 05:25:02 +02:00
|
|
|
if got := IdentQuote(test.In); got != test.Out {
|
2016-08-01 09:41:52 +02:00
|
|
|
t.Errorf("want: %s, got: %s", test.Out, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-04 06:46:58 +02:00
|
|
|
func TestIdentQuoteSlice(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
ret := IdentQuoteSlice([]string{`thing`, `null`})
|
|
|
|
if ret[0] != `"thing"` {
|
|
|
|
t.Error(ret[0])
|
|
|
|
}
|
|
|
|
if ret[1] != `null` {
|
|
|
|
t.Error(ret[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-13 15:30:53 +02:00
|
|
|
func TestIdentifier(t *testing.T) {
|
2016-07-02 00:52:40 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
In int
|
|
|
|
Out string
|
|
|
|
}{
|
|
|
|
{In: 0, Out: "a"},
|
|
|
|
{In: 25, Out: "z"},
|
|
|
|
{In: 26, Out: "ba"},
|
|
|
|
{In: 52, Out: "ca"},
|
|
|
|
{In: 675, Out: "zz"},
|
|
|
|
{In: 676, Out: "baa"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
if got := Identifier(test.In); got != test.Out {
|
|
|
|
t.Errorf("[%d] want: %q, got: %q", test.In, test.Out, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-09 05:19:26 +02:00
|
|
|
func TestPlaceholders(t *testing.T) {
|
2016-05-02 08:34:25 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-08-09 05:19:26 +02:00
|
|
|
x := Placeholders(1, 2, 1)
|
2016-08-08 15:30:29 +02:00
|
|
|
want := "$2"
|
|
|
|
if want != x {
|
|
|
|
t.Errorf("want %s, got %s", want, x)
|
|
|
|
}
|
|
|
|
|
2016-08-09 05:19:26 +02:00
|
|
|
x = Placeholders(5, 1, 1)
|
2016-08-08 15:30:29 +02:00
|
|
|
want = "$1,$2,$3,$4,$5"
|
|
|
|
if want != x {
|
|
|
|
t.Errorf("want %s, got %s", want, x)
|
|
|
|
}
|
|
|
|
|
2016-08-09 05:19:26 +02:00
|
|
|
x = Placeholders(6, 1, 2)
|
2016-08-08 15:30:29 +02:00
|
|
|
want = "($1,$2),($3,$4),($5,$6)"
|
|
|
|
if want != x {
|
|
|
|
t.Errorf("want %s, got %s", want, x)
|
|
|
|
}
|
|
|
|
|
2016-08-09 05:19:26 +02:00
|
|
|
x = Placeholders(9, 1, 3)
|
2016-08-08 15:30:29 +02:00
|
|
|
want = "($1,$2,$3),($4,$5,$6),($7,$8,$9)"
|
|
|
|
if want != x {
|
|
|
|
t.Errorf("want %s, got %s", want, x)
|
|
|
|
}
|
|
|
|
|
2016-08-09 05:19:26 +02:00
|
|
|
x = Placeholders(7, 1, 3)
|
2016-08-08 15:30:29 +02:00
|
|
|
want = "($1,$2,$3),($4,$5,$6),($7)"
|
2016-05-02 08:34:25 +02:00
|
|
|
if want != x {
|
|
|
|
t.Errorf("want %s, got %s", want, x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-18 12:26:48 +01:00
|
|
|
func TestSingular(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
In string
|
|
|
|
Out string
|
|
|
|
}{
|
|
|
|
{"hello_people", "hello_person"},
|
|
|
|
{"hello_person", "hello_person"},
|
|
|
|
{"friends", "friend"},
|
2016-08-13 15:39:13 +02:00
|
|
|
{"hello_there_people", "hello_there_person"},
|
2016-03-18 12:26:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2016-04-25 03:43:09 +02:00
|
|
|
if out := Singular(test.In); out != test.Out {
|
2016-03-18 12:26:48 +01:00
|
|
|
t.Errorf("[%d] (%s) Out was wrong: %q, want: %q", i, test.In, out, test.Out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPlural(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
In string
|
|
|
|
Out string
|
|
|
|
}{
|
|
|
|
{"hello_person", "hello_people"},
|
|
|
|
{"friend", "friends"},
|
|
|
|
{"friends", "friends"},
|
2016-08-13 15:39:13 +02:00
|
|
|
{"hello_there_person", "hello_there_people"},
|
2016-03-18 12:26:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2016-04-25 03:43:09 +02:00
|
|
|
if out := Plural(test.In); out != test.Out {
|
2016-03-18 12:26:48 +01:00
|
|
|
t.Errorf("[%d] (%s) Out was wrong: %q, want: %q", i, test.In, out, test.Out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-02 04:11:47 +01:00
|
|
|
func TestTitleCase(t *testing.T) {
|
2016-03-03 05:14:21 +01:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-03-02 04:11:47 +01:00
|
|
|
tests := []struct {
|
|
|
|
In string
|
|
|
|
Out string
|
|
|
|
}{
|
|
|
|
{"hello_there", "HelloThere"},
|
|
|
|
{"", ""},
|
|
|
|
{"fun_id", "FunID"},
|
2016-07-17 05:33:16 +02:00
|
|
|
{"uid", "UID"},
|
|
|
|
{"guid", "GUID"},
|
|
|
|
{"uid", "UID"},
|
|
|
|
{"uuid", "UUID"},
|
|
|
|
{"ssn", "SSN"},
|
|
|
|
{"tz", "TZ"},
|
|
|
|
{"thing_guid", "ThingGUID"},
|
|
|
|
{"guid_thing", "GUIDThing"},
|
|
|
|
{"thing_guid_thing", "ThingGUIDThing"},
|
2016-08-02 11:56:55 +02:00
|
|
|
{"id", "ID"},
|
2016-08-15 11:51:00 +02:00
|
|
|
{"gvzxc", "GVZXC"},
|
|
|
|
{"id_trgb_id", "IDTRGBID"},
|
|
|
|
{"vzxx_vxccb_nmx", "VZXXVXCCBNMX"},
|
|
|
|
{"thing_zxc_stuff_vxz", "ThingZXCStuffVXZ"},
|
|
|
|
{"zxc_thing_vxz_stuff", "ZXCThingVXZStuff"},
|
2016-03-02 04:11:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2016-04-25 03:43:09 +02:00
|
|
|
if out := TitleCase(test.In); out != test.Out {
|
2016-03-02 04:11:47 +01:00
|
|
|
t.Errorf("[%d] (%s) Out was wrong: %q, want: %q", i, test.In, out, test.Out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCamelCase(t *testing.T) {
|
2016-03-03 05:14:21 +01:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-03-02 04:11:47 +01:00
|
|
|
tests := []struct {
|
|
|
|
In string
|
|
|
|
Out string
|
|
|
|
}{
|
|
|
|
{"hello_there_sunny", "helloThereSunny"},
|
|
|
|
{"", ""},
|
|
|
|
{"fun_id_times", "funIDTimes"},
|
2016-07-17 05:33:16 +02:00
|
|
|
{"uid", "uid"},
|
|
|
|
{"guid", "guid"},
|
|
|
|
{"uid", "uid"},
|
|
|
|
{"uuid", "uuid"},
|
|
|
|
{"ssn", "ssn"},
|
|
|
|
{"tz", "tz"},
|
|
|
|
{"thing_guid", "thingGUID"},
|
|
|
|
{"guid_thing", "guidThing"},
|
|
|
|
{"thing_guid_thing", "thingGUIDThing"},
|
2016-03-02 04:11:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2016-04-25 03:43:09 +02:00
|
|
|
if out := CamelCase(test.In); out != test.Out {
|
2016-03-02 04:11:47 +01:00
|
|
|
t.Errorf("[%d] (%s) Out was wrong: %q, want: %q", i, test.In, out, test.Out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-13 18:51:40 +02:00
|
|
|
func TestMakeStringMap(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
var m map[string]string
|
|
|
|
r := MakeStringMap(m)
|
|
|
|
|
|
|
|
if r != "" {
|
|
|
|
t.Errorf("Expected empty result, got: %s", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
m = map[string]string{
|
|
|
|
"TestOne": "interval",
|
|
|
|
"TestTwo": "integer",
|
|
|
|
}
|
|
|
|
|
|
|
|
r = MakeStringMap(m)
|
|
|
|
|
2016-07-17 05:33:16 +02:00
|
|
|
e1 := `"TestOne": "interval", "TestTwo": "integer"`
|
|
|
|
e2 := `"TestTwo": "integer", "TestOne": "interval"`
|
|
|
|
|
|
|
|
if r != e1 && r != e2 {
|
|
|
|
t.Errorf("Got %s", r)
|
2016-07-13 18:51:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 03:45:33 +02:00
|
|
|
func TestStringMap(t *testing.T) {
|
2016-03-03 05:14:21 +01:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-06-20 03:45:33 +02:00
|
|
|
mapped := StringMap(strings.ToLower, []string{"HELLO", "WORLD"})
|
|
|
|
if got := strings.Join(mapped, " "); got != "hello world" {
|
|
|
|
t.Errorf("mapped was wrong: %q", got)
|
2016-03-02 04:11:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 03:45:33 +02:00
|
|
|
func TestPrefixStringSlice(t *testing.T) {
|
2016-03-03 05:14:21 +01:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-06-20 03:45:33 +02:00
|
|
|
slice := PrefixStringSlice("o.", []string{"one", "two"})
|
|
|
|
if got := strings.Join(slice, " "); got != "o.one o.two" {
|
|
|
|
t.Error("wrong output:", got)
|
2016-03-02 05:05:25 +01:00
|
|
|
}
|
|
|
|
}
|
2016-08-14 01:03:34 +02:00
|
|
|
|
2016-06-23 08:09:56 +02:00
|
|
|
func TestWhereClause(t *testing.T) {
|
2016-04-04 12:28:58 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-08-08 19:33:24 +02:00
|
|
|
tests := []struct {
|
|
|
|
Cols []string
|
|
|
|
Start int
|
|
|
|
Should string
|
|
|
|
}{
|
2016-08-09 10:11:28 +02:00
|
|
|
{Cols: []string{"col1"}, Start: 2, Should: `"col1"=$2`},
|
|
|
|
{Cols: []string{"col1", "col2"}, Start: 4, Should: `"col1"=$4 AND "col2"=$5`},
|
|
|
|
{Cols: []string{"col1", "col2", "col3"}, Start: 4, Should: `"col1"=$4 AND "col2"=$5 AND "col3"=$6`},
|
2016-08-08 19:33:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2016-08-09 07:57:54 +02:00
|
|
|
r := WhereClause(test.Start, test.Cols)
|
2016-08-08 19:33:24 +02:00
|
|
|
if r != test.Should {
|
2016-08-09 10:11:28 +02:00
|
|
|
t.Errorf("(%d) want: %s, got: %s\nTest: %#v", i, test.Should, r, test)
|
2016-08-08 19:33:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-27 08:54:23 +02:00
|
|
|
func TestJoinSlices(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
ret := JoinSlices("", nil, nil)
|
|
|
|
if ret != nil {
|
|
|
|
t.Error("want nil, got:", ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = JoinSlices(" ", []string{"one", "two"}, []string{"three", "four"})
|
|
|
|
if got := ret[0]; got != "one three" {
|
|
|
|
t.Error("ret element was wrong:", got)
|
|
|
|
}
|
|
|
|
if got := ret[1]; got != "two four" {
|
|
|
|
t.Error("ret element was wrong:", got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJoinSlicesFail(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if recover() == nil {
|
|
|
|
t.Error("did not panic")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
JoinSlices("", nil, []string{"hello"})
|
|
|
|
}
|