2018-01-11 22:19:25 +00:00
|
|
|
package mcfg
|
|
|
|
|
|
|
|
import (
|
2019-02-05 20:18:17 +00:00
|
|
|
"context"
|
2018-01-11 22:19:25 +00:00
|
|
|
. "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-02-05 20:18:17 +00:00
|
|
|
ctx := context.Background()
|
2019-02-09 19:08:30 +00:00
|
|
|
ctx, a := WithInt(ctx, "a", 0, "")
|
2019-02-05 20:18:17 +00:00
|
|
|
ctxChild := mctx.NewChild(ctx, "foo")
|
2019-02-09 19:08:30 +00:00
|
|
|
ctxChild, b := WithInt(ctxChild, "b", 0, "")
|
|
|
|
ctxChild, c := WithInt(ctxChild, "c", 0, "")
|
2019-02-05 20:18:17 +00:00
|
|
|
ctx = mctx.WithChild(ctx, ctxChild)
|
2018-01-11 22:19:25 +00:00
|
|
|
|
2019-04-04 16:52:43 +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-02-05 20:18:17 +00:00
|
|
|
ctx := context.Background()
|
2019-02-09 19:08:30 +00:00
|
|
|
ctx, a := WithInt(ctx, "a", 0, "")
|
2019-02-05 20:18:17 +00:00
|
|
|
ctxChild := mctx.NewChild(ctx, "foo")
|
2019-02-09 19:08:30 +00:00
|
|
|
ctxChild, b := WithInt(ctxChild, "b", 0, "")
|
|
|
|
ctxChild, c := WithRequiredInt(ctxChild, "c", "")
|
2019-02-05 20:18:17 +00:00
|
|
|
ctx = mctx.WithChild(ctx, ctxChild)
|
2018-01-11 22:19:25 +00:00
|
|
|
|
2019-04-04 16:52:43 +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-04-04 16:52:43 +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)
|
|
|
|
}
|
|
|
|
}
|