add HTML rendering capabilities and helper functions

This commit is contained in:
2024-11-12 23:46:40 +01:00
parent e44f56635b
commit f9990d37c0
8 changed files with 488 additions and 5 deletions
+33
View File
@@ -0,0 +1,33 @@
package testhelpers
func CheckSlicesEquality(a []any, b []any) bool {
if len(a) != len(b) {
return false
}
aMap := make(map[any]int)
bMap := make(map[any]int)
for _, v := range a {
aMap[v]++
}
for _, v := range b {
bMap[v]++
}
for k, v := range aMap {
if bMap[k] != v {
return false
}
}
return true
}
func StringSliceToAnySlice(s []string) []any {
var result []any
for _, v := range s {
result = append(result, v)
}
return result
}
+70
View File
@@ -0,0 +1,70 @@
package testhelpers
import (
"testing"
)
func Test_CheckSlicesEquality(t *testing.T) {
tests := []struct {
name string
ok bool
sliceA []any
sliceB []any
}{
{
name: "Integers",
ok: true,
sliceA: []any{2, 3, 1},
sliceB: []any{1, 2, 3},
},
{
name: "Strings",
ok: true,
sliceA: []any{"x", "y", "z"},
sliceB: []any{"z", "y", "x"},
},
{
name: "Integers 2",
ok: true,
sliceA: []any{1, 2, 3},
sliceB: []any{1, 2, 3},
},
{
name: "Different lengths",
ok: false,
sliceA: []any{1, 2, 3},
sliceB: []any{1, 2, 3, 4},
},
{
name: "Different lengths 2",
ok: false,
sliceA: []any{1, 2, 3, 4},
sliceB: []any{1, 2, 3},
},
{
name: "Different types",
ok: false,
sliceA: []any{1, 2, 3},
sliceB: []any{"1", "2", "3"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if result := CheckSlicesEquality(tt.sliceA, tt.sliceB); result != tt.ok {
t.Errorf("CheckSlicesEquality() = %v, want %v", result, tt.ok)
}
})
}
}
func Test_StringSliceToAnySlice(t *testing.T) {
expected := []any{"a", "b", "c"}
actual := StringSliceToAnySlice([]string{"a", "b", "c"})
if !CheckSlicesEquality(expected, actual) {
t.Errorf("StringSliceToAnySlice() = %v, want %v", actual, expected)
}
}