github.com/greenpau/go-identity@v1.1.6/internal/tests/eval.go (about) 1 // Copyright 2020 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 "github.com/google/go-cmp/cmp" 20 "io/ioutil" 21 "os" 22 "testing" 23 ) 24 25 // EvalErr evaluates whether there is an error. If there is, was it the 26 // expected error. 27 func EvalErr(t *testing.T, err error, data interface{}, shouldErr bool, expErr error) bool { 28 if !shouldErr { 29 if err == nil { 30 return false 31 } 32 t.Fatalf("expected success, but got error: %s", err) 33 } 34 if err == nil { 35 t.Fatalf("expected error '%v', but got success: %v", expErr, data) 36 } 37 if expErr == nil { 38 expErr = fmt.Errorf("") 39 } 40 if diff := cmp.Diff(expErr.Error(), err.Error()); diff != "" { 41 t.Fatalf("unexpected error (-want +got):\n%s", diff) 42 } 43 // t.Logf("received expected error: %v", err) 44 return true 45 } 46 47 // WriteLog writes logs from tests. 48 func WriteLog(t *testing.T, msgs []string) { 49 if len(msgs) == 0 { 50 return 51 } 52 for _, msg := range msgs { 53 t.Logf("%s", msg) 54 } 55 } 56 57 // EvalErrWithLog evaluates the error. 58 func EvalErrWithLog(t *testing.T, err error, data interface{}, shouldErr bool, expErr error, msgs []string) bool { 59 if !shouldErr { 60 if err == nil { 61 return false 62 } 63 WriteLog(t, msgs) 64 t.Fatalf("expected success, but got error: %s", err) 65 } 66 if err == nil { 67 WriteLog(t, msgs) 68 t.Fatalf("expected error, but got success: %v", data) 69 } 70 if expErr == nil { 71 expErr = fmt.Errorf("") 72 } 73 if diff := cmp.Diff(expErr.Error(), err.Error()); diff != "" { 74 WriteLog(t, msgs) 75 t.Fatalf("unexpected error (-want +got):\n%s", diff) 76 } 77 // t.Logf("received expected error: %v", err) 78 return true 79 } 80 81 // EvalObjects compares two objects. 82 func EvalObjects(t *testing.T, name string, want, got interface{}) { 83 if diff := cmp.Diff(want, got); diff != "" { 84 t.Fatalf("%s mismatch (-want +got):\n%s", name, diff) 85 } 86 } 87 88 // EvalObjectsWithLog compares two objects and logs extra output when 89 // detects an error 90 func EvalObjectsWithLog(t *testing.T, name string, want, got interface{}, msgs []string) { 91 if diff := cmp.Diff(want, got); diff != "" { 92 WriteLog(t, msgs) 93 t.Fatalf("%s mismatch (-want +got):\n%s", name, diff) 94 } 95 } 96 97 // TempDir creates temporary directory. 98 func TempDir(s string) (string, error) { 99 rootDir := os.TempDir() + "/testdata/go-identity/" + s 100 if err := os.MkdirAll(rootDir, 0700); err != nil { 101 return "", err 102 } 103 tmpDir, err := ioutil.TempDir(rootDir, "") 104 if err != nil { 105 return "", err 106 } 107 return tmpDir, nil 108 }