commands: fix opt.Description panic when desc was empty
License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
Jakub Sztandera committed
Dec 19, 2016 at 14:59 UTC
591e82618ea071b6509467c21fcc5a590b9cdfde
2 files changed
+21
-2
commands/option.go
+4
-1
@@ -43,7 +43,10 @@ func (o *option) Type() reflect.Kind {
43
}
44
45
func (o *option) Description() string {
46
- if o.description[len(o.description)-1] != '.' {
46
+ if len(o.description) == 0 {
47
+ return ""
48
+ }
49
+ if !strings.HasSuffix(o.description, ".") {
50
o.description += "."
51
}
52
if o.defaultVal != nil {
commands/option_test.go
+17
-1
@@ -1,6 +1,9 @@
1
package commands
2
3
-import "testing"
3
+import (
4
+ "strings"
5
+ "testing"
6
+)
7
8
func TestOptionValueExtractBoolNotFound(t *testing.T) {
9
t.Log("ensure that no error is returned when value is not found")
@@ -27,3 +30,16 @@ func TestOptionValueExtractWrongType(t *testing.T) {
30
t.Fatal("No error returned. Failure.")
31
}
32
}
33
+
34
+func TestLackOfDescriptionOfOptionDoesNotPanic(t *testing.T) {
35
+ opt := BoolOption("a", "")
36
+ opt.Description()
37
+}
38
+
39
+func TestDotIsAddedInDescripton(t *testing.T) {
40
+ opt := BoolOption("a", "desc without dot")
41
+ dest := opt.Description()
42
+ if !strings.HasSuffix(dest, ".") {
43
+ t.Fatal("dot should have been added at the end of description")
44
+ }
45
+}