Skip to content

Commit

Permalink
minimum assert
Browse files Browse the repository at this point in the history
  • Loading branch information
ktong committed Nov 12, 2023
1 parent 4ffdd10 commit b3eba21
Show file tree
Hide file tree
Showing 25 changed files with 216 additions and 209 deletions.
19 changes: 9 additions & 10 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import (
"sync"
"testing"

"github.com/stretchr/testify/require"

"github.com/ktong/konf"
"github.com/ktong/konf/internal/assert"
)

func BenchmarkNew(b *testing.B) {
Expand All @@ -24,13 +23,13 @@ func BenchmarkNew(b *testing.B) {
b.StopTimer()

konf.SetGlobal(config)
require.NoError(b, err)
require.Equal(b, "v", konf.Get[string]("k"))
assert.NoError(b, err)
assert.Equal(b, "v", konf.Get[string]("k"))
}

func BenchmarkGet(b *testing.B) {
config, err := konf.New(konf.WithLoader(mapLoader{"k": "v"}))
require.NoError(b, err)
assert.NoError(b, err)
konf.SetGlobal(config)
b.ResetTimer()

Expand All @@ -40,17 +39,17 @@ func BenchmarkGet(b *testing.B) {
}
b.StopTimer()

require.Equal(b, "v", value)
assert.Equal(b, "v", value)
}

func BenchmarkWatch(b *testing.B) {
watcher := mapWatcher(make(chan map[string]any))
config, err := konf.New(konf.WithLoader(watcher))
require.NoError(b, err)
assert.NoError(b, err)
konf.SetGlobal(config)

cfg := konf.Get[string]("config")
require.Equal(b, "string", cfg)
assert.Equal(b, "string", cfg)
var waitGroup sync.WaitGroup
waitGroup.Add(b.N)
ctx, cancel := context.WithCancel(context.Background())
Expand All @@ -61,7 +60,7 @@ func BenchmarkWatch(b *testing.B) {

cfg = konf.Get[string]("config")
})
require.NoError(b, err)
assert.NoError(b, err)
}()
b.ResetTimer()

Expand All @@ -71,5 +70,5 @@ func BenchmarkWatch(b *testing.B) {
waitGroup.Wait()
b.StopTimer()

require.Equal(b, "changed", cfg)
assert.Equal(b, "changed", cfg)
}
55 changes: 27 additions & 28 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import (
"sync"
"testing"

"github.com/stretchr/testify/require"

"github.com/ktong/konf"
"github.com/ktong/konf/internal/assert"
)

