fixtures

package module
v0.25.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 24, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package fixtures exposes a test Harness utility to test JSON and YAML transformations.

Index

Constants

This section is empty.

Variables

View Source
var EmbeddedFixtures embed.FS

Functions

func AssertYAMLEq

func AssertYAMLEq(t testing.TB, expected string, actual string, msgAndArgs ...any) bool

AssertYAMLEq is the same as assert.YAMLEq but without the dependency to go.pkg.in/yaml.v3.

NOTE: this could be reverted once https://github.com/stretchr/testify/pull/1772 is merged.

func JSONEqualOrdered

func JSONEqualOrdered(t testing.TB, expected, actual string)

JSONEqualOrdered is a replacement for require.JSONEq that checks further that two JSONs are exactly equal, with only the following tolerated differences:

  • non-significant white space
  • numerical encoding (e.g. 0.01 <=> 1e-2)
  • unicode encoding (e.g. explicitly escaped unicode sequences <=> unicode rune)

func MustLoadFixture

func MustLoadFixture(fsys fs.FS, pth string) []byte

func RequireYAMLEq

func RequireYAMLEq(t testing.TB, expected string, actual string, msgAndArgs ...any)

RequireYAMLEq is the same as require.YAMLEq but without the dependency to go.pkg.in/yaml.v3.

NOTE: this could be reverted once https://github.com/stretchr/testify/pull/1772 is merged.

func ShouldLoadFixture

func ShouldLoadFixture(t testing.TB, fsys fs.FS, pth string) []byte

func YAMLEqualOrdered

func YAMLEqualOrdered(t testing.TB, expected, actual string)

YAMLEqualOrdered is a replacement for require.YAMLEq that checks further that two YAML are exactly equal, with only the following tolerated differences:

  • non-significant white space
  • comments

Otherwise, the representation of arrays, null values and objects must match exactly, e.g this checks tells us that:

a: [1,2,3]

differs from:

a:
  - 1
  - 2
  - 3

even though we know that they have equivalent semantics.

NOTE: at this moment, this check does not support anchors.

Types

type Filter

type Filter func(*filters)

func WithError

func WithError(only bool) Filter

func WithExcludePattern

func WithExcludePattern(rex *regexp.Regexp) Filter

func WithIncludePattern

func WithIncludePattern(rex *regexp.Regexp) Filter

func WithoutError

func WithoutError(only bool) Filter

type Fixture

type Fixture struct {
	Name          string `yaml:"name"`
	Comment       string `yaml:"comment"`
	JSONPayload   string `yaml:"json_payload"`
	YAMLPayload   string `yaml:"yaml_payload"`
	Error         bool   `yaml:"error"`
	ErrorContains string `yaml:"error_contains"`
}

Fixture holds a JSON payload and its equivalent YAML payload.

func (Fixture) ExpectError

func (f Fixture) ExpectError() bool

ExpectError indicates if this test case expect an error.

func (Fixture) JSONBytes

func (f Fixture) JSONBytes() []byte

JSONBytes returns the JSON payload string as bytes.

func (Fixture) YAMLBytes

func (f Fixture) YAMLBytes() []byte

YAMLBytes returns the YAML payload string as bytes.

type Harness

type Harness struct {
	// contains filtered or unexported fields
}

Harness is a test helper to retrieve or scan over a collection of predefined test cases loaded from the embedded file system.

func NewHarness

func NewHarness(t testing.TB) *Harness

NewHarness yields a new test Harness for a given test.

The Harness requires a call to Harness.Init to be ready.

func (*Harness) AllTests

func (h *Harness) AllTests(filter ...Filter) iter.Seq2[string, Fixture]

func (*Harness) Get

func (h *Harness) Get(name string) (Fixture, bool)

func (*Harness) Init

func (h *Harness) Init()

Init loads the set of fixtures from the embedded YAML configuration file: "ordered_fixtures.yaml".

func (*Harness) ShouldGet

func (h *Harness) ShouldGet(name string) Fixture

type TestCases

type TestCases []Fixture

TestCases is a collection of test cases Fixture

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL