new tests

This commit is contained in:
Lynn Cyrin 2019-12-24 07:49:37 -08:00
parent 7b57234c15
commit b62c6419cb
No known key found for this signature in database
GPG Key ID: 9E60BEE0555C367B
2 changed files with 57 additions and 8 deletions

View File

@ -24,7 +24,12 @@ type Context struct {
// NewContext creates a new context. For use in when invoking an App or Command action. // NewContext creates a new context. For use in when invoking an App or Command action.
func NewContext(app *App, set *flag.FlagSet, parentCtx *Context) *Context { func NewContext(app *App, set *flag.FlagSet, parentCtx *Context) *Context {
c := &Context{App: app, flagSet: set, parentContext: parentCtx} c := &Context{
App: app,
flagSet: set,
parentContext: parentCtx,
}
if parentCtx != nil { if parentCtx != nil {
c.Context = parentCtx.Context c.Context = parentCtx.Context
c.shellComplete = parentCtx.shellComplete c.shellComplete = parentCtx.shellComplete

View File

@ -293,13 +293,6 @@ func TestContext_Lineage(t *testing.T) {
expect(t, lineage[1], parentCtx) expect(t, lineage[1], parentCtx)
} }
func TestContext_BackgroundContextAttributeAccessing(t *testing.T) {
parentContext := context.Background()
ctx := NewContext(nil, nil, &Context{Context: parentContext})
value := ctx.Bool("some-bool")
expect(t, value, false)
}
func TestContext_lookupFlagSet(t *testing.T) { func TestContext_lookupFlagSet(t *testing.T) {
set := flag.NewFlagSet("test", 0) set := flag.NewFlagSet("test", 0)
set.Bool("local-flag", false, "doc") set.Bool("local-flag", false, "doc")
@ -351,6 +344,57 @@ func TestContextPropagation(t *testing.T) {
} }
} }
func TestContextAttributeAccessing(t *testing.T) {
tdata := []struct {
testCase string
setBoolInput string
ctxBoolInput string
newContextInput *Context
}{
{
testCase: "empty",
setBoolInput: "",
ctxBoolInput: "",
newContextInput: nil,
},
{
testCase: "empty_with_background_context",
setBoolInput: "",
ctxBoolInput: "",
newContextInput: &Context{Context: context.Background()},
},
{
testCase: "empty_set_bool_and_present_ctx_bool",
setBoolInput: "",
ctxBoolInput: "ctx-bool",
newContextInput: nil,
},
{
testCase: "empty_set_bool_and_present_ctx_bool_with_background_context",
setBoolInput: "",
ctxBoolInput: "ctx-bool",
newContextInput: &Context{Context: context.Background()},
},
}
for _, test := range tdata {
t.Run(test.testCase, func(t *testing.T) {
// setup
set := flag.NewFlagSet("test", 0)
set.Bool(test.setBoolInput, false, "doc")
ctx := NewContext(nil, set, test.newContextInput)
// logic under test
value := ctx.Bool(test.ctxBoolInput)
// assertions
if value != false {
t.Errorf("expected test.value to be false, but it was not")
}
})
}
}
func TestCheckRequiredFlags(t *testing.T) { func TestCheckRequiredFlags(t *testing.T) {
tdata := []struct { tdata := []struct {
testCase string testCase string