mirror-to-gitea/vendor/github.com/onsi/ginkgo/extensions/table/table_entry.go
vendor/github.com/onsi/ginkgo/extensions/table/table_entry.goRaw
1package table
2
3import (
4 "fmt"
5 "reflect"
6
7 "github.com/onsi/ginkgo/internal/codelocation"
8 "github.com/onsi/ginkgo/internal/global"
9 "github.com/onsi/ginkgo/types"
10)
11
12/*
13TableEntry represents an entry in a table test. You generally use the `Entry` constructor.
14*/
15type TableEntry struct {
16 Description interface{}
17 Parameters []interface{}
18 Pending bool
19 Focused bool
20 codeLocation types.CodeLocation
21}
22
23func (t TableEntry) generateIt(itBody reflect.Value) {
24 var description string
25 descriptionValue := reflect.ValueOf(t.Description)
26 switch descriptionValue.Kind() {
27 case reflect.String:
28 description = descriptionValue.String()
29 case reflect.Func:
30 values := castParameters(descriptionValue, t.Parameters)
31 res := descriptionValue.Call(values)
32 if len(res) != 1 {
33 panic(fmt.Sprintf("The describe function should return only a value, returned %d", len(res)))
34 }
35 if res[0].Kind() != reflect.String {
36 panic(fmt.Sprintf("The describe function should return a string, returned %#v", res[0]))
37 }
38 description = res[0].String()
39 default:
40 panic(fmt.Sprintf("Description can either be a string or a function, got %#v", descriptionValue))
41 }
42
43 if t.Pending {
44 global.Suite.PushItNode(description, func() {}, types.FlagTypePending, t.codeLocation, 0)
45 return
46 }
47
48 values := castParameters(itBody, t.Parameters)
49 body := func() {
50 itBody.Call(values)
51 }
52
53 if t.Focused {
54 global.Suite.PushItNode(description, body, types.FlagTypeFocused, t.codeLocation, global.DefaultTimeout)
55 } else {
56 global.Suite.PushItNode(description, body, types.FlagTypeNone, t.codeLocation, global.DefaultTimeout)
57 }
58}
59
60func castParameters(function reflect.Value, parameters []interface{}) []reflect.Value {
61 res := make([]reflect.Value, len(parameters))
62 funcType := function.Type()
63 for i, param := range parameters {
64 if param == nil {
65 inType := funcType.In(i)
66 res[i] = reflect.Zero(inType)
67 } else {
68 res[i] = reflect.ValueOf(param)
69 }
70 }
71 return res
72}
73
74/*
75Entry constructs a TableEntry.
76
77The first argument is a required description (this becomes the content of the generated Ginkgo `It`).
78Subsequent parameters are saved off and sent to the callback passed in to `DescribeTable`.
79
80Each Entry ends up generating an individual Ginkgo It.
81*/
82func Entry(description interface{}, parameters ...interface{}) TableEntry {
83 return TableEntry{
84 Description: description,
85 Parameters: parameters,
86 Pending: false,
87 Focused: false,
88 codeLocation: codelocation.New(1),
89 }
90}
91
92/*
93You can focus a particular entry with FEntry. This is equivalent to FIt.
94*/
95func FEntry(description interface{}, parameters ...interface{}) TableEntry {
96 return TableEntry{
97 Description: description,
98 Parameters: parameters,
99 Pending: false,
100 Focused: true,
101 codeLocation: codelocation.New(1),
102 }
103}
104
105/*
106You can mark a particular entry as pending with PEntry. This is equivalent to PIt.
107*/
108func PEntry(description interface{}, parameters ...interface{}) TableEntry {
109 return TableEntry{
110 Description: description,
111 Parameters: parameters,
112 Pending: true,
113 Focused: false,
114 codeLocation: codelocation.New(1),
115 }
116}
117
118/*
119You can mark a particular entry as pending with XEntry. This is equivalent to XIt.
120*/
121func XEntry(description interface{}, parameters ...interface{}) TableEntry {
122 return TableEntry{
123 Description: description,
124 Parameters: parameters,
125 Pending: true,
126 Focused: false,
127 codeLocation: codelocation.New(1),
128 }
129}