shad-go/testequal
2024-06-05 20:36:34 +03:00
..
assertions.go go fix old build tags. 2022-03-15 23:49:56 +03:00
assertions_test.go [testequal] Simplify nil slice and map construction. 2022-03-29 02:55:30 +03:00
helper_test.go Move error message tests from helper test to separate test. 2020-03-22 06:35:22 +03:00
README.md Updated to the most recent version, which uses go 1.22 2024-06-05 20:36:34 +03:00
t.go go fix old build tags. 2022-03-15 23:49:56 +03:00

testequal

В этой задаче нужно реализовать 4 test helper'а, аналогичных функциям из testify:

func AssertEqual(t T, expected, actual interface{}, msgAndArgs ...interface{}) bool
func AssertNotEqual(t T, expected, actual interface{}, msgAndArgs ...interface{}) bool
func RequireEqual(t T, expected, actual interface{}, msgAndArgs ...interface{})
func RequireNotEqual(t T, expected, actual interface{}, msgAndArgs ...interface{})

Функции проверяют на равенство expected и actual и завершают тест, если проверка не прошла. msgAndArgs попадают в описание ошибки через fmt.Sprintf.

Пример использования:

func TestMath(t *testing.T) {
	AssertEqual(t, 1, 2, "1 == 2")
}

вывод теста:

=== RUN   TestMath
--- FAIL: TestMath (0.00s)
    math_test.go:43: not equal:
        expected: 1
        actual  : 2
        message : 1 == 2
FAIL
FAIL    gitlab.com/slon/shad-go/testequal 0.003s
FAIL

В отличие от testify реализация ограничивает набор типов, с которыми умеет работать:

  1. Целые числа: int, int64 и др (см. тесты)
  2. string
  3. map[string]string
  4. []int
  5. []byte

Ссылки

  1. testing.T: https://golang.org/pkg/testing/#T
  2. type assertions: https://golang.org/doc/effective_go.html#interface_conversions