ggassert

package module
v0.0.0-...-ff246d0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2022 License: MIT Imports: 3 Imported by: 0

README

gg-assert

gg-assert is a deadly simple & easy assertion library for golang.

It's also using generics supported since Go 1.18 later.

Requirement

go 1.18 later

Usage

ggassert.Equal(t, 2, 2, "failed") // pass
ggassert.Equal(t, 1, 2, "failed") // failed
// ggassert.Equal(t, 1, "aaa", "failed") // compile error: default type string of "aaa" does not match inferred type int for T

ggassert.LessThan(t, 1, 2, "failed")        // pass
ggassert.LessThan(t, 2, 2, "failed")        // failed
ggassert.LessThanOrEqual(t, 2, 2, "failed") // pass
// ggassert.LessThanOrEqual(t, 2, 2.0, "failed") // compile error: default type float64 of 2.0 does not match inferred type int for T

ggassert.GreaterThan(t, 2, 1, "failed")        // pass
ggassert.GreaterThan(t, 2, 2, "failed")        // failed
ggassert.GreaterThanOrEqual(t, 2, 2, "failed") // pass
// ggassert.GreaterThanOrEqual(t, 2, 2.0, "failed") // compile error: default type float64 of 2.0 does not match inferred type int for T

ggassert.ContainsSlice(t, []int{1, 2, 3}, 2, "failed") // pass
ggassert.ContainsSlice(t, []int{1, 2, 3}, 4, "failed") // failed
// ggassert.ContainsSlice(t, []int{1, 2, 3}, "aaa", "failed") // compile error: cannot use "aaa" (untyped string constant) as int value in argument to ggassert.ContainsSlice

m := map[string]int{
  "aaa": 111,
  "bbb": 222,
  "ccc": 333,
}

ggassert.ContainsMapKey(t, m, "aaa", "failed") // pass
ggassert.ContainsMapKey(t, m, "ddd", "failed") // failed
ggassert.ContainsMapValue(t, m, 111, "failed") // pass
ggassert.ContainsMapValue(t, m, 444, "failed") // failed

// ggassert.ContainsMapKey(t, m, 111, "failed")     // compile error: cannot use 111 (untyped int constant) as string value in argument to ggassert.ContainsMapKey
// ggassert.ContainsMapValue(t, m, "aaa", "failed") // compile error: cannot use "aaa" (untyped string constant) as int value in argument to ggassert.ContainsMapValue

Installation

go get github.com/thara/ggassert

License

MIT

Author

Tomochika Hara (a.k.a thara)

Documentation

Overview

Example
package main

import (
	"testing"

	"github.com/thara/ggassert"
)

func main() {
	t := new(testing.T)

	ggassert.Equal(t, 2, 2, "failed") // pass
	ggassert.Equal(t, 1, 2, "failed") // failed
	// ggassert.Equal(t, 1, "aaa", "failed") // compile error: default type string of "aaa" does not match inferred type int for T

	ggassert.LessThan(t, 1, 2, "failed")        // pass
	ggassert.LessThan(t, 2, 2, "failed")        // failed
	ggassert.LessThanOrEqual(t, 2, 2, "failed") // pass
	// ggassert.LessThanOrEqual(t, 2, 2.0, "failed") // compile error: default type float64 of 2.0 does not match inferred type int for T

	ggassert.GreaterThan(t, 2, 1, "failed")        // pass
	ggassert.GreaterThan(t, 2, 2, "failed")        // failed
	ggassert.GreaterThanOrEqual(t, 2, 2, "failed") // pass
	// ggassert.GreaterThanOrEqual(t, 2, 2.0, "failed") // compile error: default type float64 of 2.0 does not match inferred type int for T

	ggassert.ContainsSlice(t, []int{1, 2, 3}, 2, "failed") // pass
	ggassert.ContainsSlice(t, []int{1, 2, 3}, 4, "failed") // failed
	// ggassert.ContainsSlice(t, []int{1, 2, 3}, "aaa", "failed") // compile error: cannot use "aaa" (untyped string constant) as int value in argument to ggassert.ContainsSlice

	m := map[string]int{
		"aaa": 111,
		"bbb": 222,
		"ccc": 333,
	}

	ggassert.ContainsMapKey(t, m, "aaa", "failed") // pass
	ggassert.ContainsMapKey(t, m, "ddd", "failed") // failed
	ggassert.ContainsMapValue(t, m, 111, "failed") // pass
	ggassert.ContainsMapValue(t, m, 444, "failed") // failed

	// ggassert.ContainsMapKey(t, m, 111, "failed")     // compile error: cannot use 111 (untyped int constant) as string value in argument to ggassert.ContainsMapKey
	// ggassert.ContainsMapValue(t, m, "aaa", "failed") // compile error: cannot use "aaa" (untyped string constant) as int value in argument to ggassert.ContainsMapValue

}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContainsMapKey

func ContainsMapKey[K comparable, V any](t testing.TB, target map[K]V, expectedKey K, format string, args ...any)

ContainsMapKey asserts that the map contains the specified key.

func ContainsMapValue

func ContainsMapValue[K, V comparable](t testing.TB, target map[K]V, expectedValue V, format string, args ...any)

ContainsMapValue asserts that the map contains the specified value.

func ContainsSlice

func ContainsSlice[T comparable](t testing.TB, s []T, expected T, format string, args ...any)

ContainsSlice asserts that the slice contains the specified value.

func Equal

func Equal[T any](t testing.TB, expected, actual T, format string, args ...any)

Equal asserts that two any values are equal.

func GreaterThan

func GreaterThan[T constraints.Ordered](t testing.TB, a, b T, format string, args ...any)

GreaterThan asserts that the first operand is greater than the second one

func GreaterThanOrEqual

func GreaterThanOrEqual[T constraints.Ordered](t testing.TB, a, b T, format string, args ...any)

GreaterThanOrEqual asserts that the first operand is greater than or equal the second one

func LessThan

func LessThan[T constraints.Ordered](t testing.TB, a, b T, format string, args ...any)

LessThan asserts that the first operand is less than the second one

func LessThanOrEqual

func LessThanOrEqual[T constraints.Ordered](t testing.TB, a, b T, format string, args ...any)

LessThanOrEqual asserts that the first operand is less than or equal the second one

Types

This section is empty.

Jump to

Keyboard shortcuts

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