|
| 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