Skip to content

Commit

Permalink
Move error_test.go to a separate test package (#5796)
Browse files Browse the repository at this point in the history
What changed?
Moved error_test.go to a separate test package

Why?
The folder testdata is ignored when running tests, so moving this test to a test package that isn't ignored

How did you test it?
Made sure the test runs when doing go test and make test

Potential risks

Release notes

Documentation Changes
  • Loading branch information
jakobht authored Apr 8, 2024
1 parent 9024217 commit 88a4dea
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
20 changes: 3 additions & 17 deletions common/types/testdata/error_test.go → common/testing/allisset.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package testdata
package testing

import (
"reflect"
Expand All @@ -29,28 +29,14 @@ import (
"github.com/stretchr/testify/assert"
)

func TestAllFieldsSetInTestErrors(t *testing.T) {
for _, err := range Errors {
name := reflect.TypeOf(err).Elem().Name()
t.Run(name, func(t *testing.T) {
// Test all fields are set in the error
assert.True(t, checkAllIsSet(err))
})
}
}

func checkAllIsSet(err error) bool {
func allIsSet(t *testing.T, err error) {
// All the errors are pointers, so we get the value with .Elem
errValue := reflect.ValueOf(err).Elem()

for i := 0; i < errValue.NumField(); i++ {
field := errValue.Field(i)

// IsZero checks if the value is the default value (e.g. nil, "", 0 etc)
if field.IsZero() {
return false
}
assert.True(t, !field.IsZero(), "Field %s is not set", errValue.Type().Field(i).Name)
}

return true
}
40 changes: 40 additions & 0 deletions common/testing/allisset_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// The MIT License (MIT)

// Copyright (c) 2017-2020 Uber Technologies Inc.

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package testing

import (
"reflect"
"testing"

"github.com/uber/cadence/common/types/testdata"
)

func TestAllFieldsSetInTestErrors(t *testing.T) {
for _, err := range testdata.Errors {
name := reflect.TypeOf(err).Elem().Name()
t.Run(name, func(t *testing.T) {
// Test all fields are set in the error
allIsSet(t, err)
})
}
}

0 comments on commit 88a4dea

Please sign in to comment.