Skip to content

Commit

Permalink
Print correct code location of failing table test (#666)
Browse files Browse the repository at this point in the history
* Output correct code line for failing DescribeTable

See: #515

* Output correct code line for failing table.Entry

See: #515

* Add optional test description

* Refactor

Co-authored-by: David Ansari <[email protected]>

Co-authored-by: Danny Berger <[email protected]>
  • Loading branch information
ansd and dpb587-pivotal authored May 27, 2020
1 parent 149d5d9 commit c6d7afb
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 94 deletions.
37 changes: 15 additions & 22 deletions extensions/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
"fmt"
"reflect"

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

/*
Expand Down Expand Up @@ -42,57 +44,48 @@ It's important to understand that the `Describe`s and `It`s are generated at eva
Individual Entries can be focused (with FEntry) or marked pending (with PEntry or XEntry). In addition, the entire table can be focused or marked pending with FDescribeTable and PDescribeTable/XDescribeTable.
*/
func DescribeTable(description string, itBody interface{}, entries ...TableEntry) bool {
describeTable(description, itBody, entries, false, false)
describeTable(description, itBody, entries, types.FlagTypeNone)
return true
}

/*
You can focus a table with `FDescribeTable`. This is equivalent to `FDescribe`.
*/
func FDescribeTable(description string, itBody interface{}, entries ...TableEntry) bool {
describeTable(description, itBody, entries, false, true)
describeTable(description, itBody, entries, types.FlagTypeFocused)
return true
}

/*
You can mark a table as pending with `PDescribeTable`. This is equivalent to `PDescribe`.
*/
func PDescribeTable(description string, itBody interface{}, entries ...TableEntry) bool {
describeTable(description, itBody, entries, true, false)
describeTable(description, itBody, entries, types.FlagTypePending)
return true
}

/*
You can mark a table as pending with `XDescribeTable`. This is equivalent to `XDescribe`.
*/
func XDescribeTable(description string, itBody interface{}, entries ...TableEntry) bool {
describeTable(description, itBody, entries, true, false)
describeTable(description, itBody, entries, types.FlagTypePending)
return true
}

func describeTable(description string, itBody interface{}, entries []TableEntry, pending bool, focused bool) {
func describeTable(description string, itBody interface{}, entries []TableEntry, flag types.FlagType) {
itBodyValue := reflect.ValueOf(itBody)
if itBodyValue.Kind() != reflect.Func {
panic(fmt.Sprintf("DescribeTable expects a function, got %#v", itBody))
}

if pending {
ginkgo.PDescribe(description, func() {
global.Suite.PushContainerNode(
description,
func() {
for _, entry := range entries {
entry.generateIt(itBodyValue)
}
})
} else if focused {
ginkgo.FDescribe(description, func() {
for _, entry := range entries {
entry.generateIt(itBodyValue)
}
})
} else {
ginkgo.Describe(description, func() {
for _, entry := range entries {
entry.generateIt(itBodyValue)
}
})
}
},
flag,
codelocation.New(2),
)
}
33 changes: 21 additions & 12 deletions extensions/table/table_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,31 @@ package table
import (
"reflect"

"github.com/onsi/ginkgo"
"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
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 {
ginkgo.PIt(t.Description)
global.Suite.PushItNode(t.Description, func() {}, types.FlagTypePending, t.codeLocation, 0)
return
}

Expand All @@ -38,9 +47,9 @@ func (t TableEntry) generateIt(itBody reflect.Value) {
}

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

Expand All @@ -53,26 +62,26 @@ Subsequent parameters are saved off and sent to the callback passed in to `Descr
Each Entry ends up generating an individual Ginkgo It.
*/
func Entry(description string, parameters ...interface{}) TableEntry {
return TableEntry{description, parameters, false, false}
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}
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}
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}
return TableEntry{description, parameters, true, false, codelocation.New(1)}
}
Loading

0 comments on commit c6d7afb

Please sign in to comment.