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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
package internal_test
import (
"encoding/json"
"fmt"
"time"
. "github.com/onsi/ginkgo/v2"
"github.com/onsi/ginkgo/v2/internal"
"github.com/onsi/ginkgo/v2/types"
. "github.com/onsi/gomega"
)
type SomeStruct struct {
Label string
Count int
}
type StringerStruct struct {
Label string
Count int
}
func (s StringerStruct) String() string {
return fmt.Sprintf("%s %d", s.Label, s.Count)
}
type ColorableStringerStruct struct {
Label string
Count int
}
func (s ColorableStringerStruct) String() string {
return fmt.Sprintf("%s %d", s.Label, s.Count)
}
func (s ColorableStringerStruct) ColorableString() string {
return fmt.Sprintf("{{red}}%s {{green}}%d{{/}}", s.Label, s.Count)
}
func reportEntryJSONRoundTrip(reportEntry internal.ReportEntry) internal.ReportEntry {
data, err := json.Marshal(reportEntry)
ExpectWithOffset(1, err).ShouldNot(HaveOccurred())
var out internal.ReportEntry
ExpectWithOffset(1, json.Unmarshal(data, &out)).Should(Succeed())
return out
}
var _ = Describe("ReportEntry and ReportEntries", func() {
var reportEntry internal.ReportEntry
var err error
Describe("ReportEntry with no passed-in value", func() {
BeforeEach(func() {
reportEntry, err = internal.NewReportEntry("name", cl)
Ω(err).ShouldNot(HaveOccurred())
})
It("returns a correctly configured ReportEntry", func() {
Ω(reportEntry.Visibility).Should(Equal(types.ReportEntryVisibilityAlways))
Ω(reportEntry.Name).Should(Equal("name"))
Ω(reportEntry.Time).Should(BeTemporally("~", time.Now(), time.Second))
Ω(reportEntry.Location).Should(Equal(cl))
Ω(reportEntry.GetRawValue()).Should(BeNil())
})
It("has an empty StringRepresentation", func() {
Ω(reportEntry.StringRepresentation()).Should(BeZero())
})
It("round-trips through JSON correctly", func() {
rtEntry := reportEntryJSONRoundTrip(reportEntry)
Ω(rtEntry.Visibility).Should(Equal(types.ReportEntryVisibilityAlways))
Ω(rtEntry.Name).Should(Equal("name"))
Ω(rtEntry.Time).Should(BeTemporally("~", time.Now(), time.Second))
Ω(rtEntry.Location).Should(Equal(cl))
Ω(rtEntry.GetRawValue()).Should(BeNil())
Ω(rtEntry.StringRepresentation()).Should(BeZero())
})
})
Context("with a string passed-in value", func() {
BeforeEach(func() {
reportEntry, err = internal.NewReportEntry("name", cl, "bob")
Ω(err).ShouldNot(HaveOccurred())
})
It("returns a correctly configured ReportEntry", func() {
Ω(reportEntry.GetRawValue()).Should(Equal("bob"))
})
It("has the correct StringRepresentation", func() {
Ω(reportEntry.StringRepresentation()).Should(Equal("bob"))
})
It("round-trips through JSON correctly", func() {
rtEntry := reportEntryJSONRoundTrip(reportEntry)
Ω(rtEntry.GetRawValue()).Should(Equal("bob"))
Ω(rtEntry.StringRepresentation()).Should(Equal("bob"))
})
})
Context("with a numerical passed-in value", func() {
BeforeEach(func() {
reportEntry, err = internal.NewReportEntry("name", cl, 17)
Ω(err).ShouldNot(HaveOccurred())
})
It("returns a correctly configured ReportEntry", func() {
Ω(reportEntry.GetRawValue()).Should(Equal(17))
})
It("has the correct StringRepresentation", func() {
Ω(reportEntry.StringRepresentation()).Should(Equal("17"))
})
It("round-trips through JSON correctly", func() {
rtEntry := reportEntryJSONRoundTrip(reportEntry)
Ω(rtEntry.GetRawValue()).Should(Equal(float64(17)))
Ω(rtEntry.StringRepresentation()).Should(Equal("17"))
})
})
Context("with a struct passed-in value", func() {
BeforeEach(func() {
reportEntry, err = internal.NewReportEntry("name", cl, SomeStruct{"bob", 17})
Ω(err).ShouldNot(HaveOccurred())
})
It("returns a correctly configured ReportEntry", func() {
Ω(reportEntry.GetRawValue()).Should(Equal(SomeStruct{"bob", 17}))
})
It("has the correct StringRepresentation", func() {
Ω(reportEntry.StringRepresentation()).Should(Equal("{Label:bob Count:17}"))
})
It("round-trips through JSON correctly", func() {
rtEntry := reportEntryJSONRoundTrip(reportEntry)
Ω(rtEntry.GetRawValue()).Should(Equal(map[string]interface{}{"Label": "bob", "Count": float64(17)}))
Ω(rtEntry.StringRepresentation()).Should(Equal("{Label:bob Count:17}"))
})
It("can be rehydrated into the correct struct, manually", func() {
rtEntry := reportEntryJSONRoundTrip(reportEntry)
var s SomeStruct
Ω(json.Unmarshal([]byte(rtEntry.Value.AsJSON), &s)).Should(Succeed())
Ω(s).Should(Equal(SomeStruct{"bob", 17}))
})
})
Context("with a stringer passed-in value", func() {
BeforeEach(func() {
reportEntry, err = internal.NewReportEntry("name", cl, StringerStruct{"bob", 17})
Ω(err).ShouldNot(HaveOccurred())
})
It("returns a correctly configured ReportEntry", func() {
Ω(reportEntry.GetRawValue()).Should(Equal(StringerStruct{"bob", 17}))
})
It("has the correct StringRepresentation", func() {
Ω(reportEntry.StringRepresentation()).Should(Equal("bob 17"))
})
It("round-trips through JSON correctly", func() {
rtEntry := reportEntryJSONRoundTrip(reportEntry)
Ω(rtEntry.GetRawValue()).Should(Equal(map[string]interface{}{"Label": "bob", "Count": float64(17)}))
Ω(rtEntry.StringRepresentation()).Should(Equal("bob 17"))
})
})
Context("with a ColorableStringer passed-in value", func() {
BeforeEach(func() {
reportEntry, err = internal.NewReportEntry("name", cl, ColorableStringerStruct{"bob", 17})
Ω(err).ShouldNot(HaveOccurred())
})
It("returns a correctly configured ReportEntry", func() {
Ω(reportEntry.GetRawValue()).Should(Equal(ColorableStringerStruct{"bob", 17}))
})
It("has the correct StringRepresentation", func() {
Ω(reportEntry.StringRepresentation()).Should(Equal("{{red}}bob {{green}}17{{/}}"))
})
It("round-trips through JSON correctly", func() {
rtEntry := reportEntryJSONRoundTrip(reportEntry)
Ω(rtEntry.GetRawValue()).Should(Equal(map[string]interface{}{"Label": "bob", "Count": float64(17)}))
Ω(rtEntry.StringRepresentation()).Should(Equal("{{red}}bob {{green}}17{{/}}"))
})
})
Context("with multiple passed-in values", func() {
It("errors", func() {
reportEntry, err = internal.NewReportEntry("name", cl, 1, "2")
Ω(err).Should(MatchError(types.GinkgoErrors.TooManyReportEntryValues(cl, "2")))
})
})
Context("with the Offset decoration", func() {
It("computes a new offset code location", func() {
reportEntry, err = internal.NewReportEntry("name", cl, Offset(1))
Ω(reportEntry.GetRawValue()).Should(BeNil())
expectedCL := types.NewCodeLocation(2) // NewReportEntry has a BaseOffset of 2
Ω(reportEntry.Location.FileName).Should(Equal(expectedCL.FileName))
})
})
Context("with a CodeLocation", func() {
It("uses the passed-in codelocation", func() {
customCl := types.NewCustomCodeLocation("foo")
reportEntry, err = internal.NewReportEntry("name", cl, customCl)
Ω(reportEntry.GetRawValue()).Should(BeNil())
Ω(reportEntry.Location).Should(Equal(customCl))
})
})
Context("with a ReportEntryVisibility", func() {
It("uses the passed in visibility", func() {
reportEntry, err = internal.NewReportEntry("name", cl, types.ReportEntryVisibilityFailureOrVerbose)
Ω(reportEntry.GetRawValue()).Should(BeNil())
Ω(reportEntry.Visibility).Should(Equal(types.ReportEntryVisibilityFailureOrVerbose))
})
})
Context("with a time", func() {
It("uses the passed in time", func() {
t := time.Date(1984, 3, 7, 0, 0, 0, 0, time.Local)
reportEntry, err = internal.NewReportEntry("name", cl, t)
Ω(reportEntry.GetRawValue()).Should(BeNil())
Ω(reportEntry.Time).Should(Equal(t))
})
})
Describe("ReportEntries.HasVisibility", func() {
It("is true when the ReportEntries have the requested visibilities", func() {
entries := types.ReportEntries{
types.ReportEntry{Visibility: types.ReportEntryVisibilityAlways},
types.ReportEntry{Visibility: types.ReportEntryVisibilityAlways},
}
Ω(entries.HasVisibility(types.ReportEntryVisibilityNever, types.ReportEntryVisibilityAlways)).Should(BeTrue())
Ω(entries.HasVisibility(types.ReportEntryVisibilityNever, types.ReportEntryVisibilityFailureOrVerbose)).Should(BeFalse())
})
})
Describe("ReportEntries.WithVisibility", func() {
It("returns the subset of report entries with the requested visibilities", func() {
entries := types.ReportEntries{
types.ReportEntry{Name: "A", Visibility: types.ReportEntryVisibilityAlways},
types.ReportEntry{Name: "B", Visibility: types.ReportEntryVisibilityFailureOrVerbose},
types.ReportEntry{Name: "C", Visibility: types.ReportEntryVisibilityNever},
}
Ω(entries.WithVisibility(types.ReportEntryVisibilityAlways, types.ReportEntryVisibilityFailureOrVerbose)).Should(Equal(
types.ReportEntries{
types.ReportEntry{Name: "A", Visibility: types.ReportEntryVisibilityAlways},
types.ReportEntry{Name: "B", Visibility: types.ReportEntryVisibilityFailureOrVerbose},
},
))
})
})
Describe("mini-integration test - validating that the DSL correctly wires into the suite", func() {
Context("when passed a value", func() {
It("works!", func() {
AddReportEntry("A Test ReportEntry", ColorableStringerStruct{"bob", 17}, types.ReportEntryVisibilityFailureOrVerbose)
})
ReportAfterEach(func(report SpecReport) {
config, _ := GinkgoConfiguration()
if !config.DryRun && report.State.Is(types.SpecStatePassed) {
Ω(report.ReportEntries[0].StringRepresentation()).Should(Equal("{{red}}bob {{green}}17{{/}}"))
}
})
})
Context("when passed a pointer that subsequently changes", func() {
var obj *ColorableStringerStruct
BeforeEach(func() {
obj = &ColorableStringerStruct{"bob", 17}
})
It("works!", func() {
AddReportEntry("A Test ReportEntry", obj, types.ReportEntryVisibilityFailureOrVerbose)
})
AfterEach(func() {
obj.Label = "alice"
obj.Count = 42
})
ReportAfterEach(func(report SpecReport) {
config, _ := GinkgoConfiguration()
if !config.DryRun && report.State.Is(types.SpecStatePassed) {
Ω(report.ReportEntries[0].StringRepresentation()).Should(Equal("{{red}}alice {{green}}42{{/}}"))
}
})
})
})
})
|