go.nanomsg.org/mangos/v3@v3.4.3-0.20240217232803-46464076f1f5/internal/test/must.go (about) 1 // Copyright 2019 The Mangos Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use file except in compliance with the License. 5 // You may obtain a copy of the license at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES O R CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package test 16 17 import ( 18 "reflect" 19 "runtime" 20 "testing" 21 ) 22 23 // MustSucceed verifies that that the supplied error is nil. 24 // If it is not nil, we call t.Fatalf() to fail the test immediately. 25 func MustSucceed(t *testing.T, e error) { 26 if e != nil { 27 _, file, line, _ := runtime.Caller(1) 28 t.Fatalf("Failed at %s:%d: Error is not nil: %v", 29 file, line, e) 30 } 31 } 32 33 // MustFail verifies that the error is not nil. 34 // If it is nil, the test is a fatal failure. 35 func MustFail(t *testing.T, e error) { 36 if e == nil { 37 _, file, line, _ := runtime.Caller(1) 38 t.Fatalf("Failed at %s:%d: Error is nil", file, line) 39 } 40 } 41 42 // MustBeError verifies that a value is a specific error. 43 func MustBeError(t *testing.T, e error, compare error) { 44 45 if e == nil { 46 _, file, line, _ := runtime.Caller(1) 47 t.Fatalf("Failed at %s:%d: Error is nil", file, line) 48 } 49 if e.Error() != compare.Error() { 50 _, file, line, _ := runtime.Caller(1) 51 t.Fatalf("Failed at %s:%d: Error was %v, expected %v", 52 file, line, e, compare) 53 } 54 } 55 56 // MustBeTrue verifies that the condition is true. 57 func MustBeTrue(t *testing.T, b bool) { 58 if !b { 59 _, file, line, _ := runtime.Caller(1) 60 t.Fatalf("Failed at %s:%d: Condition is false", file, line) 61 } 62 } 63 64 // MustBeFalse verifies that the condition is true. 65 func MustBeFalse(t *testing.T, b bool) { 66 if b { 67 _, file, line, _ := runtime.Caller(1) 68 t.Fatalf("Failed at %s:%d: Condition is true", file, line) 69 } 70 } 71 72 // MustNotBeNil verifies that the provided value is not nil 73 func MustNotBeNil(t *testing.T, v interface{}) { 74 if reflect.ValueOf(v).IsNil() { 75 _, file, line, _ := runtime.Caller(1) 76 t.Fatalf("Failed at %s:%d: Value is nil", file, line) 77 } 78 } 79 80 // MustBeNil verifies that the provided value is nil 81 func MustBeNil(t *testing.T, v interface{}) { 82 if !reflect.ValueOf(v).IsNil() { 83 _, file, line, _ := runtime.Caller(1) 84 t.Fatalf("Failed at %s:%d: Value is not nil", file, line) 85 } 86 }