short_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package flags
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. func TestShort(t *testing.T) {
  7. var opts = struct {
  8. Value bool `short:"v"`
  9. }{}
  10. ret := assertParseSuccess(t, &opts, "-v")
  11. assertStringArray(t, ret, []string{})
  12. if !opts.Value {
  13. t.Errorf("Expected Value to be true")
  14. }
  15. }
  16. func TestShortTooLong(t *testing.T) {
  17. var opts = struct {
  18. Value bool `short:"vv"`
  19. }{}
  20. assertParseFail(t, ErrShortNameTooLong, "short names can only be 1 character long, not `vv'", &opts)
  21. }
  22. func TestShortRequired(t *testing.T) {
  23. var opts = struct {
  24. Value bool `short:"v" required:"true"`
  25. }{}
  26. assertParseFail(t, ErrRequired, fmt.Sprintf("the required flag `%cv' was not specified", defaultShortOptDelimiter), &opts)
  27. }
  28. func TestShortRequiredFalsy1(t *testing.T) {
  29. var opts = struct {
  30. Value bool `short:"v" required:"false"`
  31. }{}
  32. assertParseSuccess(t, &opts)
  33. }
  34. func TestShortRequiredFalsy2(t *testing.T) {
  35. var opts = struct {
  36. Value bool `short:"v" required:"no"`
  37. }{}
  38. assertParseSuccess(t, &opts)
  39. }
  40. func TestShortMultiConcat(t *testing.T) {
  41. var opts = struct {
  42. V bool `short:"v"`
  43. O bool `short:"o"`
  44. F bool `short:"f"`
  45. }{}
  46. ret := assertParseSuccess(t, &opts, "-vo", "-f")
  47. assertStringArray(t, ret, []string{})
  48. if !opts.V {
  49. t.Errorf("Expected V to be true")
  50. }
  51. if !opts.O {
  52. t.Errorf("Expected O to be true")
  53. }
  54. if !opts.F {
  55. t.Errorf("Expected F to be true")
  56. }
  57. }
  58. func TestShortMultiRequiredConcat(t *testing.T) {
  59. var opts = struct {
  60. V bool `short:"v" required:"true"`
  61. O bool `short:"o" required:"true"`
  62. F bool `short:"f" required:"true"`
  63. }{}
  64. ret := assertParseSuccess(t, &opts, "-vo", "-f")
  65. assertStringArray(t, ret, []string{})
  66. if !opts.V {
  67. t.Errorf("Expected V to be true")
  68. }
  69. if !opts.O {
  70. t.Errorf("Expected O to be true")
  71. }
  72. if !opts.F {
  73. t.Errorf("Expected F to be true")
  74. }
  75. }
  76. func TestShortMultiSlice(t *testing.T) {
  77. var opts = struct {
  78. Values []bool `short:"v"`
  79. }{}
  80. ret := assertParseSuccess(t, &opts, "-v", "-v")
  81. assertStringArray(t, ret, []string{})
  82. assertBoolArray(t, opts.Values, []bool{true, true})
  83. }
  84. func TestShortMultiSliceConcat(t *testing.T) {
  85. var opts = struct {
  86. Values []bool `short:"v"`
  87. }{}
  88. ret := assertParseSuccess(t, &opts, "-vvv")
  89. assertStringArray(t, ret, []string{})
  90. assertBoolArray(t, opts.Values, []bool{true, true, true})
  91. }
  92. func TestShortWithEqualArg(t *testing.T) {
  93. var opts = struct {
  94. Value string `short:"v"`
  95. }{}
  96. ret := assertParseSuccess(t, &opts, "-v=value")
  97. assertStringArray(t, ret, []string{})
  98. assertString(t, opts.Value, "value")
  99. }
  100. func TestShortWithArg(t *testing.T) {
  101. var opts = struct {
  102. Value string `short:"v"`
  103. }{}
  104. ret := assertParseSuccess(t, &opts, "-vvalue")
  105. assertStringArray(t, ret, []string{})
  106. assertString(t, opts.Value, "value")
  107. }
  108. func TestShortArg(t *testing.T) {
  109. var opts = struct {
  110. Value string `short:"v"`
  111. }{}
  112. ret := assertParseSuccess(t, &opts, "-v", "value")
  113. assertStringArray(t, ret, []string{})
  114. assertString(t, opts.Value, "value")
  115. }
  116. func TestShortMultiWithEqualArg(t *testing.T) {
  117. var opts = struct {
  118. F []bool `short:"f"`
  119. Value string `short:"v"`
  120. }{}
  121. assertParseFail(t, ErrExpectedArgument, fmt.Sprintf("expected argument for flag `%cv'", defaultShortOptDelimiter), &opts, "-ffv=value")
  122. }
  123. func TestShortMultiArg(t *testing.T) {
  124. var opts = struct {
  125. F []bool `short:"f"`
  126. Value string `short:"v"`
  127. }{}
  128. ret := assertParseSuccess(t, &opts, "-ffv", "value")
  129. assertStringArray(t, ret, []string{})
  130. assertBoolArray(t, opts.F, []bool{true, true})
  131. assertString(t, opts.Value, "value")
  132. }
  133. func TestShortMultiArgConcatFail(t *testing.T) {
  134. var opts = struct {
  135. F []bool `short:"f"`
  136. Value string `short:"v"`
  137. }{}
  138. assertParseFail(t, ErrExpectedArgument, fmt.Sprintf("expected argument for flag `%cv'", defaultShortOptDelimiter), &opts, "-ffvvalue")
  139. }
  140. func TestShortMultiArgConcat(t *testing.T) {
  141. var opts = struct {
  142. F []bool `short:"f"`
  143. Value string `short:"v"`
  144. }{}
  145. ret := assertParseSuccess(t, &opts, "-vff")
  146. assertStringArray(t, ret, []string{})
  147. assertString(t, opts.Value, "ff")
  148. }
  149. func TestShortOptional(t *testing.T) {
  150. var opts = struct {
  151. F []bool `short:"f"`
  152. Value string `short:"v" optional:"yes" optional-value:"value"`
  153. }{}
  154. ret := assertParseSuccess(t, &opts, "-fv", "f")
  155. assertStringArray(t, ret, []string{"f"})
  156. assertString(t, opts.Value, "value")
  157. }
  158. func TestShortOptionalFalsy1(t *testing.T) {
  159. var opts = struct {
  160. F []bool `short:"f"`
  161. Value string `short:"v" optional:"false" optional-value:"value"`
  162. }{}
  163. ret := assertParseSuccess(t, &opts, "-fv", "f")
  164. assertStringArray(t, ret, []string{})
  165. assertString(t, opts.Value, "f")
  166. }
  167. func TestShortOptionalFalsy2(t *testing.T) {
  168. var opts = struct {
  169. F []bool `short:"f"`
  170. Value string `short:"v" optional:"no" optional-value:"value"`
  171. }{}
  172. ret := assertParseSuccess(t, &opts, "-fv", "f")
  173. assertStringArray(t, ret, []string{})
  174. assertString(t, opts.Value, "f")
  175. }