okami.mom/glamour

git clone https://git.nkp.pet/okami.mom/glamour

Ayman BagabasMerge pull request #408 from charmbracelet/v2-expcf874d7

main
2.2 KiB53 linesraw
1package autolink_test
2
3import (
4	"testing"
5
6	"charm.land/glamour/v2/internal/autolink"
7)
8
9func TestDetect(t *testing.T) {
10	tests := []struct {
11		url      string
12		expected string
13	}{
14		{"https://github.com/owner/repo/issue/123", "owner/repo#123"},
15		{"https://github.com/owner/repo/issues/123", "owner/repo#123"},
16		{"https://github.com/owner/repo/pull/123", "owner/repo#123"},
17		{"https://github.com/owner/repo/pulls/123", "owner/repo#123"},
18		{"https://github.com/owner/repo/discussions/123", "owner/repo#123"},
19
20		{"https://github.com/owner/repo/issue/123#issuecomment-456", "owner/repo#123 (comment)"},
21		{"https://github.com/owner/repo/issues/123#issuecomment-456", "owner/repo#123 (comment)"},
22		{"https://github.com/owner/repo/pull/123#issuecomment-456", "owner/repo#123 (comment)"},
23		{"https://github.com/owner/repo/pulls/123#issuecomment-456", "owner/repo#123 (comment)"},
24
25		{"https://github.com/owner/repo/pull/123#discussion_r456", "owner/repo#123 (comment)"},
26		{"https://github.com/owner/repo/pulls/123#discussion_r456", "owner/repo#123 (comment)"},
27
28		{"https://github.com/owner/repo/pull/123#pullrequestreview-456", "owner/repo#123 (review)"},
29		{"https://github.com/owner/repo/pulls/123#pullrequestreview-456", "owner/repo#123 (review)"},
30
31		{"https://github.com/owner/repo/discussions/123#discussioncomment-456", "owner/repo#123 (comment)"},
32
33		{"https://github.com/owner/repo/commit/abcdefghijklmnopqrsxyz", "owner/repo@abcdefg"},
34
35		{"https://github.com/owner/repo/pull/123/commits/abcdefghijklmnopqrsxyz", "owner/repo@abcdefg"},
36		{"https://github.com/owner/repo/pulls/123/commits/abcdefghijklmnopqrsxyz", "owner/repo@abcdefg"},
37
38		{"https://github.com/owner/repo/commit/abcdefghijklmnopqrsxyz#diff-123", "owner/repo@abcdefg"},
39		{"https://github.com/owner/repo/pull/123/commits/abcdefghijklmnopqrsxyz#diff-123", "owner/repo@abcdefg"},
40		{"https://github.com/owner/repo/pulls/123/commits/abcdefghijklmnopqrsxyz#diff-123", "owner/repo@abcdefg"},
41	}
42	for _, test := range tests {
43		t.Run(test.url, func(t *testing.T) {
44			result, ok := autolink.Detect(test.url)
45			if !ok {
46				t.Errorf("expected to detect URL, got nil")
47			}
48			if result != test.expected {
49				t.Errorf("expected %s, got %s", test.expected, result)
50			}
51		})
52	}
53}