command_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package cli
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. )
  6. func TestCommandManagerNoCommands(t *testing.T) {
  7. m := commandManager{}
  8. _, _, err := m.parse()
  9. require.Error(t, err)
  10. t.Log("error:", err)
  11. }
  12. func TestCommandManagerRegisterRoot(t *testing.T) {
  13. opts := struct {
  14. Int int
  15. String string
  16. }{
  17. Int: 42,
  18. String: "foo",
  19. }
  20. m := commandManager{}
  21. m.register().
  22. Help("Test command.").
  23. Options(&opts)
  24. cmd, _, err := m.parse()
  25. require.NoError(t, err)
  26. require.Empty(t, cmd)
  27. require.Equal(t, 42, opts.Int)
  28. require.Equal(t, "foo", opts.String)
  29. }
  30. func TestCommandManagerRegisterRootWithOptions(t *testing.T) {
  31. opts := struct {
  32. Int int
  33. String string
  34. }{}
  35. m := commandManager{}
  36. m.register().
  37. Help("Test command.").
  38. Options(&opts)
  39. cmd, _, err := m.parse("-int", "21", "-string", "bar")
  40. require.NoError(t, err)
  41. require.Empty(t, cmd)
  42. require.Equal(t, 21, opts.Int)
  43. require.Equal(t, "bar", opts.String)
  44. }
  45. func TestCommandManagerRegisterWithBadOptions(t *testing.T) {
  46. opts := struct {
  47. Int int
  48. String string
  49. }{}
  50. m := commandManager{}
  51. m.register().
  52. Help("Test command.").
  53. Options(opts)
  54. _, _, err := m.parse("-int", "21", "-string", "bar")
  55. require.Error(t, err)
  56. t.Log("error:", err)
  57. }
  58. func TestCommandManagerRegisterMultiple(t *testing.T) {
  59. optsBar := struct {
  60. Int int
  61. String string
  62. }{}
  63. optsBoo := optsBar
  64. m := commandManager{}
  65. m.register("foo", "bar").
  66. Help("Test command.").
  67. Options(&optsBar)
  68. m.register("foo", "boo").
  69. Help("Test command 2.").
  70. Options(&optsBoo)
  71. cmd, _, err := m.parse("foo", "boo", "-int", "21", "-string", "bar")
  72. require.NoError(t, err)
  73. require.Equal(t, "foo boo", cmd)
  74. require.Equal(t, 21, optsBoo.Int)
  75. require.Equal(t, "bar", optsBoo.String)
  76. require.Zero(t, optsBar.Int)
  77. require.Zero(t, optsBar.String)
  78. }
  79. func TestCommandeString(t *testing.T) {
  80. tests := []struct {
  81. scenario string
  82. command []string
  83. expected string
  84. }{
  85. {
  86. scenario: "empty command",
  87. },
  88. {
  89. scenario: "command with one element",
  90. command: []string{"test"},
  91. expected: "test",
  92. },
  93. {
  94. scenario: "command with multiple elements",
  95. command: []string{"test", "foo", "bar"},
  96. expected: "test foo bar",
  97. },
  98. {
  99. scenario: "command with elements with trailing spaces",
  100. command: []string{"\ttest ", "foo\n", " bar"},
  101. expected: "test foo bar",
  102. },
  103. {
  104. scenario: "command with elements with empty elements",
  105. command: []string{"", "foo", "bar"},
  106. expected: "foo bar",
  107. },
  108. }
  109. for _, test := range tests {
  110. t.Run(test.scenario, func(t *testing.T) {
  111. cmd := commandString(test.command...)
  112. require.Equal(t, test.expected, cmd)
  113. })
  114. }
  115. }
  116. func TestCommandEndIndex(t *testing.T) {
  117. tests := []struct {
  118. scenario string
  119. args []string
  120. expectedIndex int
  121. }{
  122. {
  123. scenario: "args with an option at the beginning",
  124. args: []string{"-v", "foo", "bar"},
  125. expectedIndex: 0,
  126. },
  127. {
  128. scenario: "args with an option at the end",
  129. args: []string{"foo", "bar", "-v"},
  130. expectedIndex: 2,
  131. },
  132. {
  133. scenario: "args with an option at the middle",
  134. args: []string{"foo", "foo-bar", "-v", "bar"},
  135. expectedIndex: 2,
  136. },
  137. {
  138. scenario: "args without option",
  139. args: []string{"foo", "foo-bar", "bar"},
  140. expectedIndex: 3,
  141. },
  142. }
  143. for _, test := range tests {
  144. t.Run(test.scenario, func(t *testing.T) {
  145. idx := commandEndIndex(test.args)
  146. require.Equal(t, test.expectedIndex, idx)
  147. })
  148. }
  149. }