-
Notifications
You must be signed in to change notification settings - Fork 0
/
golassert.go
69 lines (62 loc) · 1.89 KB
/
golassert.go
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
package golassert
import (
"fmt"
"reflect"
"testing"
)
/*
Type is proxy func for assertType.
*/
func Type(t *testing.T, expected interface{}, result interface{}) {
assertType(t, true, expected, result)
}
/*
NotType is proxy func for assertType to check no match.
*/
func NotType(t *testing.T, expected interface{}, result interface{}) {
assertType(t, false, expected, result)
}
/*
Equal is proxy func for assertEqual.
*/
func Equal(t *testing.T, expected interface{}, result interface{}) {
assertEqual(t, true, expected, result)
}
/*
NotEqual is proxy func for assertEqual to check no match.
*/
func NotEqual(t *testing.T, expected interface{}, result interface{}) {
assertEqual(t, false, expected, result)
}
/*
assertType asserts type of expected and result.
*/
func assertType(t *testing.T, positive bool, expected interface{}, result interface{}) {
expectedType := reflect.TypeOf(expected)
resultType := reflect.TypeOf(result)
if (expectedType != resultType) == positive {
err := "Error: Type Assertion"
t.Errorf("%s\nExpected Value Type: %v\nResult: %v", err, expectedType, resultType)
}
}
/*
assertEqual asserts if expected result is same as returned result.
*/
func assertEqual(t *testing.T, positive bool, expected interface{}, result interface{}) {
assertType(t, true, expected, result)
if expected == nil && result == nil {
return
}
errMsg := fmt.Sprintf("Error: Mismatched Values\n\tExpected value: %v\n\tResult: %v", expected, result)
switch result.(type) {
case string, uint, uint8, uint16, uint32, uint64, int, int8, int16, int32, int64, uintptr, float32, float64, complex64, complex128, error, bool:
if (expected != result) == positive {
t.Errorf(errMsg)
}
default:
if (fmt.Sprintf("%v", expected) != fmt.Sprintf("%v", result)) == positive {
t.Errorf(errMsg)
fmt.Println("[WARN] For non-basic types, might give False -ves for edge cases like diff ordered arrays.")
}
}
}