From 385d9d31b4e57ea9214246a741156d1981365bd2 Mon Sep 17 00:00:00 2001 From: rhysd Date: Sun, 6 Oct 2024 08:05:54 -0700 Subject: [PATCH] add tests for linking custom auto links --- reflink_test.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/reflink_test.go b/reflink_test.go index e658f07..267a834 100644 --- a/reflink_test.go +++ b/reflink_test.go @@ -578,3 +578,54 @@ func TestLinkRefs(t *testing.T) { }) } } + +func TestLinkCustomReferences(t *testing.T) { + tests := []struct { + what string + input string + want string + }{ + { + what: "numeric reference", + input: "ref FOO-123 is numeric", + want: "ref [FOO-123](https://example.com/foo/123) is numeric", + }, + { + what: "numeric ref prefix with alphanumeric ID", + input: "ref FOO-a123 is not linked", + want: "ref FOO-a123 is not linked", + }, + { + what: "numeric reference not followed by boundary", + input: "ref FOO-123a is not linked", + want: "ref FOO-123a is not linked", + }, + { + what: "prefix not following boundary", + input: "xFOO-123 is not linked", + want: "xFOO-123 is not linked", + }, + { + what: "alphanumeric reference", + input: "ref BAR-abc123 is alphanumeric", + want: "ref [BAR-abc123](https://example.com/bar/abc123) is alphanumeric", + }, + { + what: "alphanumeric ref followed by non-alphanumeric", + input: "ref BAR-abc123あ is linked", + want: "ref [BAR-abc123](https://example.com/bar/abc123)あ is linked", + }, + } + + for _, tc := range tests { + t.Run(tc.what, func(t *testing.T) { + l := NewReflinker("https://github.com/u/r") + l.AddExtRef("FOO-", "https://example.com/foo/", false) + l.AddExtRef("BAR-", "https://example.com/bar/", true) + have := l.Link(tc.input) + if have != tc.want { + t.Fatalf("wanted %q but got %q", tc.want, have) + } + }) + } +}