-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Cleverse/feature/pure-package
Purify Golang Implementation
- Loading branch information
Showing
11 changed files
with
147 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,3 @@ | ||
module github.com/Cleverse/go-utilities/utils | ||
|
||
go 1.19 | ||
|
||
require ( | ||
github.com/Cleverse/go-utilities/errors v0.0.0-20231019072721-442842e3dc09 | ||
github.com/stretchr/testify v1.8.4 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +0,0 @@ | ||
github.com/Cleverse/go-utilities/errors v0.0.0-20231019072721-442842e3dc09 h1:kNAgub14cUBr7uoTxQSrf0qiMNJSzMhIDa8RFdv6hkk= | ||
github.com/Cleverse/go-utilities/errors v0.0.0-20231019072721-442842e3dc09/go.mod h1:1QK+h746G1DwellQ6KK2rBCJusZqIDTZ9QFVGnUX9+Q= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= | ||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package utils | ||
|
||
import ( | ||
"bytes" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
// assertEqual asserts that expected and actual are equal. | ||
func assertEqual[T comparable](t *testing.T, expected, actual T, msgAndArgs ...interface{}) bool { | ||
if expected != actual { | ||
if msg := msgFormatter(msgAndArgs...); msg != "" { | ||
t.Error(msg) | ||
} else { | ||
t.Errorf("expected: %v, got: %v", expected, actual) | ||
} | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func assertNotEqual[T comparable](t *testing.T, expected, actual T, msgAndArgs ...interface{}) bool { | ||
if expected == actual { | ||
if msg := msgFormatter(msgAndArgs...); msg != "" { | ||
t.Error(msg) | ||
} else { | ||
t.Errorf("expected: %v not equal to: %v", expected, actual) | ||
} | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
// assertNotEqual asserts that expected and actual are not equal. | ||
func assertTrue(t *testing.T, actual bool, msgAndArgs ...interface{}) bool { | ||
return assertEqual(t, true, actual, msgAndArgs...) | ||
} | ||
|
||
// assertNotEqual asserts that expected and actual are not equal. | ||
func assertFalse(t *testing.T, actual bool, msgAndArgs ...interface{}) bool { | ||
return assertEqual(t, false, actual, msgAndArgs...) | ||
} | ||
|
||
func assertEqualAny(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) (equal bool) { | ||
// check nil | ||
switch { | ||
case expected == nil && actual == nil: | ||
return true | ||
case expected == nil && actual != nil: | ||
fallthrough | ||
case expected != nil && actual == nil: | ||
t.Errorf("expected: %v, got: %v", expected, actual) | ||
return false | ||
} | ||
|
||
// if types are different, they are not equal. | ||
t1 := reflect.TypeOf(expected) | ||
t2 := reflect.TypeOf(actual) | ||
if t1 != t2 { | ||
t.Errorf("type mismatch: expected: %v, got: %v", t1, t2) | ||
return false | ||
} | ||
|
||
defer func() { | ||
if !equal { | ||
if msg := msgFormatter(msgAndArgs...); msg != "" { | ||
t.Error(msg) | ||
} else { | ||
t.Errorf("expected: %v, got: %v", expected, actual) | ||
} | ||
} | ||
}() | ||
|
||
// default compare | ||
switch expected := expected.(type) { | ||
case []byte: | ||
actual := actual.([]byte) | ||
return bytes.Equal(expected, actual) | ||
case []string: | ||
actual := actual.([]string) | ||
if len(expected) != len(actual) { | ||
return false | ||
} | ||
for i := range expected { | ||
if expected[i] != actual[i] { | ||
return false | ||
} | ||
} | ||
return true | ||
default: | ||
return expected == actual | ||
} | ||
} |