From 2e1713db490703e52b75ef839f7c86cca99fc34c Mon Sep 17 00:00:00 2001 From: Howl Date: Sun, 16 Oct 2016 18:27:12 +0200 Subject: [PATCH] add SafeUsername function --- common/utils.go | 11 +++++++++++ common/utils_test.go | 20 ++++++++++++++++++++ common/where_test.go | 6 +++--- 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 common/utils.go create mode 100644 common/utils_test.go diff --git a/common/utils.go b/common/utils.go new file mode 100644 index 0000000..df35d10 --- /dev/null +++ b/common/utils.go @@ -0,0 +1,11 @@ +package common + +import ( + "strings" +) + +// SafeUsername makes a string lowercase and replaces all spaces with +// underscores. +func SafeUsername(s string) string { + return strings.Replace(strings.ToLower(s), " ", "_", -1) +} diff --git a/common/utils_test.go b/common/utils_test.go new file mode 100644 index 0000000..2769502 --- /dev/null +++ b/common/utils_test.go @@ -0,0 +1,20 @@ +package common + +import "testing" + +func TestSafeUsername(t *testing.T) { + tests := []struct { + name string + arg string + want string + }{ + {"noChange", "no_change", "no_change"}, + {"toLower", "Change_Me", "change_me"}, + {"complete", "La_M a m m a_putt na", "la_m_a_m_m_a_putt_na"}, + } + for _, tt := range tests { + if got := SafeUsername(tt.arg); got != tt.want { + t.Errorf("%q. SafeUsername() = %v, want %v", tt.name, got, tt.want) + } + } +} diff --git a/common/where_test.go b/common/where_test.go index 4dbfc2e..144f05f 100644 --- a/common/where_test.go +++ b/common/where_test.go @@ -29,7 +29,7 @@ func Test_generateQuestionMarks(t *testing.T) { func TestWhereClause_In(t *testing.T) { type args struct { initial string - fields []interface{} + fields []string } tests := []struct { name string @@ -40,13 +40,13 @@ func TestWhereClause_In(t *testing.T) { { "simple", &WhereClause{}, - args{"users.id", []interface{}{"1", "2", "3"}}, + args{"users.id", []string{"1", "2", "3"}}, &WhereClause{"WHERE users.id IN (?, ?, ?)", []interface{}{"1", "2", "3"}}, }, { "withExisting", Where("users.username = ?", "Howl").Where("users.xd > ?", "6"), - args{"users.id", []interface{}{"1"}}, + args{"users.id", []string{"1"}}, &WhereClause{ "WHERE users.username = ? AND users.xd > ? AND users.id IN (?)", []interface{}{"Howl", "6", "1"},