193 lines
3.8 KiB
Go
193 lines
3.8 KiB
Go
package common
|
|
|
|
import (
|
|
"bufio"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func newStringScanner(s string) *bufio.Scanner {
|
|
return bufio.NewScanner(strings.NewReader(s))
|
|
}
|
|
|
|
func TestAssociatedPRScanner(t *testing.T) {
|
|
t.Run("No PRs", func(t *testing.T) {
|
|
if _, out := ExtractAssociatedDescriptionAndPRs(newStringScanner("")); len(out) != 0 {
|
|
t.Error("Unexpected output", out)
|
|
}
|
|
})
|
|
|
|
t.Run("Single PR", func(t *testing.T) {
|
|
const singlePRText = `Some header of the issue
|
|
|
|
Followed by some description
|
|
PR: test/foo#4
|
|
`
|
|
_, out := ExtractAssociatedDescriptionAndPRs(newStringScanner(singlePRText))
|
|
if len(out) != 1 {
|
|
t.Error("Unexpected output", out)
|
|
return
|
|
}
|
|
|
|
expected := BasicPR{
|
|
org: "test",
|
|
repo: "foo",
|
|
num: 4,
|
|
}
|
|
if out[0] != expected {
|
|
t.Error("Unexpected", out, "Expected", expected)
|
|
}
|
|
})
|
|
|
|
t.Run("Multiple PRs", func(t *testing.T) {
|
|
const multiplePRText = `Some header of the issue
|
|
|
|
Followed by some description
|
|
PR: test/foo#4
|
|
|
|
PR: test/goo#5
|
|
`
|
|
|
|
_, out := ExtractAssociatedDescriptionAndPRs(newStringScanner(multiplePRText))
|
|
if len(out) != 2 {
|
|
t.Error("Unexpected output", out)
|
|
return
|
|
}
|
|
|
|
expected1 := BasicPR{
|
|
org: "test",
|
|
repo: "foo",
|
|
num: 4,
|
|
}
|
|
expected2 := BasicPR{
|
|
org: "test",
|
|
repo: "goo",
|
|
num: 5,
|
|
}
|
|
|
|
if !slices.Contains(out, expected1) {
|
|
t.Error("Unexpected", out, "Expected", expected1)
|
|
}
|
|
if !slices.Contains(out, expected2) {
|
|
t.Error("Unexpected", out, "Expected", expected2)
|
|
}
|
|
})
|
|
|
|
t.Run("Multiple PRs with whitespace", func(t *testing.T) {
|
|
const whitespacePRText = `Some header of the issue
|
|
|
|
PR: test/goo#5
|
|
|
|
Followed by some description
|
|
PR: test/foo#4
|
|
`
|
|
|
|
desc, out := ExtractAssociatedDescriptionAndPRs(newStringScanner(whitespacePRText))
|
|
if len(out) != 2 {
|
|
t.Error("Unexpected output", out)
|
|
return
|
|
}
|
|
|
|
const expectedDesc = `Some header of the issue
|
|
|
|
|
|
Followed by some description`
|
|
expected1 := BasicPR{
|
|
org: "test",
|
|
repo: "foo",
|
|
num: 4,
|
|
}
|
|
expected2 := BasicPR{
|
|
org: "test",
|
|
repo: "goo",
|
|
num: 5,
|
|
}
|
|
|
|
if !slices.Contains(out, expected1) {
|
|
t.Error("Unexpected", out, "Expected", expected1)
|
|
}
|
|
if !slices.Contains(out, expected2) {
|
|
t.Error("Unexpected", out, "Expected", expected2)
|
|
}
|
|
if desc != expectedDesc {
|
|
t.Error("unexpected desc", desc)
|
|
}
|
|
})
|
|
|
|
t.Run("Multiple PRs with missing names and other special cases to ignore", func(t *testing.T) {
|
|
const whitespacePRText = `Some header of the issue
|
|
|
|
PR: foobar#5
|
|
PR: rd/goo5
|
|
PR: test/#5
|
|
PR: /goo#5
|
|
PR: test/goo#
|
|
PR: test / goo # 10
|
|
PR: test/gool# 10
|
|
PR: test/goo#5
|
|
|
|
Followed by some description
|
|
PR: test/foo#4
|
|
|
|
|
|
`
|
|
|
|
desc, out := ExtractAssociatedDescriptionAndPRs(newStringScanner(whitespacePRText))
|
|
if len(out) != 2 {
|
|
t.Error("Unexpected output", out)
|
|
return
|
|
}
|
|
|
|
const expectedDesc = `Some header of the issue
|
|
|
|
PR: foobar#5
|
|
PR: rd/goo5
|
|
PR: test/#5
|
|
PR: /goo#5
|
|
PR: test/goo#
|
|
PR: test / goo # 10
|
|
PR: test/gool# 10
|
|
|
|
Followed by some description`
|
|
|
|
if desc != expectedDesc {
|
|
t.Error(len(desc), "vs", len(expectedDesc))
|
|
t.Error("description doesn't match expected. ", desc)
|
|
}
|
|
|
|
expected1 := BasicPR{
|
|
org: "test",
|
|
repo: "foo",
|
|
num: 4,
|
|
}
|
|
expected2 := BasicPR{
|
|
org: "test",
|
|
repo: "goo",
|
|
num: 5,
|
|
}
|
|
|
|
if !slices.Contains(out, expected1) {
|
|
t.Error("Unexpected", out, "Expected", expected1)
|
|
}
|
|
if !slices.Contains(out, expected2) {
|
|
t.Error("Unexpected", out, "Expected", expected2)
|
|
}
|
|
})
|
|
|
|
t.Run("Append PRs to end of description", func(t *testing.T) {
|
|
d := AppendPRsToDescription("something", []BasicPR{
|
|
BasicPR{org: "a", repo: "b", num: 100},
|
|
})
|
|
|
|
const expectedDesc = `something
|
|
|
|
PR: a/b#100
|
|
`
|
|
if d != expectedDesc {
|
|
t.Error(len(d), "vs", len(expectedDesc))
|
|
t.Error("unpected output", d)
|
|
}
|
|
})
|
|
}
|