its

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2024 License: MIT Imports: 13 Imported by: 0

README

its -- A Matcher Library

What it is? -- yes, it's its.

its provides value matchers.

Install

go get github.com/youta-t/its

Its requires go1.21+.

Core package: its

github.com/youta-t/its package is core package. Built-in mathcers are here.

For example, its.EqEq matcher is for comparable.

import "testing"

import "github.com/youta-t/its"

func Add(a, b int) int {
    return a + b
}

func TestAdd(t *testing.T) {
    actual := Add(3, 7)
    its.EqEq(10).Match(actual).OrError(t)
}

It passes becauase 10 == 3 + 7 is true, as you see.

All matchers have example. See them in pkg.go.dev: https://pkg.go.dev/github.com/youta-t/its and doc/getting-started.

Nice message

If it does not match, it leave nice message.

import "testing"

import "github.com/youta-t/its"

// ...

func Add(a, b int) int {
	return a + b
}

func TestAdd(t *testing.T) {
	got := Add(3, 7)
	its.EqEq(got).Match(10).OrError(t)
	its.EqEq(got).Match(11).OrError(t)
}

provides,

--- FAIL: TestAdd (0.00s)
    .../example_test.go:33:
        ✘ /* got */ 11 == /* want */ 10

Error message is tailored for each matchers.

Generally, each messages start with...

  • : passed matcher
  • : failed matcher
  • ~ : failed matcher, but not matter
Composeable

Mathers of its can be composed. For example,

package example_test

import (
	"testing"

	"github.com/youta-t/its"
)

// ...

func TestBetween(t *testing.T) {
	its.All(
		its.GreaterThan(3),
		its.LesserEq(8),
	).Match(7).OrError(t)

	its.All(
		its.GreaterThan(3),
		its.LesserEq(8),
	).Match(8).OrError(t)

	its.All(
		its.GreaterThan(3),
		its.LesserEq(8),
	).Match(9).OrError(t)
}

provides,

--- FAIL: TestBetween (0.00s)
    .../example_test.go:20:
        ✘ // all: (1 ok / 2 matchers)
            ✔ /* want */ 3 < /* got */ 9
            ✘ /* want */ 8 > /* got */ 9

"A between B" means "greater than A, and lesser than B", as you know.

Not only All, there are Some (require match at least one) and Not (invert match).

Generate Struct Matcher: structer

its has a tool for //go:generate to generate matchers of struct, github.com/youta-t/its/structer.

You can get matchers for structs in and not in your package.

See structer/README.md for more details.

Generate Mocks: mocker

There are another //go:generate feature to generate mocks of functions and interfaces, github.com/youta-t/its/mocker.

You can mock builders for each type ... interface and type ... func.

And more, there are "scenario" test feature to check injected functions are called in exact order.

See mocker/README.md for more details.

DIY kit included

Matcher developmenet kit, itskit, is included.

You can create your matcher from scratch in 50 lines or so. Or, in the simplest case, you need just 10 lines per one matcher.

See doc/how-to-write-my-matcher.md to know how to.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Float

type Float interface {
	~float32 | ~float64
}

type Integer

type Integer interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

type MapSpec

type MapSpec[K comparable, V any] map[K]Matcher[V]

type Matcher added in v0.1.3

type Matcher[T any] itskit.Matcher[T]

Matcher does match got an want values, determines success or not and writes error message.

To run matching, use Matcher.Match.

func After

func After[T interface{ After(T) bool }](want T) Matcher[T]

After tests with

got.After(want)

want value can be time.Time, for example, but whatever okay if it has After().

Example
t1, err := time.Parse(
	time.RFC3339,
	"2023-10-11T12:13:14+00:00",
)
if err != nil {
	panic(err)
}
t2 := t1.Add(5 * time.Minute) // = 2023-10-11T12:18:14

its.After(t1).Match(t2).OrError(t)
its.After(t1).Match(t1).OrError(t)
its.After(t2).Match(t1).OrError(t)
Output:

✘ (/* got */ 2023-10-11 12:13:14 +0000 +0000).After(/* want */ 2023-10-11 12:13:14 +0000 +0000)		--- @ ./general_test.go:185

✘ (/* got */ 2023-10-11 12:13:14 +0000 +0000).After(/* want */ 2023-10-11 12:18:14 +0000 +0000)		--- @ ./general_test.go:186

func All

func All[T any](matchers ...Matcher[T]) Matcher[T]

All tests actual passes all specs.

If no matchers are given, it always pass.

Example (Ng)
its.All(
	its.StringHavingPrefix("abc"),
	its.StringContaining("hij"),
	its.StringHavingSuffix("xyz"),
).
	Match("abc...fghIJkl...xyz").
	OrError(t)
Output:

✘ // all: (2 ok / 3 matchers)		--- @ ./logical_test.go:18
    ✔ strings.HasPrefix(/* got */ "abc...fghIJkl...xyz", /* want */ "abc")		--- @ ./logical_test.go:19
    ✘ strings.Contains(/* got */ "abc...fghIJkl...xyz", /* want */ "hij")		--- @ ./logical_test.go:20
    ✔ strings.HasSuffix(/* got */ "abc...fghIJkl...xyz", /* want */ "xyz")		--- @ ./logical_test.go:21
Example (Ok)
its.All(
	its.StringHavingPrefix("abc"),
	its.StringContaining("hij"),
	its.StringHavingSuffix("xyz"),
).
	Match("abc...fghijkl...xyz").
	OrError(t)
Output:

func Always

func Always[T any]() Matcher[T]

always pass.

Example
its.Always[any]().Match(struct{ Arbitary string }{}).OrError(t)
Output:

func Before

func Before[T interface{ Before(T) bool }](want T) Matcher[T]

Before tests with

got.Before(want)

want value can be time.Time, for example, but whatever okay if it has Before().

Example
t1, err := time.Parse(
	time.RFC3339,
	"2023-10-11T12:13:14+00:00",
)
if err != nil {
	panic(err)
}
t2 := t1.Add(5 * time.Minute) // = 2023-10-11T12:18:14

its.Before(t1).Match(t2).OrError(t)
its.Before(t1).Match(t1).OrError(t)
its.Before(t2).Match(t1).OrError(t)
Output:

✘ (/* got */ 2023-10-11 12:18:14 +0000 +0000).Before(/* want */ 2023-10-11 12:13:14 +0000 +0000)		--- @ ./general_test.go:164

✘ (/* got */ 2023-10-11 12:13:14 +0000 +0000).Before(/* want */ 2023-10-11 12:13:14 +0000 +0000)		--- @ ./general_test.go:165

