be

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/jsteenb2/expect"
	"github.com/jsteenb2/expect/be"
)

func main() {
	t := &expect.SpyTB{}

	expect.It(t, "hello").To(
		be.Len(be.Eq(5)),
		be.Eq("hello"),
		be.Substring("ell"),
		be.Not(be.AllCaps),
	)

	fmt.Println(t.Result())
}
Output:

Test passed

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllCaps

func AllCaps(in string) expect.MatchResult

AllCaps will check if a string is in all caps.

Example
package main

import (
	"fmt"

	"github.com/jsteenb2/expect"
	"github.com/jsteenb2/expect/be"
)

func main() {
	t := &expect.SpyTB{}

	expect.It(t, "HELLO").To(be.AllCaps)

	fmt.Println(t.Result())
}
Output:

Test passed
Example (Fail)
package main

import (
	"fmt"

	"github.com/jsteenb2/expect"
	"github.com/jsteenb2/expect/be"
)

func main() {
	t := &expect.SpyTB{}

	expect.It(t, "hello").To(be.AllCaps)

	fmt.Println(t.Result())
}
Output:

Test failed: [expected hello to in all caps, but it was not in all caps]

func ContainingItem

func ContainingItem[T any](m expect.Matcher[T]) expect.Matcher[[]T]

ContainingItem checks if an array contains an item that meets a matcher's criteria.

Example
package main

import (
	"fmt"

	"github.com/jsteenb2/expect"
	"github.com/jsteenb2/expect/be"
)

func main() {
	t := &expect.SpyTB{}

	anArray := []string{"HELLO", "WORLD"}
	expect.It(t, anArray).To(be.ContainingItem(be.AllCaps))

	fmt.Println(t.Result())
}
Output:

Test passed
Example (Fail)
package main

import (
	"fmt"

	"github.com/jsteenb2/expect"
	"github.com/jsteenb2/expect/be"
)

func main() {
	t := &expect.SpyTB{}

	anArray := []string{"hello", "world"}
	expect.It(t, anArray).To(be.ContainingItem(be.AllCaps))

	fmt.Println(t.Result())
}
Output:

Test failed: [expected [hello world] to contain an item in all caps, but it did not]

func Eq

func Eq[T comparable](expected T) expect.Matcher[T]

Eq checks if a value is equal to another value.

Example
package main

import (
	"fmt"

	"github.com/jsteenb2/expect"
	"github.com/jsteenb2/expect/be"
)

func main() {
	t := &expect.SpyTB{}
	expect.It(t, 5).To(be.Eq(5))
	fmt.Println(t.Result())
}
Output:

Test passed
Example (Fail)
package main

import (
	"fmt"

	"github.com/jsteenb2/expect"
	"github.com/jsteenb2/expect/be"
)

func main() {
	t := &expect.SpyTB{}
	expect.It(t, 5).To(be.Eq(4))
	fmt.Println(t.Result())
}
Output:

Test failed: [expected 5 to be equal to 4, but it was 5]

func EveryItem

func EveryItem[T any](m expect.Matcher[T]) expect.Matcher[[]T]

EveryItem checks if every item in an array meets a matcher's criteria.

Example
package main

import (
	"fmt"

	"github.com/jsteenb2/expect"
	"github.com/jsteenb2/expect/be"
)

func main() {
	t := &expect.SpyTB{}

	anArray := []string{"hello", "world"}
	expect.It(t, anArray).To(be.EveryItem(be.Substring("o")))

	fmt.Println(t.Result())
}
Output:

Test passed
Example (Fail)
package main

import (
	"fmt"

	"github.com/jsteenb2/expect"
	"github.com/jsteenb2/expect/be"
)

func main() {
	t := &expect.SpyTB{}

	anArray := []string{"hello", "world"}
	expect.It(t, anArray).To(be.EveryItem(be.Substring("h")))

	fmt.Println(t.Result())
}
Output:

Test failed: [expected [hello world] to have every item contain "h"]

func Greater

func Greater[T cmp.Ordered](in T) expect.Matcher[T]

Greater checks if a value is greater than another value.

Example
package main

import (
	"fmt"

	"github.com/jsteenb2/expect"
	"github.com/jsteenb2/expect/be"
)

func main() {
	t := &expect.SpyTB{}
	expect.It(t, 5).To(be.Greater(4))
	fmt.Println(t.Result())
}
Output:

Test passed
Example (Fail)
package main

import (
	"fmt"

	"github.com/jsteenb2/expect"
	"github.com/jsteenb2/expect/be"
)

func main() {
	t := &expect.SpyTB{}
	expect.It(t, 5).To(be.Greater(6))
	fmt.Println(t.Result())
}
Output:

Test failed: [expected 5 to be greater than 6, but it was 5]

func Key

func Key[K comparable, V any](key K, valueMatcher expect.Matcher[V]) expect.Matcher[map[K]V]

Key checks if a map has a key with a specific value.

Example
package main

import (
	"fmt"

	"github.com/jsteenb2/expect"
	"github.com/jsteenb2/expect/be"
)

func main() {
	t := &expect.SpyTB{}

	expect.It(t, map[string]string{"hello": "world"}).To(be.Key("hello", be.Eq("world")))

	fmt.Println(t.Result())
}
Output:

Test passed
Example (Fail)
package main

import (
	"fmt"

	"github.com/jsteenb2/expect"
	"github.com/jsteenb2/expect/be"
)

func main() {
	t := &expect.SpyTB{}

	expect.It(t, map[string]int{"score": 4}).To(be.Key("score", be.Greater(5).And(be.Less(10))))

	fmt.Println(t.Result())
}
Output:

