-
Notifications
You must be signed in to change notification settings - Fork 0
/
tm.go
76 lines (66 loc) · 1.54 KB
/
tm.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
70
71
72
73
74
75
76
package dyn4go
import (
"math"
"path/filepath"
"runtime"
"strconv"
"testing"
)
func preambleCorrection(message string) string {
_, f, l, r := runtime.Caller(2)
if !r {
return message
}
return "\r\t" + filepath.Base(f) + ":" + strconv.Itoa(l) + ": " + message
}
func AssertEqual(t *testing.T, a, b interface{}) {
if a != b {
t.Error(preambleCorrection("Values not equal in assertion"))
}
}
func AssertEqualWithinError(t *testing.T, a, b, c float64) {
if math.Abs(a)-math.Abs(b) > math.Abs(c) {
t.Error(preambleCorrection("Values not equal within error in assertion"))
}
}
func AssertTrue(t *testing.T, a bool) {
if !a {
t.Error(preambleCorrection("Condition not true in assertion"))
}
}
func AssertNotEqual(t *testing.T, a, b interface{}) {
if a == b {
t.Error(preambleCorrection("Values equal in assertion"))
}
}
func AssertFalse(t *testing.T, a bool) {
if a {
t.Error(preambleCorrection("Condition not false in assertion"))
}
}
/**
* Usage:
*
* func TestSomething(t * testing.T) {
* defer AssertPanic(t)
* functionThatPanics(maybeSomeArguments)
* }
*
*/
func deferredPreambleCorrection(message string) string {
_, f, l, r := runtime.Caller(2)
if !r {
return message
}
return "\r\t" + filepath.Base(f) + ":" + strconv.Itoa(l) + ": " + message
}
func AssertPanic(t *testing.T) {
if r := recover(); r == nil {
t.Error(deferredPreambleCorrection("Function failed to panic"))
}
}
func AssertNoPanic(t *testing.T) {
if r := recover(); r != nil {
t.Error(deferredPreambleCorrection("Function paniced"))
}
}