consolesteps

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: 1

README

Terminal Emulator for cucumber/godog

GitHub Releases Build Status codecov Go Report Card GoDevDoc Donate

consolesteps provides a new Console for each cucumber/godog Scenario.

Prerequisites

  • Go >= 1.17

Install

go get go.nhat.io/consolesteps

Usage

Initialize a consolesteps.Manager with consolesteps.New() then add it into the ScenarioInitializer. If you wish to add listeners to Manager.NewConsole and Manager.CloseConsole event, use consolesteps.WithStarter and consolesteps.WithCloser option in the constructor.

For example:

package mypackage

import (
    "math/rand"
    "testing"

    expect "github.com/Netflix/go-expect"
    "github.com/cucumber/godog"
    "go.nhat.io/consolesteps"
)

type writer struct {
    console *expect.Console
}

func (w *writer) registerContext(ctx *godog.ScenarioContext) {
    ctx.Step(`write to console:`, func(s *godog.DocString) error {
        _, err := w.console.Write([]byte(s.Content))

        return err
    })
}

func (w *writer) start(_ *godog.Scenario, console *expect.Console) {
    w.console = console
}

func (w *writer) close(_ *godog.Scenario) {
    w.console = nil
}

func TestIntegration(t *testing.T) {
    t.Parallel()

    w := &writer{}
    m := consolesteps.New(t,
        consolesteps.WithStarter(w.start),
        consolesteps.WithCloser(w.close),
    )

    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.
}

See more: #Examples

Resize

In case you want to resize the terminal (default is 80x100) to avoid text wrapping, for example:

        Then console output is:
        """
        panic: could not build credentials provider option: unsupported credentials prov
        ider
        """

Use consolesteps.WithTermSize(cols, rows) while initiating with consolesteps.New(), for example:

package mypackage

import (
    "testing"

    "go.nhat.io/consolesteps"
)

func TestIntegration(t *testing.T) {
    // ...
    m := consolesteps.New(t, consolesteps.WithTermSize(100, 100))
    // ...
}

Then your step will become:

        Then console output is:
        """
        panic: could not build credentials provider option: unsupported credentials provider
        """

Steps

console output is:

Asserts the output of the console.

For example:

    Scenario: Find all transaction in range with invalid format
        When I run command "transactions -d --format invalid"

        Then console output is:
        """
        panic: unknown output format
        """

Examples

Full suite: https://github.com/nhatthm/consolesteps/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 consolesteps provides a terminal emulator for testing with cucumber/godog

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertState

func AssertState(t assert.TestingT, terminal vt10x.Terminal, expected string) bool

AssertState asserts console state.

func AssertStateRegex

func AssertStateRegex(t assert.TestingT, terminal vt10x.Terminal, expected string) bool

AssertStateRegex asserts console state.

Types

type Buffer

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

Buffer is a goroutine safe bytes.Buffer.

func (*Buffer) String

func (b *Buffer) String() string

String returns the contents of the unread portion of the buffer as a string. If the Buffer is a nil pointer, it returns "<nil>".

To build strings more efficiently, see the strings.Builder type.

func (*Buffer) Write

func (b *Buffer) Write(p []byte) (n int, err error)

Write appends the contents of p to the buffer, growing the buffer as needed. The return value n is the length of p; err is always nil. If the buffer becomes too large, Write will panic with ErrTooLarge.

type Closer

type Closer func(sc *godog.Scenario)

Closer is a callback when console closes.

type Manager

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

Manager manages console and its state.

func New

func New(t TestingT, options ...Option) *Manager

New initiates a new console Manager.

func (*Manager) CloseConsole

func (m *Manager) CloseConsole(sc *godog.Scenario)

CloseConsole closes the current console.

func (*Manager) Flush

func (m *Manager) Flush()

Flush flushes console state.

func (*Manager) NewConsole

func (m *Manager) NewConsole(sc *godog.Scenario) (*expect.Console, vt10x.Terminal)

NewConsole creates a new console.

func (*Manager) RegisterContext deprecated

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

RegisterContext register console Manager to test context.

Deprecated: Use Manager.RegisterSteps instead.

func (*Manager) RegisterSteps added in v0.2.0

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

RegisterSteps register console Manager to test context.

func (*Manager) WithCloser

func (m *Manager) WithCloser(c Closer) *Manager

WithCloser adds a Closer to Manager.

func (*Manager) WithStarter

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

WithStarter adds a Starter to Manager.

type Option

type Option func(m *Manager)

Option configures Manager.

func WithCloser

func WithCloser(c Closer) Option

WithCloser adds a Closer to Manager.

func WithStarter

func WithStarter(s Starter) Option

WithStarter adds a Starter to Manager.

func WithTermSize

func WithTermSize(cols, rows int) Option

WithTermSize sets terminal size cols x rows. Default is 80 x 100.

type Starter

type Starter func(sc *godog.Scenario, console *expect.Console)

Starter is a callback when console starts.

type TestingT

type TestingT interface {
	Errorf(format string, args ...interface{})
	FailNow()
	Log(args ...interface{})
	Logf(format string, args ...interface{})
}

TestingT is an interface wrapper around *testing.T.

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