github.com/google/yamlfmt@v0.12.2-0.20240514121411-7f77800e2681/internal/assert/assert.go (about) 1 // Copyright 2024 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this 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 OR 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 assert 16 17 var ( 18 // The failure format string for values not being equal. Formatted with `expected` then `got`. 19 EqualMessage = "value did not equal expectation.\nexpected: %v\n got: %v" 20 21 // The error format string for one or both pointers being nil. Formatted with `got` then `expected`. 22 DereferenceEqualErrMsg = "could not dereference nil pointer\ngot %v, expected %v" 23 24 // The failure format string if the err is not nil. Formatted with `err`. 25 NilErrMessage = "expected no error, got error:\n%v" 26 27 // The failure format string if the err is nil. 28 NotNilErrMesage = "expected an error, got nil" 29 30 // The failure format string for slices being different sizes. Formatted with `expected` then `got`. 31 SliceSizeMessage = "slices were different sizes.\nexpected len:%d\n got len:%d\n" 32 33 // The failure format string for slices not matching at some index. Formatted with the mismatched 34 // index, then `expected`, then `got`. 35 SliceMismatchMessage = "slices differed at index %d.\nexpected: %v\n got: %v" 36 ) 37 38 // The interface that represents the subset of `testing.T` that this package 39 // requires. Passing in a `testing.T` satisfies this interface. 40 type TestingT interface { 41 Helper() 42 Fatal(...any) 43 Fatalf(string, ...any) 44 Errorf(string, ...any) 45 } 46 47 // Assert that the passed condition is true. If not, fatally fail with 48 // `message` and format `args` into it. 49 func Assert(t TestingT, condition bool, message string, args ...any) { 50 t.Helper() 51 52 if !condition { 53 t.Fatalf(message, args...) 54 } 55 } 56 57 // Assert that `got` equals `expected`. The types between compared 58 // arguments must be the same. Uses `assert.EqualMessage`. 59 func Equal[T comparable](t TestingT, expected T, got T) { 60 t.Helper() 61 EqualMsg(t, expected, got, EqualMessage) 62 } 63 64 // Assert that the value at `got` equals the value at `expected`. Will 65 // error if either pointer is nil. Uses `assert.DereferenceEqualErrMsg` 66 // and `assert.EqualMessage`. 67 func DereferenceEqual[T comparable](t TestingT, expected *T, got *T) { 68 t.Helper() 69 DereferenceEqualMsg(t, expected, got, DereferenceEqualErrMsg, EqualMessage) 70 } 71 72 // Assert that that `err` is nil. Uses `assert.NilErrMessage`. 73 func NilErr(t TestingT, err error) { 74 t.Helper() 75 Assert(t, err == nil, NilErrMessage, err) 76 } 77 78 // Assert that that `err` is not nil. Uses `assert.NotNillErrMesage`. 79 func NotNilErr(t TestingT, err error) { 80 t.Helper() 81 Assert(t, err != nil, NotNilErrMesage) 82 } 83 84 // Assert that slices `got` and `expected` are equal. Will produce a 85 // different message if the lengths are different or if any element 86 // mismatches. Uses `assert.SliceSizeMessage` and 87 // `assert.SliceMismatchMessage`. 88 func SliceEqual[T comparable](t TestingT, expected []T, got []T) { 89 t.Helper() 90 SliceEqualMsg( 91 t, 92 expected, 93 got, 94 SliceSizeMessage, 95 SliceMismatchMessage, 96 ) 97 } 98 99 // Assert that `got` equals `expected`. The types between compared 100 // arguments must be the same. Uses `message`. 101 func EqualMsg[T comparable](t TestingT, expected T, got T, message string) { 102 t.Helper() 103 104 if got != expected { 105 t.Fatalf(message, expected, got) 106 } 107 } 108 109 // Assert that the value at `got` equals the value at `expected`. Will 110 // error if either pointer is nil. Uses `errMessage` and `mismatchMessage`. 111 func DereferenceEqualMsg[T comparable]( 112 t TestingT, 113 expected *T, 114 got *T, 115 errMessage, 116 mismatchMessage string, 117 ) { 118 t.Helper() 119 120 if got == nil || expected == nil { 121 t.Errorf(errMessage, expected, got) 122 } else { 123 EqualMsg(t, *expected, *got, mismatchMessage) 124 } 125 } 126 127 // Assert that slices `got` and `expected` are equal. Will produce a 128 // different message if the lengths are different or if any element 129 // mismatches. Uses `sizeMessage` and `mismatchMessage`. 130 func SliceEqualMsg[T comparable]( 131 t TestingT, 132 expected []T, 133 got []T, 134 sizeMessage, mismatchMessage string, 135 ) { 136 t.Helper() 137 138 if len(got) != len(expected) { 139 t.Fatalf(sizeMessage, len(expected), len(got)) 140 } else { 141 for i := range got { 142 if got[i] != expected[i] { 143 t.Fatalf(mismatchMessage, i, expected[i], got[i]) 144 } 145 } 146 } 147 }