Test failed: [expected map[score:4] to have key score with value be greater than 5 and be less than 10, but it was 4]

func Len

func Len(matcher expect.Matcher[int]) expect.Matcher[string]

Len will check a string's length meets the given matcher's criteria. TODO: would be nice to generalize this a bit more so that it works with

any slice. Finding it awkward with the existing matcher type matching.
Need to noodle on it a bit.
Example
package main

import (
	"fmt"

	"github.com/jsteenb2/expect"
	"github.com/jsteenb2/expect/be"
)

func main() {
	t := &expect.SpyTB{}

	expect.It(t, "hello").To(be.Len(be.Eq(5)))

	fmt.Println(t.Result())
}
Output:

Test passed
Example (Fail)
package main

import (
	"fmt"

	"github.com/jsteenb2/expect"
	"github.com/jsteenb2/expect/be"
)

func main() {
	t := &expect.SpyTB{}

	expect.It(t, "hello").To(be.Len(be.Eq(4)))

	fmt.Println(t.Result())
}
Output:

Test failed: [expected hello to have length be equal to 4, but it was 5]

func Less

func Less[T cmp.Ordered](in T) expect.Matcher[T]

Less checks if a value is less than another value.

Example
package main

import (
	"fmt"

	"github.com/jsteenb2/expect"
	"github.com/jsteenb2/expect/be"
)

func main() {
	t := &expect.SpyTB{}
	expect.It(t, 5).To(be.Less(6))
	fmt.Println(t.Result())
}
Output:

Test passed
Example (Fail)
package main

import (
	"fmt"

	"github.com/jsteenb2/expect"
	"github.com/jsteenb2/expect/be"
)

func main() {
	t := &expect.SpyTB{}
	expect.It(t, 5).To(be.Less(4))
	fmt.Println(t.Result())
}
Output:

Test failed: [expected 5 to be less than 4, but it was 5]

func Not

func Not[T any](matcher expect.Matcher[T]) expect.Matcher[T]

Not is a helper function to negate a matcher.

func ShallowEq

func ShallowEq[T comparable](other []T) expect.Matcher[[]T]

ShallowEq checks if two slices are equal, only works with slices of comparable types.

Example
package main

import (
	"fmt"

	"github.com/jsteenb2/expect"
	"github.com/jsteenb2/expect/be"
)

func main() {
	t := &expect.SpyTB{}

	anArray := []string{"hello", "world"}
	expect.It(t, anArray).To(be.ShallowEq([]string{"hello", "world"}))

	fmt.Println(t.Result())
}
Output:

Test passed
Example (Fail)
package main

import (
	"fmt"

	"github.com/jsteenb2/expect"
	"github.com/jsteenb2/expect/be"
)

func main() {
	t := &expect.SpyTB{}

	anArray := []string{"hello", "world"}
	expect.It(t, anArray).To(be.ShallowEq([]string{"goodbye", "world"}))

	fmt.Println(t.Result())
}
Output:

Test failed: [expected [hello world] to be equal to [goodbye world]]

func Size

func Size[T any](matcher expect.Matcher[int]) expect.Matcher[[]T]

Size checks if an array's size meets a matcher's criteria. TODO(berg): this API gets a bit awkward. The generic type matching struggles a bit here...

not sure what the resolution is here just yet. Need to play with it some more.
Example
package main

import (
	"fmt"

	"github.com/jsteenb2/expect"
	"github.com/jsteenb2/expect/be"
)

func main() {
	t := &expect.SpyTB{}

	anArray := []string{"hello", "world"}
	expect.It(t, anArray).To(be.Size[string](be.Eq(2)))

	fmt.Println(t.Result())
}
Output:

Test passed
Example (Fail)
package main

import (
	"fmt"

	"github.com/jsteenb2/expect"
	"github.com/jsteenb2/expect/be"
)

func main() {
	t := &expect.SpyTB{}

	anArray := []string{"hello", "world"}
	expect.It(t, anArray).To(be.Size[string](be.Eq(3)))

	fmt.Println(t.Result())
}
Output:

Test failed: [expected [hello world] to have a size be equal to 3, but it was 2]

func Substring

func Substring(substring string) expect.Matcher[string]

Substring will check if a string contains a given substring.

Example
package main

import (
	"fmt"

	"github.com/jsteenb2/expect"
	"github.com/jsteenb2/expect/be"
)

func main() {
	t := &expect.SpyTB{}

	expect.It(t, "hello").To(be.Substring("ell"))

	fmt.Println(t.Result())
}
Output:

Test passed
Example (Fail)
package main

import (
	"fmt"

	"github.com/jsteenb2/expect"
	"github.com/jsteenb2/expect/be"
)

func main() {
	t := &expect.SpyTB{}

	expect.It(t, "hello").To(be.Substring("goodbye"))

	fmt.Println(t.Result())
}
Output:

Test failed: [expected hello to contain "goodbye"]

func WithAnyValue

func WithAnyValue[T any]() expect.Matcher[T]

WithAnyValue lets you match any value, useful if you're just looking for the presence of a key.

Example
package main

import (
	"fmt"

	"github.com/jsteenb2/expect"
	"github.com/jsteenb2/expect/be"
)

func main() {
	t := &expect.SpyTB{}

	expect.It(t, map[string]string{"hello": "world"}).To(be.Key("goodbye", be.WithAnyValue[string]()))

	fmt.Println(t.Result())
}
Output:

Test failed: [expected map[hello:world] to have key goodbye, but it did not]

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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