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 ¶
- func Verify(t testingT, target interface{})
- type AfterVerifyFunc
- type BeforeVerifyFunc
- type CleanupFunc
- type CompareResult
- type EqualFiles
- type EqualityResult
- type FileAppenderFunc
- type FileConventionFunc
- type FileEquality
- type FilePair
- type FirstVerifyFunc
- type InstanceScrubber
- type JSONAppenderFunc
- type NewFiles
- type NotEqualFilePair
- type NotEqualFiles
- type RemoveLineFunc
- type ReplaceLineFunc
- type StreamComparerFunc
- type StringComparerFunc
- type Target
- type Verifier
- type VerifyConfigure
- func AddScrubber(fun InstanceScrubber) VerifyConfigure
- func AddScrubberForExtension(extension string, fun InstanceScrubber) VerifyConfigure
- func AutoVerify() VerifyConfigure
- func DisableDiff() VerifyConfigure
- func DontScrubGuids() VerifyConfigure
- func DontScrubTimes() VerifyConfigure
- func OmitContentFromError() VerifyConfigure
- func OnAfterVerify(fun AfterVerifyFunc) VerifyConfigure
- func OnBeforeVerify(fun BeforeVerifyFunc) VerifyConfigure
- func OnFirstVerify(fun FirstVerifyFunc) VerifyConfigure
- func OnVerifyDelete(fun VerifyDeleteFunc) VerifyConfigure
- func OnVerifyMismatch(fun VerifyMismatchFunc) VerifyConfigure
- func ScrubEmptyLines() VerifyConfigure
- func ScrubInlineGuids() VerifyConfigure
- func ScrubInlineTime(format string) VerifyConfigure
- func ScrubLines(fun RemoveLineFunc) VerifyConfigure
- func ScrubLinesContaining(stringToMatch ...string) VerifyConfigure
- func ScrubLinesContainingAnyCase(stringToMatch ...string) VerifyConfigure
- func ScrubLinesWithReplace(fun ReplaceLineFunc) VerifyConfigure
- func ScrubMachineName() VerifyConfigure
- func TestCase(name string) VerifyConfigure
- func UniqueForArchitecture() VerifyConfigure
- func UniqueForOperatingSystem() VerifyConfigure
- func UniqueForRuntime() VerifyConfigure
- func UseDirectory(directory string) VerifyConfigure
- func UseExtension(extension string) VerifyConfigure
- func UseStreamComparer(fun StreamComparerFunc) VerifyConfigure
- func UseStrictJSON() VerifyConfigure
- func UseStringComparer(fun StringComparerFunc) VerifyConfigure
- type VerifyDeleteFunc
- type VerifyMismatchFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 CompareResult ¶
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 FileConventionFunc ¶
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 ¶
InstanceScrubber a function that scrubs the input target and returns the scrubbed version
type NotEqualFilePair ¶
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 ¶
RemoveLineFunc decides if a line from the target should be removed
type ReplaceLineFunc ¶
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 ¶
GetExtension returns the extension of the target
func (*Target) GetStreamData ¶
GetStreamData returns the underlying target data as a stream
func (*Target) GetStringBuilderData ¶
GetStringBuilderData returns the underlying target data as strings.Builder
func (*Target) GetStringData ¶
GetStringData returns the underlying target data as a string
func (*Target) IsStringBuilder ¶
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 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 ¶
VerifyMismatchFunc a function to run when a content mismatch happens