Files
goext/langext/maps_test.go
T
Mikescher 02d6894ec6
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m34s
[🤖] Add Unit-Tests
2026-04-27 16:31:29 +02:00

147 lines
3.3 KiB
Go

package langext
import (
"git.blackforestbytes.com/BlackForestBytes/goext/tst"
"sort"
"testing"
)
func TestMapKeyArr(t *testing.T) {
m := map[string]int{"a": 1, "b": 2, "c": 3}
keys := MapKeyArr(m)
sort.Strings(keys)
tst.AssertArrayEqual(t, keys, []string{"a", "b", "c"})
}
func TestMapKeyArrEmpty(t *testing.T) {
m := map[string]int{}
keys := MapKeyArr(m)
tst.AssertEqual(t, len(keys), 0)
}
func TestMapValueArr(t *testing.T) {
m := map[string]int{"a": 1, "b": 2, "c": 3}
values := MapValueArr(m)
sort.Ints(values)
tst.AssertArrayEqual(t, values, []int{1, 2, 3})
}
func TestArrToMap(t *testing.T) {
type item struct {
key string
v int
}
arr := []item{{"a", 1}, {"b", 2}}
m := ArrToMap(arr, func(i item) string { return i.key })
tst.AssertEqual(t, len(m), 2)
tst.AssertEqual(t, m["a"].v, 1)
tst.AssertEqual(t, m["b"].v, 2)
}
func TestArrToKVMap(t *testing.T) {
arr := []int{1, 2, 3}
m := ArrToKVMap(arr,
func(v int) int { return v },
func(v int) string {
return [...]string{"", "one", "two", "three"}[v]
},
)
tst.AssertEqual(t, m[1], "one")
tst.AssertEqual(t, m[2], "two")
tst.AssertEqual(t, m[3], "three")
}
func TestArrToSet(t *testing.T) {
arr := []string{"a", "b", "a", "c"}
set := ArrToSet(arr)
tst.AssertEqual(t, len(set), 3)
tst.AssertEqual(t, set["a"], true)
tst.AssertEqual(t, set["b"], true)
tst.AssertEqual(t, set["c"], true)
tst.AssertEqual(t, set["d"], false)
}
func TestMapToArr(t *testing.T) {
m := map[string]int{"a": 1, "b": 2}
arr := MapToArr(m)
tst.AssertEqual(t, len(arr), 2)
roundTrip := make(map[string]int)
for _, e := range arr {
roundTrip[e.Key] = e.Value
}
tst.AssertEqual(t, roundTrip["a"], 1)
tst.AssertEqual(t, roundTrip["b"], 2)
}
func TestCopyMap(t *testing.T) {
src := map[string]int{"a": 1, "b": 2}
dst := CopyMap(src)
tst.AssertEqual(t, len(dst), 2)
tst.AssertEqual(t, dst["a"], 1)
// Mutating dst should not affect src
dst["a"] = 99
tst.AssertEqual(t, src["a"], 1)
}
func TestForceMapNil(t *testing.T) {
var m map[string]int
res := ForceMap(m)
if res == nil {
t.Errorf("expected non-nil result")
}
tst.AssertEqual(t, len(res), 0)
}
func TestForceMapNonNil(t *testing.T) {
m := map[string]int{"x": 1}
res := ForceMap(m)
tst.AssertEqual(t, res["x"], 1)
}
func TestForceJsonMapOrPanic(t *testing.T) {
type s struct {
A int `json:"a"`
B string `json:"b"`
}
res := ForceJsonMapOrPanic(s{A: 1, B: "x"})
if v, ok := res["a"].(float64); !ok || v != 1 {
t.Errorf("expected a=1, got %v", res["a"])
}
if v, ok := res["b"].(string); !ok || v != "x" {
t.Errorf("expected b=x, got %v", res["b"])
}
}
func TestForceJsonMapOrPanicPanics(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("expected panic on un-marshalable input")
}
}()
ForceJsonMapOrPanic(make(chan int))
}
func TestMapMerge(t *testing.T) {
base := map[string]int{"a": 1, "b": 2}
a := map[string]int{"b": 22, "c": 3}
b := map[string]int{"d": 4}
res := MapMerge(base, a, b)
tst.AssertEqual(t, res["a"], 1)
tst.AssertEqual(t, res["b"], 22) // overwritten
tst.AssertEqual(t, res["c"], 3)
tst.AssertEqual(t, res["d"], 4)
tst.AssertEqual(t, len(res), 4)
// base must remain untouched
tst.AssertEqual(t, base["b"], 2)
}
func TestMapMergeNoExtras(t *testing.T) {
base := map[string]int{"a": 1}
res := MapMerge(base)
tst.AssertEqual(t, res["a"], 1)
tst.AssertEqual(t, len(res), 1)
}