func BytesContaining

func BytesContaining(want []byte) Matcher[[]byte]

BytesContaining tests with bytes.Contains

Example (Ng)
its.BytesContaining([]byte("bcd")).Match([]byte("adcbe")).OrError(t)
Output:

✘ bytes.Contains(/* got */ []byte{0x61, 0x64, 0x63, 0x62, 0x65}, /* want */ []byte{0x62, 0x63, 0x64})		--- @ ./general_test.go:372
Example (Ok)
its.BytesContaining([]byte("bcd")).Match([]byte("abcde")).OrError(t)
Output:

func BytesEqual

func BytesEqual(want []byte) Matcher[[]byte]

BytesEqual tests with bytes.Equal

Example (Ng)
its.BytesEqual([]byte("abc")).Match([]byte("acb")).OrError(t)
Output:

✘ bytes.Equal(/* got */ []byte{0x61, 0x63, 0x62}, /* want */ []byte{0x61, 0x62, 0x63})		--- @ ./general_test.go:339
Example (Ok)
its.BytesEqual([]byte("abc")).Match([]byte("abc")).OrError(t)
Output:

func BytesHavingPrefix

func BytesHavingPrefix(want []byte) Matcher[[]byte]

BytesHavingPrefix tests with bytes.HasPrefix

Example (Ng)
its.BytesHavingPrefix([]byte("abc")).Match([]byte("adcbe")).OrError(t)
Output:

✘ bytes.HasPrefix(/* got */ []byte{0x61, 0x64, 0x63, 0x62, 0x65}, /* want */ []byte{0x61, 0x62, 0x63})		--- @ ./general_test.go:350
Example (Ok)
its.BytesHavingPrefix([]byte("abc")).Match([]byte("abcde")).OrError(t)
Output:

func BytesHavingSuffix

func BytesHavingSuffix(want []byte) Matcher[[]byte]

BytesHavingSuffix tests with bytes.HasSuffix

Example (Ng)
its.BytesHavingSuffix([]byte("cde")).Match([]byte("adcbe")).OrError(t)
Output:

✘ bytes.HasSuffix(/* got */ []byte{0x61, 0x64, 0x63, 0x62, 0x65}, /* want */ []byte{0x63, 0x64, 0x65})		--- @ ./general_test.go:361
Example (Ok)
its.BytesHavingSuffix([]byte("cde")).Match([]byte("abcde")).OrError(t)
Output:

func ClosedChan added in v0.1.3

func ClosedChan[C chan T | <-chan T, T any]() Matcher[C]

ClosedChan tests wheather channel is closed or not.

This matcher tries to receive from channel, it may cause sideeffect.

Example (Ng)
ch2 := make(chan string, 1)
its.ClosedChan[chan string]().Match(ch2).OrError(t)
Output:

✘ chan string is not closed.		--- @ ./general_test.go:408
Example (Ok)
ch1 := make(chan int, 1)
close(ch1)
its.ClosedChan[chan int]().Match(ch1).OrError(t)
Output:

func EqEq

func EqEq[T comparable](want T) Matcher[T]

EqEq tests of comparable with

want == got
Example (Ng)
its.EqEq(42).Match(49).OrError(t) // fail!
Output:

✘ /* got */ 49 == /* want */ 42		--- @ ./general_test.go:19
Example (Ng_non_primitive_type)
type MyType struct {
	Foo int
}

its.EqEq(MyType{Foo: 42}).Match(MyType{Foo: 24}).OrError(t) // also fail!
Output:

✘ /* got */ {Foo:24} == /* want */ {Foo:42}		--- @ ./general_test.go:29
Example (Ok)
its.EqEq(42).Match(42).OrError(t) // pass. so no messages are output
Output:

func EqEqPtr deprecated

func EqEqPtr[T comparable](want *T) Matcher[*T]

EqEqPtr tests of pointer for comparable with

(want == got) || (*want == *got)

Deprecated: Use Pointer(EqEq(...)) .

func Equal

func Equal[T any, E interface{ Equal(T) bool }](want E) Matcher[T]

Equal tests with

expcted.Equal(got)

want value can be time.Time, for example, but whatever okay if it has Equal().

Example
t1, err := time.Parse(
	time.RFC3339,
	"2023-10-11T12:13:14+00:00",
)
if err != nil {
	panic(err)
}
t2 := t1.Add(5 * time.Minute) // = 2023-10-11T12:18:14

its.Equal(t1).Match(t2).OrError(t)
its.Equal(t1).Match(t1).OrError(t)
its.Equal(t2).Match(t1).OrError(t)
Output:

✘ (/* want */ 2023-10-11 12:13:14 +0000 +0000).Equal(/* got */ 2023-10-11 12:18:14 +0000 +0000)		--- @ ./general_test.go:204

✘ (/* want */ 2023-10-11 12:18:14 +0000 +0000).Equal(/* got */ 2023-10-11 12:13:14 +0000 +0000)		--- @ ./general_test.go:206

func EquivWith

func EquivWith[T, U any](want T, equiv func(want T, got U) bool) Matcher[U]

EquivWith tests with

equiv(want, got)

Args

- want T: expectation

- equiv: function returns true if want matches with got.

Example (Ng)
its.EquivWith(
	42,
	func(want int, got string) bool { return fmt.Sprintf("%d", want) == got },
).
	Match("40").
	OrError(t)
Output:

✘ (/* want */ 42) equiv. (/* got */ 40)		--- @ ./general_test.go:225
Example (Ok)
its.EquivWith(
	42,
	func(want int, got string) bool { return fmt.Sprintf("%d", want) == got },
).
	Match("42").
	OrError(t)
Output:

func Error

func Error(want error) Matcher[error]

Error tests with errors.Is .

Example
e1 := errors.New("error")
e2 := fmt.Errorf("wrapped: %w", e1)

its.Error(e1).Match(e2).OrError(t)
its.Error(e1).Match(e1).OrError(t)
Output:

Example (Ng)
e1 := errors.New("error")
e2 := fmt.Errorf("wrapped: %w", e1)

its.Error(e2).Match(e1).OrError(t)
Output:

✘ errors.Is(/* got */ error, /* want */ wrapped: error)		--- @ ./general_test.go:249

func ErrorAs

func ErrorAs[T error]() Matcher[error]

ErrorAs tests with errors.As .

Example (Ng)
otherErr := errors.New("error by error.New")
its.ErrorAs[CustomError]().Match(otherErr).OrError(t)
Output:

✘ want := new(its_test.CustomError); errors.As(/* got */ error by error.New, want)		--- @ ./general_test.go:270
Example (Ok)
customErr := CustomError("custom error")

