Debian Codegraph @

server/thw/helper_test.go

raw
1 // Copyright 2024 The NATS Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package thw
15
16 import (
17 "strings"
18 "testing"
19 )
20
21 func require_NoError(t *testing.T, err error) {
22 t.Helper()
23 if err != nil {
24 t.Fatalf("require no error, but got: %v", err)
25 }
26 }
27
28 func require_Error(t *testing.T, err error, expected ...error) {
29 t.Helper()
30 if err == nil {
31 t.Fatalf("require error, but got none")
32 }
33 if len(expected) == 0 {
34 return
35 }
36 eStr := err.Error()
37 for _, e := range expected {
38 if err == e || strings.Contains(eStr, e.Error()) || strings.Contains(e.Error(), eStr) {
39 return
40 }
41 }
42 t.Fatalf("Expected one of %v, got '%v'", expected, err)
43 }
44
45 func require_True(t *testing.T, b bool) {
46 t.Helper()
47 if !b {
48 t.Fatalf("require true, but got false")
49 }
50 }
51
52 func require_Equal[T comparable](t *testing.T, a, b T) {
53 t.Helper()
54 if a != b {
55 t.Fatalf("require %T equal, but got: %v != %v", a, a, b)
56 }
57 }