Skip to content

Commit

Permalink
remove all imported packages
Browse files Browse the repository at this point in the history
* replace testify with custom functions
* no imports, more pure
  • Loading branch information
shockerli committed Oct 19, 2021
1 parent fac4223 commit a185f5e
Show file tree
Hide file tree
Showing 9 changed files with 244 additions and 154 deletions.
15 changes: 7 additions & 8 deletions bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"

"github.com/shockerli/cvt"
"github.com/stretchr/testify/assert"
)

func TestBool_HasDefault(t *testing.T) {
Expand Down Expand Up @@ -82,7 +81,7 @@ func TestBool_HasDefault(t *testing.T) {
msg := fmt.Sprintf("i = %d, input[%+v], def[%+v], expect[%+v]", i, tt.input, tt.def, tt.expect)

v := cvt.Bool(tt.input, tt.def)
assert.Equal(t, tt.expect, v, msg)
assertEqual(t, tt.expect, v, "[NonE] "+msg)
}
}

Expand All @@ -107,7 +106,7 @@ func TestBool_BaseLine(t *testing.T) {
msg := fmt.Sprintf("i = %d, input[%+v], expect[%+v]", i, tt.input, tt.expect)

v := cvt.Bool(tt.input)
assert.Equal(t, tt.expect, v, msg)
assertEqual(t, tt.expect, v, "[NonE] "+msg)
}
}

Expand Down Expand Up @@ -195,15 +194,15 @@ func TestBoolE(t *testing.T) {

v, err := cvt.BoolE(tt.input)
if tt.isErr {
assert.Error(t, err, msg)
assertError(t, err, "[HasErr] "+msg)
continue
}

assert.NoError(t, err, msg)
assert.Equal(t, tt.expect, v, msg)
assertNoError(t, err, "[NoErr] "+msg)
assertEqual(t, tt.expect, v, "[WithE] "+msg)

// Non-E test with no default value:
// Non-E test
v = cvt.Bool(tt.input)
assert.Equal(t, tt.expect, v, msg)
assertEqual(t, tt.expect, v, "[NonE] "+msg)
}
}
120 changes: 115 additions & 5 deletions cvte_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package cvt_test

import (
"bytes"
"errors"
"fmt"
"reflect"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/shockerli/cvt"
)

Expand Down Expand Up @@ -139,11 +141,119 @@ func TestFieldE(t *testing.T) {

v, err := cvt.FieldE(tt.input, tt.field)
if tt.isErr {
assert.Error(t, err, msg)
assertError(t, err, "[HasErr] "+msg)
continue
}

assert.NoError(t, err, msg)
assert.Equal(t, tt.expect, v, msg)
assertNoError(t, err, "[NoErr] "+msg)
assertEqual(t, tt.expect, v, "[WithE] "+msg)
}
}

////////////////////////////////////////////////////////////////////////////////

// [testing assert functions]

// assert error
func assertError(t *testing.T, err error, msgAndArgs ...interface{}) {
if err == nil {
fail(t, "An error is expected but got nil", msgAndArgs...)
return
}
return
}

// assert no error
func assertNoError(t *testing.T, err error, msgAndArgs ...interface{}) {
if err != nil {
fail(t, fmt.Sprintf("Received unexpected error:\n\t\t\t\t%+v", err), msgAndArgs...)
return
}
return
}

// assert equal
func assertEqual(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) {
if err := validateEqualArgs(expected, actual); err != nil {
fail(t, fmt.Sprintf("Invalid operation: %#v == %#v (%s)",
expected, actual, err), msgAndArgs...)
return
}

if !objectsAreEqual(expected, actual) {
fail(t, fmt.Sprintf("Not equal: \n"+
"expected: %s\n"+
"actual : %s", expected, actual), msgAndArgs...)
return
}
return
}

func fail(t *testing.T, failureMessage string, msgAndArgs ...interface{}) {
t.Errorf(`
Error: %s
Test: %s
Messages: %s`, failureMessage, t.Name(), messageFromMsgAndArgs(msgAndArgs...))
}

func validateEqualArgs(expected, actual interface{}) error {
if expected == nil && actual == nil {
return nil
}

if isFunction(expected) || isFunction(actual) {
return errors.New("cannot take func type as argument")
}
return nil
}

