0
0
mirror of https://github.com/thegeeklab/wp-plugin-go.git synced 2024-11-23 16:50:39 +00:00

refactor: use test runs and add test case names (#61)

This commit is contained in:
Robert Kaussow 2024-03-11 11:52:52 +01:00 committed by GitHub
parent e2c4966872
commit 937c247354
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 190 additions and 95 deletions

View File

@ -80,9 +80,8 @@ func TestToMarkdownFull(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
want := testFileContent(t, tt.want)
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
want := testFileContent(t, tt.want)
got, _ := ToMarkdown(tt.app) got, _ := ToMarkdown(tt.app)
assert.Equal(t, want, got) assert.Equal(t, want, got)
}) })
@ -140,9 +139,8 @@ func TestToData(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
got := GetTemplateData(tt.app)
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
got := GetTemplateData(tt.app)
assert.Equal(t, tt.want, got) assert.Equal(t, tt.want, got)
}) })
} }

View File

@ -59,6 +59,7 @@ func Test_currFromContext(t *testing.T) {
Execute: func(_ context.Context) error { return nil }, Execute: func(_ context.Context) error { return nil },
} }
t.Run(tt.name, func(t *testing.T) {
got := New(options) got := New(options)
got.App.Action = func(ctx *cli.Context) error { got.App.Action = func(ctx *cli.Context) error {
got.Metadata = MetadataFromContext(ctx) got.Metadata = MetadataFromContext(ctx)
@ -71,11 +72,12 @@ func Test_currFromContext(t *testing.T) {
assert.Equal(t, got.Metadata.Curr.Message, tt.want["message"]) assert.Equal(t, got.Metadata.Curr.Message, tt.want["message"])
assert.Equal(t, got.Metadata.Curr.Title, tt.want["title"]) assert.Equal(t, got.Metadata.Curr.Title, tt.want["title"])
assert.Equal(t, got.Metadata.Curr.Description, tt.want["desc"]) assert.Equal(t, got.Metadata.Curr.Description, tt.want["desc"])
})
} }
} }
func TestSplitMessage(t *testing.T) { func TestSplitMessage(t *testing.T) {
testCases := []struct { tests := []struct {
name string name string
message string message string
wantTitle string wantTitle string
@ -113,10 +115,12 @@ func TestSplitMessage(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tt := range tests {
gotTitle, gotDescription := splitMessage(tc.message) t.Run(tt.name, func(t *testing.T) {
gotTitle, gotDescription := splitMessage(tt.message)
assert.Equal(t, tc.wantTitle, gotTitle) assert.Equal(t, tt.wantTitle, gotTitle)
assert.Equal(t, tc.wantDescription, gotDescription) assert.Equal(t, tt.wantDescription, gotDescription)
})
} }
} }

View File

@ -8,95 +8,188 @@ import (
func Test_stripTagPrefix(t *testing.T) { func Test_stripTagPrefix(t *testing.T) {
tests := []struct { tests := []struct {
name string
before string before string
after string after string
}{ }{
{before: "refs/tags/1.0.0", after: "1.0.0"}, {name: "strip ref", before: "refs/tags/1.0.0", after: "1.0.0"},
{before: "refs/tags/v1.0.0", after: "1.0.0"}, {name: "strip ref and version prefix", before: "refs/tags/v1.0.0", after: "1.0.0"},
{before: "v1.0.0", after: "1.0.0"}, {name: "strip version prefix", before: "v1.0.0", after: "1.0.0"},
} }
for _, tt := range tests { for _, tt := range tests {
got, want := stripTagPrefix(tt.before), tt.after t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, got, want) got := stripTagPrefix(tt.before)
assert.Equal(t, got, tt.after)
})
} }
} }
func TestSemverTagsStrict(t *testing.T) { func TestSemverTagsStrict(t *testing.T) {
tests := []struct { tests := []struct {
name string
before string before string
after []string after []string
}{ }{
{before: "", after: []string{"latest"}}, {name: "empty", before: "", after: []string{"latest"}},
{before: "refs/heads/main", after: []string{"latest"}}, {name: "main branch", before: "refs/heads/main", after: []string{"latest"}},
{before: "refs/tags/0.9.0", after: []string{"0.9", "0.9.0"}}, {name: "zero version", before: "refs/tags/0.9.0", after: []string{"0.9", "0.9.0"}},
{before: "refs/tags/1.0.0", after: []string{"1", "1.0", "1.0.0"}}, {name: "regular version", before: "refs/tags/1.0.0", after: []string{"1", "1.0", "1.0.0"}},
{before: "refs/tags/v1.0.0", after: []string{"1", "1.0", "1.0.0"}}, {name: "regular version with version prefix", before: "refs/tags/v1.0.0", after: []string{"1", "1.0", "1.0.0"}},
{before: "refs/tags/v1.0.0+1", after: []string{"1", "1.0", "1.0.0"}}, {name: "regular version with meta", before: "refs/tags/v1.0.0+1", after: []string{"1", "1.0", "1.0.0"}},
{before: "refs/tags/v1.0.0-alpha.1", after: []string{"1.0.0-alpha.1"}}, {name: "regular pre-release version count", before: "refs/tags/v1.0.0-alpha.1", after: []string{"1.0.0-alpha.1"}},
{before: "refs/tags/v1.0.0-alpha", after: []string{"1.0.0-alpha"}}, {name: "regular pre-release version", before: "refs/tags/v1.0.0-alpha", after: []string{"1.0.0-alpha"}},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tags, err := SemverTags(tt.before, true) tags, err := SemverTags(tt.before, true)
assert.NoError(t, err) assert.NoError(t, err)
got, want := tags, tt.after got, want := tags, tt.after
assert.Equal(t, got, want) assert.Equal(t, got, want)
})
} }
} }
func TestSemverTags(t *testing.T) { func TestSemverTags(t *testing.T) {
tests := []struct { tests := []struct {
Before string name string
After []string before string
after []string
strict bool
wantErr error
}{ }{
{"", []string{"latest"}}, {
{"refs/heads/main", []string{"latest"}}, name: "empty",
{"refs/tags/0.9.0", []string{"0.9", "0.9.0"}}, before: "",
{"refs/tags/1.0.0", []string{"1", "1.0", "1.0.0"}}, after: []string{"latest"},
{"refs/tags/v1.0.0", []string{"1", "1.0", "1.0.0"}}, strict: false,
{"refs/tags/v1.0.0+1", []string{"1", "1.0", "1.0.0"}}, wantErr: nil,
{"refs/tags/v1.0.0-alpha.1", []string{"1.0.0-alpha.1"}}, },
{"refs/tags/v1.0.0-alpha", []string{"1.0.0-alpha"}}, {
{"refs/tags/v1.0-alpha", []string{"1.0.0-alpha"}}, name: "main branch",
{"refs/tags/22.04.0", []string{"22", "22.4", "22.4.0"}}, before: "refs/heads/main",
{"refs/tags/22.04", []string{"22", "22.4", "22.4.0"}}, after: []string{"latest"},
strict: false,
wantErr: nil,
},
{
name: "zero version",
before: "refs/tags/0.9.0",
after: []string{"0.9", "0.9.0"},
strict: false,
wantErr: nil,
},
{
name: "regular version",
before: "refs/tags/1.0.0",
after: []string{"1", "1.0", "1.0.0"},
strict: false,
wantErr: nil,
},
{
name: "regular version with prefix",
before: "refs/tags/v1.0.0",
after: []string{"1", "1.0", "1.0.0"},
strict: false,
wantErr: nil,
},
{
name: "regular version with meta",
before: "refs/tags/v1.0.0+1",
after: []string{"1", "1.0", "1.0.0"},
strict: false,
wantErr: nil,
},
{
name: "regular prerelease version count",
before: "refs/tags/v1.0.0-alpha.1",
after: []string{"1.0.0-alpha.1"},
strict: false,
wantErr: nil,
},
{
name: "regular prerelease version",
before: "refs/tags/v1.0.0-alpha",
after: []string{"1.0.0-alpha"},
strict: false,
wantErr: nil,
},
{
name: "prerelease version",
before: "refs/tags/v1.0-alpha",
after: []string{"1.0.0-alpha"},
strict: false,
wantErr: nil,
},
{
name: "regular version",
before: "refs/tags/22.04.0",
after: []string{"22", "22.4", "22.4.0"},
strict: false,
wantErr: nil,
},
{
name: "regular version",
before: "refs/tags/22.04",
after: []string{"22", "22.4", "22.4.0"},
strict: false,
wantErr: nil,
},
{
name: "invalid semver",
before: "refs/tags/x1.0.0",
strict: true,
wantErr: assert.AnError,
},
{
name: "date tag",
before: "refs/tags/20190203",
strict: true,
wantErr: assert.AnError,
},
{
name: "regular version",
before: "refs/tags/22.04.0",
strict: true,
wantErr: assert.AnError,
},
{
name: "regular version shorthand",
before: "refs/tags/22.04",
strict: true,
wantErr: assert.AnError,
},
} }
for _, tt := range tests { for _, tt := range tests {
tags, err := SemverTags(tt.Before, false) tags, err := SemverTags(tt.before, tt.strict)
if tt.wantErr != nil {
assert.Error(t, err)
continue
}
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, tags, tt.after)
got, want := tags, tt.After
assert.Equal(t, got, want)
}
}
func TestSemverTagsSrtictError(t *testing.T) {
tests := []string{
"refs/tags/x1.0.0",
"refs/tags/20190203",
"refs/tags/22.04.0",
"refs/tags/22.04",
}
for _, tt := range tests {
_, err := SemverTags(tt, true)
assert.Error(t, err, "Expect tag error for %s", tt)
} }
} }
func TestSemverTagSuffix(t *testing.T) { func TestSemverTagSuffix(t *testing.T) {
tests := []struct { tests := []struct {
name string
before string before string
suffix string suffix string
after []string after []string
}{ }{
// without suffix // without suffix
{ {
name: "empty ref",
after: []string{"latest"}, after: []string{"latest"},
}, },
{ {
name: "ref without suffix",
before: "refs/tags/v1.0.0", before: "refs/tags/v1.0.0",
after: []string{ after: []string{
"1", "1",
@ -106,10 +199,12 @@ func TestSemverTagSuffix(t *testing.T) {
}, },
// with suffix // with suffix
{ {
name: "empty ref with suffix",
suffix: "linux-amd64", suffix: "linux-amd64",
after: []string{"linux-amd64"}, after: []string{"linux-amd64"},
}, },
{ {
name: "ref with suffix",
before: "refs/tags/v1.0.0", before: "refs/tags/v1.0.0",
suffix: "linux-amd64", suffix: "linux-amd64",
after: []string{ after: []string{
@ -118,27 +213,16 @@ func TestSemverTagSuffix(t *testing.T) {
"1.0.0-linux-amd64", "1.0.0-linux-amd64",
}, },
}, },
{
suffix: "nanoserver",
after: []string{"nanoserver"},
},
{
before: "refs/tags/v1.9.2",
suffix: "nanoserver",
after: []string{
"1-nanoserver",
"1.9-nanoserver",
"1.9.2-nanoserver",
},
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tag, err := SemverTagSuffix(tt.before, tt.suffix, true) tag, err := SemverTagSuffix(tt.before, tt.suffix, true)
assert.NoError(t, err) assert.NoError(t, err)
got, want := tag, tt.after got, want := tag, tt.after
assert.Equal(t, got, want) assert.Equal(t, got, want)
})
} }
} }
@ -148,10 +232,12 @@ func Test_stripHeadPrefix(t *testing.T) {
} }
tests := []struct { tests := []struct {
name string
args args args args
want string want string
}{ }{
{ {
name: "main branch",
args: args{ args: args{
ref: "refs/heads/main", ref: "refs/heads/main",
}, },
@ -160,7 +246,9 @@ func Test_stripHeadPrefix(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := stripHeadPrefix(tt.args.ref) got := stripHeadPrefix(tt.args.ref)
assert.Equal(t, got, tt.want) assert.Equal(t, got, tt.want)
})
} }
} }

View File

@ -44,7 +44,9 @@ func TestIsTaggable(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := IsTaggable(tt.args.ref, tt.args.defaultBranch) got := IsTaggable(tt.args.ref, tt.args.defaultBranch)
assert.Equal(t, got, tt.want, "%q. IsTaggable() = %v, want %v", tt.name, got, tt.want) assert.Equal(t, got, tt.want, "%q. IsTaggable() = %v, want %v", tt.name, got, tt.want)
})
} }
} }

View File

@ -8,20 +8,23 @@ import (
func TestSplitWithEscaping(t *testing.T) { func TestSplitWithEscaping(t *testing.T) {
tests := []struct { tests := []struct {
name string
input string input string
output []string output []string
}{ }{
{input: "", output: []string{}}, {name: "empty string", input: "", output: []string{}},
{input: "a,b", output: []string{"a", "b"}}, {name: "simple comma separated", input: "a,b", output: []string{"a", "b"}},
{input: ",,,", output: []string{"", "", "", ""}}, {name: "multiple commas", input: ",,,", output: []string{"", "", "", ""}},
{input: ",a\\,", output: []string{"", "a,"}}, {name: "escaped comma", input: ",a\\,", output: []string{"", "a,"}},
{input: "a,b\\,c\\\\d,e", output: []string{"a", "b,c\\\\d", "e"}}, {name: "escaped backslash", input: "a,b\\,c\\\\d,e", output: []string{"a", "b,c\\\\d", "e"}},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
strings := splitWithEscaping(tt.input, ",", "\\") strings := splitWithEscaping(tt.input, ",", "\\")
got, want := strings, tt.output got, want := strings, tt.output
assert.Equal(t, got, want) assert.Equal(t, got, want)
})
} }
} }