Tests with AlecAivazis/survey
and cucumber/godog
Go >= 1.17
go get go.nhat.io/surveysteps
Type | Supported | Supported Actions |
---|---|---|
Confirm |
✓ |
|
Editor |
✘ | |
Input |
✘ | |
Multiline |
✓ |
|
Multiselect |
✘ | |
Password |
✓ |
|
Select |
✘ |
Step 1: Register to godog
Initialize a surveysteps.Manager
with surveysteps.New()
then add it into the ScenarioInitializer
Step 2: Pass stdio
to the prompts
Same as surveyexpect
, you have to define a way to inject terminal.Stdio
into the prompts in your code. For
every scenario, the manager will start a new terminal emulator. Without the injection, there is no way to capture and response to the prompts.
You can register to the Start
event and use the provided terminal.Stdio
accordingly.
For example:
package mypackage
import (
"math/rand"
"testing"
survey "github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/terminal"
"github.com/cucumber/godog"
"go.nhat.io/surveyexpect/options"
"go.nhat.io/surveysteps"
)
type Wizard struct {
stdio terminal.Stdio
}
func (w *Wizard) start(_ *godog.Scenario, stdio terminal.Stdio) {
w.stdio = stdio
}
func (w *Wizard) ask() (bool, error) {
var response bool
p := &survey.Confirm{Message: "Confirm?"}
err := survey.AskOne(p, &response, options.WithStdio(w.stdio))
return response, err
}
func TestIntegration(t *testing.T) {
wizard := &Wizard{}
m := surveysteps.New(t).
WithStarter(wizard.start)
suite := godog.TestSuite{
Name: "Integration",
ScenarioInitializer: func(ctx *godog.ScenarioContext) {
m.RegisterContext(ctx)
},
Options: &godog.Options{
Strict: true,
Output: out,
Randomize: rand.Int63(),
},
}
// Run the suite that triggers wizard.ask()
}
See more: #Examples
Expect to see a Confirm prompt and answer yes
.
Pattern: (?:(?:get)|(?:see))s? a(?:nother)? confirm prompt "([^"]*)".* answers? yes
Example:
Scenario: Receive a yes
Given I see a confirm prompt "Confirm? (y/N)", I answer yes
Then ask for confirm "Confirm?", receive yes
Expect to see a Confirm prompt and answer no
.
Pattern: (?:(?:get)|(?:see))s? a(?:nother)? confirm prompt "([^"]*)".* answers? no
Example:
Scenario: Receive a no
Given I see a confirm prompt "Confirm? (y/N)", I answer no
Then ask for confirm "Confirm?", receive no
Expect to see a Confirm prompt and answer an invalid response (not a yes
or no
).
Pattern: (?:(?:get)|(?:see))s? a(?:nother)? confirm prompt "([^"]*)".* answers? "([^"]*)"
Example:
Scenario: Invalid answer
Given I see a confirm prompt "Confirm? (y/N)", I answer "nahhh"
# Because the answer is invalid, survey will prompt again.
And then I see another confirm prompt "Confirm? (y/N)", I answer no
Then ask for confirm "Confirm?", receive no
Expect to see a Confirm prompt and interrupt (^C).
Pattern: (?:(?:get)|(?:see))s? a(?:nother)? confirm prompt "([^"]*)".* interrupts?
Example:
Scenario: Interrupted
Given I see a confirm prompt "Confirm? (y/N)", I interrupt
Then ask for confirm "Confirm?", get interrupted
Expect to see a Confirm prompt, ask for help and then expect to see a Help message.
Pattern: (?:(?:get)|(?:see))s? a(?:nother)? confirm prompt "([^"]*)".* asks? for help and sees? "([^"]*)"
Example:
Scenario: With help and receive a yes
Given I see a confirm prompt "Confirm? [? for help] (y/N)", I ask for help and see "This action cannot be undone"
And then I see another confirm prompt "Confirm? (y/N)", I answer yes
Then ask for confirm "Confirm?" with help "This action cannot be undone", receive yes
Expect to see a Multiline prompt and give no answer.
Pattern: (?:(?:get)|(?:see))s? a(?:nother)? multiline prompt "([^"]*)".* answers?: ""
Example:
Scenario: Receive an empty answer
Given I see a multiline prompt "Enter comment", I answer ""
Then ask for multiline "Enter comment", receive:
"""
"""
Expect to see a Multiline prompt and give an answer.
Pattern: (?:(?:get)|(?:see))s? a(?:nother)? multiline prompt "([^"]*)".* answers?:
Example:
Scenario: Receive a multiline answer
Given I see a multiline prompt "Enter comment", I answer:
"""
This is the first
line
this is the second line
"""
Then ask for multiline "Enter comment", receive:
"""
This is the first
line
this is the second line
"""
Expect to see a Multiline prompt and interrupt (^C).
Pattern: (?:(?:get)|(?:see))s? a(?:nother)? multiline prompt "([^"]*)".* interrupts?
Example:
Scenario: Interrupted
Given I see a multiline prompt "Enter comment", I interrupt
Then ask for multiline "Enter comment", get interrupted
Expect to see a Password prompt and answer it.
Pattern: (?:(?:get)|(?:see))s? a(?:nother)? password prompt "([^"]*)".* answers? "([^"]*)"
Example:
Scenario: Receive an answer
Given I see a password prompt "Enter password:", I answer "123456"
Then ask for password "Enter password:", receive "123456"
Expect to see a Password prompt and interrupt (^C).
Pattern: (?:(?:get)|(?:see))s? a(?:nother)? password prompt "([^"]*)".* interrupts?
Example:
Scenario: Interrupted
Given I see a password prompt "Enter password:", I interrupt
Then ask for password "Enter password:", get interrupted
Expect to see a Password prompt, ask for help and then expect to see a Help message.
Pattern: (?:(?:get)|(?:see))s? a(?:nother)? password prompt "([^"]*)".* asks? for help and sees? "([^"]*)"
Example:
Scenario: With help and receive an answer
Given I see a password prompt "Enter password: [? for help]", I ask for help and see "It is a secret"
And then I see another password prompt "Enter password:", I answer "123456"
Then ask for password "Enter password:" with help "It is a secret", receive "123456"
- Register for injection: https://github.com/nhatthm/surveysteps/blob/master/features/bootstrap/godog_test.go#L47
- Inject: https://github.com/nhatthm/surveysteps/blob/master/features/bootstrap/survey.go#L36-L41
- Use: https://github.com/nhatthm/surveysteps/blob/master/features/bootstrap/survey.go#L57
Full suite: https://go.nhat.io/surveysteps/tree/master/features
If this project help you reduce time to develop, you can give me a cup of coffee :)
or scan this