be

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 21, 2024 License: MIT Imports: 7 Imported by: 0

README

ExpectTo/Be: Versatile Golang Matcher Library Designed for Testing with Ginkgo and Gomock.

Expect(👨🏼‍💻).To(Be(🚀))

License Go Reference

expectto/be is a Golang package that offers a substantial collection of Be matchers. Every Be matcher is compatible with both Ginkgo/Gomega and Gomock. Where possible, arguments of matchers can be either finite values or matchers (Be/Gomega/Gomock).
Employing expectto/be matchers enables you to create straightforward, readable, and maintainable unit or integration tests in Golang. Tasks such as testing HTTP requests, validating JSON responses, and more become remarkably comprehensive and straightforward.

Table of Contents

Installation

To use Be in your Golang project, simply import it:

import "github.com/expectto/be"

Example

Consider the following example demonstrating the usage of expectto/be's HTTP request matchers:

req, err := buildRequestForServiceFoo()
Expect(err).To(Succeed())

// Matching an HTTP request
Expect(req).To(be_http.Request(
    // Matching the URL
    be_http.HavingURL(be_url.URL(
        be_url.WithHttps(),
        be_url.HavingHost("example.com"),
        be_url.HavingPath("/path"),
        be_url.HavingSearchParam("status", "active"),
        be_url.HavingSearchParam("v", be_reflected.AsNumericString()),
        be_url.HavingSearchParam("q", "Hello World"),
    )),

    // Matching the HTTP method
    be_http.HavingMethod(http.MethodPost),
    
    // Matching request's context
    be_http.HavingCtx(be_ctx.Ctx(
        be_ctx.WithDeadline(be_time.LaterThan(time.Now().Add(30*time.Minute))),
        be_ctx.WithValue("foobar", 100),
    ))

    // Matching the request body using JSON matchers
    be_http.HavingBody(
        be_json.Matcher(
            be_json.JsonAsReader,
            be_json.HaveKeyValue("hello", "world"),
            be_json.HaveKeyValue("n", be_reflected.AsIntish()),
            be_json.HaveKeyValue("ids", be_reflected.AsSliceOf[string]),
            be_json.HaveKeyValue("details", And(
                be_reflected.AsObjects(),
                be.HaveLength(2),
                ContainElements(
                    be_json.HaveKeyValue("key", "foo"),
                    be_json.HaveKeyValue("key", "bar"),
                ),
            )),
        ),

        // Matching HTTP headers
        be_http.HavingHeader("X-Custom", "Hey-There"),
        be_http.HavingHeader("Authorization",
            be_strings.Template("Bearer {{jwt}}",
                be_strings.MatchingPart("jwt",
                    be_jwt.Token(
                        be_jwt.BeingValid(),
                        be_jwt.HavingClaims("name", "John Doe"),
                    ),
                ),
            ),
        ),
    ),
))

Matchers

(core) be
Be provides a set of core matchers for common testing scenarios:
Matcher Example Usage Description
be.Always() Expect(anything()).To(be.Always()) Always succeeds (passes).
be.Never(err) Expect(anything()).To(be.Never(errors.New("custom error"))) Never succeeds and always fails with a specified error
be.All(ms ...any) Expect(m).To(be.All(HaveKey("foo"), HaveKey("bar"), ...)) Logical AND for multiple matchers. Similar to Ginkgo'sAnd()
be.Any(ms ...any) Expect(m).To(be.Any(HaveKey("foo"), HaveKey("bar"), ...) Logical OR for multiple matchers. Similar to Ginkgo's Or()
be.Eq(expected) Expect(v).To(be.Eq(expectedValue)) Checks for equality. _Similar to Ginkgo's Equal _
be.Not(matcher) Expect(v).To(be.Not(anotherMatcher)) Negates the result of another matcher. Similar to Ginkgo's Not()
be.HaveLength(args ...any) Expect(collection).To(be.HaveLength(lengthMatcher)) Matches the length of slices, arrays, strings, or maps. Supports matchers as argument
be_reflected

AssignableTo(), Implementing(), AsKind(), ...

be_math

GreaterThan(), GreaterLessThan(), ...

be_strings

EmptyString(), NonEmptyString(), Alpha(), ...

be_time

LaterThan(), LaterThanEqual(), EarlierThan(), ...

be_jwt

Token(), HavingClaims(), ...

be_url

URL() HavingHost(), HavingHostname(), ...

be_ctx

Ctx(), CtxWithValue(), CtxWithDeadline(), ...

be_json

Matcher(), HaveKeyWithValue(), ...

be_http

Request(), HavingMethod(), HavingUrl(), ...

Contributing

Be welcomes contributions! Feel free to open issues, suggest improvements, or submit pull requests. Contribution guidelines for this project

License

This project is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var HttpRequest = be_http.Request

HttpRequest is an alias for be_http.Request matcher

View Source
var JwtToken = be_jwt.Token

JwtToken is an alias for be_jwt.Token matcher

View Source
var StringAsTemplate = be_strings.MatchTemplate

StringAsTemplate is an alias for be_strings.MatchTemplate matcher

View Source
var URL = be_url.URL

URL is an alias for be_url.URL matcher

Functions

func All

func All(ms ...any) types.BeMatcher

All is like gomega.And()

func Always

func Always() types.BeMatcher

Always does always match

func Any

func Any(ms ...any) types.BeMatcher

Any is like gomega.Or()

func Eq

func Eq(expected any) types.BeMatcher

Eq is like gomega.Equal()

func HaveLength

func HaveLength(args ...any) types.BeMatcher

HaveLength is like gomega.HaveLen() HaveLength succeeds if the actual value has a length that matches the provided conditions. It accepts either a count value or one or more Gomega matchers to specify the desired length conditions.

func Never

func Never(err error) types.BeMatcher

Never does never succeed (does always fail)

func Not

func Not(expected any) types.BeMatcher

Not is like gomega.Not()

Types

This section is empty.

Directories

Path Synopsis
Package be_jwt provides Be matchers for handling JSON Web Tokens (JWT).
Package be_jwt provides Be matchers for handling JSON Web Tokens (JWT).
Package be_reflected offers Be matchers for reflection in Go, enabling expressive and versatile assertions on values' reflect kinds.
Package be_reflected offers Be matchers for reflection in Go, enabling expressive and versatile assertions on values' reflect kinds.
Package be_strings provides Be matchers for string-related assertions
Package be_strings provides Be matchers for string-related assertions
internal
psi
Package psi contains helpers that extends gomega library Name psi stands for previous letter from Omega (as we want to have a name that is close to gomega, but not to be a gomega)
Package psi contains helpers that extends gomega library Name psi stands for previous letter from Omega (as we want to have a name that is close to gomega, but not to be a gomega)
psi_matchers
Package psi_matchers is a package that contains core matchers required Psi() to work properly
Package psi_matchers is a package that contains core matchers required Psi() to work properly
reflect
Package reflect contains helpers that extends standard reflect library
Package reflect contains helpers that extends standard reflect library
testing/mocks
Code generated by MockGen.
Code generated by MockGen.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL