github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/pkg/test/asserts/asserts.go (about) 1 /* 2 Copyright 2011 Google Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // Package asserts provides a bad implementation of test predicate 18 // helpers. This package should either go away or dramatically 19 // improve. 20 package asserts 21 22 import ( 23 "strings" 24 "testing" 25 ) 26 27 func Expect(t *testing.T, got bool, what string) { 28 if !got { 29 t.Errorf("%s: got %v; expected %v", what, got, true) 30 } 31 } 32 33 func Assert(t *testing.T, got bool, what string) { 34 if !got { 35 t.Fatalf("%s: got %v; expected %v", what, got, true) 36 } 37 } 38 39 func ExpectErrorContains(t *testing.T, err error, substr, msg string) { 40 errorContains((*testing.T).Errorf, t, err, substr, msg) 41 } 42 43 func AssertErrorContains(t *testing.T, err error, substr, msg string) { 44 errorContains((*testing.T).Fatalf, t, err, substr, msg) 45 } 46 47 func errorContains(f func(*testing.T, string, ...interface{}), t *testing.T, err error, substr, msg string) { 48 if err == nil { 49 f(t, "%s: got nil error; expected error containing %q", msg, substr) 50 return 51 } 52 if !strings.Contains(err.Error(), substr) { 53 f(t, "%s: expected error containing %q; got instead error %q", msg, substr, err.Error()) 54 } 55 } 56 57 func ExpectString(t *testing.T, expect, got string, what string) { 58 if expect != got { 59 t.Errorf("%s: got %q; expected %q", what, got, expect) 60 } 61 } 62 63 func AssertString(t *testing.T, expect, got string, what string) { 64 if expect != got { 65 t.Fatalf("%s: got %q; expected %q", what, got, expect) 66 } 67 } 68 69 func ExpectBool(t *testing.T, expect, got bool, what string) { 70 if expect != got { 71 t.Errorf("%s: got %v; expected %v", what, got, expect) 72 } 73 } 74 75 func AssertBool(t *testing.T, expect, got bool, what string) { 76 if expect != got { 77 t.Fatalf("%s: got %v; expected %v", what, got, expect) 78 } 79 } 80 81 func ExpectInt(t *testing.T, expect, got int, what string) { 82 if expect != got { 83 t.Errorf("%s: got %d; expected %d", what, got, expect) 84 } 85 } 86 87 func AssertInt(t *testing.T, expect, got int, what string) { 88 if expect != got { 89 t.Fatalf("%s: got %d; expected %d", what, got, expect) 90 } 91 } 92 93 func ExpectNil(t *testing.T, v interface{}, what string) { 94 if v == nil { 95 return 96 } 97 t.Errorf("%s: expected nil; got %v", what, v) 98 } 99 100 func AssertNil(t *testing.T, v interface{}, what string) { 101 if v == nil { 102 return 103 } 104 t.Fatalf("%s: expected nil; got %v", what, v) 105 }