forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASMutableAttributedStringBuilderTests.mm
78 lines (66 loc) · 3.61 KB
/
ASMutableAttributedStringBuilderTests.mm
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
//
// ASMutableAttributedStringBuilderTests.mm
// Texture
//
// Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
// Changes after 4/13/2017 are: Copyright (c) Pinterest, Inc. All rights reserved.
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
//
#import <XCTest/XCTest.h>
#import <AsyncDisplayKit/ASMutableAttributedStringBuilder.h>
@interface ASMutableAttributedStringBuilderTests : XCTestCase
@end
@implementation ASMutableAttributedStringBuilderTests
- (NSString *)_string
{
return @"Normcore PBR hella, viral slow-carb mustache chillwave church-key cornhole messenger bag swag vinyl biodiesel ethnic. Fashion axe messenger bag raw denim street art. Flannel Wes Anderson normcore church-key 8-bit. Master cleanse four loko try-hard Carles stumptown ennui, twee literally wayfarers kitsch tofu PBR. Cliche organic post-ironic Wes Anderson kale chips fashion axe. Narwhal Blue Bottle sustainable, Odd Future Godard sriracha banjo disrupt Marfa irony pug Wes Anderson YOLO yr church-key. Mlkshk Intelligentsia semiotics quinoa, butcher meggings wolf Bushwick keffiyeh ethnic pour-over Pinterest letterpress.";
}
- (ASMutableAttributedStringBuilder *)_builder
{
return [[ASMutableAttributedStringBuilder alloc] initWithString:[self _string]];
}
- (NSRange)_randomizedRangeForStringBuilder:(ASMutableAttributedStringBuilder *)builder
{
NSUInteger loc = arc4random() % (builder.length - 1);
NSUInteger len = arc4random() % (builder.length - loc);
len = ((len > 0) ? len : 1);
return NSMakeRange(loc, len);
}
- (void)testSimpleAttributions
{
// Add a attributes, and verify that they get set on the correct locations.
for (int i = 0; i < 100; i++) {
ASMutableAttributedStringBuilder *builder = [self _builder];
NSRange range = [self _randomizedRangeForStringBuilder:builder];
NSString *keyValue = [NSString stringWithFormat:@"%d", i];
[builder addAttribute:keyValue value:keyValue range:range];
NSAttributedString *attrStr = [builder composedAttributedString];
XCTAssertEqual(builder.length, attrStr.length, @"out string should have same length as builder");
__block BOOL found = NO;
[attrStr enumerateAttributesInRange:NSMakeRange(0, attrStr.length) options:0 usingBlock:^(NSDictionary *attrs, NSRange r, BOOL *stop) {
if ([attrs[keyValue] isEqualToString:keyValue]) {
XCTAssertTrue(NSEqualRanges(range, r), @"enumerated range %@ should be equal to the set range %@", NSStringFromRange(r), NSStringFromRange(range));
found = YES;
}
}];
XCTAssertTrue(found, @"enumeration should have found the attribute we set");
}
}
- (void)testSetOverAdd
{
ASMutableAttributedStringBuilder *builder = [self _builder];
NSRange addRange = NSMakeRange(0, builder.length);
NSRange setRange = NSMakeRange(0, 1);
[builder addAttribute:@"attr" value:@"val1" range:addRange];
[builder setAttributes:@{@"attr" : @"val2"} range:setRange];
NSAttributedString *attrStr = [builder composedAttributedString];
NSRange setRangeOut;
NSString *setAttr = [attrStr attribute:@"attr" atIndex:0 effectiveRange:&setRangeOut];
XCTAssertTrue(NSEqualRanges(setRange, setRangeOut), @"The out set range should equal the range we used originally");
XCTAssertEqualObjects(setAttr, @"val2", @"the set value should be val2");
NSRange addRangeOut;
NSString *addAttr = [attrStr attribute:@"attr" atIndex:2 effectiveRange:&addRangeOut];
XCTAssertTrue(NSEqualRanges(NSMakeRange(1, builder.length - 1), addRangeOut), @"the add range should only cover beyond the set range");
XCTAssertEqualObjects(addAttr, @"val1", @"the added attribute should be present at index 2");
}
@end