verifier

package
v1.0.0-beta Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2024 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

Package verifier provides a set of APIs that tie in to the Go testing system and allow snapshot testing.

Example Usage

To verify validity of a struct in a standard test file called function:

import (
  "testing"
  "github.com/VerifyTests/Verify.Go/verifier"
)

func TestVerifyingStructs(t *testing.T) {
  var person = Person{
    ID:         "ebced679-45d3-4653-8791-3d969c4a986c",
    Title:      Mr,
    FamilyName: "Smith",
    GivenNames: "John",
    Dob:        time.Date(2022, 02, 01, 1, 2, 3, 4, time.Local),
    Spouse:     "Jill",
    Address: Address{
      Street:  "4 Puddle Lane",
      Country: &country,
    },
    Children: []string{"Sam", "Mary"},
  }
  verifier.Verify(t, person)
}

When the test is initially run will fail. If a diff tool is detected it will display the diff. To verify the result:

  • Use the diff tool to accept the changes, or
  • Manually copy the text to the new file

After verifications, all `*.verified.*` files should be committed to source control and all `*.received.*` files should be excluded from source control.

Verify allow configuring the verification process by using VerifyConfigure:

   import (
     "testing"
     "github.com/VerifyTests/Verify.Go/verifier"
   )

   func TestCustomConfiguration(t *testing.T) {
     verifier.NewVerifier(t,
	   verifier.UseDirectory("../_testdata"), //use custom data directory
	   verifier.UseExtension("ext"), //use custom extension
	 ).Verify("Hello World!")
   }

All assertion functions take, as the first argument, the `*testing.T` object provided by the testing framework. This allows the assertion funcs to write the failings and other details to the correct place.

Every assertion function also takes an optional string message as the final argument, allowing custom error messages to be appended to the message the assertion method outputs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Verify

func Verify(t testingT, target interface{})

Verify verifies the passed target with the default settings.

Types

type AfterVerifyFunc

type AfterVerifyFunc func()

AfterVerifyFunc a function to run after verification

type BeforeVerifyFunc

type BeforeVerifyFunc func()

BeforeVerifyFunc a function to run before verification

type CleanupFunc

type CleanupFunc func()

CleanupFunc cleanup function

type CompareResult

type CompareResult struct {
	IsEqual bool
	Message string
}

CompareResult specifies comparison results with an optional message

type EqualFiles

type EqualFiles []FilePair

EqualFiles a slice of FilePair that contains equal files

type EqualityResult

type EqualityResult struct {
	Equality FileEquality
	Message  string
}

EqualityResult specifies file content equality with an optional message

type FileAppenderFunc

type FileAppenderFunc func() *Target

FileAppenderFunc returns a target

type FileConventionFunc

type FileConventionFunc func(uniqueness string) (fileNamePrefix string, directory string)

FileConventionFunc provides a unique file and directory for the test

type FileEquality

type FileEquality int

FileEquality file content equality

const (
	//FileEqual designates files with equal contents
	FileEqual FileEquality = iota
	//FileNotEqual designates files with different contents
	FileNotEqual
	//FileNew designates new files
	FileNew
)

type FilePair

type FilePair struct {
	Extension    string
	ReceivedPath string
	VerifiedPath string
	ReceivedName string
	VerifiedName string
	Name         string
	IsText       bool
}

FilePair information for a file being compared containing received and verified files.

type FirstVerifyFunc

type FirstVerifyFunc func(file FilePair)

FirstVerifyFunc a function to run on first verification

type InstanceScrubber

type InstanceScrubber func(target string) string

InstanceScrubber a function that scrubs the input target and returns the scrubbed version

type JSONAppenderFunc

type JSONAppenderFunc func() *toAppend

JSONAppenderFunc returns an appender

type NewFiles

type NewFiles []FilePair

NewFiles a slice of FilePair that contains new files

type NotEqualFilePair

type NotEqualFilePair struct {
	File    FilePair
	Message string
}

NotEqualFilePair designates a non-equal file information with an optional message

type NotEqualFiles

type NotEqualFiles []NotEqualFilePair

NotEqualFiles a slice of NotEqualFilePair that contains non-equal files

type RemoveLineFunc

type RemoveLineFunc func(string) bool

RemoveLineFunc decides if a line from the target should be removed

type ReplaceLineFunc

type ReplaceLineFunc func(string) string

ReplaceLineFunc replaces a line from the target the the output

type StreamComparerFunc

type StreamComparerFunc func(received, verified []byte) CompareResult

StreamComparerFunc a function that compares stream contents of received and verified files and returns the CompareResult

type StringComparerFunc

type StringComparerFunc func(received, verified string) CompareResult

StringComparerFunc a function that compares string content of received and verified files and returns the CompareResult

type Target

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

Target contains the information about the underlying target being verified

func (*Target) GetExtension

func (t *Target) GetExtension() string

GetExtension returns the extension of the target

func (*Target) GetStreamData

func (t *Target) GetStreamData() []byte

GetStreamData returns the underlying target data as a stream

func (*Target) GetStringBuilderData

func (t *Target) GetStringBuilderData() *strings.Builder

GetStringBuilderData returns the underlying target data as strings.Builder

func (*Target) GetStringData

func (t *Target) GetStringData() string

GetStringData returns the underlying target data as a string

