Skip to content

Commit

Permalink
Oops, needed a cleaner history
Browse files Browse the repository at this point in the history
  • Loading branch information
amberpixels committed May 19, 2024
0 parents commit f7c7238
Show file tree
Hide file tree
Showing 81 changed files with 7,332 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Go workspace file
go.work

# idea
.idea
4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### Contributing
Feel free to open issues.

### TODO: stabilize with contributing guidelines
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 expectto

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
229 changes: 229 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
## Expect(👨🏼‍💻).To(Be(🚀))

[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/expectto/be/blob/main/LICENSE)
[![Go Reference](https://pkg.go.dev/badge/github.com/expectto/be.svg)](https://expectto.github.io/be/)


`expectto/be` is a Golang package that offers a substantial collection of `Be` matchers. Every `Be` matcher is
compatible with both [Ginkgo](https://github.com/onsi/ginkgo)/[Gomega](https://github.com/onsi/gomega)
and [Gomock](https://github.com/uber-go/mock). Where possible, arguments of matchers can be either finite values or
matchers (Be/Gomega/Gomock).<br>
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](#installation)
- [Example](#example)
- [Matchers](#matchers)
- [Be (core)](#core-be)
- [Be Reflected](#be_reflected)
- [Be Math](#be_math)
- [Be String](#be_string)
- [Be Time](#be_time)
- [Be JWT](#be_jwt)
- [Be URL](#be_url)
- [Be JSON](#be_json)
- [Be HTTP](#be_http)

- [Contributing](#contributing)
- [License](#license)

## Installation

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

```go
import "github.com/expectto/be"
```

## Example

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

```go
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.POST()

// 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.AsInteger()),
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_string.MatchTemplate("Bearer {{jwt}}",
be_string.Var("jwt",
be_jwt.Token(
be_jwt.Valid(),
be_jwt.HavingClaim("name", "John Doe"),
),
),
),
),
),
))
```

## Matchers

### Core Be

📦 `be` provides a set of core matchers for common testing scenarios.<br>[See detailed docs](core-be-matchers.md)

#### Core matchers:

`Always`, `Never`, `All`, `Any`, `Eq`, `Not`, `HaveLength`, `Dive`, `DiveAny`, `DiveFirst`

### be_reflected

📦 `be_reflected` provides Be matchers that use reflection, enabling expressive assertions on values' reflect kinds and
types.<br>[See detailed docs](be_reflected/README.md)

#### General Matchers based on reflect.Kind:

`AsKind`, `AsFunc`, `AsChan`, `AsPointer`, `AsFinalPointer`, `AsStruct`, `AsPointerToStruct`, `AsSlice`, `AsPointerToSlice`, `AsSliceOf`, `AsMap`, `AsPointerToMap`, `AsObject`, `AsObjects`, `AsPointerToObject`

#### Data Type Matchers based on reflect.Kind

`AsString`, `AsBytes`, `AsNumeric`, `AsNumericString`, `AsInteger`, `AsIntegerString`, `AsFloat`, `AsFloatishString`,

#### Interface Matchers based on reflect.Kind

`AsReader`,`AsStringer`

#### Matchers based on types compatibility:

`AssignableTo`, `Implementing`

### be_math

📦 `be_math` provides Be matchers for mathematical operations.<br>[See detailed docs](be_math/README.md)

#### Matchers on math:

`GreaterThan`, `GreaterThanEqual`, `LessThan`, `LessThanEqual`, `Approx`, `InRange`, `Odd`, `Even`, `Negative`, `Positive`, `Zero`, `Integral`, `DivisibleBy`

#### Shortcut aliases for math matchers:

`Gt`, `Gte`, `Lt`, `Lte`

### be_string

📦 `be_string` provides Be matchers for string-related assertions.<br>[See detailed docs](be_string/README.md)

#### Matchers on strings

`NonEmptyString`, `EmptyString`, `Alpha`, `Numeric`, `AlphaNumeric`, `AlphaNumericWithDots`, `Float`, `Titled`, `LowerCaseOnly`, `MatchWildcard`, `ValidEmail`

#### Template matchers

`MatchTemplate`

### be_time

📦 `be_time` provides Be matchers on time.Time.<br>[See detailed docs](be_time/README.md)

#### Time Matchers

`LaterThan`, `LaterThanEqual`, `EarlierThan`, `EarlierThanEqual`, `Eq`, `Approx`, <br>
`SameExactMilli`, `SameExactSecond`, `SameExactMinute`, `SameExactHour`, <br>
`SameExactDay`, `SameExactWeekday`, `SameExactWeek`, `SameExactMonth`, <br>
`SameSecond`, `SameMinute`, `SameHour`, `SameDay`, `SameYearDay`, <br>
`SameWeek`, `SameMonth`, `SameYear`, `SameTimzone`, `SameOffset`, `IsDST`

### be_jwt

📦 `be_jwt` provides Be matchers for handling JSON Web Tokens (JWT). It includes matchers for transforming and validating
JWT tokens. Matchers corresponds to specific
golang [jwt implementation](https://github.com/golang-jwt/jwt/v5).<br> [See detailed docs](be_jwt/README.md)

#### Transformers for JWT matching:

`TransformSignedJwtFromString`, `TransformJwtFromString`

#### Matchers on JWT:

`Token`, `Valid`, `HavingClaims`, `HavingClaim`, `HavingMethodAlg`, `SignedVia`

### be_url

📦 `be_url` provides Be matchers on url.URL.<br> [See detailed docs](be_jwt/README.md)

#### Transformers for URL Matchers:

`TransformUrlFromString`, `TransformSchemelessUrlFromString`

#### URL Matchers:

`URL`, `HavingHost`, `HavingHostname`, `HavingScheme`, `NotHavingScheme`, `WithHttps`, `WithHttp`, `HavingPort`, `NotHavingPort`, `HavingPath`, `HavingRawQuery`, `HavingSearchParam`, `HavingMultipleSearchParam`, `HavingUsername`, `HavingUserinfo`, `HavingPassword`

### be_ctx

📦 `be_ctx` provides Be matchers on context.Context.<br> [See detailed docs](be_ctx/README.md)

#### Context Matchers:

`Ctx`, `CtxWithValue`, `CtxWithDeadline`, `CtxWithError`

### be_json

📦 `be_json` provides Be matchers for expressive assertions on JSON.<br> [See detailed docs](be_json/README.md)

#### JSON Matchers:

`Matcher`, `HaveKeyValue`

### be_http

📦 `be_http` provides Be matchers for expressive assertions on http.Request.<br> [See detailed docs](be_http/README.md)

#### Matchers on HTTP:

`Request`, `HavingMethod`, <br>
`GET`, `HEAD`, `POST`, `PUT`, `PATCH`, `DELETE`, `OPTIONS`, `CONNECT`, `TRACE`, <br>
`HavingURL`, `HavingBody`, `HavingHost`, `HavingProto`, `HavingHeader`, `HavingHeaders`

# Contributing

`Be` welcomes contributions! Feel free to open issues, suggest improvements, or submit pull
requests. [Contribution guidelines for this project](CONTRIBUTING.md)

# License

This project is [licensed under the MIT License](LICENSE).
41 changes: 41 additions & 0 deletions be_ctx/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# be_ctx
--
import "github.com/expectto/be/be_ctx"

Package be_ctx provides Be matchers on context.Context

## Usage

#### func Ctx

```go
func Ctx(args ...any) types.BeMatcher
```
Ctx succeeds if the actual value is a context.Context. If no arguments are
provided, it matches any context.Context. Otherwise, it uses the Psi matcher to
match the provided arguments against the actual context's values.

#### func CtxWithDeadline

```go
func CtxWithDeadline(deadline any) types.BeMatcher
```
CtxWithDeadline succeeds if the actual value is a context.Context and its
deadline matches the provided deadline.

#### func CtxWithError

```go
func CtxWithError(err any) types.BeMatcher
```
CtxWithError succeeds if the actual value is a context.Context and its error
matches the provided error value.

#### func CtxWithValue

```go
func CtxWithValue(key any, vs ...any) types.BeMatcher
```
CtxWithValue succeeds if the actual value is a context.Context and contains a
key-value pair where the key matches the provided key and the value matches the
provided arguments using any other matchers.
36 changes: 36 additions & 0 deletions be_ctx/matchers_ctx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Package be_ctx provides Be matchers on context.Context
package be_ctx

import (
"github.com/expectto/be/internal/psi"
"github.com/expectto/be/internal/psi_matchers"
"github.com/expectto/be/types"
)

// Ctx succeeds if the actual value is a context.Context.
// If no arguments are provided, it matches any context.Context.
// Otherwise, it uses the Psi matcher to match the provided arguments against the actual context's values.
func Ctx(args ...any) types.BeMatcher {
if len(args) == 0 {
return psi_matchers.NewCtxMatcher()
}

// todo: weak solution, fixme
return psi.Psi(args...)
}

// CtxWithValue succeeds if the actual value is a context.Context and contains a key-value pair
// where the key matches the provided key and the value matches the provided arguments using any other matchers.
func CtxWithValue(key any, vs ...any) types.BeMatcher {
return psi_matchers.NewCtxValueMatcher(key, vs...)
}

// CtxWithDeadline succeeds if the actual value is a context.Context and its deadline matches the provided deadline.
func CtxWithDeadline(deadline any) types.BeMatcher {
return psi_matchers.NewCtxDeadlineMatcher(deadline)
}

// CtxWithError succeeds if the actual value is a context.Context and its error matches the provided error value.
func CtxWithError(err any) types.BeMatcher {
return psi_matchers.NewCtxErrMatcher(err)
}
Loading

0 comments on commit f7c7238

Please sign in to comment.