surveysteps

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 22, 2024 License: MIT Imports: 12 Imported by: 0

README

Cucumber Survey steps for Go

GitHub Releases Build Status codecov Go Report Card GoDevDoc Donate

Tests with AlecAivazis/survey and cucumber/godog

Prerequisites

  • Go >= 1.17

Install

go get go.nhat.io/surveysteps

Usage

Supported Types
Type Supported Supported Actions
Confirm
  • Answer yes, no or a custom one
  • Interrupt (^C)
  • Ask for help
Editor
Input
Multiline
  • Answer
  • No answer
  • Interrupt (^C)
Multiselect
Password
  • Answer (+ check for *)
  • No answer
  • Interrupt (^C)
  • Ask for help
Select

Setup

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

Steps

Confirm
Yes

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
No

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
Invalid answer

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
Interrupt

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
With Help

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
Multiline
No Answer

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:
        """
        """
Multiline Answer

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
        """
Interrupt

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
Password
Answer

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"
Interrupt

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
With Help

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"

Examples

Full suite: https://go.nhat.io/surveysteps/tree/master/features

Donation

If this project help you reduce time to develop, you can give me a cup of coffee :)

Paypal donation

paypal

       or scan this

Documentation

Overview

Package surveysteps provides functionalities for testing AlecAivazis/survey with cucumber/godog

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager is a wrapper around *surveyexpect.Survey to make it run with cucumber/godog.

func New

New initiates a new *surveysteps.Manager.

func (*Manager) RegisterContext deprecated

func (m *Manager) RegisterContext(s *godog.ScenarioContext)

RegisterContext register the survey to a *godog.ScenarioContext.

Deprecated: Use Manager.RegisterSteps instead.

func (*Manager) RegisterSteps added in v0.2.0

func (m *Manager) RegisterSteps(s *godog.ScenarioContext)

RegisterSteps register the survey to a *godog.ScenarioContext.

func (*Manager) WithConsole

func (m *Manager) WithConsole(console *consolesteps.Manager) *Manager

WithConsole sets console manager.

func (*Manager) WithStarter

func (m *Manager) WithStarter(s Starter) *Manager

WithStarter adds a mew Starter to Manager.

type Starter

type Starter func(sc *godog.Scenario, stdio terminal.Stdio)

Starter is a callback when survey starts.

type Survey

type Survey struct {
	*surveyexpect.Survey
	// contains filtered or unexported fields
}

Survey is a wrapper around *surveyexpect.Survey to make it run with cucumber/godog.

func NewSurvey

func NewSurvey(t surveyexpect.TestingT, options ...surveyexpect.ExpectOption) *Survey

NewSurvey creates a new survey.

func (*Survey) Close

func (s *Survey) Close()

Close notifies other parties and close the survey.

func (*Survey) Expect

func (s *Survey) Expect(c surveyexpect.Console) error

Expect runs an expectation against a given console.

func (*Survey) Start

func (s *Survey) Start(console surveyexpect.Console) *Survey

Start starts a new survey.

Directories

Path Synopsis
features
bootstrap
Package bootstrap provides integration tests.
Package bootstrap provides integration tests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL