Skip to content

Commit

Permalink
- r move failable into core, use ApprovalNamerCreator
Browse files Browse the repository at this point in the history
  • Loading branch information
hownowstephen committed Feb 6, 2025
1 parent eea80e5 commit 5459183
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 37 deletions.
10 changes: 9 additions & 1 deletion approval_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"path"
"runtime"
"strings"

"github.com/approvals/go-approval-tests/core"
)

// ApprovalName struct.
Expand All @@ -16,7 +18,13 @@ type ApprovalName struct {
fileName string
}

func getApprovalName(t Failable) *ApprovalName {
func getApprovalNameCreator() core.ApprovalNamerCreator {
return func(t core.Failable) core.ApprovalNamer {
return getApprovalName(t)
}
}

func getApprovalName(t core.Failable) *ApprovalName {
fileName, err := findFileName()
if err != nil {
t.Fatalf("approvals: could not find the test filename or approved files location")
Expand Down
34 changes: 12 additions & 22 deletions approvals.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"reflect"
"strings"

"github.com/approvals/go-approval-tests/core"
"github.com/approvals/go-approval-tests/reporters"
"github.com/approvals/go-approval-tests/utils"
)
Expand All @@ -20,31 +21,20 @@ var (
defaultFolder = ""
)

// Failable is an interface wrapper around testing.T
type Failable interface {
Fail()
Fatal(args ...interface{})
Fatalf(format string, args ...interface{})
Name() string
Log(args ...interface{})
Logf(format string, args ...interface{})
Helper()
}

// VerifyWithExtension Example:
//
// VerifyWithExtension(t, strings.NewReader("Hello"), ".json")
//
// Deprecated: Please use Verify with the Options() fluent syntax.
func VerifyWithExtension(t Failable, reader io.Reader, extWithDot string, opts ...verifyOptions) {
func VerifyWithExtension(t core.Failable, reader io.Reader, extWithDot string, opts ...verifyOptions) {
t.Helper()
Verify(t, reader, alwaysOption(opts).ForFile().WithExtension(extWithDot))
}

// Verify Example:
//
// Verify(t, strings.NewReader("Hello"))
func Verify(t Failable, reader io.Reader, opts ...verifyOptions) {
func Verify(t core.Failable, reader io.Reader, opts ...verifyOptions) {
t.Helper()

if len(opts) > 1 {
Expand All @@ -62,7 +52,7 @@ func Verify(t Failable, reader io.Reader, opts ...verifyOptions) {
}

extWithDot := opt.ForFile().GetExtension()
namer := opt.ForFile().GetNamer(t)
namer := opt.ForFile().GetNamer()(t)

approvalFile := namer.GetApprovalFile(extWithDot)
receivedFile := namer.GetReceivedFile(extWithDot)
Expand All @@ -80,7 +70,7 @@ func Verify(t Failable, reader io.Reader, 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.
func VerifyString(t Failable, s string, opts ...verifyOptions) {
func VerifyString(t core.Failable, s string, opts ...verifyOptions) {
t.Helper()
reader := strings.NewReader(s)
Verify(t, reader, opts...)
Expand All @@ -89,7 +79,7 @@ func VerifyString(t Failable, s string, opts ...verifyOptions) {
// VerifyXMLStruct Example:
//
// VerifyXMLStruct(t, xml)
func VerifyXMLStruct(t Failable, obj interface{}, opts ...verifyOptions) {
func VerifyXMLStruct(t core.Failable, obj interface{}, opts ...verifyOptions) {
t.Helper()
options := alwaysOption(opts).ForFile().WithExtension(".xml")
xmlContent, err := xml.MarshalIndent(obj, "", " ")
Expand All @@ -108,7 +98,7 @@ func VerifyXMLStruct(t Failable, obj interface{}, opts ...verifyOptions) {
// VerifyXMLBytes Example:
//
// VerifyXMLBytes(t, []byte("<Test/>"))
func VerifyXMLBytes(t Failable, bs []byte, opts ...verifyOptions) {
func VerifyXMLBytes(t core.Failable, bs []byte, opts ...verifyOptions) {
t.Helper()
type node struct {
Attr []xml.Attr
Expand All @@ -130,7 +120,7 @@ func VerifyXMLBytes(t Failable, bs []byte, opts ...verifyOptions) {
// VerifyJSONStruct Example:
//
// VerifyJSONStruct(t, json)
func VerifyJSONStruct(t Failable, obj interface{}, opts ...verifyOptions) {
func VerifyJSONStruct(t core.Failable, obj interface{}, opts ...verifyOptions) {
t.Helper()
options := alwaysOption(opts).ForFile().WithExtension(".json")
jsonb, err := json.MarshalIndent(obj, "", " ")
Expand All @@ -145,7 +135,7 @@ func VerifyJSONStruct(t Failable, obj interface{}, opts ...verifyOptions) {
// VerifyJSONBytes Example:
//
// VerifyJSONBytes(t, []byte("{ \"Greeting\": \"Hello\" }"))
func VerifyJSONBytes(t Failable, bs []byte, opts ...verifyOptions) {
func VerifyJSONBytes(t core.Failable, bs []byte, opts ...verifyOptions) {
t.Helper()
var obj map[string]interface{}
err := json.Unmarshal(bs, &obj)
Expand All @@ -160,7 +150,7 @@ func VerifyJSONBytes(t Failable, bs []byte, opts ...verifyOptions) {
// VerifyMap Example:
//
// VerifyMap(t, map[string][string] { "dog": "bark" })
func VerifyMap(t Failable, m interface{}, opts ...verifyOptions) {
func VerifyMap(t core.Failable, m interface{}, opts ...verifyOptions) {
t.Helper()
outputText := utils.PrintMap(m)
VerifyString(t, outputText, opts...)
Expand All @@ -169,7 +159,7 @@ func VerifyMap(t Failable, m interface{}, opts ...verifyOptions) {
// VerifyArray Example:
//
// VerifyArray(t, []string{"dog", "cat"})
func VerifyArray(t Failable, array interface{}, opts ...verifyOptions) {
func VerifyArray(t core.Failable, array interface{}, opts ...verifyOptions) {
t.Helper()
outputText := utils.PrintArray(array)
VerifyString(t, outputText, opts...)
Expand All @@ -178,7 +168,7 @@ func VerifyArray(t Failable, array interface{}, opts ...verifyOptions) {
// VerifyAll Example:
//
// VerifyAll(t, "uppercase", []string("dog", "cat"}, func(x interface{}) string { return strings.ToUpper(x.(string)) })
func VerifyAll(t Failable, header string, collection interface{}, transform func(interface{}) string, opts ...verifyOptions) {
func VerifyAll(t core.Failable, header string, collection interface{}, transform func(interface{}) string, opts ...verifyOptions) {
t.Helper()
if len(header) != 0 {
header = fmt.Sprintf("%s\n\n\n", header)
Expand Down
23 changes: 13 additions & 10 deletions combination_approvals.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"reflect"
"strings"

"github.com/approvals/go-approval-tests/core"
)

type emptyType struct{}
Expand All @@ -17,8 +19,9 @@ var (
)

// VerifyAllCombinationsFor1 Example:
// VerifyAllCombinationsFor1(t, "uppercase", func(x interface{}) string { return strings.ToUpper(x.(string)) }, []string("dog", "cat"})
func VerifyAllCombinationsFor1(t Failable, header string, transform func(interface{}) string, collection1 interface{}) {
//
// VerifyAllCombinationsFor1(t, "uppercase", func(x interface{}) string { return strings.ToUpper(x.(string)) }, []string("dog", "cat"})
func VerifyAllCombinationsFor1(t core.Failable, header string, transform func(interface{}) string, collection1 interface{}) {
transform2 := func(p1, p2, p3, p4, p5, p6, p7, p8, p9 interface{}) string {
return transform(p1)
}
Expand All @@ -40,7 +43,7 @@ func VerifyAllCombinationsFor1(t Failable, header string, transform func(interfa
// approved version. If the transform function returns SkipThisCombination the
// output of this combination won't be displayed inside the received file.
func VerifyAllCombinationsFor2(
t Failable,
t core.Failable,
header string,
transform func(p1, p2 interface{}) string,
collection1, collection2 interface{}) {
Expand All @@ -61,7 +64,7 @@ func VerifyAllCombinationsFor2(

// VerifyAllCombinationsFor3 is for combinations of 3.
func VerifyAllCombinationsFor3(
t Failable,
t core.Failable,
header string,
transform func(p1, p2, p3 interface{}) string,
collection1, collection2, collection3 interface{}) {
Expand All @@ -83,7 +86,7 @@ func VerifyAllCombinationsFor3(

// VerifyAllCombinationsFor4 is for combinations of 4.
func VerifyAllCombinationsFor4(
t Failable,
t core.Failable,
header string,
transform func(p1, p2, p3, p4 interface{}) string,
collection1, collection2, collection3, collection4 interface{}) {
Expand All @@ -105,7 +108,7 @@ func VerifyAllCombinationsFor4(

// VerifyAllCombinationsFor5 is for combinations of 5.
func VerifyAllCombinationsFor5(
t Failable,
t core.Failable,
header string,
transform func(p1, p2, p3, p4, p5 interface{}) string,
collection1, collection2, collection3, collection4, collection5 interface{}) {
Expand All @@ -127,7 +130,7 @@ func VerifyAllCombinationsFor5(

// VerifyAllCombinationsFor6 is for combinations of 6.
func VerifyAllCombinationsFor6(
t Failable,
t core.Failable,
header string,
transform func(p1, p2, p3, p4, p5, p6 interface{}) string,
collection1, collection2, collection3, collection4, collection5, collection6 interface{}) {
Expand All @@ -149,7 +152,7 @@ func VerifyAllCombinationsFor6(

// VerifyAllCombinationsFor7 is for combinations of 7.
func VerifyAllCombinationsFor7(
t Failable,
t core.Failable,
header string,
transform func(p1, p2, p3, p4, p5, p6, p7 interface{}) string,
collection1, collection2, collection3, collection4, collection5, collection6, collection7 interface{}) {
Expand All @@ -171,7 +174,7 @@ func VerifyAllCombinationsFor7(

// VerifyAllCombinationsFor8 is for combinations of 8.
func VerifyAllCombinationsFor8(
t Failable,
t core.Failable,
header string,
transform func(p1, p2, p3, p4, p5, p6, p7, p8 interface{}) string,
collection1, collection2, collection3, collection4, collection5, collection6, collection7, collection8 interface{}) {
Expand All @@ -193,7 +196,7 @@ func VerifyAllCombinationsFor8(

// VerifyAllCombinationsFor9 is for combinations of 9.
func VerifyAllCombinationsFor9( // nolint: funlen, gocognit
t Failable,
t core.Failable,
header string,
transform func(a, b, c, d, e, f, g, h, i interface{}) string,
collection1,
Expand Down
12 changes: 12 additions & 0 deletions core/failable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package core

// Failable is an interface wrapper around testing.T
type Failable interface {
Fail()
Fatal(args ...interface{})
Fatalf(format string, args ...interface{})
Name() string
Log(args ...interface{})
Logf(format string, args ...interface{})
Helper()
}
2 changes: 2 additions & 0 deletions core/namer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ type ApprovalNamer interface {
GetReceivedFile(extWithDot string) string
GetApprovalFile(extWithDot string) string
}

type ApprovalNamerCreator func(t Failable) ApprovalNamer
5 changes: 5 additions & 0 deletions namer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package approvals

// func NewTemplatedCustomNamer(template string) *Tem {
// return nil
// }
8 changes: 4 additions & 4 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ func (f fileOptions) GetExtension() string {
return ext.(string)
}

func (f fileOptions) WithNamer(namer core.ApprovalNamer) verifyOptions {
func (f fileOptions) WithNamer(namer core.ApprovalNamerCreator) verifyOptions {
return NewVerifyOptions(f.fields, "namer", namer)
}

func (f fileOptions) GetNamer(t Failable) core.ApprovalNamer {
ext := getField(f.fields, "namer", getApprovalName(t))
return ext.(core.ApprovalNamer)
func (f fileOptions) GetNamer() core.ApprovalNamerCreator {
ext := getField(f.fields, "namer", getApprovalNameCreator())
return ext.(core.ApprovalNamerCreator)
}

func (v verifyOptions) getField(key string, defaultValue interface{}) interface{} {
Expand Down

0 comments on commit 5459183

Please sign in to comment.