-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgmi2html_test.go
More file actions
179 lines (174 loc) · 5.2 KB
/
gmi2html_test.go
File metadata and controls
179 lines (174 loc) · 5.2 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package gmi2html
import (
"testing"
)
func TestGmi2html(t *testing.T) {
tests := []struct {
name string
input string
title string
contentOnly bool
replaceGmiExt bool
want string
wantErr bool
}{
{
name: "Basic text",
input: "Hello world",
title: "Test",
contentOnly: true,
replaceGmiExt: false,
want: "<p class=\"gemini-textline\">Hello world</p>",
wantErr: false,
},
{
name: "Headers",
input: "# Header 1\n## Header 2\n### Header 3",
title: "Test",
contentOnly: true,
replaceGmiExt: false,
want: "<h1 class=\"gemini-heading-1\">Header 1</h1><h2 class=\"gemini-heading-2\">Header 2</h2><h3 class=\"gemini-heading-3\">Header 3</h3>",
wantErr: false,
},
{
name: "List items",
input: "* Item 1\n* Item 2",
title: "Test",
contentOnly: true,
replaceGmiExt: false,
want: "<p class=\"gemini-list-item\">• Item 1</p><p class=\"gemini-list-item\">• Item 2</p>",
wantErr: false,
},
{
name: "Blockquote",
input: "> This is a quote",
title: "Test",
contentOnly: true,
replaceGmiExt: false,
want: "<blockquote class=\"gemini-blockquote\">This is a quote</blockquote>",
wantErr: false,
},
{
name: "Link",
input: "=> https://example.com Example Link",
title: "Test",
contentOnly: true,
replaceGmiExt: false,
want: "<div class=\"gemini-link-container\"><a href=\"https://example.com\">Example Link</a></div>",
wantErr: false,
},
{
name: "Link with gmi extension replacement",
input: "=> /path/file.gmi Example Link",
title: "Test",
contentOnly: true,
replaceGmiExt: true,
want: "<div class=\"gemini-link-container\"><a href=\"/path/file.html\">Example Link</a></div>",
wantErr: false,
},
{
name: "Preformatted text",
input: "```\nThis is preformatted\n```",
title: "Test",
contentOnly: true,
replaceGmiExt: false,
want: "<pre class=\"gemini-preformatted\">\nThis is preformatted\n</pre>",
wantErr: false,
},
{
name: "Mixed content",
input: "# Title\nNormal text\n=> https://example.com Link\n```\nCode\n```\n* List item",
title: "Test",
contentOnly: true,
replaceGmiExt: false,
want: "<h1 class=\"gemini-heading-1\">Title</h1><p class=\"gemini-textline\">Normal text</p><div class=\"gemini-link-container\"><a href=\"https://example.com\">Link</a></div><pre class=\"gemini-preformatted\">\nCode\n</pre><p class=\"gemini-list-item\">• List item</p>",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Gmi2html(tt.input, tt.title, tt.contentOnly, tt.replaceGmiExt)
if (err != nil) != tt.wantErr {
t.Errorf("Gmi2html() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("Gmi2html() = %v, want %v", got, tt.want)
}
})
}
}
func TestParseGeminiLink(t *testing.T) {
tests := []struct {
name string
linkLine string
replaceGmiExt bool
wantURL string
wantDesc string
wantErr bool
}{
{
name: "Basic link",
linkLine: "=> https://example.com Example Link",
replaceGmiExt: false,
wantURL: "https://example.com",
wantDesc: "Example Link",
wantErr: false,
},
{
name: "Link without description",
linkLine: "=> https://example.com",
replaceGmiExt: false,
wantURL: "https://example.com",
wantDesc: "https://example.com",
wantErr: false,
},
{
name: "Invalid link format",
linkLine: "Invalid line",
replaceGmiExt: false,
wantURL: "",
wantDesc: "",
wantErr: true,
},
{
name: "Link with .gmi extension, no replacement",
linkLine: "=> /path/file.gmi Link to Gemini file",
replaceGmiExt: false,
wantURL: "/path/file.gmi",
wantDesc: "Link to Gemini file",
wantErr: false,
},
{
name: "Link with .gmi extension, with replacement",
linkLine: "=> /path/file.gmi Link to Gemini file",
replaceGmiExt: true,
wantURL: "/path/file.html",
wantDesc: "Link to Gemini file",
wantErr: false,
},
{
name: "Link without .gmi extension, with replacement",
linkLine: "=> /path/file.txt Link to text file",
replaceGmiExt: true,
wantURL: "/path/file.txt",
wantDesc: "Link to text file",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotURL, gotDesc, err := parseGeminiLink(tt.linkLine, tt.replaceGmiExt)
if (err != nil) != tt.wantErr {
t.Errorf("parseGeminiLink() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotURL != tt.wantURL {
t.Errorf("parseGeminiLink() gotURL = %v, want %v", gotURL, tt.wantURL)
}
if gotDesc != tt.wantDesc {
t.Errorf("parseGeminiLink() gotDesc = %v, want %v", gotDesc, tt.wantDesc)
}
})
}
}