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
import Foundation
import XCTest
@testable import OldFella
final class ComposerEmojiTests: XCTestCase {
func testShortcodeAtCaretReplacesWholeToken() throws {
let text = "Look :eyes: here"
let token = try XCTUnwrap(ComposerEmoji.active(
in: text, selection: NSRange(location: "Look :ey".utf16.count, length: 0)))
XCTAssertEqual(token.query, "ey")
XCTAssertEqual(token.range, NSRange(location: 5, length: 6))
let result = try XCTUnwrap(token.inserting(emoji: "👀", in: text))
XCTAssertEqual(result.text, "Look 👀 here")
XCTAssertEqual(result.selection, NSRange(location: "Look 👀".utf16.count, length: 0))
}
func testOpenAndClosedShortcodes() throws {
for text in [":eyes", ":eyes:"] {
let token = try XCTUnwrap(ComposerEmoji.active(
in: text, selection: NSRange(location: text.utf16.count, length: 0)))
XCTAssertEqual(token.query, "eyes")
XCTAssertEqual(token.inserting(emoji: "👀", in: text)?.text, "👀 ")
}
}
func testUnicodePrefixAndMultiScalarEmojiKeepCaretAfterInsertion() throws {
let text = "🌤️ café :heart"
let token = try XCTUnwrap(ComposerEmoji.active(
in: text, selection: NSRange(location: text.utf16.count, length: 0)))
let result = try XCTUnwrap(token.inserting(emoji: "❤️", in: text))
XCTAssertEqual(result.text, "🌤️ café ❤️ ")
XCTAssertEqual(result.selection, NSRange(location: result.text.utf16.count, length: 0))
}
func testInsertionPreservesPunctuationAndWhitespace() throws {
let text = "Hello (:wave), friend"
let token = try XCTUnwrap(ComposerEmoji.active(
in: text, selection: NSRange(location: "Hello (:wave".utf16.count, length: 0)))
XCTAssertEqual(token.inserting(emoji: "👋", in: text)?.text, "Hello (👋), friend")
}
func testSecondTokenAfterNewline() throws {
let text = ":wave\n:rocket"
let token = try XCTUnwrap(ComposerEmoji.active(
in: text, selection: NSRange(location: text.utf16.count, length: 0)))
XCTAssertEqual(token.query, "rocket")
XCTAssertEqual(token.inserting(emoji: "🚀", in: text)?.text, ":wave\n🚀 ")
}
func testNumericAndUnderscoreAliases() throws {
for query in ["+1", "-1", "100", "heart_eyes", "thinking_face", "heart-eyes"] {
let text = ":" + query
let token = try XCTUnwrap(ComposerEmoji.active(
in: text, selection: NSRange(location: text.utf16.count, length: 0)))
XCTAssertEqual(token.query, query)
}
}
func testDoesNotSuggestForLinksTimesEmoticonsOrWordColons() {
for text in [
"https://example.com/:eyes", "https://example.com?q=:eyes", "https://:eyes",
"mailto:eyes", "12:30", "word:eyes", "café:eyes", "@friend:eyes", "a/:eyes",
":", ":)", ":-)", ": eyes", ":eyes ", ":eyes\n"
] {
XCTAssertNil(ComposerEmoji.active(
in: text, selection: NSRange(location: text.utf16.count, length: 0)), text)
}
}
func testSelectionAndInvalidRangesDoNotSuggestOrReplace() {
for selection in [
NSRange(location: 2, length: 1), NSRange(location: 0, length: 0),
NSRange(location: 1, length: 0), NSRange(location: 99, length: 0),
NSRange(location: NSNotFound, length: 0)
] {
XCTAssertNil(ComposerEmoji.active(in: ":eyes", selection: selection))
}
let token = ComposerEmoji(range: NSRange(location: 99, length: 3), query: "eyes")
XCTAssertNil(token.inserting(emoji: "👀", in: ":eyes"))
}
func testExactShortcodeRanksBeforeRelatedNames() {
let suggestions = EmojiCatalog.suggestions(for: "eyes")
XCTAssertEqual(suggestions.first?.emoji, "👀")
XCTAssertEqual(suggestions.first?.shortcode, "eyes")
XCTAssertTrue(suggestions.dropFirst().contains { $0.shortcode.contains("eyes") })
XCTAssertEqual(EmojiCatalog.suggestions(for: "fire").first?.emoji, "🔥")
}
func testPrefixRanksBeforeWordMatches() throws {
let suggestions = EmojiCatalog.suggestions(for: "eye", limit: 100)
let eyes = try XCTUnwrap(suggestions.firstIndex { $0.emoji == "👀" })
let heartEyes = try XCTUnwrap(suggestions.firstIndex { $0.emoji == "😍" })
XCTAssertLessThan(eyes, heartEyes)
XCTAssertEqual(suggestions.first?.shortcode, "eye")
}
func testCommonAliasesAndCaseInsensitiveQueries() {
for (query, emoji) in [
("+1", "👍"), ("thumbsup", "👍"), ("thumbs_up", "👍"), ("-1", "👎"),
("thumbsdown", "👎"), ("joy", "😂"), ("tada", "🎉"), ("thinking_face", "🤔"),
("thinking", "🤔"), ("heart", "❤️"), ("smile", "😄"), ("laughing", "😆"),
("sob", "😭"), ("EYES", "👀"), ("heart-eyes", "😍"), ("coffee", "☕️")
] {
XCTAssertEqual(EmojiCatalog.suggestions(for: query).first?.emoji, emoji, query)
}
}
func testCatalogCoversNamesBeyondCommonAliases() {
for (query, emoji) in [
("butterfly", "🦋"), ("avocado", "🥑"), ("guitar", "🎸"), ("switzerland", "🇨🇭")
] {
XCTAssertEqual(EmojiCatalog.suggestions(for: query).first?.emoji, emoji, query)
}
}
func testLimitsUnknownQueriesAndDeduplicatedResults() {
XCTAssertTrue(EmojiCatalog.suggestions(for: "").isEmpty)
XCTAssertTrue(EmojiCatalog.suggestions(for: "not_an_emoji_shortcode").isEmpty)
XCTAssertTrue(EmojiCatalog.suggestions(for: "eyes", limit: 0).isEmpty)
XCTAssertTrue(EmojiCatalog.suggestions(for: "eyes", limit: -1).isEmpty)
XCTAssertEqual(EmojiCatalog.suggestions(for: "face", limit: 3).count, 3)
let suggestions = EmojiCatalog.suggestions(for: "thumb", limit: 100)
XCTAssertEqual(suggestions.filter { $0.emoji == "👍" }.count, 1)
XCTAssertEqual(Set(suggestions.map(\.id)).count, suggestions.count)
XCTAssertEqual(suggestions, EmojiCatalog.suggestions(for: "thumb", limit: 100))
}
}