func TestConfig_Unmarshal(t *testing.T) {
Expand All @@ -26,26 +25,26 @@ func TestConfig_Unmarshal(t *testing.T) {
description: "empty values",
assert: func(config *konf.Config) {
var cfg string
require.NoError(t, config.Unmarshal("config", &cfg))
require.Equal(t, "", cfg)
assert.NoError(t, config.Unmarshal("config", &cfg))
assert.Equal(t, "", cfg)
},
},
{
description: "nil loader",
opts: []konf.Option{konf.WithLoader(nil)},
assert: func(config *konf.Config) {
var cfg string
require.NoError(t, config.Unmarshal("config", &cfg))
require.Equal(t, "", cfg)
assert.NoError(t, config.Unmarshal("config", &cfg))
assert.Equal(t, "", cfg)
},
},
{
description: "for primary type",
opts: []konf.Option{konf.WithLoader(mapLoader{"config": "string"})},
assert: func(config *konf.Config) {
var cfg string
require.NoError(t, config.Unmarshal("config", &cfg))
require.Equal(t, "string", cfg)
assert.NoError(t, config.Unmarshal("config", &cfg))
assert.Equal(t, "string", cfg)
},
},
{
Expand All @@ -55,8 +54,8 @@ func TestConfig_Unmarshal(t *testing.T) {
var cfg struct {
Config string
}
require.NoError(t, config.Unmarshal("", &cfg))
require.Equal(t, "struct", cfg.Config)
assert.NoError(t, config.Unmarshal("", &cfg))
assert.Equal(t, "struct", cfg.Config)
},
},
{
Expand All @@ -72,8 +71,8 @@ func TestConfig_Unmarshal(t *testing.T) {
},
assert: func(config *konf.Config) {
var cfg string
require.NoError(t, config.Unmarshal("config.nest", &cfg))
require.Equal(t, "string", cfg)
assert.NoError(t, config.Unmarshal("config.nest", &cfg))
assert.Equal(t, "string", cfg)
},
},
{
Expand All @@ -90,8 +89,8 @@ func TestConfig_Unmarshal(t *testing.T) {
},
assert: func(config *konf.Config) {
var cfg string
require.NoError(t, config.Unmarshal("config_nest", &cfg))
require.Equal(t, "string", cfg)
assert.NoError(t, config.Unmarshal("config_nest", &cfg))
assert.Equal(t, "string", cfg)
},
},
{
Expand All @@ -107,8 +106,8 @@ func TestConfig_Unmarshal(t *testing.T) {
},
assert: func(config *konf.Config) {
var cfg string
require.NoError(t, config.Unmarshal("config.nest", &cfg))
require.Equal(t, "", cfg)
assert.NoError(t, config.Unmarshal("config.nest", &cfg))
assert.Equal(t, "", cfg)
},
},
{
Expand All @@ -118,8 +117,8 @@ func TestConfig_Unmarshal(t *testing.T) {
},
assert: func(config *konf.Config) {
var configured bool
require.NoError(t, config.Unmarshal("configured", &configured))
require.True(t, configured)
assert.NoError(t, config.Unmarshal("configured", &configured))
assert.True(t, configured)
},
},
}
Expand All @@ -131,7 +130,7 @@ func TestConfig_Unmarshal(t *testing.T) {
t.Parallel()

config, err := konf.New(testcase.opts...)
require.NoError(t, err)
assert.NoError(t, err)
testcase.assert(config)
})
}
Expand All @@ -152,11 +151,11 @@ func TestConfig_Watch(t *testing.T) {

watcher := mapWatcher(make(chan map[string]any))
config, err := konf.New(konf.WithLoader(watcher))
require.NoError(t, err)
assert.NoError(t, err)

var cfg string
require.NoError(t, config.Unmarshal("config", &cfg))
require.Equal(t, "string", cfg)
assert.NoError(t, config.Unmarshal("config", &cfg))
assert.Equal(t, "string", cfg)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand All @@ -167,15 +166,15 @@ func TestConfig_Watch(t *testing.T) {
err := config.Watch(ctx, func(config *konf.Config) {
defer waitGroup.Done()

require.NoError(t, config.Unmarshal("config", &cfg))
assert.NoError(t, config.Unmarshal("config", &cfg))
})
require.NoError(t, err)
assert.NoError(t, err)
}()

watcher.change(map[string]any{"config": "changed"})
waitGroup.Wait()

require.Equal(t, "changed", cfg)
assert.Equal(t, "changed", cfg)
}

type mapWatcher chan map[string]any
Expand Down Expand Up @@ -203,12 +202,12 @@ func TestConfig_Watch_error(t *testing.T) {
t.Parallel()

config, err := konf.New(konf.WithLoader(errorWatcher{}))
require.NoError(t, err)
assert.NoError(t, err)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

require.EqualError(t, config.Watch(ctx), "[konf] watch configuration change: watch error")
assert.EqualError(t, config.Watch(ctx), "[konf] watch configuration change: watch error")
}

type errorWatcher struct{}
Expand All @@ -225,7 +224,7 @@ func TestConfig_error(t *testing.T) {
t.Parallel()

_, err := konf.New(konf.WithLoader(errorLoader{}))
require.EqualError(t, err, "[konf] load configuration: load error")
assert.EqualError(t, err, "[konf] load configuration: load error")
}

