usage_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package kingpin
  2. import (
  3. "bytes"
  4. "strings"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestFormatTwoColumns(t *testing.T) {
  9. buf := bytes.NewBuffer(nil)
  10. formatTwoColumns(buf, 2, 2, 20, [][2]string{
  11. {"--hello", "Hello world help with something that is cool."},
  12. })
  13. expected := ` --hello Hello
  14. world
  15. help with
  16. something
  17. that is
  18. cool.
  19. `
  20. assert.Equal(t, expected, buf.String())
  21. }
  22. func TestFormatTwoColumnsWide(t *testing.T) {
  23. samples := [][2]string{
  24. {strings.Repeat("x", 29), "29 chars"},
  25. {strings.Repeat("x", 30), "30 chars"}}
  26. buf := bytes.NewBuffer(nil)
  27. formatTwoColumns(buf, 0, 0, 200, samples)
  28. expected := `xxxxxxxxxxxxxxxxxxxxxxxxxxxxx29 chars
  29. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  30. 30 chars
  31. `
  32. assert.Equal(t, expected, buf.String())
  33. }
  34. func TestHiddenCommand(t *testing.T) {
  35. templates := []struct{ name, template string }{
  36. {"default", DefaultUsageTemplate},
  37. {"Compact", CompactUsageTemplate},
  38. {"Long", LongHelpTemplate},
  39. {"Man", ManPageTemplate},
  40. }
  41. var buf bytes.Buffer
  42. t.Log("1")
  43. a := New("test", "Test").Writer(&buf).Terminate(nil)
  44. a.Command("visible", "visible")
  45. a.Command("hidden", "hidden").Hidden()
  46. for _, tp := range templates {
  47. buf.Reset()
  48. a.UsageTemplate(tp.template)
  49. a.Parse(nil)
  50. // a.Parse([]string{"--help"})
  51. usage := buf.String()
  52. t.Logf("Usage for %s is:\n%s\n", tp.name, usage)
  53. assert.NotContains(t, usage, "hidden")
  54. assert.Contains(t, usage, "visible")
  55. }
  56. }