approvals

package module
v0.0.0-...-32d5677 Latest Latest
Warning

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

Go to latest
Published: May 30, 2022 License: Apache-2.0 Imports: 14 Imported by: 0

README

ApprovalTests.go

ApprovalTests for go

GoDoc Go Report Card Coverage Status Build and Test

Golden master Verification Library

ApprovalTests allows for easy testing of larger objects, strings and anything else that can be saved to a file (images, sounds, csv, etc...)

Examples

Basic string verification

func TestHelloWorld(t *testing.T) {
	approvals.VerifyString(t, "Hello World!")
}

Store approved files in testdata subfolder

Some people prefer to store their approved files in a subfolder "testdata" instead of in the same folder as the production code. To configure this, add a call to UseFolder to your TestMain:

func TestMain(m *testing.M) {
	UseFolder("testdata")
	os.Exit(m.Run())
}

In Project

Note: ApprovalTests uses approvals to test itself. Therefore there are many examples in the code itself.

JSON

VerifyJSONBytes - Simple Formatting for easy comparison. Also uses the .json file extension

func TestVerifyJSON(t *testing.T) {
	jsonb := []byte("{ \"foo\": \"bar\", \"age\": 42, \"bark\": \"woof\" }")
	VerifyJSONBytes(t, jsonb)
}

Matches file: approvals_test.TestVerifyJSON.received.json

{
  "age": 42,
  "bark": "woof",
  "foo": "bar"
}

Reporters

ApprovalTests becomes much more powerful with reporters. Reporters launch programs on failure to help you understand, fix and approve results.

You can make your own easily, here's an example You can also declare which one to use. Either at the

Method level
r := UseReporter(reporters.NewIntelliJ())
defer r.Close()
Test Level
func TestMain(m *testing.M) {
	r := UseReporter(reporters.NewBeyondCompareReporter())
	defer r.Close()
	UseFolder("testdata")

	os.Exit(m.Run())
}

Documentation

Overview

Package approvals allows for easy testing of larger objects that can be saved to a file (images, sounds, csv, etc...)

Index

Examples

Constants

View Source
const SkipThisCombination = "♬ SKIP THIS COMBINATION ♬"

SkipThisCombination should be returned if you do not want to process a particular combination.

Variables

This section is empty.

Functions

func Options

func Options() verifyOptions

Options enables providing individual Verify functions with customisations such as scrubbers

func UseFolder

func UseFolder(f string)

UseFolder configures which folder to use to store approval files. By default, the approval files will be stored at the same level as the code.

The following examples shows how to use the idiomatic 'testdata' folder for all of your test cases in a package directory.

func TestMain(m *testing.M) {
	approvals.UseFolder("testdata")

	os.Exit(m.Run())
}

func UseFrontLoadedReporter

func UseFrontLoadedReporter(reporter reporters.Reporter) io.Closer

UseFrontLoadedReporter configures reporters ahead of all other reporters to handle situations like CI. These reporters usually prevent reporting in scenarios that are headless.

func UseReporter

func UseReporter(reporter reporters.Reporter) io.Closer

UseReporter configures which reporter to use on failure. Add at the test or method level to configure your reporter.

The following examples shows how to use a reporter for all of your test cases in a package directory through go's setup feature.

func TestMain(m *testing.M) {
	r := approvals.UseReporter(reporters.NewBeyondCompareReporter())
	defer r.Close()

	os.Exit(m.Run())
}

func Verify

func Verify(t Failable, reader io.Reader, opts ...verifyOptions)

Verify Example:

Verify(t, strings.NewReader("Hello"))

func VerifyAll

func VerifyAll(t Failable, header string, collection interface{}, transform func(interface{}) string, opts ...verifyOptions)

VerifyAll Example:

VerifyAll(t, "uppercase", []string("dog", "cat"}, func(x interface{}) string { return strings.ToUpper(x.(string)) })

func VerifyAllCombinationsFor1

