|
| 1 | +package termlink_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/fatih/color" |
| 7 | + "github.com/savioxavier/termlink" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +func testAll(input, expectedHyperlink, expectedNoHyperlink string) func(t *testing.T) { |
| 12 | + return func(t *testing.T) { |
| 13 | + if termlink.SupportsHyperlinks() { |
| 14 | + assert.Equal(t, input, expectedHyperlink) |
| 15 | + } else { |
| 16 | + assert.Equal(t, input, expectedNoHyperlink) |
| 17 | + } |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +func TestBasicLink(t *testing.T) { |
| 22 | + input := termlink.Link("Hello", "https://google.com") |
| 23 | + expectedHyperlink := "\x1b]8;;https://google.com\aHello\x1b]8;;\a\x1b[m" |
| 24 | + expectedNoHyperlink := "Hello (\u200Bhttps://google.com)\u001b[m" |
| 25 | + |
| 26 | + testAll(input, expectedHyperlink, expectedNoHyperlink)(t) |
| 27 | +} |
| 28 | + |
| 29 | +func TestColorLink(t *testing.T) { |
| 30 | + input := termlink.ColorLink("Hello", "https://google.com", "red") |
| 31 | + expectedHyperlink := "\x1b]8;;https://google.com\a\x1b[31mHello\x1b]8;;\a\x1b[m" |
| 32 | + expectedNoHyperlink := "\x1b[31mHello (\u200bhttps://google.com)\x1b[m" |
| 33 | + |
| 34 | + testAll(input, expectedHyperlink, expectedNoHyperlink)(t) |
| 35 | +} |
| 36 | + |
| 37 | +func TestColorsPackageLink(t *testing.T) { |
| 38 | + input := color.New(color.FgCyan).SprintFunc()(termlink.Link("Hello", "https://google.com")) |
| 39 | + expectedHyperlink := "\x1b[36m\x1b]8;;https://google.com\aHello\x1b]8;;\a\x1b[m\x1b[0m" |
| 40 | + expectedNoHyperlink := "\x1b[36mHello (\u200bhttps://google.com)\x1b[m\x1b[0m" |
| 41 | + |
| 42 | + testAll(input, expectedHyperlink, expectedNoHyperlink)(t) |
| 43 | +} |
| 44 | + |
| 45 | +func TestBoldLink(t *testing.T) { |
| 46 | + input := termlink.ColorLink("Hello", "https://google.com", "bold") |
| 47 | + expectedHyperlink := "\x1b]8;;https://google.com\a\x1b[1mHello\x1b]8;;\a\x1b[m" |
| 48 | + expectedNoHyperlink := "\x1b[1mHello (\u200bhttps://google.com)\x1b[m" |
| 49 | + |
| 50 | + testAll(input, expectedHyperlink, expectedNoHyperlink)(t) |
| 51 | +} |
| 52 | + |
| 53 | +func TestItalicsLink(t *testing.T) { |
| 54 | + input := termlink.ColorLink("Hello", "https://google.com", "italic") |
| 55 | + expectedHyperlink := "\x1b]8;;https://google.com\a\x1b[3mHello\x1b]8;;\a\x1b[m" |
| 56 | + expectedNoHyperlink := "\x1b[3mHello (\u200bhttps://google.com)\x1b[m" |
| 57 | + |
| 58 | + testAll(input, expectedHyperlink, expectedNoHyperlink)(t) |
| 59 | + |
| 60 | +} |
| 61 | + |
| 62 | +func TestItalicsBoldLink(t *testing.T) { |
| 63 | + input := termlink.ColorLink("Hello", "https://google.com", "bold italic") |
| 64 | + expectedHyperlink := "\x1b]8;;https://google.com\a\x1b[1;3mHello\x1b]8;;\a\x1b[m" |
| 65 | + expectedNoHyperlink := "\x1b[1;3mHello (\u200bhttps://google.com)\x1b[m" |
| 66 | + |
| 67 | + testAll(input, expectedHyperlink, expectedNoHyperlink)(t) |
| 68 | +} |
| 69 | + |
| 70 | +func TestColorItalicsBoldLink(t *testing.T) { |
| 71 | + input := termlink.ColorLink("Hello", "https://google.com", "red bold italic") |
| 72 | + expectedHyperlink := "\x1b]8;;https://google.com\a\x1b[31;1;3mHello\x1b]8;;\a\x1b[m" |
| 73 | + expectedNoHyperlink := "\x1b[31;1;3mHello (\u200bhttps://google.com)\x1b[m" |
| 74 | + |
| 75 | + testAll(input, expectedHyperlink, expectedNoHyperlink)(t) |
| 76 | +} |
0 commit comments