8000 Add error_kind test by thom-at-redhat · Pull Request #1086 · ansible/receptor · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add error_kind test #1086

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 16, 2024< 8000 /relative-time>
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions pkg/utils/error_kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import "fmt"

// ErrorWithKind represents an error wrapped with a designation of what kind of error it is.
type ErrorWithKind struct {
err error
kind string
Err error
Kind string
}

// Error returns the error text as a string.
func (ek ErrorWithKind) Error() string {
return fmt.Sprintf("%s error: %s", ek.kind, ek.err)
return fmt.Sprintf("%s error: %v", ek.Kind, ek.Err)
}

// WrapErrorWithKind creates an ErrorWithKind that wraps an underlying error.
func WrapErrorWithKind(err error, kind string) ErrorWithKind {
return ErrorWithKind{
err: err,
kind: kind,
Err: err,
Kind: kind,
}
}

Expand All @@ -28,5 +28,5 @@ func ErrorIsKind(err error, kind string) bool {
return false
}

return ek.kind == kind
return ek.Kind == kind
}
137 changes: 137 additions & 0 deletions pkg/utils/error_kind_test.go
10000
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package utils_test

import (
"fmt"
"reflect"
"testing"

"github.com/ansible/receptor/pkg/utils"
)

const (
goodKind string = "connection"
goodErrorString string = "unit was already started"
)

var errUnitWasAlreadyStarted error = fmt.Errorf(goodErrorString)

func TestErrorWithKind_Error(t *testing.T) {
type fields struct {
err error
kind string
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Positive",
fields: fields{
err: errUnitWasAlreadyStarted,
kind: goodKind,
},
want: fmt.Sprintf("%s error: %s", goodKind, goodErrorString),
},
{
name: "Negative",
fields: fields{
err: nil,
kind: goodKind,
},
want: fmt.Sprintf("%s error: <nil>", goodKind),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ek := utils.ErrorWithKind{
Err: tt.fields.err,
Kind: tt.fields.kind,
}
if got := ek.Error(); got != tt.want {
t.Errorf("ErrorWithKind.Error() = %v, want %v", got, tt.want)
}
})
}
}

func TestWrapErrorWithKind(t *testing.T) {
type args struct {
err error
kind string
}
tests := []struct {
name string
args args
want utils.ErrorWithKind
}{
{
name: "Positive",
args: args{
err: errUnitWasAlreadyStarted,
kind: goodKind,
},
want: utils.ErrorWithKind{
Err: errUnitWasAlreadyStarted,
Kind: goodKind,
},
},
{
name: "Negative",
args: args{
err: nil,
kind: goodKind,
},
want: utils.ErrorWithKind{
Err: nil,
Kind: goodKind,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := utils.WrapErrorWithKind(tt.args.err, tt.args.kind); !reflect.DeepEqual(got, tt.want) {
t.Errorf("WrapErrorWithKind() = %v, want %v", got, tt.want)
}
})
}
}

func TestErrorIsKind(t *testing.T) {
type args struct {
err error
kind string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "Positive",
args: args{
err: utils.WrapErrorWithKind(
errUnitWasAlreadyStarted,
goodKind,
),
kind: goodKind,
},
want: true,
},
{
name: "Negative",
args: args{
err: nil,
kind: goodKind,
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := utils.ErrorIsKind(tt.args.err, tt.args.kind); got != tt.want {
t.Errorf("ErrorIsKind() = %v, want %v", got, tt.want)
}
})
}
}
Loading
0