This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1543 from OpenBazaar/brian.fix-emojis
Use go-emoji to convert to html chars
- Loading branch information
Showing
2 changed files
with
140 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package core_test | ||
|
||
import ( | ||
"regexp" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/OpenBazaar/openbazaar-go/core" | ||
) | ||
|
||
func TestEmojiToHTML(t *testing.T) { | ||
var ( | ||
expected string | ||
container = make(map[string]string) | ||
rx = regexp.MustCompile(core.EmojiPattern) | ||
text = "a #💩 #and #🍦 #😳" | ||
i = -1 | ||
replaced = rx.ReplaceAllStringFunc(text, func(s string) string { | ||
i++ | ||
key := "_$" + strconv.Itoa(i) + "_" | ||
container[key] = s | ||
return key | ||
}) | ||
) | ||
|
||
expected = "a #_$0_ #and #_$1_ #_$2_" | ||
if replaced != expected { | ||
t.Errorf("expected processed string to be %s, but was %s", expected, replaced) | ||
} | ||
|
||
htmlEnt := core.ToHtmlEntities(text) | ||
|
||
expected = "a #💩 #and #🍦 #😳" | ||
if htmlEnt != expected { | ||
t.Errorf("expected processed string to be %s, but was %s", expected, replaced) | ||
} | ||
|
||
recovered := regexp.MustCompile(`\_\$\d+\_`).ReplaceAllStringFunc(replaced, func(s string) string { | ||
return container[s] | ||
}) | ||
if recovered != text { | ||
t.Errorf("expected processed string to be %s, but was %s", text, recovered) | ||
} | ||
} | ||
|
||
func TestToHtmlEntities(t *testing.T) { | ||
tests := []struct { | ||
slug string | ||
expected string | ||
}{ | ||
{ | ||
"This_listing_is_💩💩", | ||
"This_listing_is_💩💩", | ||
}, | ||
{ | ||
"slug-with$-no_#emojis", | ||
"slug-with$-no_#emojis", | ||
}, | ||
} | ||
|
||
for i, test := range tests { | ||
transformed := core.ToHtmlEntities(test.slug) | ||
if transformed != test.expected { | ||
t.Errorf("Test %d failed. Expected %s got %s", i, test.expected, transformed) | ||
} | ||
} | ||
} |