group_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package flags
  2. import (
  3. "testing"
  4. )
  5. func TestGroupInline(t *testing.T) {
  6. var opts = struct {
  7. Value bool `short:"v"`
  8. Group struct {
  9. G bool `short:"g"`
  10. } `group:"Grouped Options"`
  11. }{}
  12. p, ret := assertParserSuccess(t, &opts, "-v", "-g")
  13. assertStringArray(t, ret, []string{})
  14. if !opts.Value {
  15. t.Errorf("Expected Value to be true")
  16. }
  17. if !opts.Group.G {
  18. t.Errorf("Expected Group.G to be true")
  19. }
  20. if p.Command.Group.Find("Grouped Options") == nil {
  21. t.Errorf("Expected to find group `Grouped Options'")
  22. }
  23. }
  24. func TestGroupAdd(t *testing.T) {
  25. var opts = struct {
  26. Value bool `short:"v"`
  27. }{}
  28. var grp = struct {
  29. G bool `short:"g"`
  30. }{}
  31. p := NewParser(&opts, Default)
  32. g, err := p.AddGroup("Grouped Options", "", &grp)
  33. if err != nil {
  34. t.Fatalf("Unexpected error: %v", err)
  35. return
  36. }
  37. ret, err := p.ParseArgs([]string{"-v", "-g", "rest"})
  38. if err != nil {
  39. t.Fatalf("Unexpected error: %v", err)
  40. return
  41. }
  42. assertStringArray(t, ret, []string{"rest"})
  43. if !opts.Value {
  44. t.Errorf("Expected Value to be true")
  45. }
  46. if !grp.G {
  47. t.Errorf("Expected Group.G to be true")
  48. }
  49. if p.Command.Group.Find("Grouped Options") != g {
  50. t.Errorf("Expected to find group `Grouped Options'")
  51. }
  52. if p.Groups()[1] != g {
  53. t.Errorf("Espected group #v, but got #v", g, p.Groups()[0])
  54. }
  55. if g.Options()[0].ShortName != 'g' {
  56. t.Errorf("Expected short name `g' but got %v", g.Options()[0].ShortName)
  57. }
  58. }
  59. func TestGroupNestedInline(t *testing.T) {
  60. var opts = struct {
  61. Value bool `short:"v"`
  62. Group struct {
  63. G bool `short:"g"`
  64. Nested struct {
  65. N string `long:"n"`
  66. } `group:"Nested Options"`
  67. } `group:"Grouped Options"`
  68. }{}
  69. p, ret := assertParserSuccess(t, &opts, "-v", "-g", "--n", "n", "rest")
  70. assertStringArray(t, ret, []string{"rest"})
  71. if !opts.Value {
  72. t.Errorf("Expected Value to be true")
  73. }
  74. if !opts.Group.G {
  75. t.Errorf("Expected Group.G to be true")
  76. }
  77. assertString(t, opts.Group.Nested.N, "n")
  78. if p.Command.Group.Find("Grouped Options") == nil {
  79. t.Errorf("Expected to find group `Grouped Options'")
  80. }
  81. if p.Command.Group.Find("Nested Options") == nil {
  82. t.Errorf("Expected to find group `Nested Options'")
  83. }
  84. }
  85. func TestDuplicateShortFlags(t *testing.T) {
  86. var opts struct {
  87. Verbose []bool `short:"v" long:"verbose" description:"Show verbose debug information"`
  88. Variables []string `short:"v" long:"variable" description:"Set a variable value."`
  89. }
  90. args := []string{
  91. "--verbose",
  92. "-v", "123",
  93. "-v", "456",
  94. }
  95. _, err := ParseArgs(&opts, args)
  96. if err == nil {
  97. t.Errorf("Expected an error with type ErrDuplicatedFlag")
  98. } else {
  99. err2 := err.(*Error)
  100. if err2.Type != ErrDuplicatedFlag {
  101. t.Errorf("Expected an error with type ErrDuplicatedFlag")
  102. }
  103. }
  104. }
  105. func TestDuplicateLongFlags(t *testing.T) {
  106. var opts struct {
  107. Test1 []bool `short:"a" long:"testing" description:"Test 1"`
  108. Test2 []string `short:"b" long:"testing" description:"Test 2."`
  109. }
  110. args := []string{
  111. "--testing",
  112. }
  113. _, err := ParseArgs(&opts, args)
  114. if err == nil {
  115. t.Errorf("Expected an error with type ErrDuplicatedFlag")
  116. } else {
  117. err2 := err.(*Error)
  118. if err2.Type != ErrDuplicatedFlag {
  119. t.Errorf("Expected an error with type ErrDuplicatedFlag")
  120. }
  121. }
  122. }