diff --git a/helper.go b/helper.go index 226ad88..fa6fac5 100644 --- a/helper.go +++ b/helper.go @@ -109,6 +109,36 @@ func (s *Steps) ReplaceFile(ctx context.Context, filePath string) (context.Conte return s.Replace(ctx, body) } +// ReplaceTable performs in-place replacement of vars in table cells. +func (s *Steps) ReplaceTable(ctx context.Context, data [][]string) (context.Context, error) { + ctx, _ = s.Vars(ctx) + + for _, r := range data { + for j, cell := range r { + _, rc, err := s.ReplaceString(ctx, cell) + if err != nil { + return ctx, err + } + + if rc != cell { + r[j] = rc + } + } + } + + return ctx, nil +} + +// ReplaceString replaces vars in a string. +func (s *Steps) ReplaceString(ctx context.Context, body string) (context.Context, string, error) { + ctx, b, err := s.Replace(ctx, []byte(body)) + if err != nil { + return ctx, "", err + } + + return ctx, string(b), nil +} + // Replace replaces vars in bytes slice. // // This function can help to interpolate variables into predefined templates. @@ -142,7 +172,12 @@ func (s *Steps) Replace(ctx context.Context, body []byte) (context.Context, []by } sort.Slice(varNames, func(i, j int) bool { - return len(varNames[i]) > len(varNames[j]) + // Compare lengths first (longer comes first) + if len(varNames[i]) != len(varNames[j]) { + return len(varNames[i]) > len(varNames[j]) + } + // If lengths are equal, sort alphabetically + return varNames[i] < varNames[j] }) for _, k := range varNames { @@ -175,6 +210,11 @@ func (s *Steps) PrepareContext(ctx context.Context) context.Context { return ctx } +// AssertString compares payloads and collects variables from JSON fields. +func (s *Steps) AssertString(ctx context.Context, expected, received string, ignoreAddedJSONFields bool) (context.Context, error) { + return s.Assert(ctx, []byte(expected), []byte(received), ignoreAddedJSONFields) +} + // Assert compares payloads and collects variables from JSON fields. func (s *Steps) Assert(ctx context.Context, expected, received []byte, ignoreAddedJSONFields bool) (context.Context, error) { ctx, jc := s.jc(ctx) diff --git a/helper_test.go b/helper_test.go new file mode 100644 index 0000000..e00126c --- /dev/null +++ b/helper_test.go @@ -0,0 +1,33 @@ +package vars_test + +import ( + "context" + "testing" + + "github.com/godogx/vars" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestSteps_ReplaceTable(t *testing.T) { + vs := vars.Steps{} + + ctx, v := vs.Vars(context.Background()) + + v.Set("$foo", 12) + v.Set("$bar", true) + v.Set("$baz", "$foo") + + var table [][]string + + table = append(table, []string{"foo", "bar", "baz"}) + table = append(table, []string{"$foo_123", "$bar", "1/$baz"}) + + _, err := vs.ReplaceTable(ctx, table) + require.NoError(t, err) + + assert.Equal(t, [][]string{ + {"foo", "bar", "baz"}, + {"12_123", "true", "1/12"}, + }, table) +}