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)
|
|
|
|
}
|
|
|
|
}
|