Skip to content

Commit

Permalink
✅ test: add more unit tests, improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 13, 2024
1 parent 39cb17c commit 1a3e1b1
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 27 deletions.
31 changes: 29 additions & 2 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"testing"

"github.com/gookit/goutil/maputil"
"github.com/gookit/goutil/testutil"
"github.com/gookit/goutil/testutil/assert"
)
Expand Down Expand Up @@ -163,11 +164,12 @@ func TestBasic(t *testing.T) {
is.Eq("default", c.Name())
is.NoErr(c.Error())

c = NewWithOptions("test", Readonly)
c = NewWithOptions("test", Readonly, WithTagName("mytag"))
opts := c.Options()
is.True(opts.Readonly)
is.Eq(JSON, opts.DumpFormat)
is.Eq(JSON, opts.ReadFormat)
is.Eq("mytag", opts.TagName)
}

func TestGetEnv(t *testing.T) {
Expand Down Expand Up @@ -249,6 +251,16 @@ func TestJSONDriver(t *testing.T) {
is.NoErr(err)
})
is.Eq(2, c.Int("key1"))

// test call JSONDriver.Encode
s := c.ToJSON()
is.StrContains(s, `{"key1":2}`)

// set MarshalIndent
JSONDriver.MarshalIndent = " "
s = c.ToJSON()
is.StrContains(s, ` "key1": 2`)
JSONDriver.MarshalIndent = "" // reset
}

func TestDriver(t *testing.T) {
Expand Down Expand Up @@ -282,6 +294,22 @@ func TestDriver(t *testing.T) {
is.True(c.HasEncoder(JSON))
}

func TestStdDriver_methods(t *testing.T) {
d1 := NewDriver("my001", JSONDecoder, JSONEncoder)
d1.WithAlias("json")
assert.Eq(t, "my001", d1.Name())
assert.Contains(t, d1.Aliases(), "json")

s := `{"age": 245}`
m := make(maputil.Map)
err := d1.Decode([]byte(s), &m)
assert.NoErr(t, err)

bs, err := d1.Encode(m)
assert.NoErr(t, err)
assert.StrContains(t, string(bs), `{"age":245}`)
}

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

Expand Down Expand Up @@ -447,4 +475,3 @@ func TestMapStringStringParseEnv(t *testing.T) {
is.Eq(shellVal, sMap["key3"])
})
}

4 changes: 1 addition & 3 deletions export.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,14 @@ func (c *Config) Structure(key string, dst any) (err error) {
return err
}

// ToJSON string
// ToJSON string, will ignore error
func (c *Config) ToJSON() string {
buf := &bytes.Buffer{}

_, err := c.DumpTo(buf, JSON)
if err != nil {
return ""
}

return buf.String()
}

Expand Down Expand Up @@ -168,7 +167,6 @@ func (c *Config) DumpTo(out io.Writer, format string) (n int64, err error) {

// write content to out
num, _ := fmt.Fprintln(out, string(encoded))

return int64(num), nil
}

Expand Down
23 changes: 20 additions & 3 deletions export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"bytes"
"errors"
"testing"

"github.com/gookit/goutil/dump"
Expand All @@ -11,7 +12,6 @@ import (

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

c := New("test")

str := c.ToJSON()
Expand All @@ -27,18 +27,35 @@ func TestExport(t *testing.T) {
_, err = c.WriteTo(buf)
is.Nil(err)

// test dump
buf = &bytes.Buffer{}

_, err = c.DumpTo(buf, "invalid")
is.Err(err)

_, err = c.DumpTo(buf, Yml)
is.Err(err)

_, err = c.DumpTo(buf, JSON)
is.Nil(err)
}

func TestDumpTo_encode_error(t *testing.T) {
is := assert.New(t)
c := NewEmpty("test")
is.NoErr(c.Set("age", 34))

drv := NewDriver(JSON, JSONDecoder, func(v any) (out []byte, err error) {
return nil, errors.New("encode data error")
})
c.WithDriver(drv)

// encode error
buf := &bytes.Buffer{}
_, err := c.DumpTo(buf, JSON)
is.ErrMsg(err, "encode data error")

is.Empty(c.ToJSON())
}

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

Expand Down
6 changes: 3 additions & 3 deletions json5/json5.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"github.com/titanous/json5"
)

// NAME for driver
const NAME = "json5"

// Name for driver
const Name = "json5"

// NAME for driver
const NAME = Name

// JSONMarshalIndent if not empty, will use json.MarshalIndent for encode data.
var JSONMarshalIndent string

Expand Down
22 changes: 21 additions & 1 deletion json5/json5_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func TestDriver(t *testing.T) {
is.True(c.HasDecoder(json5.Name))
is.True(c.HasEncoder(json5.Name))

// test use
m := struct {
N string
}{}
Expand All @@ -86,7 +87,26 @@ func TestDriver(t *testing.T) {
is.Nil(err)
is.Eq("v", m.N)

// will error on use
// load file
err = c.LoadFiles("../testdata/json_base.json5")
is.NoErr(err)
is.Eq("app", c.Get("name"))
}

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

mp := map[string]any{
"name": "app",
"age": 45,
}
bs, err := json5.Encoder(mp)
is.NoErr(err)
is.StrContains(string(bs), `"name":"app"`)

json5.JSONMarshalIndent = " "
bs, err = json5.Encoder(mp)
is.NoErr(err)
s := string(bs)
is.StrContains(s, ` "name": "app"`)
}
8 changes: 3 additions & 5 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"

"dario.cat/mergo"
"github.com/gookit/goutil"
"github.com/mitchellh/mapstructure"
)

Expand Down Expand Up @@ -149,15 +150,12 @@ func Delimiter(sep byte) func(*Options) {
}
}

// SaveFileOnSet set hook func
// SaveFileOnSet set hook func, will panic on save error
func SaveFileOnSet(fileName string, format string) func(options *Options) {
return func(opts *Options) {
opts.HookFunc = func(event string, c *Config) {
if strings.HasPrefix(event, "set.") {
err := c.DumpToFile(fileName, format)
if err != nil {
panic(err)
}
goutil.PanicErr(c.DumpToFile(fileName, format))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion testdata/issues59.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; exported at 2023-07-01 14:46:07
; exported at 2024-01-13 12:00:15

age = 123
baseKey = value
Expand Down
12 changes: 3 additions & 9 deletions toml/toml.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/*
Package toml is driver use TOML format content as config source
Usage please see example:
Package toml is driver use TOML format content as config source.
How to usage please see README and unit tests.
*/
package toml

Expand All @@ -22,13 +21,8 @@ var Decoder config.Decoder = func(blob []byte, ptr any) (err error) {
// Encoder the toml content encoder
var Encoder config.Encoder = func(ptr any) (out []byte, err error) {
buf := new(bytes.Buffer)

err = toml.NewEncoder(buf).Encode(ptr)
if err != nil {
return
}

return buf.Bytes(), nil
return buf.Bytes(), err
}

// Driver for toml format
Expand Down

0 comments on commit 1a3e1b1

Please sign in to comment.