short_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package flags
  2. import (
  3. "testing"
  4. )
  5. func TestShort(t *testing.T) {
  6. var opts = struct {
  7. Value bool `short:"v"`
  8. }{}
  9. ret := assertParseSuccess(t, &opts, "-v")
  10. assertStringArray(t, ret, []string{})
  11. if !opts.Value {
  12. t.Errorf("Expected Value to be true")
  13. }
  14. }
  15. func TestShortTooLong(t *testing.T) {
  16. var opts = struct {
  17. Value bool `short:"vv"`
  18. }{}
  19. assertParseFail(t, ErrShortNameTooLong, "short names can only be 1 character long, not `vv'", &opts)
  20. }
  21. func TestShortRequired(t *testing.T) {
  22. var opts = struct {
  23. Value bool `short:"v" required:"true"`
  24. }{}
  25. assertParseFail(t, ErrRequired, "the required flag `-v' was not specified", &opts)
  26. }
  27. func TestShortMultiConcat(t *testing.T) {
  28. var opts = struct {
  29. V bool `short:"v"`
  30. O bool `short:"o"`
  31. F bool `short:"f"`
  32. }{}
  33. ret := assertParseSuccess(t, &opts, "-vo", "-f")
  34. assertStringArray(t, ret, []string{})
  35. if !opts.V {
  36. t.Errorf("Expected V to be true")
  37. }
  38. if !opts.O {
  39. t.Errorf("Expected O to be true")
  40. }
  41. if !opts.F {
  42. t.Errorf("Expected F to be true")
  43. }
  44. }
  45. func TestShortMultiSlice(t *testing.T) {
  46. var opts = struct {
  47. Values []bool `short:"v"`
  48. }{}
  49. ret := assertParseSuccess(t, &opts, "-v", "-v")
  50. assertStringArray(t, ret, []string{})
  51. assertBoolArray(t, opts.Values, []bool{true, true})
  52. }
  53. func TestShortMultiSliceConcat(t *testing.T) {
  54. var opts = struct {
  55. Values []bool `short:"v"`
  56. }{}
  57. ret := assertParseSuccess(t, &opts, "-vvv")
  58. assertStringArray(t, ret, []string{})
  59. assertBoolArray(t, opts.Values, []bool{true, true, true})
  60. }
  61. func TestShortWithEqualArg(t *testing.T) {
  62. var opts = struct {
  63. Value string `short:"v"`
  64. }{}
  65. ret := assertParseSuccess(t, &opts, "-v=value")
  66. assertStringArray(t, ret, []string{})
  67. assertString(t, opts.Value, "value")
  68. }
  69. func TestShortWithArg(t *testing.T) {
  70. var opts = struct {
  71. Value string `short:"v"`
  72. }{}
  73. ret := assertParseSuccess(t, &opts, "-vvalue")
  74. assertStringArray(t, ret, []string{})
  75. assertString(t, opts.Value, "value")
  76. }
  77. func TestShortArg(t *testing.T) {
  78. var opts = struct {
  79. Value string `short:"v"`
  80. }{}
  81. ret := assertParseSuccess(t, &opts, "-v", "value")
  82. assertStringArray(t, ret, []string{})
  83. assertString(t, opts.Value, "value")
  84. }
  85. func TestShortMultiWithEqualArg(t *testing.T) {
  86. var opts = struct {
  87. F []bool `short:"f"`
  88. Value string `short:"v"`
  89. }{}
  90. assertParseFail(t, ErrExpectedArgument, "expected argument for flag `-v'", &opts, "-ffv=value")
  91. }
  92. func TestShortMultiArg(t *testing.T) {
  93. var opts = struct {
  94. F []bool `short:"f"`
  95. Value string `short:"v"`
  96. }{}
  97. ret := assertParseSuccess(t, &opts, "-ffv", "value")
  98. assertStringArray(t, ret, []string{})
  99. assertBoolArray(t, opts.F, []bool{true, true})
  100. assertString(t, opts.Value, "value")
  101. }
  102. func TestShortMultiArgConcatFail(t *testing.T) {
  103. var opts = struct {
  104. F []bool `short:"f"`
  105. Value string `short:"v"`
  106. }{}
  107. assertParseFail(t, ErrExpectedArgument, "expected argument for flag `-v'", &opts, "-ffvvalue")
  108. }
  109. func TestShortMultiArgConcat(t *testing.T) {
  110. var opts = struct {
  111. F []bool `short:"f"`
  112. Value string `short:"v"`
  113. }{}
  114. ret := assertParseSuccess(t, &opts, "-vff")
  115. assertStringArray(t, ret, []string{})
  116. assertString(t, opts.Value, "ff")
  117. }
  118. func TestShortOptional(t *testing.T) {
  119. var opts = struct {
  120. F []bool `short:"f"`
  121. Value string `short:"v" optional:"yes" optional-value:"value"`
  122. }{}
  123. ret := assertParseSuccess(t, &opts, "-fv", "f")
  124. assertStringArray(t, ret, []string{"f"})
  125. assertString(t, opts.Value, "value")
  126. }