|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, AcmeStack |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package assert |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + "reflect" |
| 23 | + "runtime" |
| 24 | + "testing" |
| 25 | +) |
| 26 | + |
| 27 | +/* |
| 28 | + * Judge whether the value is nil |
| 29 | + * args Error prompt content |
| 30 | + */ |
| 31 | +func IsNull(t *testing.T, got interface{}, args ...interface{}) { |
| 32 | + fn := func() { |
| 33 | + t.Errorf("! isNull") |
| 34 | + if len(args) > 0 { |
| 35 | + t.Error("!", " -", fmt.Sprint(args...)) |
| 36 | + } |
| 37 | + } |
| 38 | + result := !reflect.DeepEqual(got, nil) |
| 39 | + assert(t, result, fn, 2) |
| 40 | +} |
| 41 | + |
| 42 | +/* |
| 43 | + * Judge whether the value is true |
| 44 | + * args Error prompt content |
| 45 | + */ |
| 46 | +func IsTrue(t *testing.T, result bool, args ...interface{}) { |
| 47 | + tt(t, result, 1, args...) |
| 48 | +} |
| 49 | + |
| 50 | +/* |
| 51 | + * Judge whether the value is false |
| 52 | + * args Error prompt content |
| 53 | + */ |
| 54 | +func IsFalse(t *testing.T, result bool, args ...interface{}) { |
| 55 | + tt(t, !result, 1, args...) |
| 56 | +} |
| 57 | + |
| 58 | +/* |
| 59 | + * Judge whether the values are not equal |
| 60 | + * exp Comparison value |
| 61 | + * got Compared value |
| 62 | + * args Error prompt content |
| 63 | + */ |
| 64 | +func Equal(t *testing.T, exp, got interface{}, args ...interface{}) { |
| 65 | + equal(t, exp, got, 1, args...) |
| 66 | +} |
| 67 | + |
| 68 | +/* |
| 69 | + * Judge whether the values are equal |
| 70 | + * exp Comparison value |
| 71 | + * got Compared value |
| 72 | + * args Error prompt content |
| 73 | + */ |
| 74 | +func NotEqual(t *testing.T, exp, got interface{}, args ...interface{}) { |
| 75 | + fn := func() { |
| 76 | + t.Errorf("! Unexpected: <%#v>", exp) |
| 77 | + if len(args) > 0 { |
| 78 | + t.Error("!", " -", fmt.Sprint(args...)) |
| 79 | + } |
| 80 | + } |
| 81 | + result := reflect.DeepEqual(exp, got) |
| 82 | + assert(t, result, fn, 1) |
| 83 | +} |
| 84 | + |
| 85 | +func assert(t *testing.T, result bool, f func(), cd int) { |
| 86 | + if !result { |
| 87 | + _, file, line, _ := runtime.Caller(cd + 1) |
| 88 | + t.Errorf("%s:%d", file, line) |
| 89 | + f() |
| 90 | + t.FailNow() |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func equal(t *testing.T, exp, got interface{}, cd int, args ...interface{}) { |
| 95 | + fn := func() { |
| 96 | + t.Errorf("! Error unlike") |
| 97 | + if len(args) > 0 { |
| 98 | + t.Error("!", " -", fmt.Sprint(args...)) |
| 99 | + } |
| 100 | + } |
| 101 | + result := !reflect.DeepEqual(exp, got) |
| 102 | + assert(t, result, fn, cd+1) |
| 103 | +} |
| 104 | + |
| 105 | +func tt(t *testing.T, result bool, cd int, args ...interface{}) { |
| 106 | + fn := func() { |
| 107 | + t.Errorf("! Failure") |
| 108 | + if len(args) > 0 { |
| 109 | + t.Error("!", " -", fmt.Sprint(args...)) |
| 110 | + } |
| 111 | + } |
| 112 | + assert(t, result, fn, cd+1) |
| 113 | +} |
0 commit comments