-
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.
refactor: removed testify dependency and refactored existing unit tes…
…ts (#75)
- Loading branch information
1 parent
de5f037
commit 7f888e7
Showing
11 changed files
with
317 additions
and
142 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
exclude_paths: | ||
- "**/mock_*.go" | ||
- "**/*_test.go" | ||
- "**/*_test.go" | ||
- "assert/*" |
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,243 @@ | ||
package assert | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"fmt" | ||
"reflect" | ||
"runtime" | ||
"strings" | ||
"testing" | ||
"unicode" | ||
"unicode/utf8" | ||
) | ||
|
||
func IsType(t *testing.T, expectedType interface{}, object interface{}) bool { | ||
if !objectsAreEqual(reflect.TypeOf(object), reflect.TypeOf(expectedType)) { | ||
return fail(t, fmt.Sprintf("Object expected to be of type %v, but was %v", reflect.TypeOf(expectedType), reflect.TypeOf(object))) | ||
} | ||
|
||
return true | ||
} | ||
|
||
func Equal(t *testing.T, expected, actual interface{}) bool { | ||
if objectsAreEqual(expected, actual) { | ||
return true | ||
} | ||
return fail(t, fmt.Sprintf("Expected:\n%v\nBut Got:\n%v", expected, actual)) | ||
} | ||
|
||
func Nil(t *testing.T, object interface{}) bool { | ||
if isNil(object) { | ||
return true | ||
} | ||
return fail(t, fmt.Sprintf("Expected nil, but got: %#v", object)) | ||
} | ||
|
||
func True(t *testing.T, value bool) bool { | ||
if !value { | ||
return fail(t, "Should be true") | ||
} | ||
return true | ||
} | ||
|
||
func False(t *testing.T, value bool) bool { | ||
if value { | ||
return fail(t, "Should be false") | ||
} | ||
return true | ||
} | ||
|
||
func NoError(t *testing.T, err error) bool { | ||
if err != nil { | ||
return fail(t, fmt.Sprintf("Received unexpected error:\n%+v", err)) | ||
} | ||
return true | ||
} | ||
|
||
func EqualError(t *testing.T, theError error, errString string) bool { | ||
if !isError(t, theError) { | ||
return false | ||
} | ||
expected := errString | ||
actual := theError.Error() | ||
// don't need to use deep equals here, we know they are both strings | ||
if expected != actual { | ||
return fail(t, fmt.Sprintf("Error message not equal:\n"+ | ||
"expected: %q\n"+ | ||
"actual : %q", expected, actual)) | ||
} | ||
return true | ||
} | ||
|
||
func ErrorContains(t *testing.T, theError error, contains string) bool { | ||
if !isError(t, theError) { | ||
return false | ||
} | ||
|
||
actual := theError.Error() | ||
if !strings.Contains(actual, contains) { | ||
return fail(t, fmt.Sprintf("Error %#v does not contain %#v", actual, contains)) | ||
} | ||
|
||
return true | ||
} | ||
|
||
func objectsAreEqual(expected, actual interface{}) bool { | ||
if expected == nil || actual == nil { | ||
return expected == actual | ||
} | ||
|
||
exp, ok := expected.([]byte) | ||
if !ok { | ||
return reflect.DeepEqual(expected, actual) | ||
} | ||
|
||
act, ok := actual.([]byte) | ||
if !ok { | ||
return false | ||
} | ||
if exp == nil || act == nil { | ||
return exp == nil && act == nil | ||
} | ||
return bytes.Equal(exp, act) | ||
} | ||
|
||
func isError(t *testing.T, err error) bool { | ||
if err == nil { | ||
return fail(t, "An error is expected but got nil.") | ||
} | ||
return true | ||
} | ||
|
||
func isNil(object interface{}) bool { | ||
if object == nil { | ||
return true | ||
} | ||
|
||
value := reflect.ValueOf(object) | ||
switch value.Kind() { | ||
case | ||
reflect.Chan, reflect.Func, | ||
reflect.Interface, reflect.Map, | ||
reflect.Ptr, reflect.Slice, reflect.UnsafePointer: | ||
|
||
return value.IsNil() | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
func fail(t *testing.T, failureMessage string) bool { | ||
content := []labeledContent{ | ||
{"Error Trace", strings.Join(callerInfo(), "\n\t\t\t")}, | ||
{"Error", failureMessage}, | ||
} | ||
t.Errorf("\n%s", ""+labeledOutput(content...)) | ||
|
||
return false | ||
} | ||
|
||
func callerInfo() []string { | ||
|
||
var pc uintptr | ||
var ok bool | ||
var file string | ||
var line int | ||
var name string | ||
|
||
var callers []string | ||
for i := 0; ; i++ { | ||
pc, file, line, ok = runtime.Caller(i) | ||
if !ok { | ||
// The breaks below failed to terminate the loop, and we ran off the | ||
// end of the call stack. | ||
break | ||
} | ||
|
||
// This is a huge edge case, but it will panic if this is the case, see #180 | ||
if file == "<autogenerated>" { | ||
break | ||
} | ||
|
||
f := runtime.FuncForPC(pc) | ||
if f == nil { | ||
break | ||
} | ||
name = f.Name() | ||
|
||
// testing.tRunner is the standard library function that calls | ||
// tests. Subtests are called directly by tRunner, without going through | ||
// the Test/Benchmark/Example function that contains the t.Run calls, so | ||
// with subtests we should break when we hit tRunner, without adding it | ||
// to the list of callers. | ||
if name == "testing.tRunner" { | ||
break | ||
} | ||
|
||
parts := strings.Split(file, "/") | ||
if len(parts) > 1 { | ||
filename := parts[len(parts)-1] | ||
dir := parts[len(parts)-2] | ||
if (dir != "github.com/apimatic/go-core-runtime/assert" && dir != "mock" && dir != "require") || filename == "mock_test.go" { | ||
callers = append(callers, fmt.Sprintf("%s:%d", file, line)) | ||
} | ||
} | ||
|
||
// Drop the package | ||
segments := strings.Split(name, ".") | ||
name = segments[len(segments)-1] | ||
if isTest(name, "Test") || | ||
isTest(name, "Benchmark") || | ||
isTest(name, "Example") { | ||
break | ||
} | ||
} | ||
|
||
return callers | ||
} | ||
|
||
func isTest(name, prefix string) bool { | ||
if !strings.HasPrefix(name, prefix) { | ||
return false | ||
} | ||
if len(name) == len(prefix) { // "Test" is ok | ||
return true | ||
} | ||
r, _ := utf8.DecodeRuneInString(name[len(prefix):]) | ||
return !unicode.IsLower(r) | ||
} | ||
|
||
type labeledContent struct { | ||
label string | ||
content string | ||
} | ||
|
||
func labeledOutput(content ...labeledContent) string { | ||
longestLabel := 0 | ||
for _, v := range content { | ||
if len(v.label) > longestLabel { | ||
longestLabel = len(v.label) | ||
} | ||
} | ||
var output string | ||
for _, v := range content { | ||
output += "\t" + v.label + ":" + strings.Repeat(" ", longestLabel-len(v.label)) + "\t" + indentMessageLines(v.content, longestLabel) + "\n" | ||
} | ||
return output | ||
} | ||
|
||
func indentMessageLines(message string, longestLabelLen int) string { | ||
outBuf := new(bytes.Buffer) | ||
|
||
for i, scanner := 0, bufio.NewScanner(strings.NewReader(message)); scanner.Scan(); i++ { | ||
// no need to align first line because it starts at the correct location (after the label) | ||
if i != 0 { | ||
// append alignLen+1 spaces to align with "{{longestLabel}}:" before adding tab | ||
outBuf.WriteString("\n\t" + strings.Repeat(" ", longestLabelLen+1) + "\t") | ||
} | ||
outBuf.WriteString(scanner.Text()) | ||
} | ||
|
||
return outBuf.String() | ||
} |
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,25 +1,19 @@ | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
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/go-openapi/jsonpointer v0.20.2 h1:mQc3nmndL8ZBzStEo3JYF8wzmeWffDH4VbXz58sAx6Q= | ||
github.com/go-openapi/jsonpointer v0.20.2/go.mod h1:bHen+N0u1KEO3YlmqOjTT9Adn1RfD91Ar825/PuiRVs= | ||
github.com/go-openapi/swag v0.22.5 h1:fVS63IE3M0lsuWRzuom3RLwUMVI2peDH01s6M70ugys= | ||
github.com/go-openapi/swag v0.22.5/go.mod h1:Gl91UqO+btAM0plGGxHqJcQZ1ZTy6jbmridBTsDy8A0= | ||
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= | ||
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= | ||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= | ||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= | ||
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= | ||
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= | ||
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/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= | ||
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= | ||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= | ||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= | ||
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= | ||
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= | ||
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/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= | ||
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
Oops, something went wrong.