0
0
mirror of https://github.com/thegeeklab/wp-plugin-go.git synced 2024-11-23 06:40:40 +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 {
want := testFileContent(t, tt.want)
t.Run(tt.name, func(t *testing.T) {
want := testFileContent(t, tt.want)
got, _ := ToMarkdown(tt.app)
assert.Equal(t, want, got)
})
@ -140,9 +139,8 @@ func TestToData(t *testing.T) {
}
for _, tt := range tests {
got := GetTemplateData(tt.app)
t.Run(tt.name, func(t *testing.T) {
got := GetTemplateData(tt.app)
assert.Equal(t, tt.want, got)
})
}

View File

@ -59,23 +59,25 @@ func Test_currFromContext(t *testing.T) {
Execute: func(_ context.Context) error { return nil },
}
got := New(options)
got.App.Action = func(ctx *cli.Context) error {
got.Metadata = MetadataFromContext(ctx)
t.Run(tt.name, func(t *testing.T) {
got := New(options)
got.App.Action = func(ctx *cli.Context) error {
got.Metadata = MetadataFromContext(ctx)
return nil
}
return nil
}
_ = got.App.Run([]string{"dummy"})
_ = got.App.Run([]string{"dummy"})
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.Description, tt.want["desc"])
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.Description, tt.want["desc"])
})
}
}
func TestSplitMessage(t *testing.T) {
testCases := []struct {
tests := []struct {
name string
message string
wantTitle string
@ -113,10 +115,12 @@ func TestSplitMessage(t *testing.T) {
},
}
for _, tc := range testCases {
gotTitle, gotDescription := splitMessage(tc.message)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotTitle, gotDescription := splitMessage(tt.message)
assert.Equal(t, tc.wantTitle, gotTitle)
assert.Equal(t, tc.wantDescription, gotDescription)
assert.Equal(t, tt.wantTitle, gotTitle)
assert.Equal(t, tt.wantDescription, gotDescription)
})
}
}

View File

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

View File

@ -44,7 +44,9 @@ func TestIsTaggable(t *testing.T) {
}
for _, tt := range tests {
got := IsTaggable(tt.args.ref, tt.args.defaultBranch)
assert.Equal(t, got, tt.want, "%q. IsTaggable() = %v, want %v", tt.name, got, tt.want)
t.Run(tt.name, func(t *testing.T) {
got := IsTaggable(tt.args.ref, tt.args.defaultBranch)
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) {
tests := []struct {
name string
input string
output []string
}{
{input: "", output: []string{}},
{input: "a,b", output: []string{"a", "b"}},
{input: ",,,", output: []string{"", "", "", ""}},
{input: ",a\\,", output: []string{"", "a,"}},
{input: "a,b\\,c\\\\d,e", output: []string{"a", "b,c\\\\d", "e"}},
{name: "empty string", input: "", output: []string{}},
{name: "simple comma separated", input: "a,b", output: []string{"a", "b"}},
{name: "multiple commas", input: ",,,", output: []string{"", "", "", ""}},
{name: "escaped comma", input: ",a\\,", output: []string{"", "a,"}},
{name: "escaped backslash", input: "a,b\\,c\\\\d,e", output: []string{"a", "b,c\\\\d", "e"}},
}
for _, tt := range tests {
strings := splitWithEscaping(tt.input, ",", "\\")
got, want := strings, tt.output
t.Run(tt.name, func(t *testing.T) {
strings := splitWithEscaping(tt.input, ",", "\\")
got, want := strings, tt.output
assert.Equal(t, got, want)
assert.Equal(t, got, want)
})
}
}