commands: fix opt.Description panic when desc was empty

License: MIT
Signed-off-by: 's avatarJakub Sztandera <kubuxu@protonmail.ch>
上级 548490f5
......@@ -43,7 +43,10 @@ func (o *option) Type() reflect.Kind {
}
func (o *option) Description() string {
if o.description[len(o.description)-1] != '.' {
if len(o.description) == 0 {
return ""
}
if !strings.HasSuffix(o.description, ".") {
o.description += "."
}
if o.defaultVal != nil {
......
package commands
import "testing"
import (
"strings"
"testing"
)
func TestOptionValueExtractBoolNotFound(t *testing.T) {
t.Log("ensure that no error is returned when value is not found")
......@@ -27,3 +30,16 @@ func TestOptionValueExtractWrongType(t *testing.T) {
t.Fatal("No error returned. Failure.")
}
}
func TestLackOfDescriptionOfOptionDoesNotPanic(t *testing.T) {
opt := BoolOption("a", "")
opt.Description()
}
func TestDotIsAddedInDescripton(t *testing.T) {
opt := BoolOption("a", "desc without dot")
dest := opt.Description()
if !strings.HasSuffix(dest, ".") {
t.Fatal("dot should have been added at the end of description")
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论