configtest

package
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package configtest provides a mock implementation of the config provider API for use in tests. The mock records all calls and allows configuring per-method behavior via function fields.

mock := &configtest.MockConfig{}
mock.GetFn = func(key string) any {
    if key == "database.host" {
        return "localhost"
    }
    return nil
}

For tests that just need a working config seeded with known values, use NewMockConfigWithValues, which delegates to a real *config.Config under the hood so Get/Find/Bind retain their normal semantics.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Call

type Call struct {
	Method string
	Args   []any
}

Call records a single method invocation on the mock.

type MockConfig

type MockConfig struct {
	SetDefaultFn  func(key string, value any)
	SetDefaultsFn func(values any) error
	SetFn         func(key string, value any)
	BindFn        func(dest any, options ...config.BindOption) error
	GetFn         func(key string) any
	FindFn        func(key string) (any, bool)
	ValuesFn      func() map[string]any
	Calls         []Call
	// contains filtered or unexported fields
}

MockConfig is a configurable mock that structurally satisfies the gas.ConfigProvider interface. Each method delegates to its corresponding Fn field if set, otherwise returns the zero value. All calls are recorded in the Calls slice for assertions.

func NewMockConfigWithValues

func NewMockConfigWithValues(values map[string]any) (*MockConfig, error)

NewMockConfigWithValues returns a MockConfig whose Fn fields delegate to a real *config.Config seeded with the provided values. This preserves real key-normalization, Get/Find, and Bind semantics while still recording calls and allowing individual Fn fields to be overridden afterwards.

func (*MockConfig) Bind

func (m *MockConfig) Bind(dest any, options ...config.BindOption) error

Bind records the call and delegates to BindFn if set.

func (*MockConfig) CallCount

func (m *MockConfig) CallCount(method string) int

CallCount returns the number of times the given method was called.

func (*MockConfig) Find

func (m *MockConfig) Find(key string) (value any, exist bool)

Find records the call and delegates to FindFn if set.

func (*MockConfig) Get

func (m *MockConfig) Get(key string) any

Get records the call and delegates to GetFn if set.

func (*MockConfig) Reset

func (m *MockConfig) Reset()

Reset clears all recorded calls.

func (*MockConfig) Set

func (m *MockConfig) Set(key string, value any)

Set records the call and delegates to SetFn if set.

func (*MockConfig) SetDefault

func (m *MockConfig) SetDefault(key string, value any)

SetDefault records the call and delegates to SetDefaultFn if set.

func (*MockConfig) SetDefaults

func (m *MockConfig) SetDefaults(values any) error

SetDefaults records the call and delegates to SetDefaultsFn if set.

func (*MockConfig) Values

func (m *MockConfig) Values() map[string]any

Values records the call and delegates to ValuesFn if set.

Jump to

Keyboard shortcuts

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