]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - testutil/testutil.go
Use SPDX license identifiers
[safcm/safcm.git] / testutil / testutil.go
1 // Utility functions useful for tests
2
3 // SPDX-License-Identifier: GPL-3.0-or-later
4 // Copyright (C) 2021-2024  Simon Ruderich
5
6 package testutil
7
8 import (
9         "fmt"
10         "reflect"
11         "testing"
12
13         "github.com/google/go-cmp/cmp"
14 )
15
16 func AssertEqual(t *testing.T, name string, act, exp interface{}) {
17         t.Helper()
18
19         if !reflect.DeepEqual(act, exp) {
20                 t.Errorf("%s: %s", name, cmp.Diff(exp, act))
21         }
22 }
23
24 func AssertErrorEqual(t *testing.T, name string, act, exp error) {
25         t.Helper()
26
27         // Ugly but the simplest way to compare errors (including nil)
28         actStr := fmt.Sprintf("%v", act)
29         expStr := fmt.Sprintf("%v", exp)
30         if actStr != expStr {
31                 t.Errorf("err = %s (%#v), want %s (%#v)",
32                         actStr, act, expStr, exp)
33         }
34 }