-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhaturl_test.go
126 lines (110 loc) · 2.39 KB
/
whaturl_test.go
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
package main
import (
"context"
"net/http"
"strings"
"testing"
"time"
)
func TestGetTitle(t *testing.T) {
client := &http.Client{
Timeout: 5 * time.Second,
}
ctx := context.Background()
testCases := []struct {
name string
url string
expected string
}{
{"Valid URL", "https://example.com", "Example Domain"},
{"Invalid URL", "https://example.invalid", ""},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
title, err := getTitle(ctx, client, tc.url)
if err != nil && tc.expected != "" {
t.Fatalf("Expected %q but got an error: %v", tc.expected, err)
}
if title != tc.expected {
t.Errorf("Expected title %q but got %q", tc.expected, title)
}
})
}
}
func TestCreateLink(t *testing.T) {
testCases := []struct {
name string
url string
title string
dialect string
expected string
expectErr bool
}{
{
"Markdown",
"https://example.com",
"Example Domain",
"markdown",
"[Example Domain](https://example.com)",
false,
},
{
"Org",
"https://example.com",
"Example Domain",
"org",
"[[https://example.com][Example Domain]]",
false,
},
{
"HTML",
"https://example.com",
"Example Domain",
"html",
`<a href="https://example.com">Example Domain</a>`,
false,
},
{
"Unsupported Dialect",
"https://example.com",
"Example Domain",
"unsupported",
"",
true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
link, err := createLink(tc.url, tc.title, tc.dialect)
if tc.expectErr && err == nil {
t.Fatal("Expected an error but didn't get one")
}
if !tc.expectErr && err != nil {
t.Fatalf("Didn't expect an error but got one: %v", err)
}
if link != tc.expected {
t.Errorf("Expected link %q but got %q", tc.expected, link)
}
})
}
}
func TestProcessUrlsConcurrently(t *testing.T) {
urls := []string{
"https://example.com",
"https://example.org",
}
linkFormat := "markdown"
linksCh := processUrlsConcurrently(urls, linkFormat)
var result []string
for link := range linksCh {
result = append(result, link)
}
if len(result) != len(urls) {
t.Errorf("Expected %d links but got %d", len(urls), len(result))
}
for _, link := range result {
if !strings.HasPrefix(link, "[") || !strings.HasSuffix(link, ")") {
t.Errorf("Link %q does not have the expected markdown format", link)
}
}
}