From 681170a9e1cc83c6c084cc5104f952406b743c04 Mon Sep 17 00:00:00 2001 From: Akshay Shah Date: Wed, 24 May 2017 22:14:50 -0700 Subject: [PATCH] Move validation tests to vallie package --- container_test.go | 28 ---------------------------- vallie/vallie_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 28 deletions(-) create mode 100644 vallie/vallie_test.go diff --git a/container_test.go b/container_test.go index 86ca8b64..2dc091ea 100644 --- a/container_test.go +++ b/container_test.go @@ -25,8 +25,6 @@ import ( "reflect" "testing" - "go.uber.org/dig/vallie" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -546,32 +544,6 @@ func TestMultiObjectRegisterResolve(t *testing.T) { require.NotNil(t, third, "Child3 must have been registered") } -func Test_ZeroValueObjectValidationSuccess(t *testing.T) { - t.Parallel() - c := New() - - c.Provide(&Parent1{}) - err := c.Invoke(func(p1 *Parent1) { - er := vallie.Validate(p1) - require.NoError(t, er) - }) - require.Nil(t, err) -} - -func Test_ZeroValueObjectValidationError(t *testing.T) { - t.Parallel() - c := New() - - var zeroP12 *Parent12 - err := c.Invoke(func(p12 *Parent12) { - er := vallie.Validate(p12) - require.Error(t, er) - require.Contains(t, er.Error(), "zero value") - zeroP12 = p12 - }) - require.Nil(t, err) -} - func TestZeroValueOnInvoke(t *testing.T) { type missing struct{} type present struct{} diff --git a/vallie/vallie_test.go b/vallie/vallie_test.go new file mode 100644 index 00000000..df3586da --- /dev/null +++ b/vallie/vallie_test.go @@ -0,0 +1,35 @@ +package vallie + +import ( + "net/http" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestValidation(t *testing.T) { + type nothing struct{} + + tests := []struct { + desc string + input []interface{} + valid bool + }{ + {"empty input", []interface{}{}, true}, + {"nil pointer", []interface{}{(*nothing)(nil)}, false}, + {"zero value", []interface{}{nothing{}}, false}, + {"non-zero value", []interface{}{5}, true}, + {"mixed input", []interface{}{5, http.DefaultServeMux}, true}, + } + + for _, tt := range tests { + t.Run(tt.desc, func(t *testing.T) { + assert.Equal( + t, + tt.valid, + Validate(tt.input...) == nil, + "unexpected validation result for %v", tt.input, + ) + }) + } +}