This repository was archived by the owner on Jun 7, 2020. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathNSAttributedStringExtensionsSpec.swift
More file actions
149 lines (121 loc) · 7.87 KB
/
Copy pathNSAttributedStringExtensionsSpec.swift
File metadata and controls
149 lines (121 loc) · 7.87 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//
// NSAttributedStringExtensionsSpec.swift
// Rocket.Chat
//
// Created by Matheus Cardoso on 9/11/17.
// Copyright © 2017 Rocket.Chat. All rights reserved.
//
import XCTest
@testable import Rocket_Chat
private func assert<T: Equatable>(_ attributedString: NSAttributedString,
has attribute: (NSAttributedString.Key, T),
in range: NSRange, _ message: String) {
attributedString.enumerateAttributes(in: range, options: [], using: { attributes, crange, _ in
XCTAssert(Range(range) == Range(crange) && attributes[attribute.0] as? T == attribute.1, message)
})
}
class NSAttributedStringExtensionsSpec: XCTestCase {
func testTrimCharacters() {
let attributedString = NSMutableAttributedString(string: " a ")
attributedString.trimCharacters(in: .whitespaces)
XCTAssert(attributedString.string == "a", "trimCharacters will trim white spaces")
}
func testSetFont() {
let attributedString = NSMutableAttributedString(string: "this > word < is italic")
guard let matchWordRange = attributedString.string.range(of: "word") else {
return XCTFail("This will never happen!")
}
let wordRange = NSRange(matchWordRange, in: attributedString.string)
let font = UIFont.italicSystemFont(ofSize: 12)
attributedString.setFont(font, range: wordRange)
assert(attributedString, has: (NSAttributedString.Key.font, font), in: wordRange, "setFont will work")
}
func testSetFontColor() {
let attributedString = NSMutableAttributedString(string: "this > word < is red")
guard let matchWordRange = attributedString.string.range(of: "word") else {
return XCTFail("This will never happen!")
}
let wordRange = NSRange(matchWordRange, in: attributedString.string)
let color = UIColor.red
attributedString.setFontColor(color, range: wordRange)
assert(attributedString, has: (NSAttributedString.Key.foregroundColor, color), in: wordRange, "setFontColor will work")
}
func testSetBackgroundColor() {
let attributedString = NSMutableAttributedString(string: "this > word < is red on the background")
guard let matchWordRange = attributedString.string.range(of: "word") else {
return XCTFail("This will never happen!")
}
let wordRange = NSRange(matchWordRange, in: attributedString.string)
let color = UIColor.red
attributedString.setBackgroundColor(color, range: wordRange)
assert(attributedString, has: (NSAttributedString.Key.backgroundColor, color), in: wordRange, "setBackgroundColor will work")
}
// swiftlint:disable function_body_length
func testMentionParsing() {
let string = "Hi @rafael.kellermann, how are you doing? " +
"Is everyone @here having a great day? How about you @filipe.alvarenga? Wish you @all a happy Christmas :) from @matheus.cardoso"
let result = NSMutableAttributedString(string: string)
result.highlightMentions([
UnmanagedMention(userId: nil, realName: nil, username: "rafael.kellermann"),
UnmanagedMention(userId: nil, realName: nil, username: "here"),
UnmanagedMention(userId: nil, realName: nil, username: "filipe.alvarenga"),
UnmanagedMention(userId: nil, realName: nil, username: "all"),
UnmanagedMention(userId: nil, realName: nil, username: "matheus.cardoso")
],
currentUsername: "matheus.cardoso"
)
XCTAssertEqual(
result.string,
"Hi \u{00a0}rafael.kellermann\u{00a0}, how are you doing? Is everyone \u{00a0}here\u{00a0} having a great day? How about you \u{00a0}filipe.alvarenga\u{00a0}? Wish you \u{00a0}all\u{00a0} a happy Christmas :) from \u{00a0}matheus.cardoso\u{00a0}",
"removes at symbols and adds spacing in mentions"
)
let nsString = NSString(string: result.string)
let range0 = nsString.range(of: "Hi ")
let attributes0 = result.attributes(at: range0.location, longestEffectiveRange: nil, in: range0)
XCTAssertEqual(attributes0.count, 0, "Will have no attributes")
let range1 = nsString.range(of: "\u{00a0}rafael.kellermann\u{00a0}")
let attributes1 = result.attributes(at: range1.location, longestEffectiveRange: nil, in: range1)
XCTAssertEqual(attributes1.count, 4, "Will have 4 attributes")
XCTAssertEqual(attributes1[.link] as? String, "rocketchat://mention?name=rafael.kellermann")
XCTAssertEqual(attributes1[.backgroundColor] as? UIColor, Theme.light.actionBackgroundColor)
XCTAssertEqual(attributes1[.foregroundColor] as? UIColor, Theme.light.actionTintColor)
XCTAssertEqual(attributes1[.font] as? UIFont, MessageTextFontAttributes.boldFont)
let range2 = nsString.range(of: ", how are you doing? Is everyone ")
let attributes2 = result.attributes(at: range2.location, longestEffectiveRange: nil, in: range2)
XCTAssertEqual(attributes2.count, 0, "Will have no attributes")
let range3 = nsString.range(of: "\u{00a0}here\u{00a0}")
let attributes3 = result.attributes(at: range3.location, longestEffectiveRange: nil, in: range3)
XCTAssertEqual(attributes3.count, 3, "Will have 3 attributes")
XCTAssertEqual(attributes3[.backgroundColor] as? UIColor, .attention)
XCTAssertEqual(attributes3[.foregroundColor] as? UIColor, .white)
XCTAssertEqual(attributes3[.font] as? UIFont, MessageTextFontAttributes.boldFont)
let range4 = nsString.range(of: " having a great day? How about you ")
let attributes4 = result.attributes(at: range4.location, longestEffectiveRange: nil, in: range4)
XCTAssertEqual(attributes4.count, 0, "Will have no attributes")
let range5 = nsString.range(of: "\u{00a0}filipe.alvarenga\u{00a0}")
let attributes5 = result.attributes(at: range5.location, longestEffectiveRange: nil, in: range5)
XCTAssertEqual(attributes5.count, 4, "Will have 4 attributes")
XCTAssertEqual(attributes5[.link] as? String, "rocketchat://mention?name=filipe.alvarenga")
XCTAssertEqual(attributes5[.backgroundColor] as? UIColor, Theme.light.actionBackgroundColor)
XCTAssertEqual(attributes5[.foregroundColor] as? UIColor, Theme.light.actionTintColor)
XCTAssertEqual(attributes5[.font] as? UIFont, MessageTextFontAttributes.boldFont)
let range6 = nsString.range(of: "? Wish you ")
let attributes6 = result.attributes(at: range6.location, longestEffectiveRange: nil, in: range6)
XCTAssertEqual(attributes6.count, 0, "Will have no attributes")
let range7 = nsString.range(of: "\u{00a0}all\u{00a0}")
let attributes7 = result.attributes(at: range7.location, longestEffectiveRange: nil, in: range7)
XCTAssertEqual(attributes7.count, 3, "Will have 3 attributes")
XCTAssertEqual(attributes7[.backgroundColor] as? UIColor, .attention)
XCTAssertEqual(attributes7[.foregroundColor] as? UIColor, .white)
XCTAssertEqual(attributes7[.font] as? UIFont, MessageTextFontAttributes.boldFont)
let range8 = nsString.range(of: " a happy Christmas :) from ")
let attributes8 = result.attributes(at: range8.location, longestEffectiveRange: nil, in: range8)
XCTAssertEqual(attributes8.count, 0, "Will have no attributes")
let range9 = nsString.range(of: "\u{00a0}matheus.cardoso\u{00a0}")
let attributes9 = result.attributes(at: range9.location, longestEffectiveRange: nil, in: range9)
XCTAssertEqual(attributes9.count, 3, "Will have 3 attributes")
XCTAssertEqual(attributes9[.backgroundColor] as? UIColor, Theme.light.actionTintColor)
XCTAssertEqual(attributes9[.foregroundColor] as? UIColor, .white)
XCTAssertEqual(attributes9[.font] as? UIFont, MessageTextFontAttributes.boldFont)
}
}