-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25b41a8
commit 17e8524
Showing
3 changed files
with
95 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,55 @@ | ||
package discourseemoji | ||
|
||
import ( | ||
"net/url" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/matryer/is" | ||
) | ||
|
||
func TestReplaceHTMLImagesWithEmojis(t *testing.T) { | ||
func urlMustParse(s string) *url.URL { | ||
u, err := url.Parse(s) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return u | ||
} | ||
|
||
func TestUnmungeCookedHTML(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
expected string | ||
name string | ||
inputBaseURL *url.URL | ||
inputSource string | ||
expected string | ||
}{ | ||
{ | ||
"upside_down_face", | ||
nil, | ||
`<img src="https://emoji.discourse-cdn.com/twitter/upside_down_face.png?v=12" title=":upside_down_face:" class="emoji" alt=":upside_down_face:" loading="lazy" width="20" height="20">`, | ||
"🙃", | ||
}, | ||
{ | ||
"only-emoji end", | ||
nil, | ||
`<img src="https://emoji.discourse-cdn.com/twitter/upside_down_face.png?v=12" title=":upside_down_face:" class="emoji only-emoji" alt=":upside_down_face:" loading="lazy" width="20" height="20">`, | ||
"🙃", | ||
}, | ||
{ | ||
"only-emoji start", | ||
nil, | ||
`<img src="https://emoji.discourse-cdn.com/twitter/upside_down_face.png?v=12" title=":upside_down_face:" class="only-emoji emoji" alt=":upside_down_face:" loading="lazy" width="20" height="20">`, | ||
"🙃", | ||
}, | ||
{ | ||
"only-only-emoji", | ||
nil, | ||
`<img src="https://emoji.discourse-cdn.com/twitter/upside_down_face.png?v=12" title=":upside_down_face:" class="only-emoji" alt=":upside_down_face:" loading="lazy" width="20" height="20">`, | ||
`<img src="https://emoji.discourse-cdn.com/twitter/upside_down_face.png?v=12" title=":upside_down_face:" class="only-emoji" alt=":upside_down_face:" loading="lazy" width="20" height="20"/>`, | ||
}, | ||
{ | ||
"two paragraphs", | ||
nil, | ||
strings.TrimSpace(` | ||
<p> | ||
<img src="https://emoji.discourse-cdn.com/twitter/upside_down_face.png?v=12" title=":upside_down_face:" class="emoji" alt=":upside_down_face:" loading="lazy" width="20" height="20"> | ||
|
@@ -76,6 +91,7 @@ func TestReplaceHTMLImagesWithEmojis(t *testing.T) { | |
}, | ||
{ | ||
"unrecognized", | ||
nil, | ||
strings.TrimSpace(` | ||
<p> | ||
<img src="https://emoji.discourse-cdn.com/twitter/upside_down_face.png?v=12" title=":upside_down_face:" class="emoji" alt=":upside_down_face:" loading="lazy" width="20" height="20"> | ||
|
@@ -91,13 +107,71 @@ func TestReplaceHTMLImagesWithEmojis(t *testing.T) { | |
</p> | ||
`), | ||
}, | ||
{ | ||
"urls", | ||
nil, | ||
strings.TrimSpace(` | ||
<p> | ||
<img src="https://emoji.discourse-cdn.com/twitter/upside_down_face.png?v=12" title=":upside_down_face:" class="emoji" alt=":upside_down_face:" loading="lazy" width="20" height="20"> | ||
<img src="https://emoji.discourse-cdn.com/blah/raised_back_of_hand/2.png?v=12" title=":blah:t2:" class="emoji" alt=":blah:t2:" loading="lazy" width="20" height="20"> | ||
<img src="https://emoji.discourse-cdn.com/apple/cold_face.png?v=12" title=":cold_face:" class="emoji" alt=":cold_face:" loading="lazy" width="20" height="20"> | ||
</p> | ||
`), | ||
strings.TrimSpace(` | ||
<p> | ||
🙃 | ||
<img src="https://emoji.discourse-cdn.com/blah/raised_back_of_hand/2.png?v=12" title=":blah:t2:" class="emoji" alt=":blah:t2:" loading="lazy" width="20" height="20"/> | ||
🥶 | ||
</p> | ||
`), | ||
}, | ||
{ | ||
"preserves links without a base URL", | ||
nil, | ||
strings.TrimSpace(` | ||
<a href="https://example.com">example</a> | ||
<a href="/relative">relative</a> | ||
<a href="anchor">anchor</a> | ||
<a href="mailto:[email protected]">email</a> | ||
<a href="?query">query</a> | ||
<a href="#fragment">fragment</a> | ||
`), | ||
strings.TrimSpace(` | ||
<a href="https://example.com">example</a> | ||
<a href="/relative">relative</a> | ||
<a href="anchor">anchor</a> | ||
<a href="mailto:[email protected]">email</a> | ||
<a href="?query">query</a> | ||
<a href="#fragment">fragment</a> | ||
`), | ||
}, | ||
{ | ||
"updates links correctly with a base URL", | ||
urlMustParse("https://example.invalid/testing/foo"), | ||
strings.TrimSpace(` | ||
<a href="https://example.com">example</a> | ||
<a href="/relative">relative</a> | ||
<a href="anchor">anchor</a> | ||
<a href="mailto:[email protected]">email</a> | ||
<a href="?query">query</a> | ||
<a href="#fragment">fragment</a> | ||
`), | ||
strings.TrimSpace(` | ||
<a href="https://example.com">example</a> | ||
<a href="https://example.invalid/relative">relative</a> | ||
<a href="https://example.invalid/testing/anchor">anchor</a> | ||
<a href="mailto:[email protected]">email</a> | ||
<a href="https://example.invalid/testing/foo?query">query</a> | ||
<a href="https://example.invalid/testing/foo#fragment">fragment</a> | ||
`), | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
is := is.New(t) | ||
|
||
res, err := ReplaceHTMLImagesWithEmojis(test.input) | ||
res, err := UnmungeCookedHTML(test.inputSource, test.inputBaseURL) | ||
|
||
is.NoErr(err) | ||
is.Equal(res, test.expected) | ||
|