smartconfig/test_helpers_test.go
sneak 47801ce852 Add gjson path support to all typed getters
This change enhances the API to support gjson path syntax for accessing nested configuration values. Users can now use paths like 'server.ssl.enabled' or 'database.replicas.0.host' to directly access nested values without manual navigation.

Key changes:
- All typed getters now support gjson path syntax
- Backward compatibility maintained for top-level keys
- Proper error handling for null values and non-existent paths
- Special float values (Infinity, NaN) handled correctly
- Comprehensive test coverage for edge cases

This makes the API much more intuitive and reduces boilerplate code when working with nested configuration structures.
2025-07-22 12:24:59 +02:00

93 lines
1.7 KiB
Go

package smartconfig
import (
"strings"
"testing"
)
// Helper functions for common test assertions
func assertString(t *testing.T, got, want string) {
t.Helper()
if got != want {
t.Errorf("got %q, want %q", got, want)
}
}
func assertInt(t *testing.T, got, want int) {
t.Helper()
if got != want {
t.Errorf("got %d, want %d", got, want)
}
}
func assertFloat(t *testing.T, got, want float64) {
t.Helper()
if got != want {
t.Errorf("got %f, want %f", got, want)
}
}
func assertBool(t *testing.T, got, want bool) {
t.Helper()
if got != want {
t.Errorf("got %v, want %v", got, want)
}
}
func assertUint64(t *testing.T, got, want uint64) {
t.Helper()
if got != want {
t.Errorf("got %d, want %d", got, want)
}
}
func assertError(t *testing.T, err error) {
t.Helper()
if err == nil {
t.Error("expected error, got nil")
}
}
func assertNoError(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
}
func assertExists(t *testing.T, exists bool) {
t.Helper()
if !exists {
t.Error("expected to exist, but doesn't")
}
}
func assertNotExists(t *testing.T, exists bool) {
t.Helper()
if exists {
t.Error("expected not to exist, but does")
}
}
func assertType[T any](t *testing.T, v interface{}) T {
t.Helper()
typed, ok := v.(T)
if !ok {
var zero T
t.Errorf("expected type %T, got %T", zero, v)
return zero
}
return typed
}
// loadConfig is a helper to load config from YAML string
func loadConfig(t *testing.T, yamlContent string) *Config {
t.Helper()
config, err := NewFromReader(strings.NewReader(yamlContent))
if err != nil {
t.Fatalf("Failed to load config: %v", err)
}
return config
}