Skip to content

Commit

Permalink
refactor: Using map for describing tests
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <bob@vibioh.fr>
  • Loading branch information
ViBiOh committed Mar 25, 2022
1 parent 6c4b83e commit eb345ae
Show file tree
Hide file tree
Showing 20 changed files with 573 additions and 852 deletions.
17 changes: 7 additions & 10 deletions pkg/ketchup/renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,18 @@ func TestMin(t *testing.T) {
b uint64
}

cases := []struct {
intention string
args args
want uint64
cases := map[string]struct {
args args
want uint64
}{
{
"a",
"a": {
args{
a: 1,
b: 2,
},
1,
},
{
"b",
"b": {
args{
a: 3,
b: 2,
Expand All @@ -31,8 +28,8 @@ func TestMin(t *testing.T) {
},
}

for _, tc := range cases {
t.Run(tc.intention, func(t *testing.T) {
for intention, tc := range cases {
t.Run(intention, func(t *testing.T) {
if got := min(tc.args.a, tc.args.b); got != tc.want {
t.Errorf("min() = %d, want %d", got, tc.want)
}
Expand Down
15 changes: 6 additions & 9 deletions pkg/middleware/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@ import (
)

func TestMiddleware(t *testing.T) {
cases := []struct {
intention string
cases := map[string]struct {
next http.Handler
request *http.Request
want string
wantStatus int
wantHeader http.Header
}{
{
"simple",
"simple": {
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if _, err := w.Write([]byte(model.ReadUser(r.Context()).Email)); err != nil {
t.Errorf("unable to write: %s", err)
Expand All @@ -34,8 +32,7 @@ func TestMiddleware(t *testing.T) {
http.StatusOK,
http.Header{},
},
{
"nil",
"nil": {
nil,
httptest.NewRequest(http.MethodGet, "/", nil),
"",
Expand All @@ -44,13 +41,13 @@ func TestMiddleware(t *testing.T) {
},
}

for _, tc := range cases {
t.Run(tc.intention, func(t *testing.T) {
for intention, tc := range cases {
t.Run(intention, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

userService := mocks.NewUserService(ctrl)
if tc.intention == "simple" {
if intention == "simple" {
userService.EXPECT().StoreInContext(gomock.Any()).Return(model.StoreUser(context.Background(), model.NewUser(1, "nobody@localhost", authModel.NewUser(1, "test"))))
}

Expand Down
84 changes: 35 additions & 49 deletions pkg/model/ketchup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,26 @@ func TestParseKetchupFrequency(t *testing.T) {
value string
}

cases := []struct {
intention string
args args
want KetchupFrequency
wantErr error
cases := map[string]struct {
args args
want KetchupFrequency
wantErr error
}{
{
"UpperCase",
"UpperCase": {
args{
value: "NONE",
},
None,
nil,
},
{
"equal",
"equal": {
args{
value: "Weekly",
},
Weekly,
nil,
},
{
"not found",
"not found": {
args{
value: "wrong",
},
Expand All @@ -47,8 +43,8 @@ func TestParseKetchupFrequency(t *testing.T) {
},
}

for _, tc := range cases {
t.Run(tc.intention, func(t *testing.T) {
for intention, tc := range cases {
t.Run(intention, func(t *testing.T) {
got, gotErr := ParseKetchupFrequency(tc.args.value)

failed := false
Expand All @@ -75,13 +71,11 @@ func TestKetchupByRepositoryID(t *testing.T) {
array []Ketchup
}

cases := []struct {
intention string
args args
want []Ketchup
cases := map[string]struct {
args args
want []Ketchup
}{
{
"simple",
"simple": {
args{
array: []Ketchup{
NewKetchup(DefaultPattern, "", Daily, false, NewGithubRepository(10, "")),
Expand All @@ -97,8 +91,8 @@ func TestKetchupByRepositoryID(t *testing.T) {
},
}

for _, tc := range cases {
t.Run(tc.intention, func(t *testing.T) {
for intention, tc := range cases {
t.Run(intention, func(t *testing.T) {
sort.Sort(KetchupByRepositoryIDAndPattern(tc.args.array))
if got := tc.args.array; !reflect.DeepEqual(got, tc.want) {
t.Errorf("KetchupByRepositoryID() = %+v, want %+v", got, tc.want)
Expand All @@ -112,13 +106,11 @@ func TestKetchupByPriority(t *testing.T) {
array []Ketchup
}

cases := []struct {
intention string
args args
want []Ketchup
cases := map[string]struct {
args args
want []Ketchup
}{
{
"alphabetic",
"alphabetic": {
args{
array: []Ketchup{
NewKetchup("", "", Daily, false, NewGithubRepository(0, "abc")),
Expand All @@ -134,8 +126,7 @@ func TestKetchupByPriority(t *testing.T) {
NewKetchup("", "", Daily, false, NewGithubRepository(0, "jkl")),
},
},
{
"semver",
"semver": {
args{
array: []Ketchup{
{Semver: "Minor", Repository: NewGithubRepository(0, "abc")},
Expand All @@ -151,8 +142,7 @@ func TestKetchupByPriority(t *testing.T) {
{Semver: "", Repository: NewGithubRepository(0, "def")},
},
},
{
"full",
"full": {
args{
array: []Ketchup{
{Semver: "Major", Repository: NewGithubRepository(0, "abc")},
Expand All @@ -178,8 +168,8 @@ func TestKetchupByPriority(t *testing.T) {
},
}

for _, tc := range cases {
t.Run(tc.intention, func(t *testing.T) {
for intention, tc := range cases {
t.Run(intention, func(t *testing.T) {
sort.Sort(KetchupByPriority(tc.args.array))
if got := tc.args.array; !reflect.DeepEqual(got, tc.want) {
t.Errorf("KetchupByPriority() = %+v, want %+v", got, tc.want)
Expand All @@ -193,13 +183,11 @@ func TestReleaseByRepositoryID(t *testing.T) {
array []Release
}

cases := []struct {
intention string
args args
want []Release
cases := map[string]struct {
args args
want []Release
}{
{
"simple",
"simple": {
args{
array: []Release{
NewRelease(NewGithubRepository(10, ""), DefaultPattern, semver.Version{}),
Expand All @@ -215,8 +203,8 @@ func TestReleaseByRepositoryID(t *testing.T) {
},
}

for _, tc := range cases {
t.Run(tc.intention, func(t *testing.T) {
for intention, tc := range cases {
t.Run(intention, func(t *testing.T) {
sort.Sort(ReleaseByRepositoryIDAndPattern(tc.args.array))
if got := tc.args.array; !reflect.DeepEqual(got, tc.want) {
t.Errorf("ReleaseByRepositoryID() = %+v, want %+v", got, tc.want)
Expand All @@ -230,13 +218,11 @@ func TestReleaseByKindAndName(t *testing.T) {
array []Release
}

cases := []struct {
intention string
args args
want []Release
cases := map[string]struct {
args args
want []Release
}{
{
"simple",
"simple": {
args{
array: []Release{
NewRelease(NewHelmRepository(3, "http://chart", "app"), DefaultPattern, semver.Version{}),
Expand All @@ -252,8 +238,8 @@ func TestReleaseByKindAndName(t *testing.T) {
},
}

for _, tc := range cases {
t.Run(tc.intention, func(t *testing.T) {
for intention, tc := range cases {
t.Run(intention, func(t *testing.T) {
sort.Sort(ReleaseByKindAndName(tc.args.array))
if got := tc.args.array; !reflect.DeepEqual(got, tc.want) {
t.Errorf("ReleaseByRepositoryID() = %+v, want %+v", got, tc.want)
Expand Down
Loading

0 comments on commit eb345ae

Please sign in to comment.