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
import Foundation
import XCTest
@testable import OldFella
final class ComposerTextTests: XCTestCase {
func testCandidatesUseUTF8OffsetsAfterUnicode() throws {
let text = "🌍 café @alice.example #日本語 https://example.com"
let candidates = ComposerText.facetCandidates(in: text)
XCTAssertEqual(candidates.map(\.kind), [.mention, .tag, .link])
XCTAssertEqual(candidates.map(\.value), ["alice.example", "日本語", "https://example.com"])
let expected = ["@alice.example", "#日本語", "https://example.com"]
for (candidate, substring) in zip(candidates, expected) {
let range = try XCTUnwrap(text.range(of: substring))
XCTAssertEqual(candidate.byteStart, text[..<range.lowerBound].utf8.count)
XCTAssertEqual(candidate.byteEnd, text[..<range.upperBound].utf8.count)
XCTAssertEqual(
String(bytes: Array(text.utf8)[candidate.byteStart..<candidate.byteEnd], encoding: .utf8), substring)
}
}
func testURLSuppressesOverlappingMentionAndTag() {
let text = "https://example.com/@alice.example#swift #outside"
let candidates = ComposerText.facetCandidates(in: text)
XCTAssertEqual(candidates.map(\.kind), [.link, .tag])
XCTAssertEqual(candidates.map(\.value), ["https://example.com/@alice.example#swift", "outside"])
}
func testURLExcludesTrailingPunctuation() {
let text = "See (https://example.com/path), https://other.example/?!"
let candidates = ComposerText.facetCandidates(in: text)
XCTAssertEqual(candidates.map(\.value), ["https://example.com/path", "https://other.example/"])
for candidate in candidates {
XCTAssertEqual(candidate.byteEnd - candidate.byteStart, candidate.value.utf8.count)
}
}
func testEmailAndPartialHandleDoNotBecomeMentions() {
let text = "alice@example.com @partial @full.example"
let candidates = ComposerText.facetCandidates(in: text)
XCTAssertEqual(candidates.map(\.kind), [.mention])
XCTAssertEqual(candidates.map(\.value), ["full.example"])
}
func testRepeatedCandidatesKeepTheirOwnOffsets() {
let text = "#swift #swift"
let candidates = ComposerText.facetCandidates(in: text)
XCTAssertEqual(candidates.map(\.byteStart), [0, 7])
XCTAssertEqual(candidates.map(\.byteEnd), [6, 13])
}
}