its.ErrorAs[CustomError]().Match(customErr).OrError(t)
Output:

func ForEntries added in v0.2.7

func ForEntries[
	K comparable, T, U, X any, F func(U) Matcher[T], S func(map[K]Matcher[T]) Matcher[X],
](
	s S,
	matcherFactory F,
	wants map[K]U,
) Matcher[X]

ForEntries broadcasts matcherFactory for each values in map, and wrap with s.

This provides shordhands of Matchers using map of Matchers.

Example

ForEntries(Map, EqEq, map[string]int{"a": 1, "b": 2})

is, equiverent to

Map(map[string]Matcher[int]{
	"a": its.EqEq(1), "b": its.EqEq(2)
})

Args

- s func(map[K]Matcher[T] Matcher[X]): wraps a map of matchers comes from matcherFactory

- matcherFactory func(U)Matcher[T]: it applied for each values in enteies to generate map of Matchers.

- wants: map entries.

func ForItems added in v0.2.7

func ForItems[T, U, X any, F func(U) Matcher[T], S func(...Matcher[T]) Matcher[X]](
	s S, matcherFactory F, wants []U,
) Matcher[X]

ForItems broadcasts matcherFactory for each elements, and wrap with s.

This provides shorthands of Matchers using slice of Matchers.

Example

ForItems(Some, EqEq, []int{1, 2, 3, 4, 5})

is, equiverent to

Some(
	EqEq(1), EqEq(2), EqEq(3), EqEq(4), EqEq(5),
)

Args

- s func(matchers []Matcher[T]) Matcher[X]: wraps matchers comes from matcherFactory

- matcherFactory func(U) Matcher[T]: it applied for each items in wants to generate matchers for them

- wants []U: values.

func GreaterEq

func GreaterEq[T Numeric | ~string](want T) Matcher[T]

GreaterEq tests of numeric value with

want <= got
Example (Float)
its.GreaterEq(1.0).Match(1.1).OrError(t)
its.GreaterEq(1.0).Match(1.0).OrError(t)
its.GreaterEq(1.1).Match(1.0).OrError(t)
Output:

✘ /* want */ 1.1 <= /* got */ 1		--- @ ./general_test.go:79
Example (Int)
//      <=
its.GreaterEq(10).Match(11).OrError(t)
its.GreaterEq(11).Match(10).OrError(t)
its.GreaterEq(10).Match(10).OrError(t)
Output:

✘ /* want */ 11 <= /* got */ 10		--- @ ./general_test.go:69
Example (Sring)
its.GreaterEq("aaa").Match("aab").OrError(t)
its.GreaterEq("aaa").Match("aaa").OrError(t)
its.GreaterEq("aab").Match("aaa").OrError(t)
Output:

✘ /* want */ aab <= /* got */ aaa		--- @ ./general_test.go:87

func GreaterThan

func GreaterThan[T Numeric | string](want T) Matcher[T]

GreaterThan tests of numeric value with

want < got
Example (Float)
its.GreaterThan(1.0).Match(1.0).OrError(t)
its.GreaterThan(1.1).Match(1.0).OrError(t)
its.GreaterThan(1.0).Match(1.1).OrError(t)
Output:

✘ /* want */ 1 < /* got */ 1		--- @ ./general_test.go:46

✘ /* want */ 1.1 < /* got */ 1		--- @ ./general_test.go:47
Example (Int)
its.GreaterThan(10).Match(10).OrError(t)
its.GreaterThan(11).Match(10).OrError(t)
its.GreaterThan(10).Match(11).OrError(t)
Output:

✘ /* want */ 10 < /* got */ 10		--- @ ./general_test.go:36

✘ /* want */ 11 < /* got */ 10		--- @ ./general_test.go:37
Example (String)
its.GreaterThan("aaa").Match("aaa").OrError(t)
its.GreaterThan("aab").Match("aaa").OrError(t)
its.GreaterThan("aaa").Match("aab").OrError(t)
Output:

✘ /* want */ aaa < /* got */ aaa		--- @ ./general_test.go:56

✘ /* want */ aab < /* got */ aaa		--- @ ./general_test.go:57

func Inf

func Inf() Matcher[float64]

Inf tests with math.IsInf

This matcher will pass either positive or negative infinity.

Example
its.Inf().Match(math.Inf(1)).OrError(t)
its.Inf().Match(math.Inf(-1)).OrError(t)
Output:

Example (Ng)
its.Inf().Match(0).OrError(t)
Output:

✘ math.IsInf(/* got */ 0.000000, 0)		--- @ ./general_test.go:395

func LesserEq

func LesserEq[T Numeric | ~string](want T) Matcher[T]

LesserEq tests of numeric value with

want > got
Example (Float)
its.LesserEq(1.0).Match(1.1).OrError(t)
its.LesserEq(1.0).Match(1.0).OrError(t)
its.LesserEq(1.1).Match(1.0).OrError(t)
Output:

 ✘ /* want */ 1 >= /* got */ 1.1		--- @ ./general_test.go:137
Example (Int)
//      >=
its.LesserEq(10).Match(11).OrError(t)
its.LesserEq(11).Match(10).OrError(t)
its.LesserEq(10).Match(10).OrError(t)
Output:

✘ /* want */ 10 >= /* got */ 11		--- @ ./general_test.go:128
Example (String)
its.LesserEq("aaa").Match("aab").OrError(t)
its.LesserEq("aaa").Match("aaa").OrError(t)
its.LesserEq("aab").Match("aaa").OrError(t)
Output:

✘ /* want */ aaa >= /* got */ aab		--- @ ./general_test.go:146

func LesserThan

func LesserThan[T Numeric | ~string](want T) Matcher[T]

LesserThan tests of numeric value with

want > got
Example (Float)
its.LesserThan(1.0).Match(1.1).OrError(t)
its.LesserThan(1.0).Match(1.0).OrError(t)
its.LesserThan(1.1).Match(1.0).OrError(t)
Output:

✘ /* want */ 1 > /* got */ 1.1		--- @ ./general_test.go:105

✘ /* want */ 1 > /* got */ 1		--- @ ./general_test.go:106
Example (Int)
//       >
its.LesserThan(10).Match(11).OrError(t)
its.LesserThan(11).Match(10).OrError(t)
its.LesserThan(10).Match(10).OrError(t)
Output:

✘ /* want */ 10 > /* got */ 11		--- @ ./general_test.go:95

✘ /* want */ 10 > /* got */ 10		--- @ ./general_test.go:97
Example (String)
its.LesserThan("aaa").Match("aab").OrError(t)
its.LesserThan("aaa").Match("aaa").OrError(t)
its.LesserThan("aab").Match("aaa").OrError(t)
Output:

