sliding-sync/internal/util_test.go
2023-11-16 19:31:43 +00:00

59 lines
1.6 KiB
Go

package internal
import (
"reflect"
"sort"
"testing"
)
func TestKeys(t *testing.T) {
assertSlice(t, Keys((map[string]int)(nil)), nil)
assertSlice(t, Keys(map[string]int{}), []string{})
assertSlice(t, Keys(map[string]int{"a": 1}), []string{"a"})
assertSlice(t, Keys(map[string]int{"a": 1, "b": 2, "c": 3}), []string{"a", "b", "c"})
assertSlice(t, Keys(map[string]int{"": 1, "!": 1, "☃": 1, "\x00": 1}), []string{"", "!", "☃", "\x00"})
}
// assertSlicesSameElements errors the test if "got" and "want" have different elements.
// Both got and want are sorted in-place as a side effect.
func assertSlice(t *testing.T, got, want []string) {
if len(got) != len(want) {
t.Errorf("got length %d, expected length %d", len(got), len(want))
}
sort.Slice(got, func(i, j int) bool { return got[i] < got[j] })
sort.Slice(want, func(i, j int) bool { return want[i] < want[j] })
if !reflect.DeepEqual(got, want) {
t.Errorf("After sorting, got %v but expected %v", got, want)
}
}
func TestUnixSocket_True(t *testing.T) {
address := "/path/to/socket"
if !IsUnixSocket(address) {
t.Errorf("%s is socket", address)
}
}
func TestUnixSocket_False(t *testing.T) {
address := "localhost:8080"
if IsUnixSocket(address) {
t.Errorf("%s is not socket", address)
}
}
func TestGetBaseUrl_UnixSocket(t *testing.T) {
address := "/path/to/socket"
if GetBaseURL(address) != "http://unix" {
t.Errorf("%s is unix socket", address)
}
}
func TestGetBaseUrl_Http(t *testing.T) {
address := "localhost:8080"
if GetBaseURL(address) != "localhost:8080" {
t.Errorf("%s is not a unix socket", address)
}
}