forked from alangpierce/go-forceexport
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathforceexport_test.go
More file actions
132 lines (115 loc) · 3.23 KB
/
forceexport_test.go
File metadata and controls
132 lines (115 loc) · 3.23 KB
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
package forceexport
import (
"context"
"fmt"
"reflect"
"testing"
"time"
)
func init() {
fmt.Println(time.Now())
c, cancel := context.WithCancel(context.Background())
fmt.Println(c, cancel)
}
// This test fails when not debugging, even though checklinkname=0.
// This may be due to the optimization done by the Go compiler (withContext inlined?).
func TestContext(t *testing.T) {
c, cancel := context.WithCancel(context.Background())
cancel()
c.Done()
var v func(int) int
err := GetFunc(&v, `context.withCancel`)
if err != nil {
t.Error("Expected nil error.")
}
if v == nil {
t.Error("Expected non-nil function.")
}
}
func TestTimeNow(t *testing.T) {
// Try the new time.runtimeNow first (3 return values) - based on call stack
var runtimeNowFunc func() (int64, int32, int64)
err := GetFunc(&runtimeNowFunc, "time.runtimeNow")
if err == nil && runtimeNowFunc != nil {
sec, nsec, mono := runtimeNowFunc()
if sec == 0 || nsec == 0 {
t.Error("Expected nonzero result from time.runtimeNow().")
}
t.Logf("time.runtimeNow() returned sec=%d, nsec=%d, mono=%d", sec, nsec, mono)
return
}
// Fallback to assembly time.now (2 return values)
var timeNowFunc func() (int64, int32)
err = GetFunc(&timeNowFunc, "time.now")
if err == nil && timeNowFunc != nil {
sec, nsec := timeNowFunc()
if sec == 0 || nsec == 0 {
t.Error("Expected nonzero result from time.now().")
}
t.Logf("time.now() returned sec=%d, nsec=%d", sec, nsec)
return
}
// If both fail, report the failure
t.Fatalf("Failed to get both time.runtimeNow and time.now: %v", err)
}
// Note that we need to disable inlining here, or else the function won't be
// compiled into the binary. We also need to call it from the test so that the
// compiler doesn't remove it because it's unused.
//
//go:noinline
func addOne(x int) int {
return x + 1
}
func TestAddOne(t *testing.T) {
if addOne(3) != 4 {
t.Error("addOne should work properly.")
}
var addOneFunc func(x int) int
err := GetFunc(&addOneFunc, "github.com/szmcdull/go-forceexport.addOne")
if err != nil {
t.Error("Expected nil error.")
}
if addOneFunc(3) != 4 {
t.Error("Expected addOneFunc to add one to 3.")
}
}
func GetPointer(v interface{}) uintptr {
val := reflect.ValueOf(v)
return val.Pointer()
}
func TestFunc1(t *testing.T) {
}
func TestFunc2(t *testing.T) {
}
func TestFunc3(t *testing.T) {
}
func TestFunc4(t *testing.T) {
}
func TestInvalidFunc(t *testing.T) {
var invalidFunc func()
err := GetFunc(&invalidFunc, "invalidpackage.invalidfunction")
if err == nil {
t.Error("Expected an error.")
}
if invalidFunc != nil {
t.Error("Expected a nil function.")
}
}
func TestForceExport(t *testing.T) {
var func1, func2, func3, func4 func(*testing.T)
_ = GetFunc(&func1, `github.com/szmcdull/go-forceexport.TestFunc1`)
_ = GetFunc(&func2, `github.com/szmcdull/go-forceexport.TestFunc2`)
_ = GetFunc(&func3, `github.com/szmcdull/go-forceexport.TestFunc3`)
_ = GetFunc(&func4, `github.com/szmcdull/go-forceexport.TestFunc4`)
if func1 == nil || func2 == nil || func3 == nil || func4 == nil {
t.Error(`func == nil`)
} else {
// r1 := func1()
// r2 := func2()
// r3 := func3()
// r4 := func4()
// if r1 != 1 || r2 != 2 || r3 != 3 || r4 != 4 {
// t.Error(`result wrong`)
// }
}
}