testutils

package
v1.11.4 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

README

pkg/validator/testutils

This package provides common test utilities for validator tests to eliminate duplication across validator packages.

Purpose

Contains shared testing utilities, common test case patterns, and helper functions specifically designed for testing validator packages. This package provides standardized testing patterns to ensure consistent validation testing across all validator types.

Features

  • Common Test Cases: Standardized test case structures for validator testing
  • Test Utilities: Helper functions for setting up and running validator tests
  • Error Testing: Patterns for testing validation error scenarios
  • Result Validation: Utilities for validating ValidationResult outputs
  • Generic Test Support: Type-safe test utilities using Go generics

Usage

import "github.com/devantler-tech/ksail-go/pkg/validator/testutils"

func TestValidator(t *testing.T) {
    testCases := []testutils.ValidatorTestCase[MyConfigType]{
        {
            Name:          "valid config",
            Config:        validConfig,
            ExpectedValid: true,
        },
        {
            Name:          "invalid config",
            Config:        invalidConfig,
            ExpectedValid: false,
        },
    }

    validator := &MyValidator{}
    testutils.RunValidatorTests(t, validator, testCases)
}

Key Components

  • common.go: Common test utilities and test case structures
  • ValidatorTestCase: Generic test case structure for validator testing
  • test helpers: Utility functions for running standardized validator tests

Integration

This package is used by all validator packages for testing:

  • pkg/validator/kind: Kind validator testing
  • pkg/validator/k3d: K3d validator testing
  • pkg/validator/eks: EKS validator testing
  • pkg/validator/ksail: KSail validator testing
  • pkg/validator/metadata: Metadata validator testing

Test Patterns

The package provides standardized patterns for:

  • Valid Configuration Testing: Ensuring valid configs pass validation
  • Invalid Configuration Testing: Ensuring invalid configs fail validation
  • Error Message Testing: Validating specific error messages and types
  • Edge Case Testing: Common edge cases across all validator types

This ensures consistent and comprehensive testing across all validator functionality in KSail.


⬅️ Go Back

Documentation

Overview

Package testutils provides common test utilities for validator tests to eliminate duplication.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertValidationResult

func AssertValidationResult[T any](
	t *testing.T,
	testCase ValidatorTestCase[T],
	result *validator.ValidationResult,
)

AssertValidationResult provides a common assertion pattern for validation results.

func RunNewValidatorConstructorTest

func RunNewValidatorConstructorTest[T any](
	t *testing.T,
	constructorFunc func() validator.Validator[T],
)

RunNewValidatorConstructorTest runs a common test pattern for validator constructors. This eliminates duplication of NewValidator constructor tests across validator test files.

func RunValidateTest

func RunValidateTest[T any](
	t *testing.T,
	contractTestFunc func(*testing.T),
	edgeTestFuncs ...func(*testing.T),
)

RunValidateTest runs a common test pattern for the main Validate method. This eliminates duplication of TestValidate function structure across validator test files.

func RunValidatorTests

func RunValidatorTests[T any](
	t *testing.T,
	validatorInstance validator.Validator[T],
	testCases []ValidatorTestCase[T],
	assertFunc func(*testing.T, ValidatorTestCase[T], *validator.ValidationResult),
)

RunValidatorTests runs a common test pattern for validator implementations. This eliminates duplication between different validator test files.

Types

type MetadataTestCaseConfig

type MetadataTestCaseConfig struct {
	ExpectedKind       string
	ExpectedAPIVersion string
}

MetadataTestCaseConfig contains configuration for metadata validation test cases.

type ValidatorTestCase

type ValidatorTestCase[T any] struct {
	Name           string
	Config         T
	ExpectedValid  bool
	ExpectedErrors []validator.ValidationError
}

ValidatorTestCase represents a common test case structure for validator tests.

func CreateMetadataValidationTestCases

func CreateMetadataValidationTestCases[T any](
	configFactory func() T,
	setKind func(T, string),
	setAPIVersion func(T, string),
	config MetadataTestCaseConfig,
) []ValidatorTestCase[T]

CreateMetadataValidationTestCases creates common metadata validation test cases. This eliminates duplication of Kind and APIVersion validation tests across validators. The configFactory function should create a new instance each time it's called.

func CreateNilConfigTestCase

func CreateNilConfigTestCase[T any]() ValidatorTestCase[T]

CreateNilConfigTestCase creates a standard test case for nil configuration validation. This only works correctly for pointer types where the zero value is nil.

Jump to

Keyboard shortcuts

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