summaryrefslogtreecommitdiff
path: root/internal/processor/txt_test.go
blob: bb44c288a98fed80a45952156c8388f5b1fec45a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package processor

import (
	"strings"
	"testing"
)

func TestStripURLTrailing(t *testing.T) {
	t.Parallel()

	tests := []struct {
		name string
		in   string
		want string
	}{
		{name: "plain", in: "https://example.com", want: "https://example.com"},
		{name: "trailing period", in: "https://example.com.", want: "https://example.com"},
		{name: "multiple punctuation", in: "https://a.b/c).", want: "https://a.b/c"},
		{name: "empty", in: "", want: ""},
		{name: "only punctuation", in: "...", want: ""},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()
			got := stripURLTrailing(tt.in)
			if got != tt.want {
				t.Fatalf("stripURLTrailing(%q) = %q; want %q", tt.in, got, tt.want)
			}
		})
	}
}

func TestAutolinkLine(t *testing.T) {
	t.Parallel()

	tests := []struct {
		name string
		in   string
		want string
	}{
		{
			name: "no url escapes",
			in:   `hello <world>`,
			want: `hello &lt;world&gt;`,
		},
		{
			name: "single url",
			in:   "see https://foo.test ok",
			want: `see <a href="https://foo.test" target="_blank" rel="noopener noreferrer">https://foo.test</a> ok`,
		},
		{
			name: "url with trailing period in prose",
			in:   "Visit https://foo.test.",
			want: `Visit <a href="https://foo.test" target="_blank" rel="noopener noreferrer">https://foo.test</a>.`,
		},
		{
			name: "two urls",
			in:   "a http://a.com b https://b.org c",
			want: `a <a href="http://a.com" target="_blank" rel="noopener noreferrer">http://a.com</a> b <a href="https://b.org" target="_blank" rel="noopener noreferrer">https://b.org</a> c`,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()
			got := autolinkLine(tt.in)
			if got != tt.want {
				t.Fatalf("autolinkLine(%q) = %q; want %q", tt.in, got, tt.want)
			}
		})
	}
}

func TestFormatParagraph(t *testing.T) {
	t.Parallel()

	tests := []struct {
		name string
		in   string
		want string
	}{
		{
			name: "single line",
			in:   "hello",
			want: "hello",
		},
		{
			name: "line break",
			in:   "line one\nline two",
			want: "line one<br>\nline two",
		},
		{
			name: "skips blank lines inside para",
			in:   "a\n\nb",
			want: "a<br>\nb",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()
			got := formatParagraph(tt.in)
			if got != tt.want {
				t.Fatalf("formatParagraph(%q) = %q; want %q", tt.in, got, tt.want)
			}
		})
	}
}

func TestFormatParagraph_autolinkMultiline(t *testing.T) {
	t.Parallel()
	got := formatParagraph("u https://x.y\nv")
	if !strings.Contains(got, `<a href="https://x.y"`) {
		t.Fatalf("expected autolink in multiline paragraph, got %q", got)
	}
	if !strings.Contains(got, "<br>") {
		t.Fatalf("expected br between lines, got %q", got)
	}
}