shad-go/testequal/README.md

45 lines
1.5 KiB
Markdown
Raw Normal View History

2020-03-20 23:56:58 +00:00
## testequal
2020-03-21 00:12:06 +00:00
В этой задаче нужно реализовать 4 test helper'а, аналогичных функциям из [testify](https://github.com/stretchr/testify):
2020-03-20 23:56:58 +00:00
```
func AssertEqual(t T, expected, actual interface{}, msgAndArgs ...interface{}) bool
2020-03-26 18:18:12 +00:00
func AssertNotEqual(t T, expected, actual interface{}, msgAndArgs ...interface{}) bool
2020-03-20 23:56:58 +00:00
func RequireEqual(t T, expected, actual interface{}, msgAndArgs ...interface{})
func RequireNotEqual(t T, expected, actual interface{}, msgAndArgs ...interface{})
```
2020-03-21 00:32:54 +00:00
Функции проверяют на равенство expected и actual и завершают тест, если проверка не прошла.
msgAndArgs попадают в описание ошибки через fmt.Sprintf.
2020-03-21 00:12:06 +00:00
Пример использования:
2020-03-20 23:56:58 +00:00
```
func TestMath(t *testing.T) {
AssertEqual(t, 1, 2, "1 == 2")
}
```
2020-03-21 00:12:06 +00:00
вывод теста:
2020-03-20 23:56:58 +00:00
```
=== RUN TestMath
--- FAIL: TestMath (0.00s)
2020-03-21 00:12:06 +00:00
math_test.go:43: not equal:
2020-03-20 23:56:58 +00:00
expected: 1
actual : 2
message : 1 == 2
FAIL
2020-03-21 00:12:06 +00:00
FAIL gitlab.com/slon/shad-go/testequal 0.003s
2020-03-20 23:56:58 +00:00
FAIL
```
В отличие от testify реализация ограничивает набор типов, с которыми умеет работать:
1. Целые числа: int, in64 и др (см. тесты)
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