func isFunction(arg interface{}) bool {
if arg == nil {
return false
}
return reflect.TypeOf(arg).Kind() == reflect.Func
}

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 messageFromMsgAndArgs(msgAndArgs ...interface{}) string {
if len(msgAndArgs) == 0 || msgAndArgs == nil {
return ""
}
if len(msgAndArgs) == 1 {
msg := msgAndArgs[0]
if msgAsStr, ok := msg.(string); ok {
return msgAsStr
}
return fmt.Sprintf("%+v", msg)
}
if len(msgAndArgs) > 1 {
tpl, ok := msgAndArgs[0].(string)
if ok {
return fmt.Sprintf(tpl, msgAndArgs[1:]...)
}
for v := range msgAndArgs {
tpl += fmt.Sprintf("%+v, ", v)
}
return strings.TrimRight(tpl, ", ")
}
return ""
}
29 changes: 14 additions & 15 deletions float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"testing"

"github.com/shockerli/cvt"
"github.com/stretchr/testify/assert"
)

func TestFloat64_HasDefault(t *testing.T) {
Expand Down Expand Up @@ -76,7 +75,7 @@ func TestFloat64_HasDefault(t *testing.T) {
msg := fmt.Sprintf("i = %d, input[%+v], def[%+v], expect[%+v]", i, tt.input, tt.def, tt.expect)

v := cvt.Float64(tt.input, tt.def)
assert.Equal(t, tt.expect, v, msg)
assertEqual(t, tt.expect, v, "[NonE] "+msg)
}
}

Expand All @@ -100,7 +99,7 @@ func TestFloat64_BaseLine(t *testing.T) {
msg := fmt.Sprintf("i = %d, input[%+v], expect[%+v]", i, tt.input, tt.expect)

v := cvt.Float64(tt.input)
assert.Equal(t, tt.expect, v, msg)
assertEqual(t, tt.expect, v, "[NonE] "+msg)
}
}

Expand Down Expand Up @@ -170,7 +169,7 @@ func TestFloat32_HasDefault(t *testing.T) {
msg := fmt.Sprintf("i = %d, input[%+v], def[%+v], expect[%+v]", i, tt.input, tt.def, tt.expect)

v := cvt.Float32(tt.input, tt.def)
assert.Equal(t, tt.expect, v, msg)
assertEqual(t, tt.expect, v, "[NonE] "+msg)
}
}

Expand All @@ -194,7 +193,7 @@ func TestFloat32_BaseLine(t *testing.T) {
msg := fmt.Sprintf("i = %d, input[%+v], expect[%+v]", i, tt.input, tt.expect)

v := cvt.Float32(tt.input)
assert.Equal(t, tt.expect, v, msg)
assertEqual(t, tt.expect, v, "[NonE] "+msg)
}
}

Expand Down Expand Up @@ -275,16 +274,16 @@ func TestFloat64E(t *testing.T) {

v, err := cvt.Float64E(tt.input)
if tt.isErr {
assert.Error(t, err, msg)
assertError(t, err, "[HasErr] "+msg)
continue
}

assert.NoError(t, err, msg)
assert.Equal(t, tt.expect, v, msg)
assertNoError(t, err, "[NoErr] "+msg)
assertEqual(t, tt.expect, v, "[WithE] "+msg)

// Non-E test with no default value:
// Non-E test
v = cvt.Float64(tt.input)
assert.Equal(t, tt.expect, v, msg)
assertEqual(t, tt.expect, v, "[NonE] "+msg)
}
}

Expand Down Expand Up @@ -364,15 +363,15 @@ func TestFloat32E(t *testing.T) {

v, err := cvt.Float32E(tt.input)
if tt.isErr {
assert.Error(t, err, msg)
assertError(t, err, "[HasErr] "+msg)
continue
}

assert.NoError(t, err, msg)
assert.Equal(t, tt.expect, v, msg)
assertNoError(t, err, "[NoErr] "+msg)
assertEqual(t, tt.expect, v, "[WithE] "+msg)

// Non-E test with no default value:
// Non-E test
v = cvt.Float32(tt.input)
assert.Equal(t, tt.expect, v, msg)
assertEqual(t, tt.expect, v, "[NonE] "+msg)
}
}
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module github.com/shockerli/cvt

go 1.16

require github.com/stretchr/testify v1.7.0
12 changes: 0 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +0,0 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/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/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
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.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading

0 comments on commit a185f5e

Please sign in to comment.