Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,36 @@
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
}

Check notice on line 121 in helper.go

View workflow job for this annotation

GitHub Actions / test (stable)

1 statement(s) on lines 119:121 are not covered by tests.

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
}

Check notice on line 137 in helper.go

View workflow job for this annotation

GitHub Actions / test (stable)

1 statement(s) on lines 135:137 are not covered by tests.

return ctx, string(b), nil
}

// Replace replaces vars in bytes slice.
//
// This function can help to interpolate variables into predefined templates.
Expand Down Expand Up @@ -142,7 +172,12 @@
}

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 {
Expand Down Expand Up @@ -175,6 +210,11 @@
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)
}

Check notice on line 216 in helper.go

View workflow job for this annotation

GitHub Actions / test (stable)

1 statement(s) are not covered by tests.

// 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)
Expand Down
33 changes: 33 additions & 0 deletions helper_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading