From 5d59afefd12f0af6d091fbee655c877f5182988c Mon Sep 17 00:00:00 2001 From: Zachary Taylor Date: Thu, 25 Feb 2021 19:37:43 -0500 Subject: [PATCH] feature: add index to gstruct element func (#421) --- index.md | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/index.md b/index.md index 4b2a0840f..6f0578afd 100644 --- a/index.md +++ b/index.md @@ -2130,6 +2130,32 @@ You can also use the flag `AllowDuplicates` to permit multiple elements in your The options can be combined with the binary or: `IgnoreMissing|IgnoreExtras|AllowDuplicates`. +Additionally, `gstruct` provides `MatchAllElementsWithIndex` and `MatchElementsWithIndex` function for applying a matcher with index to each element, identified by an `IdentifierWithIndex` function. A helper function is also included with `gstruct` called `IndexIdentity` that provides the functionality of the just using the index as your identifier as seen below. + +```go + actual := []string{ + "A: foo bar baz", + "B: once upon a time", + "C: the end", + } + id := func(index int, _ interface{}) string { + return strconv.Itoa(index) + } + Expect(actual).To(MatchAllElements(id, Elements{ + "0": Not(BeZero()), + "1": MatchRegexp("[A-Z]: [a-z ]+"), + "2": ContainSubstring("end"), + })) + // IndexIdentity is a helper function equivalent to id in this example + Expect(actual).To(MatchAllElements(IndexIdentity, Elements{ + "0": Not(BeZero()), + "1": MatchRegexp("[A-Z]: [a-z ]+"), + "2": ContainSubstring("end"), + })) +``` + + The `WithIndex` variants take the same options as the other functions. + ### Testing type `map` All of the `*Fields` functions and types have a corresponding definitions `*Keys` which can perform analogous tests against map types: @@ -2210,4 +2236,4 @@ Example: "Rootfs": m.Ignore(), "Logs": m.Ignore(), })) -``` \ No newline at end of file +```