✘ /* want */ aaa > /* got */ aab		--- @ ./general_test.go:116

✘ /* want */ aaa > /* got */ aaa		--- @ ./general_test.go:117

func Map

func Map[K comparable, V any](spec map[K]Matcher[V]) Matcher[map[K]V]
Example (Different_key)
its.ForEntries(its.Map, its.EqEq, map[string]int{
	"a": 97,
	"b": 98,
	"c": 99,
}).
	Match(map[string]int{
		"a": 97,
		"b": 98,
		"d": 99,
	}).
	OrError(t)
Output:

✘ map[string]int{... ( keys: /* got */ 3, /* want */ 3; +1, -1 )		--- @ ./map_test.go:59
    ✔ a:
        ✔ /* got */ 97 == /* want */ 97		--- @ ./map_test.go:59
    ✔ b:
        ✔ /* got */ 98 == /* want */ 98		--- @ ./map_test.go:59
    ✘ c: (not in got)
        ✘ /* got */ ?? == /* want */ 99		--- @ ./map_test.go:59
    ✘ d: (not in want)
        ✘ /* got */ 99, /* want */ ??
Example (Different_value)
its.ForEntries(its.Map, its.EqEq, map[string]int{
	"a": 97,
	"b": 98,
	"c": 99,
}).
	Match(map[string]int{
		"a": 97,
		"b": 99,
		"c": 99,
	}).
	OrError(t)
Output:

✘ map[string]int{... ( keys: /* got */ 3, /* want */ 3; +1, -1 )		--- @ ./map_test.go:36
    ✔ a:
        ✔ /* got */ 97 == /* want */ 97		--- @ ./map_test.go:36
    ✘ b:
        ✘ /* got */ 99 == /* want */ 98		--- @ ./map_test.go:36
    ✔ c:
        ✔ /* got */ 99 == /* want */ 99		--- @ ./map_test.go:36
Example (Ok)
its.Map(map[string]its.Matcher[int]{
	"a": its.EqEq(97),
	"b": its.EqEq(98),
	"c": its.EqEq(99),
}).
	Match(map[string]int{
		"a": 97,
		"b": 98,
		"c": 99,
	}).
	OrError(t)

// same as above
its.ForEntries(its.Map, its.EqEq, map[string]int{
	"a": 97,
	"b": 98,
	"c": 99,
}).
	Match(map[string]int{
		"a": 97,
		"b": 98,
		"c": 99,
	}).
	OrError(t)
Output:

func MapContaining

func MapContaining[K comparable, V any](spec map[K]Matcher[V]) Matcher[map[K]V]
Example (Different_entries)
its.ForEntries(its.MapContaining, its.EqEq, map[string]int{
	"a": 96,
	"b": 98,
	"d": 99,
}).
	Match(map[string]int{
		"a": 97,
		"b": 98,
		"c": 99,
	}).
	OrError(t)
Output:

✘ map[string]int{ ... (contain; keys /* got */ 3, /* want */ 3; -2)		--- @ ./map_test.go:124
    ✘ a:
        ✘ /* got */ 97 == /* want */ 96		--- @ ./map_test.go:124
    ✔ b:
        ✔ /* got */ 98 == /* want */ 98		--- @ ./map_test.go:124
    ✘ c: (not in want)
        ✘ /* got */ 99, /* want */ ??
    ✘ d: (not in got)
        ✘ /* got */ ?? == /* want */ 99		--- @ ./map_test.go:124
Example (Ok)
its.MapContaining(map[string]its.Matcher[int]{
	"a": its.EqEq(97),
	"b": its.EqEq(98),
	"c": its.EqEq(99),
}).
	Match(map[string]int{
		"a": 97,
		"b": 98,
		"c": 99,
	}).
	OrError(t)

// same above
its.ForEntries(its.MapContaining, its.EqEq, map[string]int{
	"a": 97,
	"b": 98,
	"c": 99,
}).
	Match(map[string]int{
		"a": 97,
		"b": 98,
		"c": 99,
	}).
	OrError(t)

// less entries is ok.
its.ForEntries(its.MapContaining, its.EqEq, map[string]int{
	"a": 97,
	"c": 99,
}).
	Match(map[string]int{
		"a": 97,
		"b": 98,
		"c": 99,
	}).
	OrError(t)
Output:

func Match added in v0.1.3

func Match[T any, M interface{ Match(T) bool }](m M) Matcher[T]

Match matches with Match(T)bool method.

Example

Match[[]byte](regexp.MustCompile(`[Mm]atcher`))
Example (Ng)
pattern := regexp.MustCompile(`^[a-z]([a-z0-9.-]+[a-z])?$`)
its.Match(pattern).Match([]byte("github.com/youta-t/its")).OrError(t)
Output:

✘ (/* want */ ^[a-z]([a-z0-9.-]+[a-z])?$).Match(/* got */ [103 105 116 104 117 98 46 99 111 109 47 121 111 117 116 97 45 116 47 105 116 115])		--- @ ./general_test.go:441
Example (Ok)
pattern := regexp.MustCompile(`^[a-z]([a-z0-9.-]+[a-z])?$`)
its.Match(pattern).Match([]byte("github.com")).OrError(t)
Output:

func MatchString added in v0.1.3

func MatchString(m interface{ MatchString(string) bool }) Matcher[string]

Match matches with Match(T)bool method.

Example

MatchString(regexp.MustCompile(`[Mm]atcher`))
Example
pattern := regexp.MustCompile(`^[a-z]([a-z0-9.-]+[a-z])?$`)
its.MatchString(pattern).Match("github.com").OrError(t)
Output:

Example (Ng)
pattern := regexp.MustCompile(`^[a-z]([a-z0-9.-]+[a-z])?$`)
its.MatchString(pattern).Match("github.com/youta-t/its").OrError(t)
Output:

✘ (/* want */ ^[a-z]([a-z0-9.-]+[a-z])?$).MatchString(/* got */ "github.com/youta-t/its")		--- @ ./general_test.go:454

func Monotonic added in v0.1.2

func Monotonic[T any](matcherFactory func(T) Matcher[T]) Matcher[T]

Monotonic tests that the new got value matches with the last got value.

Monotonis is stateful matcher. Do not use this in slice related matcher.

Example

itsNewEvent := its.Monotonic(its.After[time.Time]) t1 := time.Now() itsNewEvent.Match(t1).OrError(t) t2 := time.Now() t3 := time.Now() itsNewEvent.Match(t2).OrError(t) // pass t4 := time.Now() itsNewEvent.Match(t4).OrError(t) // pass itsNewEvent.Match(t3).OrError(t) // fail!

Args

- matcherFactory: factory function that creates new Matcher with new got.

Example
itsDictinoalyOrder := its.Monotonic(its.GreaterThan[string])

itsDictinoalyOrder.Match("apple").OrError(t)
itsDictinoalyOrder.Match("banana").OrError(t)
itsDictinoalyOrder.Match("cherry").OrError(t)
itsDictinoalyOrder.Match("bacon").OrError(t)
itsDictinoalyOrder.Match("castard").OrError(t)
Output:

✘ // monotonic		--- @ ./stateful_test.go:6
    ✔ (always pass)		--- @ ./stateful_test.go:6
    ✔ /* want */ apple < /* got */ banana		--- @ ./stateful_test.go:8
    ✔ /* want */ banana < /* got */ cherry		--- @ ./stateful_test.go:9
    ✘ /* want */ cherry < /* got */ bacon		--- @ ./stateful_test.go:10

func NaN

func NaN() Matcher[float64]

NaN tests with math.IsNaN

Example (Ng)
its.NaN().Match(42).OrError(t)
Output:

✘ math.IsNaN(/* got */ 42.000000)		--- @ ./general_test.go:383
Example (Ok)
its.NaN().Match(math.NaN()).OrError(t)
Output:

func Never

func Never[T any]() Matcher[T]

always fail.

Example
its.Never[any]().Match(struct{ Arbitary string }{}).OrError(t)
Output:

✘ (never pass)		--- @ ./general_test.go:282

func Nil added in v0.2.0

func Nil[T any]() Matcher[T]
Example (Ng_chan)
chNonNil := make(chan int)
its.Nil[chan int]().Match(chNonNil).OrError(t)
Output:

✘ (/* got */ chan int) is nil		--- @ ./general_test.go:491
Example (Ng_func)
funNonNil := func() {}
its.Nil[func()]().Match(funNonNil).OrError(t)
Output:


✘ (/* got */ func()) is nil		--- @ ./general_test.go:498
Example (Ng_interface)
its.Nil[error]().Match(errors.New("error")).OrError(t)
Output:

✘ (/* got */ error) is nil		--- @ ./general_test.go:514
Example (Ng_map)
mapNonNil := map[string]int{}
its.Nil[map[string]int]().Match(mapNonNil).OrError(t)
Output:

✘ (/* got */ map[]) is nil		--- @ ./general_test.go:521
Example (Ng_nonpointer_value)
its.Nil[any]().Match(struct{}{}).OrError(t)
its.Nil[int]().Match(3).OrError(t)
Output:

✘ (/* got */ {}) is nil		--- @ ./general_test.go:505

✘ (/* got */ 3) is nil		--- @ ./general_test.go:506
Example (Ng_slice)
sliceNonNil := []int{}
its.Nil[[]int]().Match(sliceNonNil).OrError(t)
Output:


✘ (/* got */ []) is nil		--- @ ./general_test.go:528
Example (Ng_value_ptr)
three := 3
its.Nil[*int]().Match(&three).OrError(t)
Output:

✘ (/* got */ 3) is nil		--- @ ./general_test.go:483
Example (Ok)
var none *int
its.Nil[*int]().Match(none).OrError(t)

var chNil chan int = nil
its.Nil[chan int]().Match(chNil).OrError(t)

var fnNil func()
its.Nil[func()]().Match(fnNil).OrError(t)

its.Nil[any]().Match(nil).OrError(t)
its.Nil[error]().Match(nil).OrError(t)

var mapNil map[string]int
its.Nil[map[string]int]().Match(mapNil).OrError(t)

var sliceNil []int
its.Nil[[]int]().Match(sliceNil).OrError(t)
Output:

func None added in v0.1.2

func None[T any](matchers ...Matcher[T]) Matcher[T]

None tests got value does NOT match for all given matchers.

Example (Ng)
its.None(
	its.EqEq(1),
	its.EqEq(2),
	its.EqEq(3),
).
	Match(2).
	OrError(t)
Output:

✘ // none of:		--- @ ./logical_test.go:84
    ✘ /* got */ 2 == /* want */ 1		--- @ ./logical_test.go:85
    ✔ /* got */ 2 == /* want */ 2		--- @ ./logical_test.go:86
    ✘ /* got */ 2 == /* want */ 3		--- @ ./logical_test.go:87
Example (Ok)
its.None(
	its.EqEq(1),
	its.EqEq(2),
	its.EqEq(3),
).
	Match(4).
	OrError(t)
Output:

func Not

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

Inverts matcher.

Example (Ng)
its.Not(its.EqEq(42)).Match(42).OrError(t)
Output:

✘ // not:		--- @ ./logical_test.go:66
    ✔ /* got */ 42 == /* want */ 42		--- @ ./logical_test.go:66
Example (Ok)
its.Not(its.EqEq(42)).Match(24).OrError(t)
its.Not(its.EqEq(42)).Match(35).OrError(t)
Output:

func Pointer added in v0.2.3

func Pointer[T any](m Matcher[T]) Matcher[*T]

Pointer wraps matcher as matcher to pointer.

It checks got value is not nil and matches with m.

To check got is nil, use Nil[T]().

Args

- m Matcher[T]: matcher for dereferenced value.

Example (Ng)
got := 40
its.Pointer(its.EqEq(42)).Match(&got).OrError(t)

var ptrgot *int
its.Pointer(its.EqEq(42)).Match(ptrgot).OrError(t)
Output:

✘ /* got */ *int is not nil,		--- @ ./general_test.go:543
    ✘ /* got */ 40 == /* want */ 42		--- @ ./general_test.go:543

✘ /* got */ nil is not nil,		--- @ ./general_test.go:546
    ✘ /* got */ ?? == /* want */ 42		--- @ ./general_test.go:546
Example (Ok)
got := 42
its.Pointer(its.EqEq(42)).Match(&got).OrError(t)
Output:

func Property added in v0.3.0

func Property[T, U any, D string | itskit.Label](
	description D,
	prop func(T) U,
	m Matcher[U],
) Matcher[T]

Property creates a matcher for property U calcurated from type T.

Args

- description: description of property. It can be string for a static message, or itskit.Label for a dinamic message.

- prop: calcuration extracting U from T

- m: matcher for U

Example (Ng)
got := "abcde"
its.Property(
	itskit.NewLabelWithLocation("len(%s) == 4", itskit.Got),
	func(got string) int { return len(got) },
	its.EqEq(4),
).
	Match(got).
	OrError(t)
Output:

✘ len(/* got */ abcde) == 4		--- @ ./general_test.go:699
    ✘ /* got */ 5 == /* want */ 4		--- @ ./general_test.go:701
Example (Ng_no_placeholder)
got := "abcde"
its.Property(
	"len == 4",
	func(got string) int { return len(got) },
	its.EqEq(4),
).
	Match(got).
	OrError(t)
Output:

✘ len == 4 :		--- @ ./general_test.go:712
    ✘ /* got */ 5 == /* want */ 4		--- @ ./general_test.go:715
Example (Ok)
got := "abcde"
its.Property(
	itskit.NewLabel("len(%s) == 5", itskit.Got),
	func(got string) int { return len(got) },
	its.EqEq(5),
).
	Match(got).
	OrError(t)
Output:

Example (Ok_no_placeholder)
got := "abcde"
its.Property(
	"len == 5",
	func(got string) int { return len(got) },
	its.EqEq(5),
).
	Match(got).
	OrError(t)
Output:

func Singuler added in v0.1.2

func Singuler[T any](matcherFactory func(T) Matcher[T]) Matcher[T]

Singuler tests that new got value DO NOT match with any former got values.

Singuler is stateful matcher. Do not use this in slice related matcher.

Example

// match when all got strings are different
itsUniqueId := its.Singuler(its.EqEq[string])
itsUniqueId.Match("id=a").OrError(t)
itsUniqueId.Match("id=b").OrError(t)  // pass
itsUniqueId.Match("id=c").OrError(t)  // pass
itsUniqueId.Match("id=b").OrError(t)  // fail!

Args

- matcherFactory: factory function creates a new mathcer for new got.

Example
itsUniqueId := its.Singuler(its.EqEq[string])
itsUniqueId.Match("id: a").OrError(t)
itsUniqueId.Match("id: b").OrError(t)
itsUniqueId.Match("id: c").OrError(t)
itsUniqueId.Match("id: b").OrError(t)
itsUniqueId.Match("id: e").OrError(t)
Output:

✘ //do not match with values have been gotten		--- @ ./stateful_test.go:22
    ✔ (always pass)		--- @ ./stateful_test.go:23
    ✔ // none of:		--- @ ./stateful_test.go:24
        ~ /* got */ id: b == /* want */ id: a		--- @ ./stateful_test.go:23
    ✔ // none of:		--- @ ./stateful_test.go:25
        ~ /* got */ id: c == /* want */ id: a		--- @ ./stateful_test.go:23
        ~ /* got */ id: c == /* want */ id: b		--- @ ./stateful_test.go:24
    ✘ // none of:		--- @ ./stateful_test.go:26
        ✘ /* got */ id: b == /* want */ id: a		--- @ ./stateful_test.go:23
        ✔ /* got */ id: b == /* want */ id: b		--- @ ./stateful_test.go:24
        ✘ /* got */ id: b == /* want */ id: c		--- @ ./stateful_test.go:25

func Slice

func Slice[T any](matchers ...Matcher[T]) Matcher[[]T]

Slice tests actual slice elements satisfies specs for same index.

Args

- mathcers: matchers for each element. Each matchers is tried multiple times with many element. Do not use stateful matcher.

Example (Different_length)
// fail. Actual has not enough 3.
its.ForItems(its.Slice, its.EqEq, []int{1, 2, 3, 3}).
	Match([]int{1, 2, 3}).
	OrError(t)

// fail. Actual has too much 3.
its.ForItems(its.Slice, its.EqEq, []int{1, 2, 3}).
	Match([]int{1, 2, 3, 3}).
	OrError(t)
Output:

✘ []int{ ... (len: /* got */ 3, /* want */ 4; +0, -1)		--- @ ./slice_test.go:48
    ✔ /* got */ 1 == /* want */ 1		--- @ ./slice_test.go:48
    ✔ /* got */ 2 == /* want */ 2		--- @ ./slice_test.go:48
    ✔ /* got */ 3 == /* want */ 3		--- @ ./slice_test.go:48
    ✘ - /* got */ ?? == /* want */ 3		--- @ ./slice_test.go:48

✘ []int{ ... (len: /* got */ 4, /* want */ 3; +1, -0)		--- @ ./slice_test.go:53
    ✔ /* got */ 1 == /* want */ 1		--- @ ./slice_test.go:53
    ✔ /* got */ 2 == /* want */ 2		--- @ ./slice_test.go:53
    ✔ /* got */ 3 == /* want */ 3		--- @ ./slice_test.go:53
    ✘ + /* got */ 3
Example (Different_order)
// fail. Order is matter.
its.Slice(its.EqEq(2), its.EqEq(1), its.EqEq(3)).
	Match([]int{1, 2, 3}).
	OrError(t)

// same as above
its.ForItems(its.Slice, its.EqEq, []int{2, 1, 3}).
	Match([]int{1, 2, 3}).
	OrError(t)
Output:

✘ []int{ ... (len: /* got */ 3, /* want */ 3; +1, -1)		--- @ ./slice_test.go:23
    ✘ - /* got */ ?? == /* want */ 2		--- @ ./slice_test.go:23
    ✔ /* got */ 1 == /* want */ 1		--- @ ./slice_test.go:23
    ✘ + /* got */ 2
    ✔ /* got */ 3 == /* want */ 3		--- @ ./slice_test.go:23

✘ []int{ ... (len: /* got */ 3, /* want */ 3; +1, -1)		--- @ ./slice_test.go:28
    ✘ - /* got */ ?? == /* want */ 2		--- @ ./slice_test.go:28
    ✔ /* got */ 1 == /* want */ 1		--- @ ./slice_test.go:28
    ✘ + /* got */ 2
    ✔ /* got */ 3 == /* want */ 3		--- @ ./slice_test.go:28
Example (Empty)
// pass. its empty.
its.Slice[int]().Match([]int{}).OrError(t)

// fail
its.Slice(its.EqEq(1)).Match([]int{}).OrError(t)

// fail. its empty.
its.Slice[int]().Match([]int{1}).OrError(t)
Output:

✘ []int{ ... (len: /* got */ 0, /* want */ 1; +0, -1)		--- @ ./slice_test.go:76
    ✘ - /* got */ ?? == /* want */ 1		--- @ ./slice_test.go:76

✘ []int{ ... (len: /* got */ 1, /* want */ 0; +1, -0)		--- @ ./slice_test.go:79
    ✘ + /* got */ 1
Example (Ok)
// pass
its.Slice(its.EqEq(1), its.EqEq(2), its.EqEq(3)).
	Match([]int{1, 2, 3}).
	OrError(t)

// it is same thing as above
its.ForItems(its.Slice, its.EqEq, []int{1, 2, 3}).
	Match([]int{1, 2, 3}).
	OrError(t)
Output:

func SliceUnordered

func SliceUnordered[T any](specs ...Matcher[T]) Matcher[[]T]

Set test that for each element in actual slice matches each spec.

Args

- mathcers: matchers for each element. Each matchers is tried multiple times with many element. Do not use stateful matcher.

Example (Different_length)
// fail. there is an extra item 42.
its.ForItems(its.SliceUnordered, its.EqEq, []int{1, 2, 3}).
	Match([]int{1, 2, 3, 3}).
	OrError(t)

// fail. 3 is missing.
its.ForItems(its.SliceUnordered, its.EqEq, []int{1, 2, 3}).
	Match([]int{1, 2}).
	OrError(t)
Output:

✘ []int{ ... (unordered; len: /* want */ 3, /* got */ 4; +1, -0)		--- @ ./slice_test.go:115
    ✔ /* got */ 1 == /* want */ 1		--- @ ./slice_test.go:115
    ✔ /* got */ 2 == /* want */ 2		--- @ ./slice_test.go:115
    ✔ /* got */ 3 == /* want */ 3		--- @ ./slice_test.go:115
    ✘ + /* got */ 3

✘ []int{ ... (unordered; len: /* want */ 3, /* got */ 2; +0, -1)		--- @ ./slice_test.go:120
    ✔ /* got */ 1 == /* want */ 1		--- @ ./slice_test.go:120
    ✔ /* got */ 2 == /* want */ 2		--- @ ./slice_test.go:120
    ✘ - /* got */ ?? == /* want */ 3		--- @ ./slice_test.go:120
Example (Empty)
// pass. its empty.
its.SliceUnordered[int]().Match([]int{}).OrError(t)

// fail
its.SliceUnordered(its.EqEq(1)).Match([]int{}).OrError(t)

// fail. its empty.
its.SliceUnordered[int]().Match([]int{1}).OrError(t)
Output:

✘ []int{ ... (unordered; len: /* want */ 1, /* got */ 0; +0, -1)		--- @ ./slice_test.go:142
    ✘ - /* got */ ?? == /* want */ 1		--- @ ./slice_test.go:142

✘ []int{ ... (unordered; len: /* want */ 0, /* got */ 1; +1, -0)		--- @ ./slice_test.go:145
    ✘ + /* got */ 1
Example (Ok)
// pass
its.SliceUnordered(its.EqEq(1), its.EqEq(2), its.EqEq(3)).
	Match([]int{1, 2, 3}).
	OrError(t)

// same as above
its.ForItems(its.SliceUnordered, its.EqEq, []int{1, 2, 3}).
	Match([]int{1, 2, 3}).
	OrError(t)

// pass. order is not matter.
its.SliceUnordered(its.EqEq(1), its.EqEq(2), its.EqEq(3)).
	Match([]int{3, 1, 2}).
	OrError(t)

// same as above
its.ForItems(its.SliceUnordered, its.EqEq, []int{1, 2, 3}).
	Match([]int{3, 1, 2}).
	OrError(t)
Output:

func SliceUnorderedContaining

func SliceUnorderedContaining[T any](spec ...Matcher[T]) Matcher[[]T]

SliceContainsUnordered test that actual slice contain elements satisfy each specs.

Args

- mathcers: matchers for each element. Each matchers is tried multiple times with many element. Do not use stateful matcher.

Example (Empty)
// pass. its empty.
its.SliceUnorderedContaining[int]().Match([]int{}).OrError(t)

// fail
its.SliceUnorderedContaining(its.EqEq(1)).Match([]int{}).OrError(t)

// pass.
its.SliceUnorderedContaining[int]().Match([]int{1}).OrError(t)
Output:

✘ []int{ ... (unordered, contain; len: /* got */ 0, /* want */ 1; -1)		--- @ ./slice_test.go:207
    ✘ - /* got */ ?? == /* want */ 1		--- @ ./slice_test.go:207
Example (Missing_item)
// fail. 3 is missing.
its.ForItems(its.SliceUnorderedContaining, its.EqEq, []int{1, 2, 3}).
	Match([]int{1, 2}).
	OrError(t)
Output:

✘ []int{ ... (unordered, contain; len: /* got */ 2, /* want */ 3; -1)		--- @ ./slice_test.go:191
    ✔ /* got */ 1 == /* want */ 1		--- @ ./slice_test.go:191
    ✔ /* got */ 2 == /* want */ 2		--- @ ./slice_test.go:191
    ✘ - /* got */ ?? == /* want */ 3		--- @ ./slice_test.go:191
Example (Ok)
// pass
its.SliceUnorderedContaining(its.EqEq(1), its.EqEq(2), its.EqEq(3)).
	Match([]int{1, 2, 3}).
	OrError(t)

// same as above
its.ForItems(its.SliceUnorderedContaining, its.EqEq, []int{1, 2, 3}).
	Match([]int{1, 2, 3}).
	OrError(t)

// pass. order is not matter.
its.SliceUnorderedContaining(its.EqEq(1), its.EqEq(2), its.EqEq(3)).
	Match([]int{3, 1, 2}).
	OrError(t)

// same as above
its.ForItems(its.SliceUnorderedContaining, its.EqEq, []int{1, 2, 3}).
	Match([]int{3, 1, 2}).
	OrError(t)

// pass. extra item is okay.
its.SliceUnorderedContaining(its.EqEq(1), its.EqEq(2), its.EqEq(3)).
	Match([]int{1, 2, 3, 3}).
	OrError(t)

// same as above
its.ForItems(its.SliceUnorderedContaining, its.EqEq, []int{1, 2, 3}).
	Match([]int{1, 2, 3, 3}).
	OrError(t)

// Ouptut:
Output:

func Some

func Some[T any](matchers ...Matcher[T]) Matcher[T]

All tests actual passes at least one spec.

If no matchers are given, it always fail.

Example (Ng)
its.Some(
	its.StringHavingPrefix("abc"),
	its.StringContaining("hij"),
	its.StringHavingSuffix("xyz"),
).
	Match("The quick brown fox jumps over the lazy dog").
	OrError(t)
Output:

✘ // some: (0 ok / 3 matchers)		--- @ ./logical_test.go:45
    ✘ strings.HasPrefix(/* got */ "The quick brown fox jumps over the lazy dog", /* want */ "abc")		--- @ ./logical_test.go:46
    ✘ strings.Contains(/* got */ "The quick brown fox jumps over the lazy dog", /* want */ "hij")		--- @ ./logical_test.go:47
    ✘ strings.HasSuffix(/* got */ "The quick brown fox jumps over the lazy dog", /* want */ "xyz")		--- @ ./logical_test.go:48
Example (Ok)
its.Some(
	its.StringHavingPrefix("abc"),
	its.StringContaining("hij"),
	its.StringHavingSuffix("xyz"),
).
	Match("abc...fghIJkl...xyz").
	OrError(t)
Output:

func StringContaining

func StringContaining(want string) Matcher[string]

StringContaining tests with strings.Contains

Example (Ng)
its.StringContaining("bcd").Match("adcbe").OrError(t)
Output:

✘ strings.Contains(/* got */ "adcbe", /* want */ "bcd")		--- @ ./general_test.go:316
Example (Ok)
its.StringContaining("bcd").Match("abcde").OrError(t)
Output:

func StringEqualFold

func StringEqualFold(want string) Matcher[string]

StringEqualFold tests with strings.EqualFold

Example (Ng)
its.StringEqualFold("abc").Match("αβγ").OrError(t)
Output:

✘ strings.EqualFold(/* got */ "αβγ", /* want */ "abc")		--- @ ./general_test.go:328
Example (Ok)
its.StringEqualFold("abc").Match("abc").OrError(t)
its.StringEqualFold("aBc").Match("AbC").OrError(t)
Output:

func StringHavingPrefix

func StringHavingPrefix(want string) Matcher[string]

StringHavingPrefix tests with strings.HasPrefix

Example (Ng)
its.StringHavingPrefix("abc").Match("adcbe").OrError(t)
Output:

✘ strings.HasPrefix(/* got */ "adcbe", /* want */ "abc")		--- @ ./general_test.go:293
Example (Ok)
its.StringHavingPrefix("abc").Match("abcde").OrError(t)
Output:

func StringHavingSuffix

func StringHavingSuffix(want string) Matcher[string]

StringHavingSuffix tests with strings.HasSuffix

Example (Ng)
its.StringHavingSuffix("cde").Match("adcbe").OrError(t)
Output:

✘ strings.HasSuffix(/* got */ "adcbe", /* want */ "cde")		--- @ ./general_test.go:305
Example (Ok)
its.StringHavingSuffix("cde").Match("abcde").OrError(t)
Output:

func Text added in v0.2.5

func Text(want string) Matcher[string]

Text returns a matcher for a long text.

When it get unmatch, it shows diff of text.

Example
its.Text(`
Lorem Ipsum:

    Lorem ipsum dolor sit amet,
    consectetur adipiscing elit,
    sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

    Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
    nisi ut aliquip ex ea commodo consequat.
    Duis aute irure dolor in reprehenderit in voluptate velit
    esse cillum dolore eu fugiat nulla pariatur.

    Excepteur sint occaecat cupidatat non proident,
    sunt in culpa qui officia deserunt mollit anim id est laborum.
`).Match(`
Lorem Ipsum:

    Lorem ipsum dolor sit amet,
    consectetur adipiscing elit,
    sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

    nisi ut aliquip ex ea commodo consequat.
    Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
    Duis aute irure dolor in reprehenderit in voluptate velit
    esse cillum dolore eu fugiat nulla pariatur.

    sunt in culpa qui officia deserunt mollit anim id est laborum.
    Excepteur sint occaecat cupidatat non proident,
`).OrError(t)
Output:

✘ (+ = got, - = want)		--- @ ./general_test.go:622
      |
      | Lorem Ipsum:
      |
      |     Lorem ipsum dolor sit amet,
      |     consectetur adipiscing elit,
      |     sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
      |
    - |     Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
      |     nisi ut aliquip ex ea commodo consequat.
    + |     Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
      |     Duis aute irure dolor in reprehenderit in voluptate velit
      |     esse cillum dolore eu fugiat nulla pariatur.
      |
    - |     Excepteur sint occaecat cupidatat non proident,
      |     sunt in culpa qui officia deserunt mollit anim id est laborum.
    + |     Excepteur sint occaecat cupidatat non proident,
      |
Example (Ascii)
// London Bridge Is Falling Down
its.Text(`
Build it up with bricks and mortar,
Bricks and mortar, bricks and mortar,
Build it up with bricks and mortar,
My fair lady.
`).Match(`
Build it up with iron and steel,
Iron and steel, iron and steel,
Build it up with iron and steel,
My fair lady.
`).OrError(t)
Output:


✘ (+ = got, - = want)		--- @ ./general_test.go:596
      |
    - | Build it up with bricks and mortar,
    - | Bricks and mortar, bricks and mortar,
    - | Build it up with bricks and mortar,
    + | Build it up with iron and steel,
    + | Iron and steel, iron and steel,
    + | Build it up with iron and steel,
      | My fair lady.
      |
Example (Multibytes)
// "風景" (純銀もざいく; 山村暮鳥): https://www.aozora.gr.jp/cards/000136/files/52348_42039.html
its.Text(`
いちめんのなのはな
いちめんのなのはな
いちめんのなのはな
いちめんのなのはな
いちめんのなのはな
いちめんのなのはな
いちめんのなのはな
かすかなるむぎぶえ
いちめんのなのはな
`).Match(`
いちめんのなのはな
いちめんのなのはな
いちめんのなのはな
いちめんのなのはな
いちめんのなのはな
いちめんのなのはな
いちめんのなのはな
ひばりのおしやべり
いちめんのなのはな
`).OrError(t)
Output:

✘ (+ = got, - = want)		--- @ ./general_test.go:557
      |
      | いちめんのなのはな
      | いちめんのなのはな
      | いちめんのなのはな
      | いちめんのなのはな
      | いちめんのなのはな
      | いちめんのなのはな
      | いちめんのなのはな
    - | かすかなるむぎぶえ
    + | ひばりのおしやべり
      | いちめんのなのはな
      |

func Type

func Type[T any]() Matcher[any]

Type tests got value is a type.

Example (Non_primitive)
type T struct {
	Foo int
}
type U struct {
	Bar int
}
its.Type[T]().Match(T{Foo: 42}).OrError(t)
its.Type[U]().Match(T{Foo: 42}).OrError(t)
Output:

✘ /* got */ {Foo:42} is a its_test.U		--- @ ./general_test.go:428
Example (Primitive)
its.Type[string]().Match("text value").OrError(t)
its.Type[string]().Match(42).OrError(t)
Output:

✘ /* got */ 42 is a string		--- @ ./general_test.go:415

type Numeric

type Numeric interface {
	Integer | Float
}

Jump to

Keyboard shortcuts

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