Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinJWendt committed Jun 20, 2022
1 parent 22dee76 commit 0609e02
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 3 deletions.
8 changes: 7 additions & 1 deletion _examples/interactive_multiselect/demo/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ func init() {
time.Sleep(time.Second)
for i := 0; i < 10; i++ {
keyboard.SimulateKeyPress(keys.Down)
time.Sleep(time.Millisecond * 250)
if i%2 == 0 {
time.Sleep(time.Millisecond * 100)
keyboard.SimulateKeyPress(keys.Enter)
}
time.Sleep(time.Millisecond * 500)
}
time.Sleep(time.Second)

Expand All @@ -33,6 +37,8 @@ func init() {
}

keyboard.SimulateKeyPress(keys.Enter)
time.Sleep(time.Millisecond * 350)
keyboard.SimulateKeyPress(keys.Tab)
}()
}
}
10 changes: 9 additions & 1 deletion interactive_confirm_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var (
// Pressing enter without typing "y" or "n" will return the configured default value (by default set to "no").
DefaultInteractiveConfirm = InteractiveConfirmPrinter{
DefaultValue: false,
DefaultText: "Please confirm",
TextStyle: &ThemeDefault.PrimaryStyle,
ConfirmText: "Yes",
ConfirmStyle: &ThemeDefault.SuccessMessageStyle,
Expand All @@ -27,6 +28,7 @@ var (
// InteractiveConfirmPrinter is a printer for interactive confirm prompts.
type InteractiveConfirmPrinter struct {
DefaultValue bool
DefaultText string
TextStyle *Style
ConfirmText string
ConfirmStyle *Style
Expand All @@ -35,6 +37,12 @@ type InteractiveConfirmPrinter struct {
SuffixStyle *Style
}

// WithDefaultText sets the default text.
func (p InteractiveConfirmPrinter) WithDefaultText(text string) *InteractiveConfirmPrinter {
p.DefaultText = text
return &p
}

// WithDefaultValue sets the default value, which will be returned when the user presses enter without typing "y" or "n".
func (p InteractiveConfirmPrinter) WithDefaultValue(value bool) *InteractiveConfirmPrinter {
p.DefaultValue = value
Expand Down Expand Up @@ -86,7 +94,7 @@ func (p InteractiveConfirmPrinter) Show(text ...string) (bool, error) {
var result bool

if len(text) == 0 || text[0] == "" {
text = []string{"Please confirm"}
text = []string{p.DefaultText}
}

p.TextStyle.Print(text[0] + " " + p.getSuffix() + ": ")
Expand Down
40 changes: 40 additions & 0 deletions interactive_confirm_printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"atomicgo.dev/keyboard"
"atomicgo.dev/keyboard/keys"
"github.com/MarvinJWendt/testza"

"github.com/pterm/pterm"
)

Expand Down Expand Up @@ -47,3 +48,42 @@ func TestInteractiveConfirmPrinter_WithDefaultValue_true(t *testing.T) {
result, _ := p.Show()
testza.AssertTrue(t, result)
}

func TestInteractiveConfirmPrinter_WithConfirmStyle(t *testing.T) {
style := pterm.NewStyle(pterm.FgRed)
p := pterm.DefaultInteractiveConfirm.WithConfirmStyle(style)
testza.AssertEqual(t, p.ConfirmStyle, style)
}

func TestInteractiveConfirmPrinter_WithConfirmText(t *testing.T) {
p := pterm.DefaultInteractiveConfirm.WithConfirmText("confirm")
testza.AssertEqual(t, p.ConfirmText, "confirm")
}

func TestInteractiveConfirmPrinter_WithDefaultText(t *testing.T) {
p := pterm.DefaultInteractiveConfirm.WithDefaultText("default")
testza.AssertEqual(t, p.DefaultText, "default")
}

func TestInteractiveConfirmPrinter_WithRejectStyle(t *testing.T) {
style := pterm.NewStyle(pterm.FgRed)
p := pterm.DefaultInteractiveConfirm.WithRejectStyle(style)
testza.AssertEqual(t, p.RejectStyle, style)
}

func TestInteractiveConfirmPrinter_WithRejectText(t *testing.T) {
p := pterm.DefaultInteractiveConfirm.WithRejectText("reject")
testza.AssertEqual(t, p.RejectText, "reject")
}

func TestInteractiveConfirmPrinter_WithSuffixStyle(t *testing.T) {
style := pterm.NewStyle(pterm.FgRed)
p := pterm.DefaultInteractiveConfirm.WithSuffixStyle(style)
testza.AssertEqual(t, p.SuffixStyle, style)
}

func TestInteractiveConfirmPrinter_WithTextStyle(t *testing.T) {
style := pterm.NewStyle(pterm.FgRed)
p := pterm.DefaultInteractiveConfirm.WithTextStyle(style)
testza.AssertEqual(t, p.TextStyle, style)
}
2 changes: 1 addition & 1 deletion interactive_multiselect_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func (p *InteractiveMultiselectPrinter) selectOption(optionText string) {

func (p *InteractiveMultiselectPrinter) renderSelectMenu() string {
var content string
content += Sprintf("%s %s: %s\n", p.text, ThemeDefault.SecondaryStyle.Sprint("[type to search]"), p.fuzzySearchString)
content += Sprintf("%s %s: %s\n", p.text, ThemeDefault.SecondaryStyle.Sprint("[select with enter, type to search, confirm with tab]"), p.fuzzySearchString)

// find options that match fuzzy search string
rankedResults := fuzzy.RankFindFold(p.fuzzySearchString, p.Options)
Expand Down
6 changes: 6 additions & 0 deletions interactive_select_printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"atomicgo.dev/keyboard"
"atomicgo.dev/keyboard/keys"
"github.com/MarvinJWendt/testza"

"github.com/pterm/pterm"
)

Expand All @@ -19,6 +20,11 @@ func TestInteractiveSelectPrinter_Show(t *testing.T) {
testza.AssertEqual(t, "d", result)
}

func TestInteractiveSelectPrinter_WithDefaultText(t *testing.T) {
p := pterm.DefaultInteractiveSelect.WithDefaultText("default")
testza.AssertEqual(t, p.DefaultText, "default")
}

func TestInteractiveSelectPrinter_WithDefaultOption(t *testing.T) {
p := pterm.DefaultInteractiveSelect.WithDefaultOption("default")
testza.AssertEqual(t, p.DefaultOption, "default")
Expand Down
30 changes: 30 additions & 0 deletions interactive_textinput_printer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package pterm_test

import (
"testing"

"github.com/MarvinJWendt/testza"

"github.com/pterm/pterm"
)

func TestInteractiveTextInputPrinter_WithDefaultText(t *testing.T) {
p := pterm.DefaultInteractiveTextInput.WithDefaultText("default")
testza.AssertEqual(t, p.DefaultText, "default")
}

func TestInteractiveTextInputPrinter_WithMultiLine_true(t *testing.T) {
p := pterm.DefaultInteractiveTextInput.WithMultiLine()
testza.AssertTrue(t, p.MultiLine)
}

func TestInteractiveTextInputPrinter_WithMultiLine_false(t *testing.T) {
p := pterm.DefaultInteractiveTextInput.WithMultiLine(false)
testza.AssertFalse(t, p.MultiLine)
}

func TestInteractiveTextInputPrinter_WithTextStyle(t *testing.T) {
style := pterm.NewStyle(pterm.FgRed)
p := pterm.DefaultInteractiveTextInput.WithTextStyle(style)
testza.AssertEqual(t, p.TextStyle, style)
}

0 comments on commit 0609e02

Please sign in to comment.