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 88 89 90 91 92 93 94 95 96 97 98 99 100
|
package types_test
import (
"context"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/ginkgo/v2/types"
)
var _ = Describe("AroundNodes", func() {
cl := types.NewCodeLocation(0)
Describe("building an AroundNode", func() {
It("captures the code location", func() {
an := types.AroundNode(func(ctx context.Context, body func(context.Context)) {
body(context.WithValue(ctx, "key", "value"))
}, cl)
Ω(an).ShouldNot(BeZero())
Ω(an.CodeLocation).Should(Equal(cl))
c := context.Background()
var calledContext context.Context
an.Body(c, func(ctx context.Context) {
calledContext = ctx
})
Ω(calledContext.Value("key")).Should(Equal("value"))
})
It("supports bare functions", func() {
called := false
an := types.AroundNode(func() {
called = true
}, cl)
c := context.Background()
Ω(an).ShouldNot(BeZero())
var calledContext context.Context
an.Body(c, func(ctx context.Context) {
calledContext = ctx
})
Ω(called).Should(BeTrue())
Ω(calledContext).Should(Equal(c))
})
It("supports context transformers", func() {
an := types.AroundNode(func(c context.Context) context.Context {
return context.WithValue(c, "key", "value")
}, cl)
c := context.Background()
Ω(an).ShouldNot(BeZero())
var calledContext context.Context
an.Body(c, func(ctx context.Context) {
calledContext = ctx
})
Ω(calledContext.Value("key")).Should(Equal("value"))
})
})
Describe("Clone", func() {
It("clones the slice", func() {
an1 := types.AroundNode(func() {}, types.NewCodeLocation(0))
an2 := types.AroundNode(func() {}, types.NewCodeLocation(0))
an3 := types.AroundNode(func() {}, types.NewCodeLocation(0))
original := types.AroundNodes{an1, an2, an3}
clone := original.Clone()
an4 := types.AroundNode(func() {}, types.NewCodeLocation(0))
clone[2] = an4
Ω(original).Should(HaveExactElements(
HaveField("CodeLocation", an1.CodeLocation),
HaveField("CodeLocation", an2.CodeLocation),
HaveField("CodeLocation", an3.CodeLocation),
))
Ω(clone).Should(HaveExactElements(
HaveField("CodeLocation", an1.CodeLocation),
HaveField("CodeLocation", an2.CodeLocation),
HaveField("CodeLocation", an4.CodeLocation),
))
})
})
Describe("Append", func() {
It("appends the node to the slice", func() {
an1 := types.AroundNode(func() {}, types.NewCodeLocation(0))
an2 := types.AroundNode(func() {}, types.NewCodeLocation(0))
an3 := types.AroundNode(func() {}, types.NewCodeLocation(0))
nodes := types.AroundNodes{an1, an2}
newNodes := nodes.Append(an3)
Ω(nodes).Should(HaveExactElements(
HaveField("CodeLocation", an1.CodeLocation),
HaveField("CodeLocation", an2.CodeLocation),
))
Ω(newNodes).Should(HaveExactElements(
HaveField("CodeLocation", an1.CodeLocation),
HaveField("CodeLocation", an2.CodeLocation),
HaveField("CodeLocation", an3.CodeLocation),
))
})
})
})
|