Fixed: CPAttributedString isEqual: despite attribute differences.

As soon as a range reached the end of the string the equality test would end with a YES, without even comparing the final attribute dict.
This commit is contained in:
Alexander Ljungberg
2014-02-13 12:41:14 +00:00
parent 7e20422e20
commit a31ad2c367
2 changed files with 34 additions and 2 deletions
+2 -2
View File
@@ -413,7 +413,7 @@
comparisonAttributes = [aString attributesAtIndex:0 effectiveRange:comparisonRange],
length = _string.length;
while (CPMaxRange(CPUnionRange(myRange, comparisonRange)) < length)
do
{
if (CPIntersectionRange(myRange, comparisonRange).length > 0 &&
![myAttributes isEqualToDictionary:comparisonAttributes])
@@ -424,7 +424,7 @@
myAttributes = [self attributesAtIndex:CPMaxRange(myRange) effectiveRange:myRange];
else
comparisonAttributes = [aString attributesAtIndex:CPMaxRange(comparisonRange) effectiveRange:comparisonRange];
}
} while (CPMaxRange(CPUnionRange(myRange, comparisonRange)) < length);
return YES;
}
+32
View File
@@ -207,6 +207,38 @@ var sharedObject = [CPObject new];
[self assertFalse:[a isEqual:@"HELLO!"] message:"Expected a to not equal 'HELLO!', but it did"];
}
- (void)testIsEqualEmpty
{
var a = [[CPMutableAttributedString alloc] initWithString:@""],
b = [[CPMutableAttributedString alloc] initWithString:@""];
[self assertTrue:[a isEqual:b]];
}
- (void)testIsEqualWithSimpleAttribute
{
var a = [[CPMutableAttributedString alloc] initWithString:@"GREETINGS!"],
b = [[CPMutableAttributedString alloc] initWithString:@"GREETINGS!"];
[a addAttribute:"color" value:[CPColor redColor] range:CPMakeRange(0, 5)];
[self assertFalse:[a isEqual:b] message:"red string should not equal string without color attribute"];
[b addAttribute:"color" value:[CPColor redColor] range:CPMakeRange(0, 5)];
[self assertTrue:[a isEqual:b]];
}
- (void)testIsEqualWithTwoAttributes
{
var a = [[CPMutableAttributedString alloc] initWithString:@"GREETINGS!"],
b = [[CPMutableAttributedString alloc] initWithString:@"GREETINGS!"];
[a addAttribute:"color" value:[CPColor redColor] range:CPMakeRange(0, 9)];
[b addAttribute:"color" value:[CPColor redColor] range:CPMakeRange(0, 9)];
[a addAttribute:"font" value:"Helvetica" range:CPMakeRange(1, 4)];
[self assertFalse:[a isEqual:b] message:"font difference from index 1 should be found"];
[b addAttribute:"font" value:"Helvetica" range:CPMakeRange(1, 4)];
[self assertTrue:[a isEqual:b]];
}
//Extracting a Substring
//- (CPAttributedString)attributedSubstringFromRange:(CPRange)aRange
- (void)testAttributedSubstringFromRange