Fixed: CPNumberFormatter minimum and maximum wrong encoding when set to nil

Previously, a decoded CPNumberFormatter was converting the minimum and maximum values to 0 if they were set to nil. This was causing unexpected behavior when creating a CPNumberFormatter from a xib. This patch actually decodes _minimum and _maximum values as objects to preserve nil.

Test added in Foundation/CPNumberFormatter.j
This commit is contained in:
Antoine Mercadal committed 2013-10-16 16:07:00 -07:00
1 parent 2f18218ce5
commit f75955bb04
2 files changed
+35 -2

No files matched your search

+5 -2
View File
@@ -309,13 +309,16 @@ var CPNumberFormatterStyleKey = @"CPNumberFormatterStyleKey",
_numberStyle = [aCoder decodeIntForKey:CPNumberFormatterStyleKey];
_minimumFractionDigits = [aCoder decodeIntForKey:CPNumberFormatterMinimumFractionDigitsKey];
_maximumFractionDigits = [aCoder decodeIntForKey:CPNumberFormatterMaximumFractionDigitsKey];
_minimum = [aCoder decodeIntForKey:CPNumberFormatterMinimumKey];
_maximum = [aCoder decodeIntForKey:CPNumberFormatterMaximumKey];
_roundingMode = [aCoder decodeIntForKey:CPNumberFormatterRoundingModeKey];
_groupingSeparator = [aCoder decodeObjectForKey:CPNumberFormatterGroupingSeparatorKey];
_currencyCode = [aCoder decodeObjectForKey:CPNumberFormatterCurrencyCodeKey];
_currencySymbol = [aCoder decodeObjectForKey:CPNumberFormatterCurrencySymbolKey];
_generatesDecimalNumbers = [aCoder decodeBoolForKey:CPNumberFormatterGeneratesDecimalNumbers];
// We decode _minimum and _maximum as object here because otherwise, nil values are not preserved
// causing a min and max always set to 0 after an decoding.
_minimum = [aCoder decodeObjectForKey:CPNumberFormatterMinimumKey];
_maximum = [aCoder decodeObjectForKey:CPNumberFormatterMaximumKey];
}
return self;
+30
View File
@@ -251,4 +251,34 @@
[self assert:1.23456 equals:generated];
}
- (void)testEncodingDecodingWithZeroMinMaxValues
{
var numberFormatter = [CPNumberFormatter new];
[numberFormatter setMaximum:0];
[numberFormatter setMinimum:0];
var encoded = [CPKeyedArchiver archivedDataWithRootObject:numberFormatter],
decoded = [CPKeyedUnarchiver unarchiveObjectWithData:encoded];
[self assertNotNull:[decoded minimum]];
[self assertNotNull:[decoded maximum]];
[self assert:0 equals:[decoded minimum]];
[self assert:0 equals:[decoded maximum]];
}
- (void)testEncodingDecodingWithNilMinMaxValues
{
var numberFormatter = [CPNumberFormatter new];
[numberFormatter setMaximum:nil];
[numberFormatter setMinimum:nil];
var encoded = [CPKeyedArchiver archivedDataWithRootObject:numberFormatter],
decoded = [CPKeyedUnarchiver unarchiveObjectWithData:encoded];
[self assertNull:[decoded minimum]];
[self assertNull:[decoded maximum]];
}
@end