jsondump

package module
v0.0.0-...-b250232 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2026 License: BSD-3-Clause Imports: 16 Imported by: 0

README

jsondump

Go Reference

Purpose

jsondump is a Go testing utility designed to simplify JSON assertions in tests. It dumps JSON representations of Go data structures to files that can be used for snapshot testing, making it easy to verify that your code produces the expected output.

Example Usage

package main

import (
    "testing"
    "time"

    "github.com/alextanhongpin/testdump/jsondump"
)

func TestUserData(t *testing.T) {
    // Basic usage
    user := map[string]any{
        "name": "John",
        "age": 30,
    }
    jsondump.Dump(t, user)
    
    // Ignoring time-based fields that change between test runs
    type User struct {
        Name      string    `json:"name"`
        CreatedAt time.Time `json:"createdAt"`
    }
    
    u := User{
        Name:      "John",
        CreatedAt: time.Now(),
    }
    jsondump.Dump(t, u, jsondump.IgnoreFields("createdAt"))
    
    // Masking sensitive information
    credentials := map[string]string{
        "username": "john_doe",
        "password": "secret123",
    }
    jsondump.Dump(t, credentials, jsondump.MaskFields("password"))
}

Useful git

# Regenerate 
TESTDUMP=1 go test -v 
git diff -U0 -I start -I CreatedAt -I ID | git apply --cached --unidiff-zero

Benefits

  • Simplified Testing: No need to write lengthy assertion code - just dump the data and compare with the expected output.
  • Snapshot Testing: Perfect for snapshot testing where you want to ensure your data structure doesn't change unexpectedly.
  • Flexible Options:
    • Ignore dynamic values like timestamps and IDs
    • Mask sensitive fields like passwords and API keys
    • Customize output with transformers
    • Register type-specific options for consistent handling
  • Path-based Operations: Target specific nested fields using JSONPath syntax
  • Schema Validation: Integrates with CUE for schema validation to ensure data conforms to expected shapes
  • Type Support: Works with any Go data structure that can be serialized to JSON

Advanced Features

  • Create custom transformers to modify output
  • Apply different options to different test files
  • Register type-specific options for consistent handling across tests
  • Validate against CUE schemas for additional constraints

Documentation

Index

Constants

View Source
const UUIDPattern = `[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[1-5][a-fA-F0-9]{3}-[89abAB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}`

Variables

This section is empty.

Functions

func Dump

func Dump(t *testing.T, v any, opts ...Option)

func Register

func Register(v any, opts ...Option)

Types

type Dumper

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

func New

func New(opts ...Option) *Dumper

New creates a new Dumper with the given options. The Dumper can be used to dump values to a file.

func (*Dumper) Dump

func (d *Dumper) Dump(t *testing.T, v any, opts ...Option)

func (*Dumper) Register

func (d *Dumper) Register(v any, opts ...Option)

type Masker

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

Define a struct for a Masker

func NewMask

func NewMask(mask string) *Masker

NewMask is a constructor for the Masker struct

func (*Masker) MaskFields

func (m *Masker) MaskFields(fields ...string) Option

MaskFields is a method on Masker that masks certain fields

func (*Masker) MaskPaths

func (m *Masker) MaskPaths(paths ...string) Option

MaskPaths is a method on Masker that masks certain paths

type Option

type Option func(o *options)

Define a function type Option that takes a pointer to an options struct

func CmpOpts

func CmpOpts(opts ...cmp.Option) Option

CmpOpts is an Option that adds comparison options

func Colors

func Colors(colors bool) Option

Colors is an Option that sets the colors flag

func Env

func Env(name string) Option

Env is an Option that sets the environment variable name

func File

func File(name string) Option

File is an Option that sets the file name

func IgnoreFields

func IgnoreFields(fields ...string) Option

IgnoreFields is an Option that ignores certain fields

func IgnorePaths

func IgnorePaths(paths ...string) Option

IgnorePaths is an Option that ignores certain paths

func IgnorePatterns

func IgnorePatterns(patterns ...string) Option

func MaskFields

func MaskFields(mask string, fields []string) Option

MaskFields is an Option that masks certain fields

func MaskPaths

func MaskPaths(mask string, paths []string) Option

MaskPaths is an Option that masks certain paths

func Transformers

func Transformers(p ...func([]byte) ([]byte, error)) Option

Transformers is an Option that adds a transformer function

func Validate

func Validate(vs ...Validator) Option

type Registry

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

func NewRegistry

func NewRegistry() *Registry

func (*Registry) Get

func (r *Registry) Get(v any) []Option

func (*Registry) Register

func (r *Registry) Register(v any, opts ...Option)

type Validator

type Validator interface {
	Validate([]byte) error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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