Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add async analogue of ginkgo.It to table.Entry #497

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 28 additions & 6 deletions extensions/table/table_entry.go
Expand Up @@ -22,12 +22,14 @@ func (t TableEntry) generateIt(itBody reflect.Value) {
return
}

values := []reflect.Value{}
values := make([]reflect.Value, 0)
itBodyType := itBody.Type()

for i, param := range t.Parameters {
var value reflect.Value

if param == nil {
inType := itBody.Type().In(i)
inType := itBodyType.In(i)
value = reflect.Zero(inType)
} else {
value = reflect.ValueOf(param)
Expand All @@ -36,14 +38,34 @@ func (t TableEntry) generateIt(itBody reflect.Value) {
values = append(values, value)
}

body := func() {
itBody.Call(values)
var (
body interface{}
timeout []float64
)

if itBodyType.NumIn() >= 1 && itBodyType.In(0).Kind() == reflect.Chan &&
itBodyType.In(0).Elem().Kind() == reflect.Interface {

lenValues := len(values)
if lenValues > 0 && values[lenValues-1].Kind() == reflect.Float64 {
timeout = append(timeout, values[lenValues-1].Interface().(float64))
values = values[:lenValues-1]
}

body = func(done chan<- interface{}) {
values = append([]reflect.Value{reflect.ValueOf(done)}, values...)
itBody.Call(values)
}
} else {
body = func() {
itBody.Call(values)
}
}

if t.Focused {
ginkgo.FIt(t.Description, body)
ginkgo.FIt(t.Description, body, timeout...)
} else {
ginkgo.It(t.Description, body)
ginkgo.It(t.Description, body, timeout...)
}
}

Expand Down
20 changes: 19 additions & 1 deletion extensions/table/table_test.go
Expand Up @@ -3,10 +3,11 @@ package table_test
import (
"strings"

. "github.com/onsi/ginkgo/extensions/table"
. "github.com/Antonov-guap/ginkgo/extensions/table"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"time"
)

var _ = Describe("Table", func() {
Expand All @@ -19,6 +20,23 @@ var _ = Describe("Table", func() {
Entry("x < y", 0, 1, false),
)

DescribeTable("a simple table with async flow",
func(done Done, msg string, after time.Duration) { //use done Done in params to make async flow
c := make(chan string, 0)

go func(c chan string) {
time.Sleep(after)
c <- "Hello, " + msg
}(c)

Expect(<-c).To(ContainSubstring(msg))

close(done)
},
Entry("Sam", "Sam", 100*time.Millisecond), //default timeout 1 sec + 100ms latency
Entry("John", "John", 1050*time.Millisecond, 1.2), //set timeout 1.2 sec (last param) + 1050ms latency
)

type ComplicatedThings struct {
Superstructure string
Substructure string
Expand Down