Skip to content

Commit

Permalink
up: use gookit/goutil/testutil instead of stretchr/testify on sub pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 20, 2022
1 parent a43a79c commit 0eaa807
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 80 deletions.
18 changes: 9 additions & 9 deletions hcl/hcl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ import (

"github.com/gookit/config/v2"
"github.com/gookit/goutil/dump"
"github.com/stretchr/testify/assert"
"github.com/gookit/goutil/testutil/assert"
)

func TestDriver(t *testing.T) {
st := assert.New(t)
is := assert.New(t)

st.Equal("hcl", Driver.Name())
is.Eq("hcl", Driver.Name())

c := config.NewEmpty("test")
st.False(c.HasDecoder(config.Hcl))
is.False(c.HasDecoder(config.Hcl))

c.AddDriver(Driver)
st.True(c.HasDecoder(config.Hcl))
st.True(c.HasEncoder(config.Hcl))
is.True(c.HasDecoder(config.Hcl))
is.True(c.HasEncoder(config.Hcl))

_, err := Encoder("some data")
st.Error(err)
is.Err(err)
}

func TestLoadFile(t *testing.T) {
Expand All @@ -30,10 +30,10 @@ func TestLoadFile(t *testing.T) {
c.AddDriver(Driver)

err := c.LoadFiles("../testdata/hcl_base.hcl")
is.NoError(err)
is.NoErr(err)
dump.Println(c.Data())

err = c.LoadFiles("../testdata/hcl_example.conf")
is.NoError(err)
is.NoErr(err)

}
12 changes: 6 additions & 6 deletions hclv2/hcl_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (

"github.com/gookit/config/v2"
"github.com/gookit/goutil/dump"
"github.com/stretchr/testify/assert"
"github.com/gookit/goutil/testutil/assert"
)

func TestDriver(t *testing.T) {
is := assert.New(t)
is.Equal("hcl", Driver.Name())
is.Equal(config.Hcl, Driver.Name())
is.Eq("hcl", Driver.Name())
is.Eq(config.Hcl, Driver.Name())

c := config.NewEmpty("test")
is.False(c.HasDecoder(config.Hcl))
Expand All @@ -21,7 +21,7 @@ func TestDriver(t *testing.T) {
is.True(c.HasEncoder(config.Hcl))

_, err := Encoder("some data")
is.Error(err)
is.Err(err)
}

func TestLoadFile(t *testing.T) {
Expand All @@ -33,9 +33,9 @@ func TestLoadFile(t *testing.T) {
t.Skip("Not completed")
return
err := c.LoadFiles("../testdata/hcl2_base.hcl")
is.NoError(err)
is.NoErr(err)
dump.Println(c.Data())

err = c.LoadFiles("../testdata/hcl2_example.hcl")
is.NoError(err)
is.NoErr(err)
}
6 changes: 3 additions & 3 deletions ini/ini_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

"github.com/gookit/config/v2"
"github.com/stretchr/testify/assert"
"github.com/gookit/goutil/testutil/assert"
)

func Example() {
Expand Down Expand Up @@ -56,7 +56,7 @@ func Example() {
func TestDriver(t *testing.T) {
st := assert.New(t)

st.Equal("ini", Driver.Name())
st.Eq("ini", Driver.Name())
// st.IsType(new(Encoder), JSONDriver.GetEncoder())

c := config.NewEmpty("test")
Expand All @@ -70,5 +70,5 @@ func TestDriver(t *testing.T) {
st.Nil(err)

_, err = Encoder("invalid")
st.Error(err)
st.Err(err)
}
18 changes: 9 additions & 9 deletions json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

"github.com/gookit/config/v2"
"github.com/stretchr/testify/assert"
"github.com/gookit/goutil/testutil/assert"
)

func Example() {
Expand Down Expand Up @@ -64,16 +64,16 @@ func Example() {
}

func TestDriver(t *testing.T) {
st := assert.New(t)
is := assert.New(t)

st.Equal("json", Driver.Name())
is.Eq("json", Driver.Name())

c := config.NewEmpty("test")
st.False(c.HasDecoder(config.JSON))
is.False(c.HasDecoder(config.JSON))
c.AddDriver(Driver)

st.True(c.HasDecoder(config.JSON))
st.True(c.HasEncoder(config.JSON))
is.True(c.HasDecoder(config.JSON))
is.True(c.HasEncoder(config.JSON))

m := struct {
N string
Expand All @@ -82,8 +82,8 @@ func TestDriver(t *testing.T) {
// comments
"n":"v"}
`), &m)
st.Nil(err)
st.Equal("v", m.N)
is.Nil(err)
is.Eq("v", m.N)

// disable clear comments
old := config.JSONAllowComments
Expand All @@ -92,7 +92,7 @@ func TestDriver(t *testing.T) {
// comments
"n":"v"}
`), &m)
st.Error(err)
is.Err(err)

config.JSONAllowComments = old
}
6 changes: 3 additions & 3 deletions json5/json5_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/gookit/config/v2"
"github.com/gookit/config/v2/json5"
"github.com/stretchr/testify/assert"
"github.com/gookit/goutil/testutil/assert"
)

func Example() {
Expand Down Expand Up @@ -67,7 +67,7 @@ func Example() {
func TestDriver(t *testing.T) {
is := assert.New(t)

is.Equal(json5.Name, json5.Driver.Name())
is.Eq(json5.Name, json5.Driver.Name())

c := config.NewEmpty("test")
is.False(c.HasDecoder(json5.Name))
Expand All @@ -84,5 +84,5 @@ func TestDriver(t *testing.T) {
"n":"v"}
`), &m)
is.Nil(err)
is.Equal("v", m.N)
is.Eq("v", m.N)
}
16 changes: 8 additions & 8 deletions other/other_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ import (
"testing"

"github.com/gookit/config/v2"
"github.com/stretchr/testify/assert"
"github.com/gookit/goutil/testutil/assert"
)

func TestOtherDriver(t *testing.T) {
st := assert.New(t)
is := assert.New(t)

st.Equal("other", Driver.Name())
is.Eq("other", Driver.Name())

c := config.NewEmpty("test")
st.False(c.HasDecoder("other"))
is.False(c.HasDecoder("other"))

c.AddDriver(Driver)
st.True(c.HasDecoder("other"))
st.True(c.HasEncoder("other"))
is.True(c.HasDecoder("other"))
is.True(c.HasEncoder("other"))

_, err := Encoder(map[string]interface{}{"k": "v"})
st.Nil(err)
is.Nil(err)

_, err = Encoder("invalid")
st.Error(err)
is.Err(err)
}

func TestOtherLoader(t *testing.T) {
Expand Down
20 changes: 10 additions & 10 deletions toml/toml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

"github.com/gookit/config/v2"
"github.com/stretchr/testify/assert"
"github.com/gookit/goutil/testutil/assert"
)

var tomlStr = `
Expand Down Expand Up @@ -88,25 +88,25 @@ func Example() {
}

func TestDriver(t *testing.T) {
st := assert.New(t)
is := assert.New(t)

st.Equal("toml", Driver.Name())
is.Eq("toml", Driver.Name())

c := config.NewEmpty("test")
st.False(c.HasDecoder(config.Toml))
is.False(c.HasDecoder(config.Toml))

c.AddDriver(Driver)
st.True(c.HasDecoder(config.Toml))
st.True(c.HasEncoder(config.Toml))
is.True(c.HasDecoder(config.Toml))
is.True(c.HasEncoder(config.Toml))

tg := new(map[string]interface{})
err := Decoder([]byte("invalid"), tg)
st.Error(err)
is.Err(err)

_, err = Encoder("invalid")
st.Error(err)
is.Err(err)

out, err := Encoder(map[string]interface{}{"k": "v"})
st.Nil(err)
st.Contains(string(out), `k = "v"`)
is.Nil(err)
is.Contains(string(out), `k = "v"`)
}
32 changes: 16 additions & 16 deletions yaml/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/gookit/config/v2"
"github.com/gookit/goutil/testutil"
"github.com/stretchr/testify/assert"
"github.com/gookit/goutil/testutil/assert"
)

var yamlStr = `
Expand Down Expand Up @@ -86,7 +86,7 @@ func TestDumpConfig(t *testing.T) {
// Notice: before dump please set driver encoder
c.AddDriver(Driver)
err := c.LoadStrings(config.Yaml, yamlStr)
is.NoError(err)
is.NoErr(err)

buf := new(bytes.Buffer)
_, err = c.DumpTo(buf, config.Yaml)
Expand All @@ -108,7 +108,7 @@ func TestLoadFile(t *testing.T) {
}

fmt.Printf("config data: \n %#v\n", c.Data())
assert.Equal(t, "app", c.String("name"))
assert.Eq(t, "app", c.String("name"))

err = c.LoadFiles("../testdata/yml_other.yml")
// config.LoadFiles("testdata/yml_base.yml", "testdata/yml_other.yml")
Expand All @@ -117,13 +117,13 @@ func TestLoadFile(t *testing.T) {
}

fmt.Printf("config data: \n %#v\n", c.Data())
assert.Equal(t, "app2", c.String("name"))
assert.Eq(t, "app2", c.String("name"))
}

func TestDriver(t *testing.T) {
is := assert.New(t)

is.Equal("yaml", Driver.Name())
is.Eq("yaml", Driver.Name())
// is.IsType(new(Encoder), JSONDriver.GetEncoder())

c := config.NewEmpty("test")
Expand All @@ -136,7 +136,7 @@ func TestDriver(t *testing.T) {
// Support "=", ":", "." characters for default values
// see https://github.com/gookit/config/issues/9
func TestIssue2(t *testing.T) {
ris := assert.New(t)
is := assert.New(t)

c := config.NewEmpty("test")
c.AddDriver(Driver)
Expand All @@ -145,24 +145,24 @@ func TestIssue2(t *testing.T) {
err := c.LoadStrings(config.Yaml, `
command: ${APP_COMMAND|app:run}
`)
ris.NoError(err)
is.NoErr(err)
testutil.MockEnvValue("APP_COMMAND", "new val", func(nv string) {
ris.Equal("new val", nv)
ris.Equal("new val", c.String("command"))
is.Eq("new val", nv)
is.Eq("new val", c.String("command"))
})

ris.Equal("", config.Getenv("APP_COMMAND"))
ris.Equal("app:run", c.String("command"))
is.Eq("", config.Getenv("APP_COMMAND"))
is.Eq("app:run", c.String("command"))

c.ClearAll()
err = c.LoadStrings(config.Yaml, `
command: ${ APP_COMMAND | app:run }
`)
ris.NoError(err)
is.NoErr(err)
testutil.MockEnvValue("APP_COMMAND", "new val", func(nv string) {
ris.Equal("new val", nv)
ris.Equal("new val", c.String("command"))
is.Eq("new val", nv)
is.Eq("new val", c.String("command"))
})
ris.Equal("", config.Getenv("APP_COMMAND"))
ris.Equal("app:run", c.String("command"))
is.Eq("", config.Getenv("APP_COMMAND"))
is.Eq("app:run", c.String("command"))
}
Loading

0 comments on commit 0eaa807

Please sign in to comment.