Skip to content
This repository has been archived by the owner on Sep 7, 2021. It is now read-only.

Commit

Permalink
Split RegisterContext to make it more flexible for dependents to just…
Browse files Browse the repository at this point in the history
… register what is needed
  • Loading branch information
nhatthm committed Jul 22, 2021
1 parent 0b7cb8f commit a0723a3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
10 changes: 8 additions & 2 deletions expander.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
)

const varDelimiter = "$"

// ErrUnsupportedExpander indicates that the provided expander is not supported.
var ErrUnsupportedExpander = errors.New("unsupported expander")

Expand All @@ -32,7 +34,7 @@ func BeforeScenario(provide func() Pairs) func() Expander {
}

func placeholder(name string) string {
return fmt.Sprintf("$%s", name)
return fmt.Sprintf("%s%s", varDelimiter, name)
}

// runtimeExpander expands variables from a provider.
Expand Down Expand Up @@ -74,7 +76,7 @@ func newExpander(e interface{}) Expander {
case Pairs:
return mapExpander(e)

case func() map[string]string:
case func() Pairs:
return runtimeExpander(e)

case func() Expander:
Expand All @@ -92,3 +94,7 @@ func newExpander(e interface{}) Expander {

panic(fmt.Errorf("%w: got %T", ErrUnsupportedExpander, e))
}

func doExpand(expand Expander, s string) string {
return expand(s)
}
16 changes: 11 additions & 5 deletions step.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ type StepExpander struct {
expand Expander
}

// RegisterContext register StepExpander to the test suite.
func (m *StepExpander) RegisterContext(s *godog.ScenarioContext) {
// RegisterExpander registers only the expander to the test suite to let it work.
// There will be no registration of step definition in this method.
func (m *StepExpander) RegisterExpander(s *godog.ScenarioContext) {
s.BeforeScenario(func(sc *godog.Scenario) {
m.expand = chainExpanders(m.expanders...)
})
Expand All @@ -22,6 +23,11 @@ func (m *StepExpander) RegisterContext(s *godog.ScenarioContext) {
})
}

// RegisterContext register everything to the test suite.
func (m *StepExpander) RegisterContext(s *godog.ScenarioContext) {
m.RegisterExpander(s)
}

// NewStepExpander initiates a new variable expanders for cucumber steps.
func NewStepExpander(expanders ...interface{}) *StepExpander {
return &StepExpander{
Expand All @@ -35,20 +41,20 @@ func ExpandStep(st *godog.Step, expanders ...interface{}) {
}

func expandStep(st *godog.Step, expand Expander) {
st.Text = expand(st.Text)
st.Text = doExpand(expand, st.Text)

if st.Argument == nil {
return
}

switch msg := st.Argument.Message.(type) {
case *messages.PickleStepArgument_DocString:
msg.DocString.Content = expand(msg.DocString.Content)
msg.DocString.Content = doExpand(expand, msg.DocString.Content)

case *messages.PickleStepArgument_DataTable:
for _, row := range msg.DataTable.Rows {
for _, cell := range row.Cells {
cell.Value = expand(cell.Value)
cell.Value = doExpand(expand, cell.Value)
}
}
}
Expand Down

0 comments on commit a0723a3

Please sign in to comment.