Skip to content

Commit

Permalink
complete more get testing
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 23, 2018
1 parent 6284748 commit e620e3d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ golang application config manage tool library.
- support get sub value by path, like `map.key` `arr.2`
- support parse env name. like `envKey: ${SHELL}` -> `envKey: /bin/zsh`
- generic api `Get` `Int` `String` `Bool` `Ints` `IntMap` `Strings` `StringMap` ...
- complete unit test(coverage > 90%)

> **[中文说明](README_cn.md)**
Expand Down
3 changes: 2 additions & 1 deletion README_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ golang应用程序配置管理工具库。
- 支持数据覆盖合并
- 支持按路径获取子级值, e.g `map.key` `arr.2`
- 支持解析ENV变量名称. like `envKey: ${SHELL}` -> `envKey: /bin/zsh`
- 通用的使用API `Get` `Int` `String` `Bool` `Ints` `IntMap` `Strings` `StringMap` ...
- 简洁的使用API `Get` `Int` `String` `Bool` `Ints` `IntMap` `Strings` `StringMap` ...
- 完善的单元测试(coverage > 90%)

> **[EN README](README.md)**
Expand Down
1 change: 1 addition & 0 deletions config_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func (c *Config) Int(key string) (value int, ok bool) {
return value, true
}

ok = false
return
}

Expand Down
36 changes: 33 additions & 3 deletions config_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func TestGet(t *testing.T) {
st.False(ok)
st.Nil(val)

val, ok = Get("notExist.sub")
st.False(ok)
st.Nil(val)

val, ok = c.Get("arr1.100")
st.False(ok)
st.Nil(val)
Expand All @@ -48,6 +52,9 @@ func TestGet(t *testing.T) {
st.True(ok)
st.Equal(123, iv)

iv, ok = Int("name")
st.False(ok)

iv = DefInt("notExist", 34)
st.Equal(34, iv)

Expand Down Expand Up @@ -100,6 +107,9 @@ func TestGet(t *testing.T) {
st.True(ok)
st.Equal("app", val)

val, ok = String("arr1")
st.False(ok)

str, ok := String("notExists")
st.False(ok)
st.Equal("", str)
Expand All @@ -108,7 +118,7 @@ func TestGet(t *testing.T) {
st.Equal("defVal", str)

str = c.MustString("name")
st.Equal("app", val)
st.Equal("app", str)
str = c.MustString("notExist")
st.Equal("", str)

Expand All @@ -131,7 +141,10 @@ func TestGet(t *testing.T) {
st.Nil(err)

// get int arr
iarr, ok := Ints("notExist")
iarr, ok := Ints("name")
st.False(ok)

iarr, ok = Ints("notExist")
st.False(ok)

iarr, ok = Ints("iArr")
Expand All @@ -146,7 +159,9 @@ func TestGet(t *testing.T) {
st.False(ok)

// get int map
imp, ok := IntMap("notExist")
imp, ok := IntMap("name")
st.False(ok)
imp, ok = IntMap("notExist")
st.False(ok)

imp, ok = IntMap("iMap")
Expand All @@ -170,6 +185,9 @@ func TestGet(t *testing.T) {
err = c.LoadData(map[string]interface{}{
"newIArr": []int{2, 3},
"newSArr": []string{"a", "b"},
"newIArr1": []interface{}{12, 23},
"newIArr2": []interface{}{12, "abc"},
"invalidMap": map[string]int{"k": 1},
"yMap": map[interface{}]interface{}{
"k0": "v0",
"k1": 23,
Expand All @@ -187,6 +205,12 @@ func TestGet(t *testing.T) {
st.True(ok)
st.Equal("[2 3]", fmt.Sprintf("%v", iarr))

iarr,ok = Ints("newIArr1")
st.True(ok)
st.Equal("[12 23]", fmt.Sprintf("%v", iarr))
iarr,ok = Ints("newIArr2")
st.False(ok)

iv, ok = Int("newIArr.1")
st.True(ok)
st.Equal(3, iv)
Expand All @@ -201,6 +225,12 @@ func TestGet(t *testing.T) {
val, ok = String("newSArr.100")
st.False(ok)

smp, ok = StringMap("invalidMap")
st.False(ok)

smp, ok = StringMap("yMap.notExist")
st.False(ok)

smp, ok = StringMap("yMap")
st.True(ok)
st.Equal("v0", smp["k0"])
Expand Down

0 comments on commit e620e3d

Please sign in to comment.