type errorLoader struct{}
Expand Down
27 changes: 13 additions & 14 deletions global_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,57 +10,56 @@ import (
"sync"
"testing"

"github.com/stretchr/testify/require"

"github.com/ktong/konf"
"github.com/ktong/konf/internal/assert"
)

func TestUnmarshal(t *testing.T) {
t.Parallel()

cfg, err := konf.New(konf.WithLoader(mapLoader{"config": "string"}))
require.NoError(t, err)
assert.NoError(t, err)
konf.SetGlobal(cfg)

var v string
require.NoError(t, konf.Unmarshal("config", &v))
require.Equal(t, "string", v)
assert.NoError(t, konf.Unmarshal("config", &v))
assert.Equal(t, "string", v)
}

func TestGet(t *testing.T) {
t.Parallel()

cfg, err := konf.New(konf.WithLoader(mapLoader{"config": "string"}))
require.NoError(t, err)
assert.NoError(t, err)
konf.SetGlobal(cfg)

require.Equal(t, "string", konf.Get[string]("config"))
assert.Equal(t, "string", konf.Get[string]("config"))
}

func TestGet_error(t *testing.T) {
cfg, err := konf.New(konf.WithLoader(mapLoader{"config": "string"}))
require.NoError(t, err)
assert.NoError(t, err)
konf.SetGlobal(cfg)

buf := new(bytes.Buffer)
log.SetOutput(buf)
log.SetFlags(0)

require.False(t, konf.Get[bool]("config"))
assert.True(t, !konf.Get[bool]("config"))
expected := "ERROR Could not read config, return empty value instead." +
" error=\"[konf] decode: cannot parse '' as bool: strconv.ParseBool: parsing \\\"string\\\": invalid syntax\"" +
" path=config type=bool\n"
require.Equal(t, expected, buf.String())
assert.Equal(t, expected, buf.String())
}

func TestWatch(t *testing.T) {
watcher := mapWatcher(make(chan map[string]any))
config, err := konf.New(konf.WithLoader(watcher))
require.NoError(t, err)
assert.NoError(t, err)
konf.SetGlobal(config)

cfg := konf.Get[string]("config")
require.Equal(t, "string", cfg)
assert.Equal(t, "string", cfg)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand All @@ -73,11 +72,11 @@ func TestWatch(t *testing.T) {

cfg = konf.Get[string]("config")
})
require.NoError(t, err)
assert.NoError(t, err)
}()

watcher.change(map[string]any{"config": "changed"})
waitGroup.Wait()

require.Equal(t, "changed", cfg)
assert.Equal(t, "changed", cfg)
}
11 changes: 0 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,3 @@ module github.com/ktong/konf
go 1.21

require github.com/mitchellh/mapstructure v1.5.0

require github.com/stretchr/testify v1.8.4

require ( // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
18 changes: 0 additions & 18 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,20 +1,2 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
49 changes: 49 additions & 0 deletions internal/assert/assert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) 2023 The konf authors
// Use of this source code is governed by a MIT license found in the LICENSE file.

package assert

import (
"reflect"
"testing"
)

func Equal[T any](tb testing.TB, expected, actual T) {
tb.Helper()

if !reflect.DeepEqual(expected, actual) {
tb.Errorf("expected: %v; actual: %v", expected, actual)
}
}

func NoError(tb testing.TB, err error) {
tb.Helper()

if err != nil {
tb.Errorf("unexpected error: %v", err)
}
}

func EqualError(tb testing.TB, err error, message string) {
tb.Helper()

if err.Error() != message {
tb.Errorf("expected: %v; actual: %v", message, err.Error())
}
}

func True(tb testing.TB, value bool) {
tb.Helper()

if !value {
tb.Errorf("expected True")
}
}

func NotEmpty(tb testing.TB, value any) {
tb.Helper()

if !reflect.ValueOf(value).IsZero() {
tb.Errorf("expected not empty")
}
}
Loading

0 comments on commit b3eba21

Please sign in to comment.