mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-06-16 05:55:20 +00:00

git-subtree-dir: submodules/AsyncDisplayKit git-subtree-mainline: d06f423e0ed3df1fed9bd10d79ee312a9179b632 git-subtree-split: 02bedc12816e251ad71777f9d2578329b6d2bef6
79 lines
3.6 KiB
Plaintext
79 lines
3.6 KiB
Plaintext
//
|
|
// 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
|