func VerifyAllCombinationsFor1(t Failable, header string, transform func(interface{}) string, collection1 interface{})

VerifyAllCombinationsFor1 Example:

VerifyAllCombinationsFor1(t, "uppercase", func(x interface{}) string { return strings.ToUpper(x.(string)) }, []string("dog", "cat"})

func VerifyAllCombinationsFor2

func VerifyAllCombinationsFor2(
	t Failable,
	header string,
	transform func(p1, p2 interface{}) string,
	collection1, collection2 interface{})

VerifyAllCombinationsFor2 calls the transform function with all combinations from collection 1 and collection 2. The resulting received file contains all inputs and the resulting outputs. The received file is then compared to the approved version. If the transform function returns SkipThisCombination the output of this combination won't be displayed inside the received file.

Example
t = makeExamplesRunLikeTests("ExampleVerifyAllCombinationsFor2")

letters := []string{"aaaaa", "bbbbb", "ccccc"}
numbers := []int{2, 3}

functionToTest := func(text interface{}, length interface{}) string {
	return text.(string)[:length.(int)]
}

approvals.VerifyAllCombinationsFor2(t, "substring", functionToTest, letters, numbers)
printFileContent("examples_test.ExampleVerifyAllCombinationsFor2.received.txt")
Output:

This produced the file examples_test.ExampleVerifyAllCombinationsFor2.received.txt
It will be compared against the examples_test.ExampleVerifyAllCombinationsFor2.approved.txt file
and contains the text:

substring

[aaaaa,2] => aa
[aaaaa,3] => aaa
[bbbbb,2] => bb
[bbbbb,3] => bbb
[ccccc,2] => cc
[ccccc,3] => ccc
Example (WithSkip)
t = makeExamplesRunLikeTests("ExampleVerifyAllCombinationsFor2_withSkip")

words := []string{"stack", "fold"}
otherWords := []string{"overflow", "trickle"}

functionToTest := func(firstWord interface{}, secondWord interface{}) string {
	first := firstWord.(string)
	second := secondWord.(string)
	if first+second == "stackoverflow" {
		return approvals.SkipThisCombination
	}
	return first + second
}

approvals.VerifyAllCombinationsFor2(t, "combineWords", functionToTest, words, otherWords)
printFileContent("examples_test.ExampleVerifyAllCombinationsFor2_withSkip.received.txt")
Output:

This produced the file examples_test.ExampleVerifyAllCombinationsFor2_withSkip.received.txt
It will be compared against the examples_test.ExampleVerifyAllCombinationsFor2_withSkip.approved.txt file
and contains the text:

combineWords

[stack,trickle] => stacktrickle
[fold,overflow] => foldoverflow
[fold,trickle] => foldtrickle

func VerifyAllCombinationsFor3

func VerifyAllCombinationsFor3(
	t Failable,
	header string,
	transform func(p1, p2, p3 interface{}) string,
	collection1, collection2, collection3 interface{})

VerifyAllCombinationsFor3 is for combinations of 3.

func VerifyAllCombinationsFor4

func VerifyAllCombinationsFor4(
	t Failable,
	header string,
	transform func(p1, p2, p3, p4 interface{}) string,
	collection1, collection2, collection3, collection4 interface{})

VerifyAllCombinationsFor4 is for combinations of 4.

func VerifyAllCombinationsFor5

func VerifyAllCombinationsFor5(
	t Failable,
	header string,
	transform func(p1, p2, p3, p4, p5 interface{}) string,
	collection1, collection2, collection3, collection4, collection5 interface{})

VerifyAllCombinationsFor5 is for combinations of 5.

func VerifyAllCombinationsFor6

func VerifyAllCombinationsFor6(
	t Failable,
	header string,
	transform func(p1, p2, p3, p4, p5, p6 interface{}) string,
	collection1, collection2, collection3, collection4, collection5, collection6 interface{})

