autogits/bots-common/associated_pr_scanner_test.go

148 lines
3.7 KiB
Go
Raw Normal View History

2024-12-05 18:38:35 +01:00
package common
import (
"bufio"
"slices"
"strings"
"testing"
)
func newStringScanner(s string) *bufio.Scanner {
return bufio.NewScanner(strings.NewReader(s))
}
func TestAssociatedPRScanner(t *testing.T) {
2024-12-07 14:35:34 +01:00
testTable := []struct {
name string
input string
prs []BasicPR
desc string
}{
{
"No PRs",
"",
[]BasicPR{},
"",
},
{
"Single PRs",
"Some header of the issue\n\nFollowed by some description\n\nPR: test/foo#4\n",
[]BasicPR{{org: "test", repo: "foo", num: 4}},
"Some header of the issue\n\nFollowed by some description",
},
{
"Multiple PRs",
"Some header of the issue\n\nFollowed by some description\nPR: test/foo#4\n\nPR: test/goo#5\n",
[]BasicPR{
{org: "test", repo: "foo", num: 4},
{org: "test", repo: "goo", num: 5},
},
"Some header of the issue\n\nFollowed by some description",
},
{
"Multiple PRs with whitespace",
"Some header of the issue\n\n\tPR: test/goo#5\n\n Followed by some description\n \t PR: test/foo#4\n",
[]BasicPR{
{org: "test", repo: "foo", num: 4},
{org: "test", repo: "goo", num: 5},
},
"Some header of the issue\n\n\n Followed by some description",
},
{
"Multiple PRs with missing names and other special cases to ignore",
"Some header of the issue\n\n\n\t PR: foobar#5 \n\t PR: rd/goo5 \n\t PR: test/#5 \n" +
"\t PR: /goo#5 \n\t PR: test/goo# \n\t PR: test / goo # 10 \n\tPR: test/gool# 10 \n" +
"\t PR: test/goo#5 \n\t\n Followed by some description\n\t PR: test/foo#4 \n\t\n\n",
[]BasicPR{
{
org: "test",
repo: "foo",
num: 4,
},
{
org: "test",
repo: "goo",
num: 5,
},
},
"Some header of the issue\n\n\n\t PR: foobar#5 \n\t PR: rd/goo5 \n\t PR: test/#5 \n" +
"\t PR: /goo#5 \n\t PR: test/goo# \n\t PR: test / goo # 10 \n\tPR: test/gool# 10 \n" +
"\t\n Followed by some description",
},
}
for _, test := range testTable {
t.Run(test.name, func(t *testing.T) {
desc, prs := ExtractPRsFromDescription(newStringScanner(test.input))
if len(prs) != len(test.prs) {
t.Error("Unexpected length:", len(prs), "expected:", len(test.prs))
return
}
for _, p := range test.prs {
if !slices.Contains(prs, p) {
t.Error("missing expected PR", p)
}
}
if desc != test.desc {
t.Error("Desc output", len(desc), "!=", len(test.desc), ":", desc)
}
2024-12-05 18:38:35 +01:00
})
2024-12-07 14:35:34 +01:00
}
}
2024-12-05 18:38:35 +01:00
2024-12-07 14:35:34 +01:00
func TestAppendingPRsToDescription(t *testing.T) {
testTable := []struct {
name string
desc string
PRs []BasicPR
output string
}{
{
"Append single PR to end of description",
"something",
[]BasicPR{
{org: "a", repo: "b", num: 100},
},
"something\n\nPR: a/b#100",
},
{
"Append multiple PR to end of description",
"something",
[]BasicPR{
{org: "a1", repo: "b", num: 100},
{org: "a1", repo: "c", num: 100},
{org: "a1", repo: "c", num: 101},
{org: "b", repo: "b", num: 100},
{org: "c", repo: "b", num: 100},
},
"something\n\nPR: a1/b#100\nPR: a1/c#100\nPR: a1/c#101\nPR: b/b#100\nPR: c/b#100",
},
{
"Append multiple sorted PR to end of description and remove dups",
"something",
[]BasicPR{
{org: "a1", repo: "c", num: 101},
{org: "a1", repo: "c", num: 100},
{org: "c", repo: "b", num: 100},
{org: "b", repo: "b", num: 100},
{org: "a1", repo: "c", num: 101},
{org: "a1", repo: "c", num: 101},
{org: "a1", repo: "b", num: 100},
},
"something\n\nPR: a1/b#100\nPR: a1/c#100\nPR: a1/c#101\nPR: b/b#100\nPR: c/b#100",
},
}
for _, test := range testTable {
t.Run(test.name, func(t *testing.T) {
d := AppendPRsToDescription(test.desc, test.PRs)
if d != test.output {
t.Error(len(d), "vs", len(test.output))
t.Error("unpected output", d)
}
})
}
2024-12-05 18:38:35 +01:00
}