From 0563e727059f78b6365206869287b0dc4d192973 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Sun, 6 Oct 2024 06:35:01 -0300 Subject: src/gobang.go: Change Some{,Fn}Error() to receive varargs over array Only to improve usability, e.g. no need to declare a [](func() error) variable. --- src/gobang.go | 6 +++--- tests/gobang.go | 35 ++++++----------------------------- 2 files changed, 9 insertions(+), 32 deletions(-) diff --git a/src/gobang.go b/src/gobang.go index 8bff74e..0e5bbd9 100644 --- a/src/gobang.go +++ b/src/gobang.go @@ -67,7 +67,7 @@ func ValidSQLTablePrefix(prefix string) bool { return _SQLTablePrefixRE.MatchString(prefix) } -func SomeError(errs []error) error { +func SomeError(errs ...error) error { for _, err := range errs { if err != nil { return err @@ -76,12 +76,12 @@ func SomeError(errs []error) error { return nil } -func SomeFnError(fns [](func() error)) error { +func SomeFnError(fns ...func() error) error { errs := make([]error, len(fns)) for i, fn := range fns { errs[i] = fn() } - return SomeError(errs) + return SomeError(errs...) } func Random(length int) []byte { diff --git a/tests/gobang.go b/tests/gobang.go index 3033268..9073b66 100644 --- a/tests/gobang.go +++ b/tests/gobang.go @@ -49,28 +49,17 @@ func test_SomeError() { TestStart("SomeError()") Testing("an empty array is nil", func() { - given := []error{} - TAssertEqual(SomeError(given), nil) + TAssertEqual(SomeError(), nil) }) Testing("only nil values is nil", func() { - given := []error{ - nil, - nil, - nil, - } - TAssertEqual(SomeError(given), nil) + TAssertEqual(SomeError(nil, nil, nil), nil) }) Testing("we get the first non-nil value", func() { err1 := errors.New("error 1") err2 := errors.New("error 2") - given := []error{ - nil, - err1, - err2, - } - TAssertEqual(SomeError(given), err1) + TAssertEqual(SomeError(nil, err1, err2), err1) }) } @@ -78,8 +67,7 @@ func test_SomeFnError() { TestStart("SomeFnError()") Testing("an empty arrays is also nil", func() { - given := [](func() error){} - TAssertEqual(SomeFnError(given), nil) + TAssertEqual(SomeFnError(), nil) }) Testing("all functions are called", func() { @@ -88,12 +76,7 @@ func test_SomeFnError() { i++ return nil } - given := [](func() error){ - fn, - fn, - fn, - } - TAssertEqual(SomeFnError(given), nil) + TAssertEqual(SomeFnError(fn, fn, fn), nil) TAssertEqual(i, 3) }) @@ -110,13 +93,7 @@ func test_SomeFnError() { i++ return errs[i] } - given := [](func() error){ - fn, - fn, - fn, - fn, - } - TAssertEqual(SomeFnError(given), errs[2]) + TAssertEqual(SomeFnError(fn, fn, fn, fn), errs[2]) TAssertEqual(i, 4) }) } -- cgit v1.2.3