/gomega:composing-matchersfrom gomega
Combine Gomega matchers with And/Or/Not, WithTransform, Satisfy, HaveField, HaveEach, and nested matchers for compound assertions. Useful for multiple requirements in one Expect or deep value assertions.
Ships in gomega. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Gomega matchers are values, so you can wrap and nest them to express several requirements in a single Expect/Eventually. Start with gomega:matchers for the catalog; reach here when one assertion needs to do more. Docs: https://onsi.github.io/gomega/#composing-matchers.
Expect(n).To(And(BeNumerically(">", 0), BeNumerically("<", 10))) // all must pass
Expect(msg).To(Or(Equal("Success"), MatchRegexp(`^Error .+$`))) // any may pass
Expect(s).To(Not(BeEmpty())) // negate
And(ms ...GomegaMatcher) / SatisfyAll(...) — identical; succeeds only if every matcher passes. Short-circuits on the first failure.Or(ms ...GomegaMatcher) / SatisfyAny(...) — identical; succeeds if any matcher passes. Short-circuits on the first success.Not(matcher GomegaMatcher) — logical negation; usually clearer than the .NotTo(...) form when nested.These are the building blocks for lightweight named matchers:
func BeBetween(min, max int) GomegaMatcher {
return SatisfyAll(BeNumerically(">", min), BeNumerically("<", max))
}
Expect(n).To(BeBetween(0, 10))
WithTransform(transform any, matcher GomegaMatcher) applies transform to ACTUAL and matches the result.
Expect(element).To(WithTransform(func(e Element) Color { return e.Color }, Equal(BLUE)))
Transform-function rules: it must take exactly one argument (the actual) and return either one value, or (value, error). Returning an error fails the assertion gracefully instead of panicking — useful for transforms that accept several input types:
func HaveSprocketName(name string) GomegaMatcher {
return WithTransform(func(actual any) (string, error) {
switch s := actual.(type) {
case *Sprocket: return s.Name, nil
case Sprocket: return s.Name, nil
default: return "", fmt.Errorf("expected a Sprocket, got %T", actual)
}
}, Equal(name))
}
This is the standard pattern for distilling a WithTransform into a reusable named matcher.
Satisfy(predicate any) passes when predicate(ACTUAL) returns true. The predicate takes one argument and returns one bool.
Expect(n).To(Satisfy(func(i int) bool { return i%2 == 0 }))
Reach for Satisfy for a one-off check; WithTransform when you want the inner matcher's richer failure message. For anything reused, write a real matcher → gomega:custom-matchers.
HaveValue(matcher GomegaMatcher) dereferences ACTUAL through pointers/interfaces (up to 31 levels) and applies matcher to the underlying value, so the same matcher works whether the actual is a value or a pointer. Fails on nil.
i := 42
Expect(&i).To(HaveValue(Equal(42)))
Expect(i).To(HaveValue(Equal(42)))
Not(HaveValue(...)) does not suppress the nil error. To accept nil while rejecting a specific value, use Or(BeNil(), Not(HaveValue(...))).
HaveField(field string, value any) extracts a field (or method result) from a struct and matches it. If value is a matcher it is used directly; otherwise it is wrapped in Equal.
Expect(book).To(HaveField("Title", "Les Miserables"))
Expect(book).To(HaveField("Title", ContainSubstring("Les Mis"))) // value can be a matcher
Expect(book).To(HaveField("Author.Name", "Victor Hugo")) // nested with "."
Expect(book).To(HaveField("Author.DOB.Year()", BeNumerically("<", 1900))) // "()" calls a method
The () suffix invokes a method that takes no arguments and returns exactly one value. A missing field is an error, not a failed match. To guard, pair with HaveExistingField(field): And(HaveExistingField("X"), HaveField("X", VALUE)) — handy when reusing HaveField as a ContainElement filter.
HaveEach(element any) passes when every element of an array, slice, map (values), or iter.Seq/iter.Seq2 matches. A plain value is wrapped in Equal; pass a matcher for richer checks.
Expect([]string{"Foo", "FooBar"}).To(HaveEach(ContainSubstring("Foo")))
An empty (or nil) collection is an error — it is ambiguous whether "each" holds. If empty is acceptable, use Or(BeEmpty(), HaveEach(...)).
Nearly every collection/channel matcher accepts matchers where it accepts values, so you compose by nesting rather than learning new operators:
Expect(xs).To(ContainElement(HaveField("Name", "gomega")))
Expect(xs).To(ContainElement(HaveField("Name", BeKeyOf(names)), &found)) // matcher + capture pointer
Expect(books).To(ConsistOf(
HaveField("Author.Name", "Victor Hugo"),
And(HaveField("Pages", BeNumerically(">", 100)), HaveField("InPrint", BeTrue())),
))
Expect(m).To(HaveKeyWithValue("user", HaveField("Active", BeTrue())))
Eventually(ch).Should(Receive(HaveField("Kind", "sesame")))
This composes cleanly with Eventually/Consistently too → gomega:async.
For deep, partial matching across nested structs, slices, and maps, hand-rolled HaveField/And/HaveEach trees get noisy. gstruct's MatchFields/MatchAllElements/MatchKeys are purpose-built for that shape → gomega:gstruct.
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub onsi/gomega --plugin gomega12 skills in this plugin
/assertionsWrite correct synchronous Gomega assertions — Expect/Ω notation, the To/NotTo/ToNot/Should/ShouldNot equivalences, the multi-return error idiom, Succeed vs HaveOccurred, the .Error() chaining form, annotating assertions (format-string and func()string), tuning failure output via the format subpackage (MaxLength/MaxDepth/UseStringerRepresentation/GomegaStringer/TruncatedDiff/RegisterCustomFormatter/format.Object), and asserting inside helper functions with GinkgoHelper/WithOffset/ExpectWithOffset, NewWithT(t) for plain testing, and the g Gomega callback. Use when writing or reviewing synchronous (non-polling) Gomega assertions./asyncPolling assertions in Gomega — Eventually (poll until it passes) and Consistently (must keep passing), the func(g Gomega) callback idiom, WithTimeout/WithPolling/Within/ProbeEvery, WithContext and Ginkgo SpecContext, StopTrying/TryAgainAfter bail-outs, MustPassRepeatedly, and default-interval tuning. Use when an assertion can't be true synchronously — anything involving goroutines, channels, network calls, eventual consistency, or "wait until / stays true"./custom-matchersWriting your own Gomega matchers — the GomegaMatcher interface (Match/FailureMessage/NegatedFailureMessage), gcustom.MakeMatcher with message templates and template data, the format package helpers (format.Message/format.Object), MatchMayChangeInTheFuture and StopTrying for Eventually/Consistently, and how to test and package custom matchers. Use when a built-in or composed matcher can't express your domain assertion and you need to build one./gbytesTesting streaming io buffers with gbytes — gbytes.NewBuffer() (an io.Writer also returned by gexec sessions), the Say(regexp) matcher that forward-scans from a moving read cursor, the canonical Eventually(buffer).Should(Say(...)) streaming pattern, sequential cursor-advancing Say calls, Contents(), BufferWithBytes/BufferReader, buffer.Detect for branching, and TimeoutReader/Writer/Closer for testing blocking io.Reader/Writer/Closer. Use when asserting on streaming or incremental output (process stdout/stderr, API streams, io.Readers) rather than a complete value./gexecTesting external processes with gexec — compile binaries with Build/BuildWithEnvironment/BuildIn and CleanupBuildArtifacts, start them with Start returning a *Session, await exit with the Exit matcher (Eventually(session).Should(Exit(0))), Wait/ExitCode, signal via Kill/Terminate/Interrupt/Signal and package-level KillAndWait/TerminateAndWait, and assert on session.Out/Err which are gbytes buffers (Say, Contents). Use when building, running, signaling, or asserting on subprocesses in Go tests./ghttpThe ghttp test HTTP server for testing HTTP clients — NewServer/NewTLSServer, AppendHandlers, the Verify* assertions (VerifyRequest/VerifyHeader/VerifyHeaderKV/VerifyJSON/VerifyForm/VerifyBasicAuth/VerifyContentType/VerifyBody), RespondWith/RespondWithJSONEncoded(Ptr), CombineHandlers, RouteToHandler for unordered MUXed routes, AllowUnhandledRequests, RoundTripper, and TLS. Use when testing code that makes outbound HTTP requests.Other plugins' skills for the same job