expandog

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2021 License: MIT Imports: 2 Imported by: 1

README

⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️

This module is deprecated. Use github.com/godogx/expandvars instead.

Variables Expander for Cucumber Steps

GitHub Releases codecov Go Report Card GoDevDoc Donate

A lifesaver expander for cucumber/godog because, sometimes, you have to use variables in your steps.

Prerequisites

  • Go >= 1.16

Install

go get github.com/nhatthm/expandog

Usage

Initiate a new StepExpander with expandog.NewStepExpander() then add it to ScenarioInitializer by calling StepExpander.RegisterContext(*testing.T, *godog.ScenarioContext)

package main

import (
    "fmt"
    "math/rand"
    "strings"
    "testing"

    "github.com/cucumber/godog"
    "github.com/nhatthm/expandog"
    "github.com/stretchr/testify/assert"
)

func TestIntegration(t *testing.T) {
    expander := expandog.NewStepExpander(
        strings.NewReplacer("$TO", "Berlin"),
        expandog.Pairs{
            "HUSBAND": "John",
        },
        func() expandog.Pairs {
            return expandog.Pairs{
                "RAND": fmt.Sprintf("%d", rand.Int63()),
            }
        },
        expandog.BeforeScenario(func() expandog.Pairs {
            return expandog.Pairs{
                "SCENARIO_RAND": fmt.Sprintf("%d", rand.Int63()),
            }
        }),
        func(s string) string {
            return strings.ReplaceAll(s, "$FROM", "Paris")
        },
        expandog.Expander(func(s string) string {
            return strings.ReplaceAll(s, "$TRANSPORT", "by bus")
        }),
        // OS env vars.
        expandog.EnvExpander,
    )

    suite := godog.TestSuite{
        Name: "Integration",
        ScenarioInitializer: func(ctx *godog.ScenarioContext) {
            expander.RegisterContext(ctx)
        },
        Options: &godog.Options{
            Strict:    true,
            Randomize: rand.Int63(),
        },
    }

    // Run the suite.
}

In your tests, just use $VARIABLE_NAME in the step or the argument, like this:

    Scenario: var is replaced
        Given var NAME is replaced in step text: $NAME

        Then step text is:
        """
        map var NAME is replaced in step text: John
        """

        Given var NAME is replaced in step argument (string)
        """
        NAME=$NAME
        """

        Then step argument is a string:
        """
        NAME=John
        """

        Given env var NAME is replaced in step argument (table)
            | col 1   | col 2 | col 3   |
            | value 1 | $NAME | value 3 |

        Then step argument is a table:
            | col 1   | col 2 | col 3   |
            | value 1 | John  | value 3 |
    Scenario: .github files
        Then there should be only these files in "$TEST_DIR/.github":
        """
        - workflows:
            - golangci-lint.yaml
            - test.yaml
        """
Expanders

The expanders could be any of these:

  1. A Replacer interface
type Replacer interface {
    Replace(string) string
}
  1. A Replacer func(string) string function.

    For example, you could use os.ExpandEnv or its alias expandog.EnvExpander

  2. A map or vars (without the $) map[string]string

var _ = expandog.NewStepExpander(expandog.Pairs{
    "HUSBAND": "John",
    "WIFE":    "Jane",
})
  1. A provider that provides a map of vars (without the $) map[string]string. The provider will be called every step.
var _ = expandog.NewStepExpander(func() expandog.Pairs {
    return map[string]string{
        "RAND": fmt.Sprintf("%d", rand.Int63()),
    }
})
  1. A BeforeScenario provides a map of vars (without the $) map[string]string. The provider will be called only once before every scenario.

Note: If you need expandog.EnvExpander or os.ExpandEnv, put it in the end of the chain. Because it replaces not-found vars with empty strings, other expanders won't have a chance to do their jobs if you put it in the beginning.

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 expandog provides functionalities to expand variables in cucumber steps.

Deprecated: Use github.com/godogx/expandvars instead.

Index

Constants

This section is empty.

Variables

View Source
var EnvExpander = expandvars.EnvExpander

EnvExpander expands variables using env vars.

Deprecated: Use expandvars.EnvExpander instead.

View Source
var ErrUnsupportedExpander = expandvars.ErrUnsupportedExpander

ErrUnsupportedExpander indicates that the provided expander is not supported.

Deprecated: Use expandvars.ErrUnsupportedExpander instead.

Functions

func BeforeScenario deprecated

func BeforeScenario(provide func() Pairs) func() Expander

BeforeScenario expands variables from a provider that will be called only once before every scenario.

Deprecated: Use expandvars.BeforeScenario instead.

func ExpandStep deprecated added in v0.2.0

func ExpandStep(st *godog.Step, expanders ...interface{})

ExpandStep expands variables in the step definition using an expander.

Deprecated: Use expandvars.ExpandStep instead.

Types

type Expander deprecated

type Expander = expandvars.Expander

Expander expands the variables in a string.

Deprecated: Use expandvars.Expander instead.

type Pairs deprecated

type Pairs = expandvars.Pairs

Pairs is a pair of old and new to be replaced.

Deprecated: Use expandvars.Pairs instead.

type Replacer deprecated

type Replacer = expandvars.Replacer

Replacer replace string.

Deprecated: Use expandvars.Replacer instead.

type StepExpander deprecated

type StepExpander = expandvars.StepExpander

StepExpander expands variables in cucumber steps.

Deprecated: Use expandvars.StepExpander instead.

func NewStepExpander deprecated

func NewStepExpander(expanders ...interface{}) *StepExpander

NewStepExpander initiates a new variable expanders for cucumber steps.

Deprecated: Use expandvars.NewStepExpander instead.

Jump to

Keyboard shortcuts

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