func (*Target) IsStream

func (t *Target) IsStream() bool

IsStream checks if the target is a binary data

func (*Target) IsString

func (t *Target) IsString() bool

IsString checks if the target is a string data

func (*Target) IsStringBuilder

func (t *Target) IsStringBuilder() bool

IsStringBuilder checks if the target is a strings.Builder type.

type Verifier

type Verifier interface {
	Verify(target interface{})
	Configure(configure ...VerifyConfigure) Verifier
}

Verifier is the main interface for verification process.

func NewVerifier

func NewVerifier(t testingT, configure ...VerifyConfigure) Verifier

NewVerifier creates a new Verifier with the associated settings

type VerifyConfigure

type VerifyConfigure = func(settings *verifySettings)

func AddScrubber

func AddScrubber(fun InstanceScrubber) VerifyConfigure

AddScrubber add a function to the front of the scrubber collections.

func AddScrubberForExtension

func AddScrubberForExtension(extension string, fun InstanceScrubber) VerifyConfigure

AddScrubberForExtension adds a function for a specified extension to the front of the scrubber collection.

func AutoVerify

func AutoVerify() VerifyConfigure

AutoVerify automatically accepts the received files

func DisableDiff

func DisableDiff() VerifyConfigure

DisableDiff enables the diff tools

func DontScrubGuids

func DontScrubGuids() VerifyConfigure

DontScrubGuids do not auto-scrub UUID values

func DontScrubTimes

func DontScrubTimes() VerifyConfigure

DontScrubTimes do not auto-scrub time.Time values

func OmitContentFromError

func OmitContentFromError() VerifyConfigure

OmitContentFromError show the content differences when a mismatch occurs during verification

func OnAfterVerify

func OnAfterVerify(fun AfterVerifyFunc) VerifyConfigure

OnAfterVerify callback that is executed after the verify process

func OnBeforeVerify

func OnBeforeVerify(fun BeforeVerifyFunc) VerifyConfigure

OnBeforeVerify callback that is executed before the verify process

func OnFirstVerify

func OnFirstVerify(fun FirstVerifyFunc) VerifyConfigure

OnFirstVerify callback that is executed on the first verify

func OnVerifyDelete

func OnVerifyDelete(fun VerifyDeleteFunc) VerifyConfigure

OnVerifyDelete callback that is executed before a file is deleted

func OnVerifyMismatch

func OnVerifyMismatch(fun VerifyMismatchFunc) VerifyConfigure

OnVerifyMismatch callback that is executed when a mismatch happens

func ScrubEmptyLines

func ScrubEmptyLines() VerifyConfigure

ScrubEmptyLines scrubs all the empty lines from the target

func ScrubInlineGuids

func ScrubInlineGuids() VerifyConfigure

ScrubInlineGuids scrubs inline UUID values with string types

func ScrubInlineTime

func ScrubInlineTime(format string) VerifyConfigure

ScrubInlineTime scrubs inline Time values with string types

func ScrubLines

func ScrubLines(fun RemoveLineFunc) VerifyConfigure

ScrubLines scrub target lines with the provided function

func ScrubLinesContaining

func ScrubLinesContaining(stringToMatch ...string) VerifyConfigure

ScrubLinesContaining scrubs the line containing specified strings

func ScrubLinesContainingAnyCase

func ScrubLinesContainingAnyCase(stringToMatch ...string) VerifyConfigure

ScrubLinesContainingAnyCase scrubs strings that match the data in the target

func ScrubLinesWithReplace

func ScrubLinesWithReplace(fun ReplaceLineFunc) VerifyConfigure

ScrubLinesWithReplace scrubs target lines and replace with the value provided by the function

func ScrubMachineName

func ScrubMachineName() VerifyConfigure

ScrubMachineName scrubs the machine name from the target data

func TestCase

func TestCase(name string) VerifyConfigure

TestCase specify a case name for the test.

func UniqueForArchitecture

func UniqueForArchitecture() VerifyConfigure

UniqueForArchitecture create file names based on the runtime architecture

func UniqueForOperatingSystem

func UniqueForOperatingSystem() VerifyConfigure

UniqueForOperatingSystem create file names based on the runtime operating system

func UniqueForRuntime

func UniqueForRuntime() VerifyConfigure

UniqueForRuntime create file names based on the Go runtime versions

func UseDirectory

func UseDirectory(directory string) VerifyConfigure

UseDirectory place the output files in the specified directory

func UseExtension

func UseExtension(extension string) VerifyConfigure

UseExtension specify an extension to use for the outputted files

func UseStreamComparer

func UseStreamComparer(fun StreamComparerFunc) VerifyConfigure

UseStreamComparer use the specified function for stream comparison

func UseStrictJSON

func UseStrictJSON() VerifyConfigure

UseStrictJSON use .json extension for the outputted files

func UseStringComparer

func UseStringComparer(fun StringComparerFunc) VerifyConfigure

UseStringComparer use the specified function for string comparison

type VerifyDeleteFunc

type VerifyDeleteFunc func(file string)

VerifyDeleteFunc a function to run before deletion of a file

type VerifyMismatchFunc

type VerifyMismatchFunc func(file FilePair, message string)

VerifyMismatchFunc a function to run when a content mismatch happens

Jump to

Keyboard shortcuts

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