File: error_kind_test.go

package info (click to toggle)
receptor 1.5.5-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,772 kB
  • sloc: python: 1,643; makefile: 305; sh: 174
file content (137 lines) | stat: -rw-r--r-- 2,478 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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) //nolint:staticcheck

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)
			}
		})
	}
}