From f75955bb04852b50d6e1fc7dd578f3151f0e3111 Mon Sep 17 00:00:00 2001 From: Antoine Mercadal Date: Wed, 16 Oct 2013 16:07:00 -0700 Subject: [PATCH] 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 --- Foundation/CPNumberFormatter.j | 7 ++++-- Tests/Foundation/CPNumberFormatterTest.j | 30 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/Foundation/CPNumberFormatter.j b/Foundation/CPNumberFormatter.j index 8b485ef13..a9823cf97 100644 --- a/Foundation/CPNumberFormatter.j +++ b/Foundation/CPNumberFormatter.j @@ -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; diff --git a/Tests/Foundation/CPNumberFormatterTest.j b/Tests/Foundation/CPNumberFormatterTest.j index 5f474ed5d..81d994824 100644 --- a/Tests/Foundation/CPNumberFormatterTest.j +++ b/Tests/Foundation/CPNumberFormatterTest.j @@ -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