Skip to content

Commit eae6428

Browse files
committed
Improve tests and code coverage
1 parent 98e47c8 commit eae6428

File tree

2 files changed

+253
-34
lines changed

2 files changed

+253
-34
lines changed

flagset/flagset_in_test.go

+157-34
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
. "github.com/smartystreets/goconvey/convey"
1515
)
1616

17-
func TestFlagSet(t *testing.T) {
17+
func TestFlagSet_1(t *testing.T) {
1818
Convey("should return the correct flag set and values", t, func() {
1919
flags01 := struct {
2020
Default bool `short:"d" long:"default" default:"false" description:"Default argument"`
@@ -647,51 +647,159 @@ func TestFlagSet(t *testing.T) {
647647
So(fmt.Sprint(flags01.Command), ShouldEqual, fmt.Sprint(v.value))
648648
}
649649
}
650+
})
651+
}
652+
653+
func TestFlagSet_2(t *testing.T) {
654+
Convey("should return the correct flag set and values", t, func() {
655+
flags01 := struct {
656+
String1 string `long:"s1"`
657+
String2 string `long:"s2"`
658+
CommandFoo struct {
659+
String3 string `long:"s3"`
660+
String4 string `long:"s4"`
661+
CommandBar struct {
662+
String5 string `long:"s5"`
663+
String6 string `long:"s6"`
664+
CommandBaz struct {
665+
String7 string `long:"s7"`
666+
String8 string `long:"s8"`
667+
CommandQux struct {
668+
String9 string `long:"s9"`
669+
String10 string `long:"s10"`
670+
} `command:"qux"`
671+
} `command:"baz"`
672+
} `command:"bar"`
673+
} `command:"foo"`
674+
}{}
675+
args := []string{
676+
"./app",
677+
"--s1=foo1",
678+
"foo",
679+
"--s3=foo3",
680+
"bar",
681+
"--s5=foo5",
682+
"baz",
683+
"--s7=foo7",
684+
"qux",
685+
"--s9=foo9",
686+
}
687+
flagSet, err := New(Options{Flags: &flags01, Args: args})
688+
So(err, ShouldBeNil)
689+
So(flagSet, ShouldNotBeNil)
690+
So(flagSet.Errors(), ShouldBeNil)
691+
So(flagSet.flags, ShouldHaveLength, 14)
692+
So(flags01.String1, ShouldEqual, "foo1")
693+
So(flags01.String2, ShouldEqual, "")
694+
So(flags01.CommandFoo.String3, ShouldEqual, "foo3")
695+
So(flags01.CommandFoo.String4, ShouldEqual, "")
696+
So(flags01.CommandFoo.CommandBar.String5, ShouldEqual, "foo5")
697+
So(flags01.CommandFoo.CommandBar.String6, ShouldEqual, "")
698+
So(flags01.CommandFoo.CommandBar.CommandBaz.String7, ShouldEqual, "foo7")
699+
So(flags01.CommandFoo.CommandBar.CommandBaz.String8, ShouldEqual, "")
700+
So(flags01.CommandFoo.CommandBar.CommandBaz.CommandQux.String9, ShouldEqual, "foo9")
701+
So(flags01.CommandFoo.CommandBar.CommandBaz.CommandQux.String10, ShouldEqual, "")
650702

651703
flags02 := struct {
652-
Foo bool `short:"f"`
653-
String string `short:"s"`
654-
CommandBar struct {
655-
String string `short:"s"`
656-
CommandQux struct {
657-
String string `short:"s"`
658-
Settings bool `settings:"true" allow-unknown-arg:"true"`
659-
} `command:"qux"`
660-
} `command:"bar"`
704+
String1 string `long:"s1"`
705+
String2 string `long:"s2"`
706+
CommandFoo struct {
707+
String3 string `long:"s3"`
708+
String4 string `long:"s4"`
709+
CommandBar struct {
710+
String5 string `long:"s5"`
711+
String6 string `long:"s6"`
712+
CommandBaz struct {
713+
String7 string `long:"s7"`
714+
String8 string `long:"s8"`
715+
CommandQux struct {
716+
String9 string `long:"s9"`
717+
String10 string `long:"s10"`
718+
} `command:"qux"`
719+
} `command:"baz"`
720+
} `command:"bar"`
721+
} `command:"foo"`
661722
}{}
662723
args = []string{
663724
"./app",
664-
"-f",
665-
"-s",
666-
"foo1",
667-
"-s=foo2",
725+
"--s2=foo2",
726+
"foo",
727+
"--s4=foo4",
668728
"bar",
669-
"-s=foo3",
729+
"--s6=foo6",
730+
"baz",
731+
"--s8=foo8",
670732
"qux",
671-
"-s=foo4",
672-
"quux",
733+
"--s10=foo10",
673734
}
674735
flagSet, err = New(Options{Flags: &flags02, Args: args})
675736
So(err, ShouldBeNil)
676737
So(flagSet, ShouldNotBeNil)
677738
So(flagSet.Errors(), ShouldBeNil)
678-
So(flags02.Foo, ShouldEqual, true)
679-
So(flags02.String, ShouldEqual, "foo2")
680-
So(flags02.CommandBar.String, ShouldEqual, "foo3")
681-
So(flags02.CommandBar.CommandQux.String, ShouldEqual, "foo4")
682-
So(flagSet.flags, ShouldHaveLength, 6)
683-
So(flagSet.flags[0].args, ShouldNotBeNil)
684-
So(flagSet.flags[0].args, ShouldHaveLength, 1)
685-
So(flagSet.flags[1].args, ShouldNotBeNil)
686-
So(flagSet.flags[1].args, ShouldHaveLength, 2)
687-
So(flagSet.flags[2].args, ShouldNotBeNil)
688-
So(flagSet.flags[2].args, ShouldHaveLength, 2)
689-
So(flagSet.flags[3].args, ShouldNotBeNil)
690-
So(flagSet.flags[3].args, ShouldHaveLength, 1)
691-
So(flagSet.flags[4].args, ShouldNotBeNil)
692-
So(flagSet.flags[4].args, ShouldHaveLength, 3)
693-
So(flagSet.flags[5].args, ShouldNotBeNil)
694-
So(flagSet.flags[5].args, ShouldHaveLength, 1)
739+
So(flagSet.flags, ShouldHaveLength, 14)
740+
So(flags02.String1, ShouldEqual, "")
741+
So(flags02.String2, ShouldEqual, "foo2")
742+
So(flags02.CommandFoo.String3, ShouldEqual, "")
743+
So(flags02.CommandFoo.String4, ShouldEqual, "foo4")
744+
So(flags02.CommandFoo.CommandBar.String5, ShouldEqual, "")
745+
So(flags02.CommandFoo.CommandBar.String6, ShouldEqual, "foo6")
746+
So(flags02.CommandFoo.CommandBar.CommandBaz.String7, ShouldEqual, "")
747+
So(flags02.CommandFoo.CommandBar.CommandBaz.String8, ShouldEqual, "foo8")
748+
So(flags02.CommandFoo.CommandBar.CommandBaz.CommandQux.String9, ShouldEqual, "")
749+
So(flags02.CommandFoo.CommandBar.CommandBaz.CommandQux.String10, ShouldEqual, "foo10")
750+
751+
flags03 := struct {
752+
String1 string `long:"s1"`
753+
String2 string `long:"s2"`
754+
CommandFoo struct {
755+
String1 string `long:"s1"`
756+
String2 string `long:"s2"`
757+
CommandBar struct {
758+
String1 string `long:"s1"`
759+
String2 string `long:"s2"`
760+
CommandBaz struct {
761+
String1 string `long:"s1"`
762+
String2 string `long:"s2"`
763+
CommandQux struct {
764+
String1 string `long:"s1"`
765+
String2 string `long:"s2"`
766+
} `command:"qux"`
767+
} `command:"baz"`
768+
} `command:"bar"`
769+
} `command:"foo"`
770+
}{}
771+
args = []string{
772+
"./app",
773+
"--s1=foo1",
774+
"--s2=foo2",
775+
"foo",
776+
"--s1=foo3",
777+
"--s2=foo4",
778+
"bar",
779+
"--s1=foo5",
780+
"--s2=foo6",
781+
"baz",
782+
"--s1=foo7",
783+
"--s2=foo8",
784+
"qux",
785+
"--s1=foo9",
786+
"--s2=foo10",
787+
}
788+
flagSet, err = New(Options{Flags: &flags03, Args: args})
789+
So(err, ShouldBeNil)
790+
So(flagSet, ShouldNotBeNil)
791+
So(flagSet.Errors(), ShouldBeNil)
792+
So(flagSet.flags, ShouldHaveLength, 14)
793+
So(flags03.String1, ShouldEqual, "foo1")
794+
So(flags03.String2, ShouldEqual, "foo2")
795+
So(flags03.CommandFoo.String1, ShouldEqual, "foo3")
796+
So(flags03.CommandFoo.String2, ShouldEqual, "foo4")
797+
So(flags03.CommandFoo.CommandBar.String1, ShouldEqual, "foo5")
798+
So(flags03.CommandFoo.CommandBar.String2, ShouldEqual, "foo6")
799+
So(flags03.CommandFoo.CommandBar.CommandBaz.String1, ShouldEqual, "foo7")
800+
So(flags03.CommandFoo.CommandBar.CommandBaz.String2, ShouldEqual, "foo8")
801+
So(flags03.CommandFoo.CommandBar.CommandBaz.CommandQux.String1, ShouldEqual, "foo9")
802+
So(flags03.CommandFoo.CommandBar.CommandBaz.CommandQux.String2, ShouldEqual, "foo10")
695803
})
696804
}
697805

@@ -803,6 +911,21 @@ func TestFlagSet_parseSettings(t *testing.T) {
803911
So(flagSet, ShouldNotBeNil)
804912
flagErrors := flagSet.Errors()
805913
So(flagErrors, ShouldContain, errors.New("duplicate settings tag for `Foo` and `Settings` flags"))
914+
915+
flags02 := struct {
916+
CommandBar struct {
917+
Settings bool `settings:"true" allow-unknown-arg:"true"`
918+
} `command:"bar"`
919+
}{}
920+
args = []string{
921+
"./app",
922+
"bar",
923+
"foo",
924+
}
925+
flagSet, err = New(Options{Flags: &flags02, Args: args})
926+
So(err, ShouldBeNil)
927+
So(flagSet, ShouldNotBeNil)
928+
So(flagSet.Errors(), ShouldBeNil)
806929
})
807930
}
808931

