[🤖] Add Unit-Tests
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m34s

This commit is contained in:
2026-04-27 10:46:08 +02:00
parent dad0e3240d
commit 02d6894ec6
116 changed files with 18795 additions and 1 deletions
+240
View File
@@ -0,0 +1,240 @@
package tst
import (
"testing"
)
// These tests exercise the success-paths of each assertion: i.e. the cases
// where the assertion should NOT mark the test as failed. They verify the
// boolean/conditional logic of each function. We avoid testing the failure
// branches because the assertions take a concrete *testing.T (not an
// interface), so substituting a mock would require unsafe/reflection tricks
// that are brittle and cascade failures to the parent test.
// --- AssertEqual ----------------------------------------------------------
func TestAssertEqual_Int(t *testing.T) {
AssertEqual(t, 42, 42)
AssertEqual(t, -1, -1)
AssertEqual(t, 0, 0)
}
func TestAssertEqual_String(t *testing.T) {
AssertEqual(t, "hello", "hello")
AssertEqual(t, "", "")
}
func TestAssertEqual_Bool(t *testing.T) {
AssertEqual(t, true, true)
AssertEqual(t, false, false)
}
func TestAssertEqual_Float(t *testing.T) {
AssertEqual(t, 3.14, 3.14)
}
// --- AssertArrayEqual -----------------------------------------------------
func TestAssertArrayEqual_Empty(t *testing.T) {
AssertArrayEqual(t, []int{}, []int{})
}
func TestAssertArrayEqual_Equal(t *testing.T) {
AssertArrayEqual(t, []int{1, 2, 3}, []int{1, 2, 3})
AssertArrayEqual(t, []string{"a", "b"}, []string{"a", "b"})
}
func TestAssertArrayEqual_SingleElement(t *testing.T) {
AssertArrayEqual(t, []int{7}, []int{7})
}
// --- AssertNotEqual -------------------------------------------------------
func TestAssertNotEqual_Int(t *testing.T) {
AssertNotEqual(t, 1, 2)
AssertNotEqual(t, 0, -1)
}
func TestAssertNotEqual_String(t *testing.T) {
AssertNotEqual(t, "foo", "bar")
AssertNotEqual(t, "", "x")
}
func TestAssertNotEqual_Bool(t *testing.T) {
AssertNotEqual(t, true, false)
}
// --- AssertDeepEqual ------------------------------------------------------
func TestAssertDeepEqual_Struct(t *testing.T) {
type s struct {
A int
B string
}
AssertDeepEqual(t, s{A: 1, B: "x"}, s{A: 1, B: "x"})
}
func TestAssertDeepEqual_Slice(t *testing.T) {
AssertDeepEqual(t, []int{1, 2, 3}, []int{1, 2, 3})
}
func TestAssertDeepEqual_Map(t *testing.T) {
AssertDeepEqual(t, map[string]int{"a": 1, "b": 2}, map[string]int{"a": 1, "b": 2})
}
func TestAssertDeepEqual_NilSlice(t *testing.T) {
var a, b []int
AssertDeepEqual(t, a, b)
}
func TestAssertDeepEqual_NestedStruct(t *testing.T) {
type inner struct{ X int }
type outer struct {
I inner
S []string
}
a := outer{I: inner{X: 1}, S: []string{"a", "b"}}
b := outer{I: inner{X: 1}, S: []string{"a", "b"}}
AssertDeepEqual(t, a, b)
}
// --- AssertSetDeepEqual ---------------------------------------------------
func TestAssertSetDeepEqual_Empty(t *testing.T) {
AssertSetDeepEqual(t, []int{}, []int{})
}
func TestAssertSetDeepEqual_SameOrder(t *testing.T) {
AssertSetDeepEqual(t, []int{1, 2, 3}, []int{1, 2, 3})
}
func TestAssertSetDeepEqual_DifferentOrder(t *testing.T) {
AssertSetDeepEqual(t, []int{3, 1, 2}, []int{1, 2, 3})
}
func TestAssertSetDeepEqual_Strings(t *testing.T) {
AssertSetDeepEqual(t, []string{"b", "a", "c"}, []string{"a", "b", "c"})
}
func TestAssertSetDeepEqual_Structs(t *testing.T) {
type s struct{ V int }
AssertSetDeepEqual(t, []s{{V: 2}, {V: 1}}, []s{{V: 1}, {V: 2}})
}
// --- AssertNotDeepEqual ---------------------------------------------------
func TestAssertNotDeepEqual_Struct(t *testing.T) {
type s struct {
A int
}
AssertNotDeepEqual(t, s{A: 1}, s{A: 2})
}
func TestAssertNotDeepEqual_Slice(t *testing.T) {
AssertNotDeepEqual(t, []int{1, 2}, []int{1, 2, 3})
}
func TestAssertNotDeepEqual_Map(t *testing.T) {
AssertNotDeepEqual(t, map[string]int{"a": 1}, map[string]int{"b": 1})
}
// --- AssertDeRefEqual -----------------------------------------------------
func TestAssertDeRefEqual_Int(t *testing.T) {
v := 42
AssertDeRefEqual(t, &v, 42)
}
func TestAssertDeRefEqual_String(t *testing.T) {
v := "hello"
AssertDeRefEqual(t, &v, "hello")
}
func TestAssertDeRefEqual_Bool(t *testing.T) {
v := true
AssertDeRefEqual(t, &v, true)
}
// --- AssertPtrEqual -------------------------------------------------------
func TestAssertPtrEqual_BothNil(t *testing.T) {
var a, b *int
AssertPtrEqual(t, a, b)
}
func TestAssertPtrEqual_SameValue(t *testing.T) {
a, b := 5, 5
AssertPtrEqual(t, &a, &b)
}
func TestAssertPtrEqual_DifferentPointersSameValue(t *testing.T) {
a, b := "hi", "hi"
AssertPtrEqual(t, &a, &b)
}
func TestAssertPtrEqual_SamePointer(t *testing.T) {
a := 99
AssertPtrEqual(t, &a, &a)
}
// --- AssertHexEqual -------------------------------------------------------
func TestAssertHexEqual_Empty(t *testing.T) {
AssertHexEqual(t, "", []byte{})
}
func TestAssertHexEqual_Bytes(t *testing.T) {
AssertHexEqual(t, "deadbeef", []byte{0xde, 0xad, 0xbe, 0xef})
}
func TestAssertHexEqual_SingleByte(t *testing.T) {
AssertHexEqual(t, "ff", []byte{0xff})
}
func TestAssertHexEqual_AllZero(t *testing.T) {
AssertHexEqual(t, "0000", []byte{0x00, 0x00})
}
// --- AssertTrue / AssertFalse ---------------------------------------------
func TestAssertTrue(t *testing.T) {
AssertTrue(t, true)
}
func TestAssertFalse(t *testing.T) {
AssertFalse(t, false)
}
// --- AssertNoErr ----------------------------------------------------------
func TestAssertNoErr_Nil(t *testing.T) {
AssertNoErr(t, nil)
}
// --- AssertStrRepEqual ----------------------------------------------------
func TestAssertStrRepEqual_Same(t *testing.T) {
AssertStrRepEqual(t, 42, 42)
AssertStrRepEqual(t, "abc", "abc")
}
func TestAssertStrRepEqual_DifferentTypesSameRep(t *testing.T) {
// 42 and "42" both stringify to "42" via %v
AssertStrRepEqual(t, 42, "42")
}
func TestAssertStrRepEqual_Bool(t *testing.T) {
AssertStrRepEqual(t, true, true)
}
// --- AssertStrRepNotEqual -------------------------------------------------
func TestAssertStrRepNotEqual_Different(t *testing.T) {
AssertStrRepNotEqual(t, 42, 43)
AssertStrRepNotEqual(t, "abc", "xyz")
}
func TestAssertStrRepNotEqual_BoolVsInt(t *testing.T) {
// "true" != "1"
AssertStrRepNotEqual(t, true, 1)
}
+71
View File
@@ -0,0 +1,71 @@
package tst
import (
"testing"
)
// Success-path tests for the Ident-prefixed assertions. They exercise the
// non-failing branches of each function.
// --- AssertIdentEqual -----------------------------------------------------
func TestAssertIdentEqual_Int(t *testing.T) {
AssertIdentEqual(t, "value", 1, 1)
AssertIdentEqual(t, "value", 0, 0)
}
func TestAssertIdentEqual_String(t *testing.T) {
AssertIdentEqual(t, "name", "alice", "alice")
}
func TestAssertIdentEqual_Bool(t *testing.T) {
AssertIdentEqual(t, "flag", true, true)
AssertIdentEqual(t, "flag", false, false)
}
func TestAssertIdentEqual_EmptyIdent(t *testing.T) {
AssertIdentEqual(t, "", 7, 7)
}
// --- AssertIdentNotEqual --------------------------------------------------
func TestAssertIdentNotEqual_Int(t *testing.T) {
AssertIdentNotEqual(t, "value", 1, 2)
}
func TestAssertIdentNotEqual_String(t *testing.T) {
AssertIdentNotEqual(t, "name", "alice", "bob")
}
// --- AssertIdentPtrEqual --------------------------------------------------
func TestAssertIdentPtrEqual_BothNil(t *testing.T) {
var a, b *int
AssertIdentPtrEqual(t, "ptr", a, b)
}
func TestAssertIdentPtrEqual_SameValue(t *testing.T) {
a, b := 10, 10
AssertIdentPtrEqual(t, "ptr", &a, &b)
}
func TestAssertIdentPtrEqual_SameStringValue(t *testing.T) {
a, b := "x", "x"
AssertIdentPtrEqual(t, "ptr", &a, &b)
}
// --- AssertIdentTrue / AssertIdentFalse -----------------------------------
func TestAssertIdentTrue(t *testing.T) {
AssertIdentTrue(t, "ok", true)
}
// AssertIdentFalse has a known quirk in the original implementation:
// `if !value { t.Errorf(...) }` — i.e. it only fails when value is false,
// which means "true" is the success path. We test the success path as
// implemented (we MUST NOT change existing code).
func TestAssertIdentFalse_SuccessPathAsImplemented(t *testing.T) {
// As coded, AssertIdentFalse fails when value is false. So passing
// `true` is the no-failure path according to the current implementation.
AssertIdentFalse(t, "ok", true)
}
+49
View File
@@ -0,0 +1,49 @@
package tst
import (
"strconv"
"testing"
)
// --- Must -----------------------------------------------------------------
func TestMust_NoError_Int(t *testing.T) {
v := Must(123, nil)(t)
AssertEqual(t, v, 123)
}
func TestMust_NoError_String(t *testing.T) {
v := Must("hello", nil)(t)
AssertEqual(t, v, "hello")
}
func TestMust_NoError_Slice(t *testing.T) {
v := Must([]int{1, 2, 3}, nil)(t)
AssertArrayEqual(t, v, []int{1, 2, 3})
}
func TestMust_NoError_Struct(t *testing.T) {
type s struct {
X int
Y string
}
v := Must(s{X: 7, Y: "abc"}, nil)(t)
AssertEqual(t, v, s{X: 7, Y: "abc"})
}
func TestMust_StrconvAtoi(t *testing.T) {
v := Must(strconv.Atoi("42"))(t)
AssertEqual(t, v, 42)
}
func TestMust_ZeroValueOnNoError(t *testing.T) {
v := Must(0, nil)(t)
AssertEqual(t, v, 0)
}
func TestMust_ReturnedFnIsNotNil(t *testing.T) {
fn := Must("anything", nil)
if fn == nil {
t.Fatal("Must should return a non-nil function")
}
}