Documentation
¶
Overview ¶
Package approvals allows for easy testing of larger objects that can be saved to a file (images, sounds, csv, etc...)
Index ¶
- Constants
- func Options() verifyOptions
- func UseFolder(f string)
- func UseFrontLoadedReporter(reporter reporters.Reporter) io.Closer
- func UseReporter(reporter reporters.Reporter) io.Closer
- func UseUpdateOption(option bool)
- func Verify(t Failable, reader io.Reader, opts ...verifyOptions)
- func VerifyAll(t Failable, header string, collection interface{}, ...)
- func VerifyAllCombinationsFor1(t Failable, header string, transform func(interface{}) string, ...)
- func VerifyAllCombinationsFor2(t Failable, header string, transform func(p1, p2 interface{}) string, ...)
- func VerifyAllCombinationsFor3(t Failable, header string, transform func(p1, p2, p3 interface{}) string, ...)
- func VerifyAllCombinationsFor4(t Failable, header string, transform func(p1, p2, p3, p4 interface{}) string, ...)
- func VerifyAllCombinationsFor5(t Failable, header string, ...)
- func VerifyAllCombinationsFor6(t Failable, header string, ...)
- func VerifyAllCombinationsFor7(t Failable, header string, ...)
- func VerifyAllCombinationsFor8(t Failable, header string, ...)
- func VerifyAllCombinationsFor9(t Failable, header string, ...)
- func VerifyArray(t Failable, array interface{}, opts ...verifyOptions)
- func VerifyJSONBytes(t Failable, bs []byte, opts ...verifyOptions)
- func VerifyJSONStruct(t Failable, obj interface{}, opts ...verifyOptions)
- func VerifyMap(t Failable, m interface{}, opts ...verifyOptions)
- func VerifyString(t Failable, s string, opts ...verifyOptions)
- func VerifyWithExtension(t Failable, reader io.Reader, extWithDot string, opts ...verifyOptions)
- func VerifyXMLBytes(t Failable, bs []byte, opts ...verifyOptions)
- func VerifyXMLStruct(t Failable, obj interface{}, opts ...verifyOptions)
- type ApprovalName
- type Failable
Examples ¶
Constants ¶
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 ¶
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 ¶
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 UseUpdateOption ¶
func UseUpdateOption(option bool)
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 ¶
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 ¶
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 ¶
VerifyWithExtension Example:
VerifyWithExtension(t, strings.NewReader("Hello"), ".json")
Deprecated: Please use Verify with the Options() fluent syntax.
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.