170 lines
3.8 KiB
Go
170 lines
3.8 KiB
Go
package stringUtil
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestMapToString(t *testing.T) {
|
|
var data map[string]int
|
|
separator1 := ","
|
|
separator2 := ";"
|
|
|
|
got, err := MapToString(data, separator1, separator2)
|
|
if err != nil {
|
|
t.Errorf("There should be no error, but now there is:%s", err)
|
|
return
|
|
}
|
|
|
|
data1 := make([]int, 0, 4)
|
|
data1 = append(data1, 1)
|
|
got, err = MapToString(data1, separator1, separator2)
|
|
if err == nil {
|
|
t.Errorf("There should be an error, but now there is not")
|
|
return
|
|
}
|
|
|
|
data2 := make(map[string]int, 4)
|
|
data2["Jordan"] = 34
|
|
data2["Thomas"] = 6
|
|
expected1 := "Jordan,34;Thomas,6"
|
|
expected2 := "Thomas,6;Jordan,34"
|
|
|
|
got, err = MapToString(data2, separator1, separator2)
|
|
if err != nil {
|
|
t.Errorf("There should be no error, but now there is:%s", err)
|
|
return
|
|
}
|
|
if got != expected1 && got != expected2 {
|
|
t.Errorf("Expected to get:%s or %s, but got:%s", expected1, expected2, got)
|
|
}
|
|
}
|
|
|
|
func TestMapToString2(t *testing.T) {
|
|
var data map[string]int
|
|
separator1 := ","
|
|
separator2 := ";"
|
|
|
|
got, err := MapToString2(data, separator1, separator2, valGetFunc)
|
|
if err != nil {
|
|
t.Errorf("There should be no error, but now there is:%s", err)
|
|
return
|
|
}
|
|
|
|
data1 := make([]int, 0, 4)
|
|
data1 = append(data1, 1)
|
|
got, err = MapToString2(data1, separator1, separator2, valGetFunc)
|
|
if err == nil {
|
|
t.Errorf("There should be an error, but now there is not")
|
|
return
|
|
}
|
|
|
|
data2 := make(map[string]int, 4)
|
|
data2["Jordan"] = 34
|
|
data2["Thomas"] = 6
|
|
expected1 := "Jordan,34;Thomas,6"
|
|
expected2 := "Thomas,6;Jordan,34"
|
|
|
|
got, err = MapToString2(data2, separator1, separator2, valGetFunc)
|
|
if err != nil {
|
|
t.Errorf("There should be no error, but now there is:%s", err)
|
|
return
|
|
}
|
|
if got != expected1 && got != expected2 {
|
|
t.Errorf("Expected to get:%s or %s, but got:%s", expected1, expected2, got)
|
|
}
|
|
}
|
|
|
|
func valGetFunc(val interface{}) interface{} {
|
|
return val
|
|
}
|
|
|
|
func TestSliceToString(t *testing.T) {
|
|
// Test with nil value
|
|
var value interface{} = nil
|
|
expected := ""
|
|
got, err := SliceToString(value, ",")
|
|
if err != nil {
|
|
t.Errorf("There should be no error, but now got one. %s", err)
|
|
return
|
|
}
|
|
|
|
// Test with wrong type value
|
|
value = "hello"
|
|
got, err = SliceToString(value, ",")
|
|
if err == nil {
|
|
t.Errorf("There should be an error, but now there isn't.")
|
|
return
|
|
}
|
|
|
|
// Test with correct value
|
|
value = []int{1, 2, 3, 4, 5}
|
|
expected = "1,2,3,4,5"
|
|
|
|
got, err = SliceToString(value, ",")
|
|
if err != nil {
|
|
t.Errorf("There should be no error, but now got one. %s", err)
|
|
return
|
|
}
|
|
|
|
if got != expected {
|
|
t.Errorf("Expected %s, but got %s", expected, got)
|
|
return
|
|
}
|
|
}
|
|
|
|
func TestStringListToString(t *testing.T) {
|
|
list := make([]string, 0, 4)
|
|
list = append(list, "Hello")
|
|
list = append(list, "World")
|
|
list = append(list, "Hello")
|
|
list = append(list, "Apple")
|
|
|
|
expected := "Hello,World,Hello,Apple"
|
|
got := StringListToString(list, ",")
|
|
if expected != got {
|
|
t.Errorf("Expected:%s, but got:%s", expected, got)
|
|
}
|
|
}
|
|
|
|
func TestIntListToString(t *testing.T) {
|
|
list := make([]int, 0, 4)
|
|
list = append(list, 1)
|
|
list = append(list, 2)
|
|
list = append(list, 3)
|
|
list = append(list, 4)
|
|
|
|
expected := "1,2,3,4"
|
|
got := IntListToString(list, ",")
|
|
if expected != got {
|
|
t.Errorf("Expected:%s, but got:%s", expected, got)
|
|
}
|
|
}
|
|
|
|
func TestInt64ListToString(t *testing.T) {
|
|
list := make([]int64, 0, 4)
|
|
list = append(list, 1)
|
|
list = append(list, 2)
|
|
list = append(list, 3)
|
|
list = append(list, 4)
|
|
|
|
expected := "1,2,3,4"
|
|
got := Int64ListToString(list, ",")
|
|
if expected != got {
|
|
t.Errorf("Expected:%s, but got:%s", expected, got)
|
|
}
|
|
}
|
|
|
|
func TestInt32ListToString(t *testing.T) {
|
|
list := make([]int32, 0, 4)
|
|
list = append(list, 1)
|
|
list = append(list, 2)
|
|
list = append(list, 3)
|
|
list = append(list, 4)
|
|
|
|
expected := "1,2,3,4"
|
|
got := Int32ListToString(list, ",")
|
|
if expected != got {
|
|
t.Errorf("Expected:%s, but got:%s", expected, got)
|
|
}
|
|
}
|