2017-10-06 15:29:20 +00:00
|
|
|
package kingpin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2018-01-03 19:19:49 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-10-06 15:29:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFormatTwoColumns(t *testing.T) {
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
formatTwoColumns(buf, 2, 2, 20, [][2]string{
|
|
|
|
{"--hello", "Hello world help with something that is cool."},
|
|
|
|
})
|
|
|
|
expected := ` --hello Hello
|
|
|
|
world
|
|
|
|
help with
|
|
|
|
something
|
|
|
|
that is
|
|
|
|
cool.
|
|
|
|
`
|
|
|
|
assert.Equal(t, expected, buf.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFormatTwoColumnsWide(t *testing.T) {
|
|
|
|
samples := [][2]string{
|
|
|
|
{strings.Repeat("x", 29), "29 chars"},
|
|
|
|
{strings.Repeat("x", 30), "30 chars"}}
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
formatTwoColumns(buf, 0, 0, 200, samples)
|
|
|
|
expected := `xxxxxxxxxxxxxxxxxxxxxxxxxxxxx29 chars
|
|
|
|
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
|
|
|
30 chars
|
|
|
|
`
|
|
|
|
assert.Equal(t, expected, buf.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHiddenCommand(t *testing.T) {
|
|
|
|
templates := []struct{ name, template string }{
|
|
|
|
{"default", DefaultUsageTemplate},
|
|
|
|
{"Compact", CompactUsageTemplate},
|
|
|
|
{"Long", LongHelpTemplate},
|
|
|
|
{"Man", ManPageTemplate},
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
t.Log("1")
|
|
|
|
|
|
|
|
a := New("test", "Test").Writer(&buf).Terminate(nil)
|
|
|
|
a.Command("visible", "visible")
|
|
|
|
a.Command("hidden", "hidden").Hidden()
|
|
|
|
|
|
|
|
for _, tp := range templates {
|
|
|
|
buf.Reset()
|
|
|
|
a.UsageTemplate(tp.template)
|
|
|
|
a.Parse(nil)
|
|
|
|
// a.Parse([]string{"--help"})
|
|
|
|
usage := buf.String()
|
|
|
|
t.Logf("Usage for %s is:\n%s\n", tp.name, usage)
|
|
|
|
|
|
|
|
assert.NotContains(t, usage, "hidden")
|
|
|
|
assert.Contains(t, usage, "visible")
|
|
|
|
}
|
|
|
|
}
|