-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogging_test.go
82 lines (76 loc) · 2.13 KB
/
logging_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
// -----------------------------------------------------------------------------
// ZR Library zr/[logging_test.go]
// (c) balarabe@protonmail.com License: MIT
// -----------------------------------------------------------------------------
package zr
// to test all items in logging.go use:
// go test --run Test_logg_
//
// to generate a test coverage report for the whole module use:
// go test -coverprofile cover.out
// go tool cover -html=cover.out
import (
"fmt"
"strings"
"testing"
)
// go test --run Test_logg_CallerList_
// only use standard testing functions in this unit test (not zr.Test...)
func Test_logg_CallerList_(t *testing.T) {
// test without deep nesting
{
ar := CallerList()
if len(ar) != 1 {
t.Error("Expected 1 item in returned slice, but got", len(ar))
}
const ExpectName = "zr.Test_logg_CallerList_:"
if len(ar) == 1 && !strings.HasPrefix(ar[0], ExpectName) {
t.Errorf(
"Expected %q in returned slice, but got %q",
ExpectName,
ar[0],
)
}
}
// test call stack with nested functions
{
var ar []string
f1 := func() {
f2 := func() {
f3 := func() {
ar = CallerList()
}
f3()
}
f2()
}
f1()
// the returned list should contain this function and 3 nested functions
if len(ar) != 4 {
t.Error("Expected 4 items in returned slice, but got", len(ar))
}
for i, prefix := range []string{
"Test_logg_CallerList_.func1.1.1",
"Test_logg_CallerList_.func1.1",
"Test_logg_CallerList_.func1",
"zr.Test_logg_CallerList_",
} {
if !strings.HasPrefix(ar[i], prefix) {
t.Errorf(
"In returned slice at index %d: expected %q but got %q",
i, prefix, ar[i],
)
}
}
}
} // Test_logg_CallerList_
// go test --run Test_logg_NoE_
func Test_logg_NoE_(t *testing.T) {
const check = "xyz"
fn := func() (string, error) {
return "xyz", fmt.Errorf("error 123")
}
s := NoE(fn()).(string)
TTrue(t, s == check)
} // Test_logg_NoE_
// end