forked from harness/gitness
/
registration.go
74 lines (60 loc) · 1.52 KB
/
registration.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
package convey
import (
"reflect"
"runtime"
"github.com/smartystreets/goconvey/convey/gotest"
)
type registration struct {
Situation string
action *action
Test t
File string
Line int
Focus bool
}
func (self *registration) ShouldBeTopLevel() bool {
return self.Test != nil
}
func newRegistration(situation string, action *action, test t) *registration {
file, line, _ := gotest.ResolveExternalCaller()
return ®istration{
Situation: situation,
action: action,
Test: test,
File: file,
Line: line,
}
}
////////////////////////// action ///////////////////////
type action struct {
wrapped func()
name string
failureMode FailureMode
}
func (self *action) Invoke() {
self.wrapped()
}
func newAction(wrapped func(), mode FailureMode) *action {
return &action{
name: functionName(wrapped),
wrapped: wrapped,
failureMode: mode,
}
}
func newSkippedAction(wrapped func(), mode FailureMode) *action {
// The choice to use the filename and line number as the action name
// reflects the need for something unique but also that corresponds
// in a determinist way to the action itself.
return &action{
name: gotest.FormatExternalFileAndLine(),
wrapped: wrapped,
failureMode: mode,
}
}
///////////////////////// helpers //////////////////////////////
func functionName(action func()) string {
return runtime.FuncForPC(functionId(action)).Name()
}
func functionId(action func()) uintptr {
return reflect.ValueOf(action).Pointer()
}