Documentation
¶
Overview ¶
Package vars manages shared variables in godog gherkin tests.
Index ¶
- func Assert(ctx context.Context, expected, received []byte, ignoreAddedJSONFields bool) (context.Context, error)
- func AssertFile(ctx context.Context, filePath string, received []byte, ...) (context.Context, error)
- func AssertJSONPaths(ctx context.Context, jsonPaths *godog.Table, received []byte, ...) (context.Context, error)
- func FromContext(ctx context.Context) map[string]interface{}
- func Infer(s string) interface{}
- func Replace(ctx context.Context, body []byte) (context.Context, []byte, error)
- func ReplaceFile(ctx context.Context, filePath string) (context.Context, []byte, error)
- func ToContext(ctx context.Context, key string, value interface{}) context.Context
- func Vars(ctx context.Context) (context.Context, *shared.Vars)
- type Factory
- type Steps
- func (s *Steps) AddFactory(name string, f Factory)
- func (s *Steps) AddGenerator(name string, f func() (interface{}, error))
- func (s *Steps) Assert(ctx context.Context, expected, received []byte, ignoreAddedJSONFields bool) (context.Context, error)
- func (s *Steps) AssertFile(ctx context.Context, filePath string, received []byte, ...) (context.Context, error)
- func (s *Steps) AssertJSONPaths(ctx context.Context, jsonPaths *godog.Table, received []byte, ...) (context.Context, error)
- func (s *Steps) AssertString(ctx context.Context, expected, received string, ignoreAddedJSONFields bool) (context.Context, error)
- func (s *Steps) PrepareContext(ctx context.Context) context.Context
- func (s *Steps) Register(sc *godog.ScenarioContext)
- func (s *Steps) Replace(ctx context.Context, body []byte) (context.Context, []byte, error)
- func (s *Steps) ReplaceFile(ctx context.Context, filePath string) (context.Context, []byte, error)
- func (s *Steps) ReplaceString(ctx context.Context, body string) (context.Context, string, error)
- func (s *Steps) ReplaceTable(ctx context.Context, data [][]string) (context.Context, error)
- func (s *Steps) Vars(ctx context.Context) (context.Context, *shared.Vars)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Assert ¶ added in v0.1.6
func Assert(ctx context.Context, expected, received []byte, ignoreAddedJSONFields bool) (context.Context, error)
Assert compares payloads and collects variables from JSON fields.
Example ¶
package main
import (
"context"
"fmt"
"github.com/godogx/vars"
)
func main() {
// Variables are passed via chained context.
ctx := context.Background()
expected := []byte(`{"foo":"$foo","bar":123}`)
received := []byte(`{"foo":321,"bar":123,"baz":true}`)
// No error, $foo is populated with 321, "baz" is ignored for true `ignoreAddedJSONFields` argument.
ctx, err := vars.Assert(ctx, expected, received, true)
if err != nil {
fmt.Println("assertion failed: " + err.Error())
}
expected = []byte(`{"foo":"$foo","bar":123,"prefixed_foo":"ooo::$foo"}`)
received = []byte(`{"foo":313,"bar":123,"baz":true,"prefixed_foo":"ooo::321"}`)
// Assertion fails.
_, err = vars.Assert(ctx, expected, received, false)
if err != nil {
fmt.Println("assertion failed: " + err.Error())
}
}
Output: assertion failed: not equal: { "bar": 123, - "foo": 321, + "foo": 313, "prefixed_foo": "ooo::321" + "baz": true }
func AssertFile ¶ added in v0.1.6
func AssertFile(ctx context.Context, filePath string, received []byte, ignoreAddedJSONFields bool) (context.Context, error)
AssertFile compares payloads and collects variables from JSON fields.
func AssertJSONPaths ¶ added in v0.1.6
func AssertJSONPaths(ctx context.Context, jsonPaths *godog.Table, received []byte, ignoreAddedJSONFields bool) (context.Context, error)
AssertJSONPaths compares payload with a list of JSON path expectations.
func FromContext ¶
FromContext returns variables from context.
func Infer ¶ added in v0.1.9
func Infer(s string) interface{}
Infer parses a value from a string into a suitable type. If s only contains digits, int64 is returned. If s is parsed as float, float64 is returned. If s has unquoted values of true or false, bool is returned. If s is equal to unquoted null, nil is returned. If s can be parsed as time.RFC3339, time.RFC3339Nano, "2006-01-02" or "2006-01-02 15:04:05", time.Time is returned. If s starts with double quote ", string decoded from JSON is returned (mind JSON escaping rules). If s starts with [ or {, any decoded from JSON is returned. Otherwise, s is returned as is.
func Replace ¶ added in v0.1.6
Replace replaces vars in bytes slice.
This function can help to interpolate variables into predefined templates. It is generally used to prepare `expected` value.
Example ¶
package main
import (
"context"
"fmt"
"github.com/godogx/vars"
)
func main() {
// Variables are passed via chained context.
ctx := context.Background()
ctx = vars.ToContext(ctx, "$foo", 321)
expected := []byte(`{"foo":"$foo","bar":123, "prefixed_foo":"ooo::$foo"}`)
_, expected, err := vars.Replace(ctx, expected)
if err != nil {
fmt.Println("replace failed: " + err.Error())
}
fmt.Println(string(expected))
}
Output: {"foo":321,"bar":123,"prefixed_foo":"ooo::321"}
func ReplaceFile ¶ added in v0.1.6
ReplaceFile replaces vars in file contents.
It works same as Replace using a file for input.
Types ¶
type Steps ¶
type Steps struct {
JSONComparer assertjson.Comparer
// contains filtered or unexported fields
}
Steps provides godog gherkin step definitions.
func (*Steps) AddFactory ¶ added in v0.1.7
AddFactory registers user-defined factory function, suitable for resource creation.
Example ¶
package main
import (
"context"
"errors"
"fmt"
"io"
"time"
"github.com/cucumber/godog"
"github.com/godogx/vars"
)
func main() {
vs := &vars.Steps{}
vs.AddFactory("now", func(ctx context.Context, args ...interface{}) (context.Context, interface{}, error) {
// "Now" is mocked with a constant value to reproducibility.
return ctx, time.Date(2023, 5, 22, 19, 38, 0, 0, time.UTC), nil
})
vs.AddFactory("addDuration", func(ctx context.Context, args ...interface{}) (context.Context, interface{}, error) {
if len(args) != 2 {
return ctx, nil, errors.New("addDuration expects 2 arguments: base time, duration")
}
var (
base time.Time
dur time.Duration
)
switch v := args[0].(type) {
case time.Time:
base = v
case string:
t, err := time.Parse(time.RFC3339Nano, v)
if err != nil {
return ctx, nil, fmt.Errorf("parsing base time: %w", err)
}
base = t
default:
return ctx, nil, fmt.Errorf("unexpected type %T for base time, string or time.Time expected", v)
}
switch v := args[1].(type) {
case time.Duration:
dur = v
case string:
d, err := time.ParseDuration(v)
if err != nil {
return ctx, nil, fmt.Errorf("parsing duration: %w", err)
}
dur = d
default:
return ctx, nil, fmt.Errorf("unexpected type %T for duration, string or time.Duration expected", v)
}
return ctx, base.Add(dur), nil
})
vs.AddFactory("newUserID", func(ctx context.Context, args ...interface{}) (context.Context, interface{}, error) {
if len(args) != 2 {
return ctx, nil, errors.New("newUserID expects 2 arguments: name, registeredAt")
}
var (
name string
registeredAt time.Time
)
switch v := args[0].(type) {
case string:
name = v
default:
return ctx, nil, fmt.Errorf("unexpected type %T for name, string expected", v)
}
switch v := args[1].(type) {
case time.Time:
registeredAt = v
case string:
t, err := time.Parse(time.RFC3339Nano, v)
if err != nil {
return ctx, nil, fmt.Errorf("parsing registeredAt: %w", err)
}
registeredAt = t
default:
return ctx, nil, fmt.Errorf("unexpected type %T for registeredAt, string or time.Time expected", v)
}
fmt.Println("creating user", name, registeredAt)
// Return relevant value, for example user id.
return ctx, 123, nil
})
s := godog.TestSuite{}
s.ScenarioInitializer = func(sc *godog.ScenarioContext) {
vs.Register(sc)
}
s.Options = &godog.Options{
Format: "pretty",
Output: io.Discard,
FeatureContents: []godog.Feature{
{
Name: "example",
Contents: []byte(`
Feature: example
Scenario: using var factory
Given variable $myUserID is set to newUserID("John Doe", addDuration(now(), "-10h"))
`),
},
},
}
s.Run()
}
Output: creating user John Doe 2023-05-22 09:38:00 +0000 UTC
func (*Steps) AddGenerator ¶ added in v0.1.4
AddGenerator registers user-defined generator function, suitable for random identifiers.
func (*Steps) Assert ¶ added in v0.1.5
func (s *Steps) Assert(ctx context.Context, expected, received []byte, ignoreAddedJSONFields bool) (context.Context, error)
Assert compares payloads and collects variables from JSON fields.
func (*Steps) AssertFile ¶ added in v0.1.5
func (s *Steps) AssertFile(ctx context.Context, filePath string, received []byte, ignoreAddedJSONFields bool) (context.Context, error)
AssertFile compares payloads and collects variables from JSON fields.
func (*Steps) AssertJSONPaths ¶ added in v0.1.5
func (s *Steps) AssertJSONPaths(ctx context.Context, jsonPaths *godog.Table, received []byte, ignoreAddedJSONFields bool) (context.Context, error)
AssertJSONPaths compares payload with a list of JSON path expectations.
func (*Steps) AssertString ¶ added in v0.1.11
func (s *Steps) AssertString(ctx context.Context, expected, received string, ignoreAddedJSONFields bool) (context.Context, error)
AssertString compares payloads and collects variables from JSON fields.
func (*Steps) PrepareContext ¶ added in v0.1.8
PrepareContext makes sure context is instrumented with a valid comparer and does not need to be updated later.
func (*Steps) Register ¶
func (s *Steps) Register(sc *godog.ScenarioContext)
Register add steps to scenario context.
func (*Steps) Replace ¶ added in v0.1.5
Replace replaces vars in bytes slice.
This function can help to interpolate variables into predefined templates. It is generally used to prepare `expected` value.
func (*Steps) ReplaceFile ¶ added in v0.1.5
ReplaceFile replaces vars in file contents.
It works same as Replace using a file for input.
func (*Steps) ReplaceString ¶ added in v0.1.11
ReplaceString replaces vars in a string.
func (*Steps) ReplaceTable ¶ added in v0.1.11
ReplaceTable performs in-place replacement of vars in table cells.