2018-01-11 22:19:25 +00:00
|
|
|
package mcfg
|
|
|
|
|
|
|
|
import (
|
|
|
|
. "testing"
|
|
|
|
|
2019-01-08 19:21:55 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mctx"
|
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-01-08 19:21:55 +00:00
|
|
|
ctx := mctx.New()
|
|
|
|
a := Int(ctx, "a", 0, "")
|
|
|
|
ctxChild := mctx.ChildOf(ctx, "foo")
|
|
|
|
b := Int(ctxChild, "b", 0, "")
|
|
|
|
c := Int(ctxChild, "c", 0, "")
|
2018-01-11 22:19:25 +00:00
|
|
|
|
2019-01-08 19:21:55 +00:00
|
|
|
err := Populate(ctx, 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // test that required params are enforced
|
2019-01-08 19:21:55 +00:00
|
|
|
ctx := mctx.New()
|
|
|
|
a := Int(ctx, "a", 0, "")
|
|
|
|
ctxChild := mctx.ChildOf(ctx, "foo")
|
|
|
|
b := Int(ctxChild, "b", 0, "")
|
|
|
|
c := RequiredInt(ctxChild, "c", "")
|
2018-01-11 22:19:25 +00:00
|
|
|
|
2019-01-08 19:21:55 +00:00
|
|
|
err := Populate(ctx, SourceCLI{
|
2018-01-11 22:19:25 +00:00
|
|
|
Args: []string{"--a=1", "--foo-b=2"},
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
2019-01-08 19:21:55 +00:00
|
|
|
err = Populate(ctx, 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)
|
|
|
|
}
|
|
|
|
}
|