aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/onsi/ginkgo/extensions/table/table_entry.go
blob: 1a919a1fe350a2b3ee20a1ced2a072809625b134 (plain)
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
package table

import (
	"reflect"

	"github.com/onsi/ginkgo/internal/codelocation"
	"github.com/onsi/ginkgo/internal/global"
	"github.com/onsi/ginkgo/types"
)

/*
TableEntry represents an entry in a table test.  You generally use the `Entry` constructor.
*/
type TableEntry struct {
	Description  string
	Parameters   []interface{}
	Pending      bool
	Focused      bool
	codeLocation types.CodeLocation
}

func (t TableEntry) generateIt(itBody reflect.Value) {
	if t.codeLocation == (types.CodeLocation{}) {
		// The user created the TableEntry struct directly instead of having used the (F/P/X)Entry constructors.
		// Therefore default to the code location of the surrounding DescribeTable.
		t.codeLocation = codelocation.New(5)
	}

	if t.Pending {
		global.Suite.PushItNode(t.Description, func() {}, types.FlagTypePending, t.codeLocation, 0)
		return
	}

	values := make([]reflect.Value, len(t.Parameters))
	iBodyType := itBody.Type()
	for i, param := range t.Parameters {
		if param == nil {
			inType := iBodyType.In(i)
			values[i] = reflect.Zero(inType)
		} else {
			values[i] = reflect.ValueOf(param)
		}
	}

	body := func() {
		itBody.Call(values)
	}

	if t.Focused {
		global.Suite.PushItNode(t.Description, body, types.FlagTypeFocused, t.codeLocation, global.DefaultTimeout)
	} else {
		global.Suite.PushItNode(t.Description, body, types.FlagTypeNone, t.codeLocation, global.DefaultTimeout)
	}
}

/*
Entry constructs a TableEntry.

The first argument is a required description (this becomes the content of the generated Ginkgo `It`).
Subsequent parameters are saved off and sent to the callback passed in to `DescribeTable`.

Each Entry ends up generating an individual Ginkgo It.
*/
func Entry(description string, parameters ...interface{}) TableEntry {
	return TableEntry{description, parameters, false, false, codelocation.New(1)}
}

/*
You can focus a particular entry with FEntry.  This is equivalent to FIt.
*/
func FEntry(description string, parameters ...interface{}) TableEntry {
	return TableEntry{description, parameters, false, true, codelocation.New(1)}
}

/*
You can mark a particular entry as pending with PEntry.  This is equivalent to PIt.
*/
func PEntry(description string, parameters ...interface{}) TableEntry {
	return TableEntry{description, parameters, true, false, codelocation.New(1)}
}

/*
You can mark a particular entry as pending with XEntry.  This is equivalent to XIt.
*/
func XEntry(description string, parameters ...interface{}) TableEntry {
	return TableEntry{description, parameters, true, false, codelocation.New(1)}
}