github.com/greenpau/go-authcrunch@v1.1.4/internal/tests/eval.go (about) 1 // Copyright 2022 Paul Greenberg greenpau@outlook.com 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 tests 16 17 import ( 18 "fmt" 19 20 "github.com/google/go-cmp/cmp" 21 "io/ioutil" 22 "os" 23 "regexp" 24 "runtime" 25 "testing" 26 ) 27 28 var ( 29 pr = regexp.MustCompile(`.*(github\\.com/greenpau/go-authcrunch/.*)$`) 30 ) 31 32 // EvalErr evaluates whether there is an error. If there is, was it the 33 // expected error. 34 func EvalErr(t *testing.T, err error, data interface{}, shouldErr bool, expErr error) bool { 35 if !shouldErr { 36 if err == nil { 37 return false 38 } 39 t.Fatalf("expected success, but got error: %s", err) 40 } 41 if err == nil { 42 t.Fatalf("expected error '%v', but got success: %v", expErr, data) 43 } 44 if expErr == nil { 45 expErr = fmt.Errorf("") 46 } 47 if diff := cmp.Diff(expErr.Error(), err.Error()); diff != "" { 48 t.Fatalf("unexpected error (-want +got):\n%s", diff) 49 } 50 // t.Logf("received expected error: %v", err) 51 return true 52 } 53 54 // WriteLog writes logs from tests. 55 func WriteLog(t *testing.T, msgs []string) { 56 if len(msgs) == 0 { 57 return 58 } 59 for _, msg := range msgs { 60 t.Logf("%s", msg) 61 } 62 } 63 64 // EvalErrPhaseWithLog evaluates the error according to the phase of execution. 65 func EvalErrPhaseWithLog(t *testing.T, err error, errPhase, expErrPhase string, shouldErr bool, expErr error, msgs []string) bool { 66 if expErrPhase == errPhase { 67 return EvalErrWithLog(t, err, errPhase, shouldErr, expErr, msgs) 68 } 69 return EvalErrWithLog(t, err, errPhase, false, nil, msgs) 70 } 71 72 // EvalErrWithLog evaluates the error. 73 func EvalErrWithLog(t *testing.T, err error, data interface{}, shouldErr bool, expErr error, msgs []string) bool { 74 _, fileName, lineNum, ok := runtime.Caller(1) 75 if ok { 76 fileName = pr.ReplaceAllString(fileName, "$1") 77 msgs = append([]string{fmt.Sprintf("source: %s:%d", fileName, lineNum)}, msgs...) 78 } 79 if !shouldErr { 80 if err == nil { 81 return false 82 } 83 WriteLog(t, msgs) 84 t.Fatalf("expected success, but got error: %s", err) 85 } 86 if err == nil { 87 WriteLog(t, msgs) 88 t.Fatalf("expected error, but got success: %v", data) 89 } 90 if expErr == nil { 91 expErr = fmt.Errorf("") 92 } 93 if diff := cmp.Diff(expErr.Error(), err.Error()); diff != "" { 94 WriteLog(t, msgs) 95 t.Fatalf("unexpected error (-want +got):\n%s", diff) 96 } 97 // t.Logf("received expected error: %v", err) 98 return true 99 } 100 101 // EvalObjects compares two objects. 102 func EvalObjects(t *testing.T, name string, want, got interface{}) { 103 if diff := cmp.Diff(want, got); diff != "" { 104 t.Fatalf("%s mismatch (-want +got):\n%s", name, diff) 105 } 106 } 107 108 // EvalObjectsWithLog compares two objects and logs extra output when 109 // detects an error. 110 func EvalObjectsWithLog(t *testing.T, name string, want, got interface{}, msgs []string) { 111 _, fileName, lineNum, ok := runtime.Caller(1) 112 if ok { 113 fileName = pr.ReplaceAllString(fileName, "$1") 114 msgs = append([]string{fmt.Sprintf("source: %s:%d", fileName, lineNum)}, msgs...) 115 } 116 if diff := cmp.Diff(want, got); diff != "" { 117 WriteLog(t, msgs) 118 t.Fatalf("%s mismatch (-want +got):\n%s", name, diff) 119 } 120 } 121 122 // CustomEvalObjectsWithLog compares two objects and logs extra output when 123 // detects an error. 124 func CustomEvalObjectsWithLog(t *testing.T, name string, want, got interface{}, msgs []string, typs interface{}) { 125 _, fileName, lineNum, ok := runtime.Caller(1) 126 if ok { 127 fileName = pr.ReplaceAllString(fileName, "$1") 128 msgs = append([]string{fmt.Sprintf("source: %s:%d", fileName, lineNum)}, msgs...) 129 } 130 if diff := cmp.Diff(want, got, cmp.AllowUnexported(typs)); diff != "" { 131 WriteLog(t, msgs) 132 t.Fatalf("%s mismatch (-want +got):\n%s", name, diff) 133 } 134 } 135 136 // TempDir creates temporary directory. 137 func TempDir(s string) (string, error) { 138 rootDir := os.TempDir() + "/testdata/go-authcrunch/" + s 139 if err := os.MkdirAll(rootDir, 0700); err != nil { 140 return "", err 141 } 142 tmpDir, err := ioutil.TempDir(rootDir, "") 143 if err != nil { 144 return "", err 145 } 146 return tmpDir, nil 147 }