This commit is contained in:
@@ -2,14 +2,12 @@ package test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/test/require"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -21,17 +19,17 @@ const (
|
||||
func Run(t *testing.T, provider config.Provider, read []Read) {
|
||||
t.Helper()
|
||||
|
||||
prov := &ProviderSuite{
|
||||
provider: provider,
|
||||
read: read,
|
||||
}
|
||||
suite.Run(t, prov)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
for idx, read := range read {
|
||||
t.Run(fmt.Sprintf("%v:%v", idx, read.Key), func(t *testing.T) {
|
||||
val, err := provider.Value(ctx, read.Key...)
|
||||
require.NoError(t, err, read.Key)
|
||||
read.Assert(t, val)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
type ProviderSuite struct {
|
||||
suite.Suite
|
||||
provider config.Provider
|
||||
read []Read
|
||||
}
|
||||
|
||||
type Read struct {
|
||||
@@ -110,7 +108,7 @@ func NewRead(expected interface{}, key ...string) Read {
|
||||
val, err = v.ParseTime()
|
||||
short = v.Time()
|
||||
default:
|
||||
require.Fail(t, "unexpected type", "type:%+T", expected)
|
||||
require.Fail(t, "unexpected type:%+T", expected)
|
||||
}
|
||||
|
||||
require.Equalf(t, val, short, "type:%T", expected)
|
||||
@@ -119,22 +117,3 @@ func NewRead(expected interface{}, key ...string) Read {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (ps *ProviderSuite) TestReadKeys() {
|
||||
ctx := context.Background()
|
||||
|
||||
for _, read := range ps.read {
|
||||
val, err := ps.provider.Value(ctx, read.Key...)
|
||||
require.NoError(ps.T(), err, read.Key)
|
||||
read.Assert(ps.T(), val)
|
||||
}
|
||||
}
|
||||
|
||||
func LoadConfig(t *testing.T, path string) []byte {
|
||||
t.Helper()
|
||||
|
||||
file, err := ioutil.ReadFile(filepath.Clean(path))
|
||||
require.NoError(t, err)
|
||||
|
||||
return file
|
||||
}
|
||||
|
||||
28
test/require/equal.go
Normal file
28
test/require/equal.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package require
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Equal(t *testing.T, expected interface{}, actual interface{}, msgAndArgs ...interface{}) {
|
||||
t.Helper()
|
||||
|
||||
if reflect.DeepEqual(expected, actual) {
|
||||
return
|
||||
}
|
||||
|
||||
t.Error(msgAndArgs...)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
func Equalf(t *testing.T, expected interface{}, actual interface{}, msg string, args ...interface{}) {
|
||||
t.Helper()
|
||||
|
||||
if reflect.DeepEqual(expected, actual) {
|
||||
return
|
||||
}
|
||||
|
||||
t.Errorf(msg, args...)
|
||||
t.FailNow()
|
||||
}
|
||||
23
test/require/error.go
Normal file
23
test/require/error.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package require
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func NoError(t *testing.T, err error, msgAndArgs ...interface{}) {
|
||||
t.Helper()
|
||||
|
||||
if err != nil {
|
||||
t.Error(msgAndArgs...)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func NoErrorf(t *testing.T, err error, msg string, args ...interface{}) {
|
||||
t.Helper()
|
||||
|
||||
if err != nil {
|
||||
t.Errorf(msg, args...)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
9
test/require/fail.go
Normal file
9
test/require/fail.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package require
|
||||
|
||||
import "testing"
|
||||
|
||||
func Fail(t *testing.T, msg string, args ...interface{}) {
|
||||
t.Helper()
|
||||
t.Errorf(msg, args...)
|
||||
t.FailNow()
|
||||
}
|
||||
14
test/require/true.go
Normal file
14
test/require/true.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package require
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Truef(t *testing.T, value bool, msg string, args ...interface{}) {
|
||||
t.Helper()
|
||||
|
||||
if !value {
|
||||
t.Errorf(msg, args...)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user