Created
May 29, 2011 08:43
-
-
Save ma11hew28/997581 to your computer and use it in GitHub Desktop.
Kiwi specs for NSDecimalNumber+Additions (iPhone & iPad)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface NSDecimalNumber (Additions) | |
+ (NSDecimalNumber *)decimalNumberWithString:(NSString *)string scale:(short)scale; | |
- (NSDecimalNumber *)decimalNumberByRoundingByScale:(short)scale; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "NSDecimalNumber+Additions.h" | |
@implementation NSDecimalNumber (Additions) | |
+ (NSDecimalNumber *)decimalNumberWithString:(NSString *)numberValue scale:(short)scale { | |
NSDecimalNumber *decimal = [[self alloc] initWithString:numberValue]; | |
NSDecimalNumber *decimalRounded = [decimal decimalNumberByRoundingByScale:scale]; | |
[decimal release]; | |
return decimalRounded; | |
} | |
- (NSDecimalNumber *)decimalNumberByRoundingByScale:(short)scale { | |
if (!self) return nil; | |
NSDecimal result = [self decimalValue]; | |
NSDecimalRound(&result, &result, scale, NSRoundPlain); | |
return [NSDecimalNumber decimalNumberWithDecimal:result]; | |
} | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "NSDecimalNumber+Additions.h" | |
#import "Kiwi.h" | |
SPEC_BEGIN(NSDecimalNumber_AdditionsSpec) | |
describe(@"NSDecimalNumber+Additions", ^{ | |
describe(@"+decimalNumberWithString:scale:", ^{ | |
it(@"calls +alloc, -initWithString and then -decimalNumberByRoundingByScale:", ^{ | |
id stringMock = [NSString mock]; | |
short scale = 3; | |
// +alloc | |
id decimalAlloced = [NSDecimalNumber mock]; | |
[NSDecimalNumber stub:@selector(alloc) andReturn:decimalAlloced]; | |
// -initWithString | |
id decimalFromString = [NSDecimalNumber mock]; | |
[[decimalAlloced should] receive:@selector(initWithString:) | |
andReturn:decimalFromString withArguments:stringMock]; | |
// -decimalNumberByRoundingByScale: | |
id decimalRounded = [NSDecimalNumber mock]; | |
[[decimalFromString should] receive:@selector(decimalNumberByRoundingByScale:) | |
andReturn:decimalRounded withArguments:theValue(scale)]; | |
[[[NSDecimalNumber decimalNumberWithString:stringMock scale:scale] should] | |
equal:decimalRounded]; | |
}); | |
it(@"returns NaN for non-numeric strings", ^{ | |
NSArray *nonNumericValues = [[NSArray alloc] initWithObjects: | |
[NSNull null], | |
@"", | |
@",", | |
@"$123", | |
// @".", // 0 | |
// @"123f", // 123 | |
// @"456w", // 456 | |
// @"123e2", // 12300 | |
// @"-1", // -1 | |
// @"+1", // 1 | |
nil]; | |
for(NSString *nonNumericValue in nonNumericValues) { | |
nonNumericValue = [nonNumericValue isEqual:[NSNull null]] ? nil : nonNumericValue; | |
[[[NSDecimalNumber decimalNumberWithString:nonNumericValue scale:2] should] | |
equal:[NSDecimalNumber notANumber]]; | |
} | |
[nonNumericValues release]; | |
}); | |
}); | |
describe(@"-decimalNumberByRoundingByScale:", ^{ | |
it(@"rounds to 2 decimal places", ^{ | |
NSDictionary *numberValues = [[NSDictionary alloc] initWithObjectsAndKeys: | |
@"0", @"0", | |
@"3.2", @"3.2", | |
@"0.34", @"0.343", // round down | |
@"0.35", @"0.345", // round up | |
nil]; | |
NSDecimalNumber *decimal; | |
NSDecimalNumber *decimalRounded; | |
for (NSString *input in numberValues) { | |
decimal = [[NSDecimalNumber alloc] initWithString:input]; | |
NSString *output = [numberValues objectForKey:input]; | |
decimalRounded = [[NSDecimalNumber alloc] initWithString:output]; | |
[[[decimal decimalNumberByRoundingByScale:2] should] equal:decimalRounded]; | |
[decimal release]; | |
[decimalRounded release]; | |
} | |
[numberValues release]; | |
}); | |
}); | |
}); | |
SPEC_END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment