generated from inherelab/go-pkg-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
up: update some parse logic, add more unit tests
- Loading branch information
Showing
10 changed files
with
239 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package properties_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gookit/goutil/maputil" | ||
"github.com/gookit/goutil/testutil/assert" | ||
"github.com/gookit/properties" | ||
) | ||
|
||
func TestEncode(t *testing.T) { | ||
e := properties.NewEncoder() | ||
bs, err := e.Marshal(maputil.Data{ | ||
"name": "inhere", | ||
"age": 234, | ||
}) | ||
|
||
assert.NoErr(t, err) | ||
assert.NotEmpty(t, bs) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package properties_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/gookit/goutil/strutil" | ||
"github.com/gookit/goutil/testutil/assert" | ||
"github.com/gookit/properties" | ||
) | ||
|
||
func TestOptions_InlineComment(t *testing.T) { | ||
text := ` | ||
key = value # inline comments | ||
key2 = value2 // inline comments | ||
key3 = value3 | ||
` | ||
|
||
p := properties.NewParser() | ||
err := p.Parse(text) | ||
assert.NoErr(t, err) | ||
assert.Eq(t, "value # inline comments", p.Str("key")) | ||
|
||
p = properties.NewParser(func(opts *properties.Options) { | ||
opts.InlineComment = true | ||
}) | ||
err = p.Parse(text) | ||
assert.NoErr(t, err) | ||
assert.Eq(t, "value", p.Str("key")) | ||
} | ||
|
||
func TestParseTime(t *testing.T) { | ||
text := ` | ||
// properties string | ||
name = inhere | ||
age = 200 | ||
expire = 3s | ||
` | ||
|
||
type MyConf struct { | ||
Name string `properties:"name"` | ||
Age int `properties:"age"` | ||
Expire time.Duration `properties:"expire"` | ||
} | ||
|
||
p, err := properties.Parse(text, properties.ParseTime) | ||
assert.NoErr(t, err) | ||
|
||
cfg := &MyConf{} | ||
err = p.MapStruct("", cfg) | ||
assert.NoErr(t, err) | ||
assert.Eq(t, "inhere", cfg.Name) | ||
assert.Eq(t, 3*time.Second, cfg.Expire) | ||
|
||
err = p.MapStruct("not-found", cfg) | ||
assert.Err(t, err) | ||
} | ||
|
||
func TestOptions_BeforeCollect(t *testing.T) { | ||
text := ` | ||
// properties string | ||
name = inhere | ||
age = 200 | ||
expire = 3s | ||
` | ||
|
||
type MyConf struct { | ||
Name string `properties:"name"` | ||
Age int `properties:"age"` | ||
Expire time.Duration `properties:"expire"` | ||
} | ||
|
||
// tests Unmarshal, BeforeCollect | ||
p := properties.NewParser(properties.ParseTime, func(opts *properties.Options) { | ||
opts.BeforeCollect = func(name string, val interface{}) interface{} { | ||
if name == "name" { | ||
return strutil.Upper(val.(string)) | ||
} | ||
return val | ||
} | ||
}) | ||
|
||
cfg := &MyConf{} | ||
err := p.Unmarshal([]byte(text), cfg) | ||
assert.NoErr(t, err) | ||
assert.Eq(t, "INHERE", cfg.Name) | ||
assert.Eq(t, 3*time.Second, cfg.Expire) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.