2018-01-11 22:19:25 +00:00
|
|
|
package mcfg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-08-13 18:17:53 +00:00
|
|
|
"strings"
|
2018-01-11 22:19:25 +00:00
|
|
|
. "testing"
|
2018-08-13 18:17:53 +00:00
|
|
|
"time"
|
2018-01-11 22:19:25 +00:00
|
|
|
|
2019-01-08 19:21:55 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mctx"
|
2018-07-03 00:20:00 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mrand"
|
2018-08-13 19:03:30 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mtest/mchk"
|
2018-01-11 22:19:25 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSourceCLIHelp(t *T) {
|
2019-01-08 19:21:55 +00:00
|
|
|
ctx := mctx.New()
|
|
|
|
Int(ctx, "foo", 5, "Test int param")
|
|
|
|
Bool(ctx, "bar", "Test bool param")
|
|
|
|
String(ctx, "baz", "baz", "Test string param")
|
|
|
|
String(ctx, "baz2", "", "")
|
2018-01-11 22:19:25 +00:00
|
|
|
src := SourceCLI{}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
2019-01-08 19:21:55 +00:00
|
|
|
pM, err := src.cliParams(collectParams(ctx))
|
2018-01-11 22:19:25 +00:00
|
|
|
require.NoError(t, err)
|
2018-08-14 01:02:06 +00:00
|
|
|
SourceCLI{}.printHelp(buf, pM)
|
2018-01-11 22:19:25 +00:00
|
|
|
|
|
|
|
exp := `
|
|
|
|
--bar (Flag)
|
|
|
|
Test bool param
|
|
|
|
|
|
|
|
--baz (Default: "baz")
|
|
|
|
Test string param
|
|
|
|
|
|
|
|
--baz2
|
|
|
|
|
|
|
|
--foo (Default: 5)
|
|
|
|
Test int param
|
|
|
|
|
|
|
|
`
|
|
|
|
assert.Equal(t, exp, buf.String())
|
|
|
|
}
|
2018-08-13 18:17:53 +00:00
|
|
|
|
2018-08-13 19:03:30 +00:00
|
|
|
func TestSourceCLI(t *T) {
|
|
|
|
type state struct {
|
2018-08-13 23:40:41 +00:00
|
|
|
srcCommonState
|
2018-08-13 19:03:30 +00:00
|
|
|
SourceCLI
|
2018-08-13 18:17:53 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 19:03:30 +00:00
|
|
|
type params struct {
|
2018-08-13 23:40:41 +00:00
|
|
|
srcCommonParams
|
|
|
|
nonBoolWEq bool // use equal sign when setting value
|
2018-08-13 18:17:53 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 19:03:30 +00:00
|
|
|
chk := mchk.Checker{
|
|
|
|
Init: func() mchk.State {
|
|
|
|
var s state
|
2018-08-13 23:40:41 +00:00
|
|
|
s.srcCommonState = newSrcCommonState()
|
2018-08-13 18:17:53 +00:00
|
|
|
s.SourceCLI.Args = make([]string, 0, 16)
|
|
|
|
return s
|
|
|
|
},
|
2018-08-13 19:03:30 +00:00
|
|
|
Next: func(ss mchk.State) mchk.Action {
|
|
|
|
s := ss.(state)
|
|
|
|
var p params
|
2018-08-13 23:40:41 +00:00
|
|
|
p.srcCommonParams = s.srcCommonState.next()
|
|
|
|
// if the param is a bool or unset this won't get used, but w/e
|
2018-08-13 19:03:30 +00:00
|
|
|
p.nonBoolWEq = mrand.Intn(2) == 0
|
|
|
|
return mchk.Action{Params: p}
|
|
|
|
},
|
|
|
|
Apply: func(ss mchk.State, a mchk.Action) (mchk.State, error) {
|
|
|
|
s := ss.(state)
|
|
|
|
p := a.Params.(params)
|
|
|
|
|
2019-01-08 19:21:55 +00:00
|
|
|
s.srcCommonState = s.srcCommonState.applyCtxAndPV(p.srcCommonParams)
|
2018-08-13 19:03:30 +00:00
|
|
|
if !p.unset {
|
|
|
|
arg := cliKeyPrefix
|
|
|
|
if len(p.path) > 0 {
|
|
|
|
arg += strings.Join(p.path, cliKeyJoin) + cliKeyJoin
|
|
|
|
}
|
|
|
|
arg += p.name
|
|
|
|
if !p.isBool {
|
|
|
|
if p.nonBoolWEq {
|
|
|
|
arg += "="
|
|
|
|
} else {
|
|
|
|
s.SourceCLI.Args = append(s.SourceCLI.Args, arg)
|
|
|
|
arg = ""
|
|
|
|
}
|
|
|
|
arg += p.nonBoolVal
|
|
|
|
}
|
|
|
|
s.SourceCLI.Args = append(s.SourceCLI.Args, arg)
|
|
|
|
}
|
|
|
|
|
2018-08-13 23:40:41 +00:00
|
|
|
err := s.srcCommonState.assert(s.SourceCLI)
|
|
|
|
return s, err
|
2018-08-13 18:17:53 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-08-13 23:40:41 +00:00
|
|
|
if err := chk.RunFor(2 * time.Second); err != nil {
|
2018-08-13 18:17:53 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|