1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
// Copyright 2017 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.
// Package assert provides developer a way to assert expression and output useful contextual information automatically when a case fails.
// With this package, we can focus on writing test code without worrying about how to print lots of verbose debug information for debug.
//
// See project page for more samples.
// https://github.com/huandu/go-assert
package assert
import (
"testing"
"github.com/huandu/go-assert/internal/assertion"
)
// Assert tests expr and call `t.Fatalf` to terminate test case if expr is false-equivalent value.
// `false`, 0, nil and empty string are false-equivalent values.
//
// Sample code.
//
// import "github.com/huandu/go-assert"
//
// func TestSomething(t *testing.T) {
// a, b := 1, 2
// assert.Assert(t, a > b)
// }
//
// Output:
//
// Assertion failed:
// a > b
// Referenced variables are assigned in following statements:
// a, b := 1, 2
func Assert(t *testing.T, expr interface{}) {
assertion.Assert(t, expr, &assertion.Trigger{
FuncName: "Assert",
Skip: 1,
Args: []int{1},
})
}
// Equal uses `reflect.DeepEqual` to test v1 and v2 equality.
//
// Sample code.
//
// import "github.com/huandu/go-assert"
//
// func TestSomething(t *testing.T) {
// assert.Equal(t, []int{1,2}, []int{1})
// }
//
// Output:
//
// Assertion failed:
// assert.Equal(t, []int{1, 2}, []int{1})
// The value of following expression should equal.
// [1] []int{1, 2}
// [2] []int{1}
// Values:
// [1] -> ([]int)[1 2]
// [2] -> ([]int)[1]
func Equal(t *testing.T, v1, v2 interface{}) {
assertion.AssertEqual(t, v1, v2, &assertion.Trigger{
FuncName: "Equal",
Skip: 1,
Args: []int{1, 2},
})
}
// NotEqual uses `reflect.DeepEqual` to test v1 and v2 equality.
//
// Sample code.
//
// import "github.com/huandu/go-assert"
//
// func TestSomething(t *testing.T) {
// assert.NotEqual(t, []int{1}, []int{1})
// }
//
// Output:
//
// Assertion failed:
// assert.NotEqual(t, []int{1}, []int{1})
// The value of following expression should not equal.
// [1] []int{1}
// [2] []int{1}
func NotEqual(t *testing.T, v1, v2 interface{}) {
assertion.AssertNotEqual(t, v1, v2, &assertion.Trigger{
FuncName: "NotEqual",
Skip: 1,
Args: []int{1, 2},
})
}
// AssertEqual uses `reflect.DeepEqual` to test v1 and v2 equality.
//
// Note: as golint dislike the name of this function,
// it will be removed in the future. Use Equal instead.
//
// Sample code.
//
// import "github.com/huandu/go-assert"
//
// func TestSomething(t *testing.T) {
// assert.AssertEqual(t, []int{1,2}, []int{1})
// }
//
// Output:
//
// Assertion failed:
// assert.AssertEqual(t, []int{1, 2}, []int{1})
// The value of following expression should equal.
// [1] []int{1, 2}
// [2] []int{1}
// Values:
// [1] -> ([]int)[1 2]
// [2] -> ([]int)[1]
func AssertEqual(t *testing.T, v1, v2 interface{}) {
assertion.AssertEqual(t, v1, v2, &assertion.Trigger{
FuncName: "AssertEqual",
Skip: 1,
Args: []int{1, 2},
})
}
// AssertNotEqual uses `reflect.DeepEqual` to test v1 and v2 equality.
//
// Note: as golint dislike the name of this function,
// it will be removed in the future. Use NotEqual instead.
//
// Sample code.
//
// import "github.com/huandu/go-assert"
//
// func TestSomething(t *testing.T) {
// assert.AssertNotEqual(t, []int{1}, []int{1})
// }
//
// Output:
//
// Assertion failed:
// assert.AssertNotEqual(t, []int{1}, []int{1})
// The value of following expression should not equal.
// [1] []int{1}
// [2] []int{1}
func AssertNotEqual(t *testing.T, v1, v2 interface{}) {
assertion.AssertNotEqual(t, v1, v2, &assertion.Trigger{
FuncName: "AssertNotEqual",
Skip: 1,
Args: []int{1, 2},
})
}
|