VerifyAllCombinationsFor6 is for combinations of 6.

func VerifyAllCombinationsFor7

func VerifyAllCombinationsFor7(
	t Failable,
	header string,
	transform func(p1, p2, p3, p4, p5, p6, p7 interface{}) string,
	collection1, collection2, collection3, collection4, collection5, collection6, collection7 interface{})

VerifyAllCombinationsFor7 is for combinations of 7.

func VerifyAllCombinationsFor8

func VerifyAllCombinationsFor8(
	t Failable,
	header string,
	transform func(p1, p2, p3, p4, p5, p6, p7, p8 interface{}) string,
	collection1, collection2, collection3, collection4, collection5, collection6, collection7, collection8 interface{})

VerifyAllCombinationsFor8 is for combinations of 8.

func VerifyAllCombinationsFor9

func VerifyAllCombinationsFor9(
	t Failable,
	header string,
	transform func(a, b, c, d, e, f, g, h, i interface{}) string,
	collection1,
	collection2,
	collection3,
	collection4,
	collection5,
	collection6,
	collection7,
	collection8,
	collection9 interface{})

VerifyAllCombinationsFor9 is for combinations of 9.

func VerifyArray

func VerifyArray(t Failable, array interface{}, opts ...verifyOptions)

VerifyArray Example:

VerifyArray(t, []string{"dog", "cat"})

func VerifyJSONBytes

func VerifyJSONBytes(t Failable, bs []byte, opts ...verifyOptions)

VerifyJSONBytes Example:

VerifyJSONBytes(t, []byte("{ \"Greeting\": \"Hello\" }"))

func VerifyJSONStruct

func VerifyJSONStruct(t Failable, obj interface{}, opts ...verifyOptions)

VerifyJSONStruct Example:

VerifyJSONStruct(t, json)

func VerifyMap

func VerifyMap(t Failable, m interface{}, opts ...verifyOptions)

VerifyMap Example:

VerifyMap(t, map[string][string] { "dog": "bark" })

func VerifyString

func VerifyString(t Failable, s string, opts ...verifyOptions)

VerifyString stores the passed string into the received file and confirms that it matches the approved local file. On failure, it will launch a reporter.

Example
approvals.VerifyString(t, "Hello World!")
printFileContent("examples_test.ExampleVerifyString.received.txt")
Output:

This produced the file examples_test.ExampleVerifyString.received.txt
It will be compared against the examples_test.ExampleVerifyString.approved.txt file
and contains the text:

Hello World!

func VerifyWithExtension

func VerifyWithExtension(t Failable, reader io.Reader, extWithDot string, opts ...verifyOptions)

VerifyWithExtension Example:

VerifyWithExtension(t, strings.NewReader("Hello"), ".json")

Deprecated: Please use Verify with the Options() fluent syntax.

func VerifyXMLBytes

func VerifyXMLBytes(t Failable, bs []byte, opts ...verifyOptions)

VerifyXMLBytes Example:

VerifyXMLBytes(t, []byte("<Test/>"))

func VerifyXMLStruct

func VerifyXMLStruct(t Failable, obj interface{}, opts ...verifyOptions)

VerifyXMLStruct Example:

VerifyXMLStruct(t, xml)

Types

type ApprovalName

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

ApprovalName struct.

func NewApprovalName

func NewApprovalName(name string, fileName string) ApprovalName

NewApprovalName returns a new ApprovalName object.

type Failable

type Failable interface {
	Fail()
	Fatal(args ...interface{})
	Fatalf(format string, args ...interface{})
	Name() string
	Log(args ...interface{})
	Logf(format string, args ...interface{})
	Helper()
}

Failable is an interface wrapper around testing.T

Directories

Path Synopsis
Package reporters provides types to report comparison results.
Package reporters provides types to report comparison results.
Package utils contains functions supporting approval testing.
Package utils contains functions supporting approval testing.

Jump to

Keyboard shortcuts

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