gocmd_test.go

+96
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,60 @@ func ExampleNew_usage() {
265265
resetArgs()
266266
}
267267

268+
func ExampleNew_usage_h() {
269+
os.Args = []string{"gocmd.test", "-h"}
270+
271+
_, err := gocmd.New(gocmd.Options{
272+
Name: "basic",
273+
Version: "1.0.0",
274+
Description: "A basic app",
275+
Flags: &struct {
276+
Help bool `short:"h" long:"help" description:"Display usage" global:"true"`
277+
}{},
278+
AutoHelp: true,
279+
AnyError: true,
280+
})
281+
if err != nil {
282+
log.Fatal(err)
283+
}
284+
// Output:
285+
// Usage: basic [options...]
286+
//
287+
// A basic app
288+
//
289+
// Options:
290+
// -h, --help Display usage
291+
292+
resetArgs()
293+
}
294+
295+
func ExampleNew_usage_help() {
296+
os.Args = []string{"gocmd.test", "--help"}
297+
298+
_, err := gocmd.New(gocmd.Options{
299+
Name: "basic",
300+
Version: "1.0.0",
301+
Description: "A basic app",
302+
Flags: &struct {
303+
Help bool `long:"help" description:"Display usage" global:"true"`
304+
}{},
305+
AutoHelp: true,
306+
AnyError: true,
307+
})
308+
if err != nil {
309+
log.Fatal(err)
310+
}
311+
// Output:
312+
// Usage: basic [options...]
313+
//
314+
// A basic app
315+
//
316+
// Options:
317+
// --help Display usage
318+
319+
resetArgs()
320+
}
321+
268322
func ExampleNew_version() {
269323
os.Args = []string{"gocmd.test", "-vv"}
270324

@@ -289,6 +343,48 @@ func ExampleNew_version() {
289343
resetArgs()
290344
}
291345

346+
func ExampleNew_version_v() {
347+
os.Args = []string{"gocmd.test", "-v"}
348+
349+
_, err := gocmd.New(gocmd.Options{
350+
Name: "basic",
351+
Version: "1.0.0",
352+
Description: "A basic app",
353+
Flags: &struct {
354+
Version bool `short:"v" long:"version" description:"Display version"`
355+
}{},
356+
AutoVersion: true,
357+
})
358+
if err != nil {
359+
log.Fatal(err)
360+
}
361+
// Output:
362+
// 1.0.0
363+
364+
resetArgs()
365+
}
366+
367+
func ExampleNew_version_version() {
368+
os.Args = []string{"gocmd.test", "--version"}
369+
370+
_, err := gocmd.New(gocmd.Options{
371+
Name: "basic",
372+
Version: "1.0.0",
373+
Description: "A basic app",
374+
Flags: &struct {
375+
Version bool `long:"version" description:"Display version"`
376+
}{},
377+
AutoVersion: true,
378+
})
379+
if err != nil {
380+
log.Fatal(err)
381+
}
382+
// Output:
383+
// 1.0.0
384+
385+
resetArgs()
386+
}
387+
292388
func ExampleNew_command() {
293389
os.Args = []string{"gocmd.test", "math", "sqrt", "-n=9"}
294390

0 commit comments

Comments
 (0)