Documentation
¶
Overview ¶
Package diff - Type and Value Formatting This file contains functions for formatting Go types and values for better diff representation
Package diff provides utilities for generating, comparing, and visualizing differences between expected and actual values in various formats.
The package offers multiple ways to compare values: - Generic type comparisons using go-cmp - String-based diffing with unified diff format - Character-level diffing for detailed text comparison
It's designed to be used in testing scenarios but can be used in any context where difference visualization is needed.
Package diff - Diff Output Enrichment This file contains functions to enhance and format diff outputs
Package diff - Testing Utilities This file contains testing utilities for comparing values and generating diff reports
Package diff - UnifiedDiff implementation This file contains the functionality for parsing and formatting unified diffs
Index ¶
- func AltEnrichUnifiedDiff(diff string) string
- func ConvertToRawUnifiedDiffString(want string, got string) string
- func ConvolutedFormatReflectType(s reflect.Type) string
- func ConvolutedFormatReflectValueAsJSON(s reflect.Value) any
- func EnrichCmpDiff(diff string) string
- func EnrichUnifiedDiff(diff string) string
- func Equal[T any](t *testing.T, want, got T, opts ...OptTestingOptsSetter) bool
- func FormatDiff(diff *ProcessedDiff) string
- func RequireEqual[T any](t *testing.T, want, got T, opts ...OptTestingOptsSetter)
- func RequireKnownValueEqual[T any](t *testing.T, want, got T, opts ...OptTestingOptsSetter)
- func RequireTypeEqual(t *testing.T, want, got reflect.Type, opts ...OptTestingOptsSetter)
- func RequireUnknownTypeEqual(t *testing.T, want, got reflect.Type, opts ...OptTestingOptsSetter)
- func RequireUnknownValueEqualAsJSON(t *testing.T, want, got reflect.Value)
- func RequireValueEqual(t *testing.T, want, got reflect.Value)
- func SingleLineStringDiff(want string, got string) string
- func Strip(str string) string
- func TypeEqual(t *testing.T, want, got reflect.Type, opts ...OptTestingOptsSetter) bool
- func TypedDiff[T any](want T, got T, opts ...OptTestingOptsSetter) string
- func ValueEqual(t *testing.T, want, got reflect.Value) bool
- type ColoredRune
- type ColoredString
- func (s *ColoredString) Annotate(colord *color.Color)
- func (cs *ColoredString) AppendToEnd(r rune, c ...color.Attribute)
- func (cs *ColoredString) AppendToStart(r rune, c ...color.Attribute)
- func (cs *ColoredString) ColoredString() string
- func (cs *ColoredString) MultiAppendToEnd(rs ...*ColoredRune)
- func (cs *ColoredString) MultiAppendToStart(rs ...*ColoredRune)
- type DiffChange
- type DiffChangeType
- type DiffFormatter
- type DiffHunk
- type DiffLine
- type DiffLineType
- type DiffResult
- type Differ
- type OptTestingOptsSetter
- type ProcessedDiff
- type Req
- type TestingOpts
- type UnifiedDiff
- type ValueComparison
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AltEnrichUnifiedDiff ¶
AltEnrichUnifiedDiff provides an alternative implementation of UnifiedDiff enrichment This uses the structured diff parser and renderer
func ConvertToRawUnifiedDiffString ¶
GenerateUnifiedDiff creates a unified diff between two strings. It formats the output as a standard unified diff with context.
func ConvolutedFormatReflectType ¶
ConvolutedFormatReflectType formats a reflect.Type into a standardized string representation. It handles struct types specially, formatting them with consistent field ordering and indentation for better readability.
func ConvolutedFormatReflectValueAsJSON ¶
ConvolutedFormatReflectValue formats a reflect.Value into a standardized string representation suitable for comparison. It converts the value to JSON and then uses a stable ordering to ensure consistent output.
func EnrichCmpDiff ¶
EnrichCmpDiff enhances a diff produced by cmp.Diff with colors and formatting to make it more readable.
func EnrichUnifiedDiff ¶
EnrichUnifiedDiff enhances a unified diff with colors and formatting to make it more readable.
func Equal ¶
func Equal[T any](t *testing.T, want, got T, opts ...OptTestingOptsSetter) bool
Equal compares two values of the same type and returns true if they are equal
func FormatDiff ¶
func FormatDiff(diff *ProcessedDiff) string
FormatDiff applies color formatting to a ProcessedDiff
func RequireEqual ¶
func RequireEqual[T any](t *testing.T, want, got T, opts ...OptTestingOptsSetter)
RequireEqual compares two values and fails the test if they are not equal
func RequireKnownValueEqual ¶
func RequireKnownValueEqual[T any](t *testing.T, want, got T, opts ...OptTestingOptsSetter)
RequireKnownValueEqual compares two values of the same type and fails the test if they are not equal This is maintained for backward compatibility
func RequireTypeEqual ¶
func RequireTypeEqual(t *testing.T, want, got reflect.Type, opts ...OptTestingOptsSetter)
RequireTypeEqual compares two reflect.Type values and fails the test if they are not equal
func RequireUnknownTypeEqual ¶
func RequireUnknownTypeEqual(t *testing.T, want, got reflect.Type, opts ...OptTestingOptsSetter)
RequireUnknownTypeEqual compares two reflect.Type values and fails the test if they are not equal This is maintained for backward compatibility
func RequireUnknownValueEqualAsJSON ¶
RequireUnknownValueEqualAsJSON compares two reflect.Value values and fails the test if they are not equal This is maintained for backward compatibility
func RequireValueEqual ¶
RequireValueEqual compares two reflect.Value values and fails the test if they are not equal
func SingleLineStringDiff ¶
SingleLineStringDiff performs character-level diffing between two strings. It highlights specific characters that differ, which is useful for single-line string comparisons.
func TypedDiff ¶
func TypedDiff[T any](want T, got T, opts ...OptTestingOptsSetter) string
Types ¶
type ColoredRune ¶
func (*ColoredRune) Add ¶
func (cr *ColoredRune) Add(c ...color.Attribute)
func (*ColoredRune) MarkIsSpecial ¶
func (cr *ColoredRune) MarkIsSpecial()
type ColoredString ¶
type ColoredString struct {
// contains filtered or unexported fields
}
func NewColoredString ¶
func NewColoredString(s string) *ColoredString
func (*ColoredString) Annotate ¶
func (s *ColoredString) Annotate(colord *color.Color)
func (*ColoredString) AppendToEnd ¶
func (cs *ColoredString) AppendToEnd(r rune, c ...color.Attribute)
func (*ColoredString) AppendToStart ¶
func (cs *ColoredString) AppendToStart(r rune, c ...color.Attribute)
func (*ColoredString) ColoredString ¶
func (cs *ColoredString) ColoredString() string
func (*ColoredString) MultiAppendToEnd ¶
func (cs *ColoredString) MultiAppendToEnd(rs ...*ColoredRune)
func (*ColoredString) MultiAppendToStart ¶
func (cs *ColoredString) MultiAppendToStart(rs ...*ColoredRune)
type DiffChange ¶
type DiffChange struct { Text string Type DiffChangeType }
DiffChange represents a character-level change in a line
type DiffChangeType ¶
type DiffChangeType string
DiffChangeType indicates whether a change is an addition, removal, or unchanged text
const ( // DiffChangeUnchanged represents text that is the same in both versions DiffChangeUnchanged DiffChangeType = "unchanged" // DiffChangeAdded represents text that was added DiffChangeAdded DiffChangeType = "added" // DiffChangeRemoved represents text that was removed DiffChangeRemoved DiffChangeType = "removed" )
type DiffFormatter ¶
type DiffFormatter interface { // Format formats a diff string with visual enhancements Format(diff string) string }
DiffFormatter defines the interface for formatters that can render diffs
type DiffLine ¶
type DiffLine struct { Content string Applied string Type DiffLineType Changes []DiffChange // Character-level changes within the line }
DiffLine represents a line in the unified diff with metadata
type DiffLineType ¶
type DiffLineType string
DiffLineType indicates whether a line is context, addition, or removal
const ( // DiffLineContext represents an unchanged line shown for context DiffLineContext DiffLineType = "context" // DiffLineAdded represents a line that was added DiffLineAdded DiffLineType = "added" // DiffLineRemoved represents a line that was removed DiffLineRemoved DiffLineType = "removed" )
type DiffResult ¶
type DiffResult struct { // Content is the formatted diff content Content string // IsEqual is true if there is no difference IsEqual bool }
DiffResult represents the output of a diff operation
type Differ ¶
type Differ interface { // Diff generates a diff between two values Diff(want, got interface{}) DiffResult }
Differ defines the interface for types that can generate diffs
type OptTestingOptsSetter ¶
type OptTestingOptsSetter func(o *TestingOpts)
func WithCmpOpts ¶
func WithCmpOpts(opt []cmp.Option) OptTestingOptsSetter
func WithLogRawDiffOnFail ¶
func WithLogRawDiffOnFail(opt bool) OptTestingOptsSetter
func WithUnexportedType ¶
func WithUnexportedType[T any]() OptTestingOptsSetter
WithUnexportedType adds an option to allow comparing unexported fields in a type This is useful when testing struct values with private fields
type ProcessedDiff ¶
ProcessedDiff represents a fully processed diff with all metadata
type Req ¶
type Req struct {
// contains filtered or unexported fields
}
func (*Req) Opts ¶
func (r *Req) Opts(opts ...OptTestingOptsSetter) *Req
type TestingOpts ¶
type TestingOpts struct {
// contains filtered or unexported fields
}
TestingOpts contains options for diff testing functionality
func NewTestingOpts ¶
func NewTestingOpts( options ...OptTestingOptsSetter, ) TestingOpts
func (*TestingOpts) Validate ¶
func (o *TestingOpts) Validate() error
type UnifiedDiff ¶
UnifiedDiff represents a parsed unified diff with methods to process and format it
func ParseUnifiedDiff ¶
func ParseUnifiedDiff(diffStr string) (*UnifiedDiff, error)
ParseUnifiedDiff parses a unified diff string into a structured format It normalizes the input and uses go-diff to parse the unified diff format
func (*UnifiedDiff) PrettyPrint ¶
func (ud *UnifiedDiff) PrettyPrint() string
PrettyPrint formats the unified diff with colors
func (*UnifiedDiff) ProcessDiff ¶
func (ud *UnifiedDiff) ProcessDiff() (*ProcessedDiff, error)
ProcessDiff converts a UnifiedDiff into a ProcessedDiff This is the core logic that can be tested separately from color formatting
type ValueComparison ¶
type ValueComparison struct {
// contains filtered or unexported fields
}
ValueComparison provides methods for comparing different types of values
func NewValueComparison ¶
func NewValueComparison(t *testing.T) *ValueComparison
NewValueComparison creates a new value comparison helper for testing