Skip to content

Commit 30c3ab4

Browse files
jkmasselclaude
andcommitted
Add tests for applyAttribute/applyAttributes helpers with emoji
Verify that applyAttribute, applyAttributes, and applyForegroundColor correctly cover the full string length when the string contains multi-byte characters like emoji. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent eb54f14 commit 30c3ab4

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import Testing
2+
import UIKit
3+
4+
@testable import WordPressUI
5+
6+
struct NSMutableAttributedStringHelpersTests {
7+
8+
// MARK: - applyAttribute(_:value:)
9+
10+
@Test("applyAttribute covers the full string including emoji")
11+
func applyAttributeWithEmoji() {
12+
// πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦ is 1 grapheme cluster but 11 UTF-16 code units
13+
let string = NSMutableAttributedString(string: "Hello πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦ World")
14+
string.applyAttribute(.foregroundColor, value: UIColor.red)
15+
16+
var effectiveRange = NSRange()
17+
let value = string.attribute(.foregroundColor, at: 0, effectiveRange: &effectiveRange)
18+
19+
#expect(value != nil)
20+
#expect(effectiveRange.location == 0)
21+
#expect(effectiveRange.length == string.length)
22+
}
23+
24+
// MARK: - applyAttributes(_:)
25+
26+
@Test("applyAttributes covers the full string including emoji")
27+
func applyAttributesWithEmoji() {
28+
let string = NSMutableAttributedString(string: "πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦ γƒ†γ‚Ήγƒˆ")
29+
let attrs: [NSAttributedString.Key: Any] = [
30+
.foregroundColor: UIColor.blue,
31+
.font: UIFont.systemFont(ofSize: 14)
32+
]
33+
string.applyAttributes(attrs)
34+
35+
var colorRange = NSRange()
36+
let color = string.attribute(.foregroundColor, at: 0, effectiveRange: &colorRange)
37+
38+
var fontRange = NSRange()
39+
let font = string.attribute(.font, at: 0, effectiveRange: &fontRange)
40+
41+
#expect(color != nil)
42+
#expect(colorRange.length == string.length)
43+
#expect(font != nil)
44+
#expect(fontRange.length == string.length)
45+
}
46+
47+
// MARK: - applyForegroundColor(_:)
48+
49+
@Test("applyForegroundColor covers the full string including emoji")
50+
func applyForegroundColorWithEmoji() {
51+
let string = NSMutableAttributedString(string: "🌍 Hello 🌍")
52+
string.applyForegroundColor(.green)
53+
54+
var effectiveRange = NSRange()
55+
let value = string.attribute(.foregroundColor, at: 0, effectiveRange: &effectiveRange)
56+
57+
#expect(value as? UIColor == .green)
58+
#expect(effectiveRange.length == string.length)
59+
}
60+
}

0 commit comments

Comments
Β (0)