-
Notifications
You must be signed in to change notification settings - Fork 2
/
trace_test.go
91 lines (80 loc) · 1.88 KB
/
trace_test.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package trace
import (
"testing"
"io/ioutil"
"fmt"
"runtime"
"os"
"strings"
"strconv"
)
var tempFile *os.File
// CAUTION!!!! - Line sensitive test - Line = 20
func TestWrite(t *testing.T) {
init_trace()
defer close_trace()
someVariable := "write-test"
Write(someVariable)
verify(t, someVariable, 20, "trace_test.go")
}
// CAUTION!!!! - Line sensitive test - Line = 29
func TestWritef(t *testing.T) {
init_trace()
defer close_trace()
someVariable := "write-test"
Writef("format-test %s", someVariable)
finalmsg := fmt.Sprintf("format-test %s", someVariable)
verify(t, finalmsg, 29, "trace_test.go")
}
// CAUTION!!!! - Line sensitive test - Line = 38
func TestAssert(t *testing.T) {
init_trace()
defer close_trace()
Assert(2 == 3)
verify(t, "Assertion Failed!", 38, "trace_test.go")
}
// CAUTION!!!! - Line sensitive test - Line = 46
func TestAssertf(t *testing.T) {
init_trace()
defer close_trace()
Assertf(2 == 3, "False %s", "Assertion Failed!")
verify(t, "False Assertion Failed!", 46, "trace_test.go")
}
func verify(t *testing.T, message string, line int, file string) {
msg, err := ioutil.ReadAll(tempFile)
if err != nil {
assert(t, false, 1)
}
if !strings.Contains(string(msg), message) {
assert(t, false, 1)
}
fileLine := file + ":" + strconv.Itoa(line)
if !strings.Contains(string(msg), fileLine) {
assert(t, false, 1)
}
}
func init_trace() {
ENABLE_TRACE = true
var err error
tempFile, err = ioutil.TempFile("", "trace_test")
if err != nil {
fmt.Println("Couldnot create temp file.")
runtime.Goexit()
}
TRACE_FILE_PATH = tempFile.Name()
SetMessageFormat(PRINT_ALL)
SetOut(OUT_ALL)
}
func close_trace() {
tempFile.Close()
os.Remove(TRACE_FILE_PATH)
SetOut(OUT_NONE)
ENABLE_TRACE = false
}
func assert(t *testing.T, result bool, cd int) {
if !result {
_, file, line, _ := runtime.Caller(cd + 1)
t.Errorf("%s:%d", file, line)
t.FailNow()
}
}