forked from writefreely/writefreely
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathposts_test.go
More file actions
93 lines (85 loc) · 2.43 KB
/
Copy pathposts_test.go
File metadata and controls
93 lines (85 loc) · 2.43 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
/*
* Copyright © 2020-2021 Musing Studio LLC.
*
* This file is part of WriteFreely.
*
* WriteFreely is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, included
* in the LICENSE file in this source code package.
*/
package writefreely
import (
"testing"
"github.com/guregu/null/zero"
"github.com/stretchr/testify/assert"
)
func TestPostSummary(t *testing.T) {
testCases := map[string]struct {
given Post
expected string
}{
"no special chars": {givenPost("Content."), "Content."},
"HTML content": {givenPost("Content <p>with a</p> paragraph."), "Content with a paragraph."},
"content with escaped char": {givenPost("Content's all OK."), "Content's all OK."},
"multiline content": {givenPost(`Content
in
multiple
lines.`), "Content in multiple lines."},
}
for name, test := range testCases {
t.Run(name, func(t *testing.T) {
actual := test.given.Summary()
assert.Equal(t, test.expected, actual)
})
}
}
func givenPost(content string) Post {
return Post{Title: zero.StringFrom("Title"), Content: content}
}
func TestExtractImageAltText(t *testing.T) {
testCases := map[string]struct {
content string
expected map[string]string
}{
"basic image": {
"",
map[string]string{"https://example.com/cat.png": "a cat"},
},
"image with title": {
``,
map[string]string{"https://example.com/cat.png": "a cat"},
},
"empty alt text is omitted": {
"",
map[string]string{},
},
"whitespace-only alt text is omitted": {
"",
map[string]string{},
},
"alt text is trimmed": {
"",
map[string]string{"https://example.com/cat.png": "a cat"},
},
"multiple images": {
" and ",
map[string]string{
"https://example.com/cat.png": "cat",
"https://example.com/dog.jpg": "dog",
},
},
"bare url has no alt text": {
"See https://example.com/cat.png",
map[string]string{},
},
"non-image markdown link ignored": {
"[a link](https://example.com/page)",
map[string]string{},
},
}
for name, test := range testCases {
t.Run(name, func(t *testing.T) {
assert.Equal(t, test.expected, extractImageAltText(test.content))
})
}
}