-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package genericcli | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/metal-stack/metal-lib/pkg/testcommon" | ||
) | ||
|
||
func TestPromptCustom(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
c *PromptConfig | ||
input string | ||
want string | ||
wantErr error | ||
}{ | ||
{ | ||
name: "default prompt config answered with yes", | ||
input: "yes\n", | ||
want: "Do you want to continue? [y/n] ", | ||
}, | ||
{ | ||
name: "default prompt config answered with no", | ||
input: "no\n", | ||
want: "Do you want to continue? [y/n] ", | ||
wantErr: fmt.Errorf(`aborting due to given answer ("no")`), | ||
}, | ||
{ | ||
name: "custom prompt config", | ||
input: "ack\n", | ||
c: &PromptConfig{ | ||
Message: "Do you get it?", | ||
ShowAnswers: true, | ||
AcceptedAnswers: []string{"ack", "a"}, | ||
DefaultAnswer: "ack", | ||
No: "nack", | ||
}, | ||
want: "Do you get it? [Ack/nack] ", | ||
}, | ||
{ | ||
name: "custom prompt config, default is no answer", | ||
input: "ack\n", | ||
c: &PromptConfig{ | ||
Message: "Do you get it?", | ||
ShowAnswers: true, | ||
AcceptedAnswers: []string{"ack", "a"}, | ||
DefaultAnswer: "nack", | ||
No: "nack", | ||
}, | ||
want: "Do you get it? [ack/Nack] ", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var ( | ||
in bytes.Buffer | ||
out bytes.Buffer | ||
) | ||
|
||
if tt.c == nil { | ||
tt.c = promptDefaultConfig() | ||
} | ||
tt.c.In = &in | ||
tt.c.Out = &out | ||
|
||
in.WriteString(tt.input) | ||
|
||
err := PromptCustom(tt.c) | ||
if diff := cmp.Diff(tt.wantErr, err, testcommon.ErrorStringComparer()); diff != "" { | ||
t.Errorf("error diff (+got -want):\n %s", diff) | ||
} | ||
if diff := cmp.Diff(tt.want, out.String()); diff != "" { | ||
t.Errorf("diff (+got -want):\n %s", diff) | ||
} | ||
}) | ||
} | ||
} |