From a0723a3c75f2246815fb1f2b4423606f05378871 Mon Sep 17 00:00:00 2001 From: nhatthm Date: Thu, 22 Jul 2021 10:29:53 +0200 Subject: [PATCH] Split RegisterContext to make it more flexible for dependents to just register what is needed --- expander.go | 10 ++++++++-- step.go | 16 +++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/expander.go b/expander.go index 97e71b0..1ed12d3 100644 --- a/expander.go +++ b/expander.go @@ -7,6 +7,8 @@ import ( "strings" ) +const varDelimiter = "$" + // ErrUnsupportedExpander indicates that the provided expander is not supported. var ErrUnsupportedExpander = errors.New("unsupported expander") @@ -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. @@ -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: @@ -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) +} diff --git a/step.go b/step.go index 4b11363..05860f5 100644 --- a/step.go +++ b/step.go @@ -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...) }) @@ -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{ @@ -35,7 +41,7 @@ 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 @@ -43,12 +49,12 @@ func expandStep(st *godog.Step, expand Expander) { 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) } } }