github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/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 // NOTE: THESE FUNCTIONS ARE DEPRECATED. PLEASE DO NOT USE THEM IN 28 // NEW CODE. 29 30 func Expect(t *testing.T, got bool, what string) { 31 if !got { 32 t.Errorf("%s: got %v; expected %v", what, got, true) 33 } 34 } 35 36 func Assert(t *testing.T, got bool, what string) { 37 if !got { 38 t.Fatalf("%s: got %v; expected %v", what, got, true) 39 } 40 } 41 42 func ExpectErrorContains(t *testing.T, err error, substr, msg string) { 43 errorContains((*testing.T).Errorf, t, err, substr, msg) 44 } 45 46 func AssertErrorContains(t *testing.T, err error, substr, msg string) { 47 errorContains((*testing.T).Fatalf, t, err, substr, msg) 48 } 49 50 func errorContains(f func(*testing.T, string, ...interface{}), t *testing.T, err error, substr, msg string) { 51 if err == nil { 52 f(t, "%s: got nil error; expected error containing %q", msg, substr) 53 return 54 } 55 if !strings.Contains(err.Error(), substr) { 56 f(t, "%s: expected error containing %q; got instead error %q", msg, substr, err.Error()) 57 } 58 } 59 60 func ExpectString(t *testing.T, expect, got string, what string) { 61 if expect != got { 62 t.Errorf("%s: got %q; expected %q", what, got, expect) 63 } 64 } 65 66 func AssertString(t *testing.T, expect, got string, what string) { 67 if expect != got { 68 t.Fatalf("%s: got %q; expected %q", what, got, expect) 69 } 70 } 71 72 func ExpectBool(t *testing.T, expect, got bool, what string) { 73 if expect != got { 74 t.Errorf("%s: got %v; expected %v", what, got, expect) 75 } 76 } 77 78 func AssertBool(t *testing.T, expect, got bool, what string) { 79 if expect != got { 80 t.Fatalf("%s: got %v; expected %v", what, got, expect) 81 } 82 } 83 84 func ExpectInt(t *testing.T, expect, got int, what string) { 85 if expect != got { 86 t.Errorf("%s: got %d; expected %d", what, got, expect) 87 } 88 } 89 90 func AssertInt(t *testing.T, expect, got int, what string) { 91 if expect != got { 92 t.Fatalf("%s: got %d; expected %d", what, got, expect) 93 } 94 } 95 96 func ExpectNil(t *testing.T, v interface{}, what string) { 97 if v == nil { 98 return 99 } 100 t.Errorf("%s: expected nil; got %v", what, v) 101 } 102 103 func AssertNil(t *testing.T, v interface{}, what string) { 104 if v == nil { 105 return 106 } 107 t.Fatalf("%s: expected nil; got %v", what, v) 108 }