update values
This commit is contained in:
@@ -24,6 +24,7 @@ func Equalf(t *testing.T, expected any, actual any, msg string, args ...any) boo
|
||||
return true
|
||||
}
|
||||
|
||||
t.Errorf("not equal expect:%#v acctual: %#v", expected, actual)
|
||||
t.Errorf(msg, args...)
|
||||
|
||||
return false
|
||||
|
||||
@@ -24,7 +24,7 @@ func Run(t *testing.T, provider config.Provider, read []Read) {
|
||||
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.Error(t, err)
|
||||
read.Assert(t, val)
|
||||
})
|
||||
}
|
||||
@@ -33,6 +33,7 @@ func Run(t *testing.T, provider config.Provider, read []Read) {
|
||||
type Read struct {
|
||||
Key []string
|
||||
Assert func(t *testing.T, v config.Value)
|
||||
Error func(t *testing.T, ex error)
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
@@ -57,6 +58,14 @@ func NewReadUnmarshal(expected, target any, key ...string) Read {
|
||||
require.NoErrorf(t, v.Unmarshal(target), "unmarshal")
|
||||
require.Equal(t, expected, target, "unmarshal")
|
||||
},
|
||||
Error: exError(key...),
|
||||
}
|
||||
}
|
||||
|
||||
func exError(path ...string) func(t *testing.T, err error) {
|
||||
return func(t *testing.T, err error) {
|
||||
t.Helper()
|
||||
require.NoError(t, err, path)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,7 +78,8 @@ func Time(value string) time.Time {
|
||||
// NewRead test data.
|
||||
func NewRead(expected any, key ...string) Read {
|
||||
return Read{
|
||||
Key: key,
|
||||
Key: key,
|
||||
Error: exError(key...),
|
||||
Assert: func(t *testing.T, v config.Value) {
|
||||
t.Helper()
|
||||
|
||||
@@ -111,9 +121,20 @@ func NewRead(expected any, key ...string) Read {
|
||||
require.Fail(t, "unexpected type:%+T", expected)
|
||||
}
|
||||
|
||||
require.Equalf(t, val, short, "type:%T", expected)
|
||||
require.NoErrorf(t, err, "type:%T", expected)
|
||||
require.Equalf(t, expected, val, "type:%T", expected)
|
||||
require.Equalf(t, val, short, "%q!=%q, type:%T", val, short, expected)
|
||||
require.NoErrorf(t, err, "err:%v type:%T", err, expected)
|
||||
require.Equalf(t, expected, val, "%q!=%q type:%T", expected, val, expected)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func NewErrorIs(exErr error, path ...string) Read {
|
||||
return Read{
|
||||
Key: path,
|
||||
Assert: func(*testing.T, config.Value) {},
|
||||
Error: func(t *testing.T, err error) {
|
||||
t.Helper()
|
||||
require.ErrorIsf(t, exErr, err, "except err %v != %v", exErr, err)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,5 @@ func Equalf(t *testing.T, expected any, actual any, msg string, args ...any) {
|
||||
return
|
||||
}
|
||||
|
||||
t.Errorf(msg, args...)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ func NoError(t *testing.T, err error, msgAndArgs ...any) {
|
||||
t.Helper()
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("no error got:%v", err)
|
||||
t.Error(msgAndArgs...)
|
||||
t.FailNow()
|
||||
}
|
||||
@@ -26,8 +27,18 @@ func NoErrorf(t *testing.T, err error, msg string, args ...any) {
|
||||
func ErrorIs(t *testing.T, err, ex error, msgAndArgs ...any) {
|
||||
t.Helper()
|
||||
|
||||
if errors.Is(ex, err) {
|
||||
if !errors.Is(err, ex) {
|
||||
t.Errorf("expect:%#v got:%#v", ex, err)
|
||||
t.Error(msgAndArgs...)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func ErrorIsf(t *testing.T, err, ex error, msg string, args ...any) {
|
||||
t.Helper()
|
||||
|
||||
if !errors.Is(ex, err) {
|
||||
t.Errorf(msg, args...)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,14 +72,6 @@ func ParseBool(s string) (bool, error) {
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func JUnmarshal(b []byte, v any) error {
|
||||
if err := json.Unmarshal(b, v); err != nil {
|
||||
return fmt.Errorf("%w: %w", config.ErrInvalidValue, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func ParseUint64(s string) (uint64, error) {
|
||||
i, err := strconv.ParseUint(s, 10, 64)
|
||||
if err != nil {
|
||||
@@ -88,3 +80,17 @@ func ParseUint64(s string) (uint64, error) {
|
||||
|
||||
return i, nil
|
||||
}
|
||||
|
||||
func JUnmarshal(b []byte, v any) error {
|
||||
if err := json.Unmarshal(b, v); err != nil {
|
||||
return fmt.Errorf("%w: %w", config.ErrInvalidValue, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func JParce[T any](b []byte) (T, error) {
|
||||
var data T
|
||||
|
||||
return data, JUnmarshal(b, &data)
|
||||
}
|
||||
|
||||
@@ -15,39 +15,41 @@ func (s JBytes) Unmarshal(v any) error {
|
||||
}
|
||||
|
||||
func (s JBytes) ParseString() (string, error) {
|
||||
return s.String(), nil
|
||||
var data string
|
||||
|
||||
return data, s.Unmarshal(&data)
|
||||
}
|
||||
|
||||
func (s JBytes) ParseInt() (int, error) {
|
||||
return Atoi(s.String())
|
||||
return JParce[int](s.Bytes())
|
||||
}
|
||||
|
||||
func (s JBytes) ParseInt64() (int64, error) {
|
||||
return ParseInt64(s.String())
|
||||
return JParce[int64](s.Bytes())
|
||||
}
|
||||
|
||||
func (s JBytes) ParseUint() (uint, error) {
|
||||
return ParseUint(s.String())
|
||||
return JParce[uint](s.Bytes())
|
||||
}
|
||||
|
||||
func (s JBytes) ParseUint64() (uint64, error) {
|
||||
return ParseUint64(s.String())
|
||||
return JParce[uint64](s.Bytes())
|
||||
}
|
||||
|
||||
func (s JBytes) ParseFloat64() (float64, error) {
|
||||
return ParseFloat(s.String())
|
||||
return JParce[float64](s.Bytes())
|
||||
}
|
||||
|
||||
func (s JBytes) ParseBool() (bool, error) {
|
||||
return ParseBool(s.String())
|
||||
return JParce[bool](s.Bytes())
|
||||
}
|
||||
|
||||
func (s JBytes) ParseDuration() (time.Duration, error) {
|
||||
return ParseDuration(s.String())
|
||||
return JParce[time.Duration](s.Bytes())
|
||||
}
|
||||
|
||||
func (s JBytes) ParseTime() (time.Time, error) {
|
||||
return ParseTime(s.String())
|
||||
return JParce[time.Time](s.Bytes())
|
||||
}
|
||||
|
||||
func (s JBytes) Bytes() []byte {
|
||||
@@ -55,7 +57,9 @@ func (s JBytes) Bytes() []byte {
|
||||
}
|
||||
|
||||
func (s JBytes) String() string {
|
||||
return string(s)
|
||||
data, _ := s.ParseString()
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
func (s JBytes) Int() int {
|
||||
@@ -107,7 +111,7 @@ func (s JBytes) Time() time.Time {
|
||||
}
|
||||
|
||||
func (s JBytes) Any() any {
|
||||
return s
|
||||
return s.Bytes()
|
||||
}
|
||||
|
||||
func (s JBytes) IsEquals(in config.Value) bool {
|
||||
|
||||
42
value/jbytes_test.go
Normal file
42
value/jbytes_test.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package value_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/test/require"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
)
|
||||
|
||||
func TestJBytes_String(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
data := value.JBytes([]byte("\"data\""))
|
||||
res, err := data.ParseString()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, res, "data")
|
||||
|
||||
dataErr := value.JBytes([]byte("data"))
|
||||
res2, err2 := dataErr.ParseString()
|
||||
require.ErrorIs(t, err2, config.ErrInvalidValue)
|
||||
require.Equal(t, "", res2)
|
||||
}
|
||||
|
||||
func TestJBytes_Unmarshal(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
data, err := json.Marshal([]string{"test1", "test2"})
|
||||
require.NoError(t, err)
|
||||
|
||||
sta := value.JBytes(data)
|
||||
|
||||
ac := ""
|
||||
|
||||
aca := []string{}
|
||||
require.NoError(t, sta.Unmarshal(&aca))
|
||||
require.Equal(t, []string{"test1", "test2"}, aca)
|
||||
|
||||
require.ErrorIs(t, sta.Unmarshal(ac), config.ErrInvalidValue)
|
||||
require.ErrorIs(t, sta.Unmarshal(&ac), config.ErrInvalidValue)
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package value_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
@@ -14,21 +13,12 @@ func TestStringUnmarshal(t *testing.T) {
|
||||
|
||||
st := value.String("test")
|
||||
|
||||
data, err := json.Marshal([]string{"test1", "test2"})
|
||||
require.NoError(t, err)
|
||||
|
||||
sta := value.JBytes(data)
|
||||
|
||||
ac := ""
|
||||
require.NoError(t, st.Unmarshal(&ac))
|
||||
require.Equal(t, "test", ac)
|
||||
|
||||
aca := []string{}
|
||||
require.NoError(t, sta.Unmarshal(&aca))
|
||||
require.Equal(t, []string{"test1", "test2"}, aca)
|
||||
|
||||
require.ErrorIs(t, sta.Unmarshal(ac), config.ErrWrongType)
|
||||
require.ErrorIs(t, sta.Unmarshal(&ac), config.ErrWrongType)
|
||||
require.ErrorIs(t, st.Unmarshal(aca), config.ErrWrongType)
|
||||
require.ErrorIs(t, st.Unmarshal(&aca), config.ErrWrongType)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user