2018-01-11 22:19:25 +00:00
|
|
|
package mcfg
|
|
|
|
|
|
|
|
import (
|
|
|
|
. "testing"
|
|
|
|
|
2019-06-15 22:45:53 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mcmp"
|
2018-01-11 22:19:25 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2019-01-08 19:21:55 +00:00
|
|
|
func TestPopulate(t *T) {
|
2018-01-11 22:19:25 +00:00
|
|
|
{
|
2019-06-15 22:45:53 +00:00
|
|
|
cmp := new(mcmp.Component)
|
|
|
|
a := Int(cmp, "a")
|
|
|
|
cmpFoo := cmp.Child("foo")
|
|
|
|
b := Int(cmpFoo, "b")
|
|
|
|
c := Int(cmpFoo, "c")
|
|
|
|
d := Int(cmp, "d", ParamDefault(4))
|
2018-01-11 22:19:25 +00:00
|
|
|
|
2019-06-15 22:45:53 +00:00
|
|
|
err := Populate(cmp, &SourceCLI{
|
2018-01-11 22:19:25 +00:00
|
|
|
Args: []string{"--a=1", "--foo-b=2"},
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, *a)
|
|
|
|
assert.Equal(t, 2, *b)
|
|
|
|
assert.Equal(t, 0, *c)
|
2019-06-15 22:45:53 +00:00
|
|
|
assert.Equal(t, 4, *d)
|
2018-01-11 22:19:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{ // test that required params are enforced
|
2019-06-15 22:45:53 +00:00
|
|
|
cmp := new(mcmp.Component)
|
|
|
|
a := Int(cmp, "a")
|
|
|
|
cmpFoo := cmp.Child("foo")
|
|
|
|
b := Int(cmpFoo, "b")
|
|
|
|
c := Int(cmpFoo, "c", ParamRequired())
|
2018-01-11 22:19:25 +00:00
|
|
|
|
2019-06-15 22:45:53 +00:00
|
|
|
err := Populate(cmp, &SourceCLI{
|
2018-01-11 22:19:25 +00:00
|
|
|
Args: []string{"--a=1", "--foo-b=2"},
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
2019-06-15 22:45:53 +00:00
|
|
|
err = Populate(cmp, &SourceCLI{
|
2018-01-11 22:19:25 +00:00
|
|
|
Args: []string{"--a=1", "--foo-b=2", "--foo-c=3"},
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, *a)
|
|
|
|
assert.Equal(t, 2, *b)
|
|
|
|
assert.Equal(t, 3, *c)
|
|
|
|
}
|
|
|
|
}
|
2019-06-23 18:55:11 +00:00
|
|
|
|
|
|
|
func TestParamDefaultOrRequired(t *T) {
|
|
|
|
{
|
|
|
|
cmp := new(mcmp.Component)
|
|
|
|
Int(cmp, "a", ParamDefaultOrRequired(0))
|
|
|
|
params := CollectParams(cmp)
|
|
|
|
assert.Equal(t, "a", params[0].Name)
|
|
|
|
assert.Equal(t, true, params[0].Required)
|
|
|
|
assert.Equal(t, new(int), params[0].Into)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
cmp := new(mcmp.Component)
|
|
|
|
Int(cmp, "a", ParamDefaultOrRequired(1))
|
|
|
|
i := 1
|
|
|
|
params := CollectParams(cmp)
|
|
|
|
assert.Equal(t, "a", params[0].Name)
|
|
|
|
assert.Equal(t, false, params[0].Required)
|
|
|
|
assert.Equal(t, &i, params[0].Into)
|
|
|
|
}
|
|
|
|
}
|