test

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2022 License: Unlicense Imports: 2 Imported by: 0

README

Testing tools for APItalist

This repository provides basic testing tools for APItalist.

Import

You can import this library by typing:

go get github.com/apitalist/test

Documentation

For documentation please see pkg.go.dev/github.com/apitalist/test.

License

APItalist is licensed under the Unlicense and released under public domain.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertEquals

func AssertEquals[T comparable](t *testing.T, value, expected T, message string, args ...interface{})

AssertEquals tests if the value is equal to the expected value.

For example:

func TestAssertEquals(t *testing.T) {
   a := "c"
   b := "c"
   test.AssertEquals(t, a, b, "%s should be equal to %s", a, b)
}

Comparing structs:

When comparing structs, the AssertEquals function behaves as expected as long as the structs are not referenced by pointers. For example:

func TestAssertEquals(t *testing.T) {
   a := myTestStruct{}
   b := myTestStruct{}
   test.AssertEquals(t, a, b, "%v should be equal to %v", a, b)
}

However, it won't work with pointers as the pointer addresses are not equal:

func TestAssertEquals(t *testing.T) {
   a := &myTestStruct{}
   b := &myTestStruct{}
   // This will fail
   test.AssertEquals(t, a, b, "%v should be equal to %v", a, b)
}

Similarly, other assertions involving pointers will only succeed if the pointers point to the same address. If you want to compare the objects

func AssertEqualsObject

func AssertEqualsObject[T any, K lang.Equals[T]](
	t *testing.T,
	value K,
	expected T,
	message string,
	args ...interface{},
)

AssertEqualsObject compares two objects using the Equals interface.

Example:

type testObject struct {
   n int
}

func (e *testObject) Equals(other *testObject) bool {
   return e.n == other.n
}

func TestAssertEqualsObject(t *testing.T) {
   a := &testObject{
       1,
   }
   b := &testObject{
       1,
   }
   test.AssertEqualsObject(t, a, b, "The two test objects should be the same.")
}

func AssertInSlice

func AssertInSlice[T lang.Ordered](t *testing.T, search T, slice []T, message string, args ...interface{})

AssertInSlice searches for the search argument in the provided slice and fails if the element is not found.

For example:

func TestAssertInSlice(t *testing.T) {
   slice := []string{"a", "b", "c"}
   element := "b"
   test.AssertInSlice(t, element, slice, "%s should be in %s", element, strings.Join(slice, ","))
}

func AssertLargerThan

func AssertLargerThan[T lang.Ordered](t *testing.T, value, expected T, message string, args ...interface{})

AssertLargerThan tests if the specified value is larger than the expected value.

For example:

func TestAssertLargerThan(t *testing.T) {
   actual := 3
   expected := 2
   test.AssertLargerThan(t, actual, expected, "Expected %d to be larger than %d", actual, expected)
}

func AssertLargerThanOrEqualTo

func AssertLargerThanOrEqualTo[T lang.Ordered](t *testing.T, value, expected T, message string, args ...interface{})

AssertLargerThanOrEqualTo tests if the specified value is larger than or equal to the expected value.

For example:

func TestAssertLargerOrEqualThan(t *testing.T) {
   actual := 3
   expected := 2
   test.AssertLargerThanOrEqualTo(t, actual, expected, "Expected %d to be larger than %d", actual, expected)
}

func AssertSmallerThan

func AssertSmallerThan[T lang.Ordered](t *testing.T, value, expected T, message string, args ...interface{})

AssertSmallerThan tests if the specified value is smaller than the expected value.

For example:

func TestAssertSmallerThan(t *testing.T) {
    actual   := 1
    expected := 2
    test.AssertSmallerThan(t, actual, expected, "Expected %d to be smaller than %d", actual, expected)
}

func AssertSmallerThanOrEqualTo

func AssertSmallerThanOrEqualTo[T lang.Ordered](t *testing.T, value, expected T, message string, args ...interface{})

AssertSmallerThanOrEqualTo tests if the specified value is smaller than or equal to the expected value.

For example:

func AssertSmallerThanOrEqualTo(t *testing.T) {
    actual   := 1
    expected := 2
    test.AssertSmallerThanOrEqualTo(t, actual, expected, "Expected %d to be smaller than %d", actual, expected)
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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