github.com/intel/goresctrl@v0.5.0/pkg/testutils/verify.go (about) 1 // Copyright 2020-2021 Intel Corporation. All Rights Reserved. 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 testutils 16 17 import ( 18 "reflect" 19 "strings" 20 "testing" 21 ) 22 23 // VerifyDeepEqual checks that two values (including structures) are equal, or else it fails the test. 24 func VerifyDeepEqual(t *testing.T, valueName string, expectedValue interface{}, seenValue interface{}) bool { 25 if reflect.DeepEqual(expectedValue, seenValue) { 26 return true 27 } 28 t.Errorf("expected %s value %+v, got %+v", valueName, expectedValue, seenValue) 29 return false 30 } 31 32 // VerifyError checks a (multi)error has expected properties, or else it fails the test. 33 func VerifyError(t *testing.T, err error, expectedCount int, expectedSubstrings []string) bool { 34 if expectedCount > 0 { 35 if err == nil { 36 t.Errorf("error expected, got nil") 37 return false 38 } 39 if merr, ok := err.(interface{ Unwrap() []error }); !ok { 40 if expectedCount > 1 { 41 t.Errorf("expected %d errors, but got %#v instead of multierror", expectedCount, err) 42 return false 43 } 44 // If exactly one error is expected, then err 45 // is allowed to be any error, not just a 46 // multierror. 47 } else if errs := merr.Unwrap(); len(errs) != expectedCount { 48 t.Errorf("expected %d errors, but got %d: %v", expectedCount, len(errs), merr) 49 return false 50 } 51 } else if expectedCount == 0 { 52 if err != nil { 53 t.Errorf("expected 0 errors, but got: %v", err) 54 return false 55 } 56 } 57 for _, substring := range expectedSubstrings { 58 if !strings.Contains(err.Error(), substring) { 59 t.Errorf("expected error with substring %#v, got \"%v\"", substring, err) 60 } 61 } 62 return true 63 } 64 65 func VerifyNoError(t *testing.T, err error) bool { 66 if err != nil { 67 t.Errorf("expected no error, got %v", err) 68 return false 69 } 70 return true 71 } 72 73 func VerifyStrings(t *testing.T, expected string, got string) bool { 74 if expected != got { 75 t.Errorf("Strings differ: expected %q, got %q", expected, got) 76 return false 77 } 78 return true 79 } 80 81 func VerifyStringSlices(t *testing.T, expected []string, got []string) bool { 82 if len(expected) != len(got) { 83 t.Errorf("Expected string slice of length %d, got %d", len(expected), len(got)) 84 return false 85 } 86 for i, es := range expected { 87 if es != got[i] { 88 t.Errorf("Slices differ: expected[%d]=%q, got[%d]=%q", i, es, i, got[i]) 89 return false 90 } 91 } 92 return true 93 }