1package domain
2
3import (
4 "reflect"
5 "strings"
6 "testing"
7)
8
9func TestFindReferences(t *testing.T) {
10 tests := []struct {
11 text string
12 mentions, closes []int64
13 }{
14 {"abc#3 a_b#3 1#3 ' ##3 #3abc #03 #0", nil, nil},
15 {"(#3) see #3, \"#3\"", []int64{3}, nil},
16 {"fixes #3.", []int64{3}, []int64{3}},
17 {"Fixes: #3", []int64{3}, []int64{3}},
18 {"fixes:#3", []int64{3}, []int64{3}},
19 {"Closes #3", []int64{3}, []int64{3}},
20 {"RESOLVED #3", []int64{3}, []int64{3}},
21 {"fix #3", []int64{3}, []int64{3}},
22 {"prefixes #3", []int64{3}, nil},
23 {"fixes 3 fixes#3", nil, nil},
24 {"fixes #3, #4 and #5", []int64{3, 4, 5}, []int64{3, 4, 5}},
25 {"fixes #3 #4", []int64{3, 4}, []int64{3}},
26 {"#5,#3,#5", []int64{3, 5}, nil},
27 {"#1234567890 #12345678901 !3 owner/repo#3", []int64{1234567890}, nil},
28 {"`fixes #3`\n```\ncloses #4\n```", []int64{3, 4}, []int64{3, 4}},
29 {"close #1 closed #2 fixes #3 fixed #4 resolve #5 resolves #6", []int64{1, 2, 3, 4, 5, 6}, []int64{1, 2, 3, 4, 5, 6}},
30 }
31 for _, tt := range tests {
32 t.Run(tt.text, func(t *testing.T) {
33 if got := FindReferences(tt.text); !reflect.DeepEqual(got, tt.mentions) {
34 t.Errorf("mentions = %v, want %v", got, tt.mentions)
35 }
36 if got := FindClosingReferences(tt.text); !reflect.DeepEqual(got, tt.closes) {
37 t.Errorf("closes = %v, want %v", got, tt.closes)
38 }
39 })
40 }
41}
42
43func TestScanMergeRequest(t *testing.T) {
44 got := ScanMergeRequest("fixes #3", "#2", []string{"closes #4"}, []string{"resolved #5", strings.Repeat(" ", MaxReferenceTextBytes) + "fixes #6"})
45 want := MergeRequestReferences{Mentions: []int64{2, 3, 4, 5}, Closes: []int64{3, 5}}
46 if !reflect.DeepEqual(got, want) {
47 t.Fatalf("got %+v, want %+v", got, want)
48 }
49 for _, scan := range []func(string) MergeRequestReferences{
50 func(s string) MergeRequestReferences { return ScanMergeRequest(s, "", nil, nil) },
51 func(s string) MergeRequestReferences { return ScanMergeRequest("", s, nil, nil) },
52 func(s string) MergeRequestReferences { return ScanMergeRequest("", "", []string{s}, nil) },
53 func(s string) MergeRequestReferences { return ScanMergeRequest("", "", nil, []string{s}) },
54 } {
55 if got := scan(strings.Repeat(" ", MaxReferenceTextBytes) + "#7"); len(got.Mentions) != 0 || len(got.Closes) != 0 {
56 t.Errorf("text beyond limit scanned: %+v", got)
57 }
58 }
59}
60
61func TestScanMergeRequestTruncationDropsPartialToken(t *testing.T) {
62 prefix := strings.Repeat(" ", MaxReferenceTextBytes-len("fixes #123"))
63 got := ScanMergeRequest("", "", nil, []string{prefix + "fixes #12345"})
64 if len(got.Mentions) != 0 || len(got.Closes) != 0 {
65 t.Errorf("split number reported: %+v", got)
66 }
67 // Fully before the limit, followed by a split token.
68 full := "fixes #7 " + strings.Repeat("a", MaxReferenceTextBytes)
69 got = ScanMergeRequest("", "", nil, []string{full})
70 if !reflect.DeepEqual(got, MergeRequestReferences{Mentions: []int64{7}, Closes: []int64{7}}) {
71 t.Errorf("got %+v", got)
72 }
73 // No whitespace at all: nothing scanned.
74 got = ScanMergeRequest("", "", nil, []string{strings.Repeat("a", MaxReferenceTextBytes-3) + " #1" + "2"})
75 if len(got.Mentions) != 0 {
76 t.Errorf("got %+v", got)
77 }
78}