Skip to content

Instantly share code, notes, and snippets.

@dav
Last active August 29, 2015 14:24
Show Gist options
  • Save dav/599d7b4299c0de109cc3 to your computer and use it in GitHub Desktop.
Save dav/599d7b4299c0de109cc3 to your computer and use it in GitHub Desktop.
describe(@"cardinalityPhraseWithNumber:singularPhrase:pluralPhrase:", ^{
it(@"should pluralize 0", ^{
NSString *phrase = [NSString cardinalityPhraseWithInteger:0 singularPhrase:@"bug" pluralPhrase:@"bugs"];
[[phrase should] equal:@"0 bugs"];
});
it(@"should singularize 1", ^{
NSString *phrase = [NSString cardinalityPhraseWithInteger:1 singularPhrase:@"bug" pluralPhrase:@"bugs"];
[[phrase should] equal:@"1 bug"];
});
it(@"should pluralize -1", ^{
NSString *phrase = [NSString cardinalityPhraseWithInteger:-1 singularPhrase:@"bug" pluralPhrase:@"bugs"];
[[phrase should] equal:@"-1 bugs"];
});
it(@"should pluralize 2", ^{
NSString *phrase = [NSString cardinalityPhraseWithInteger:2 singularPhrase:@"bug" pluralPhrase:@"bugs"];
[[phrase should] equal:@"2 bugs"];
});
});
----------------
+ (NSString *)cardinalityPhraseWithInteger:(NSInteger)number singularPhrase:(NSString *)singularPhrase pluralPhrase:(NSString *)pluralPhrase
{
return [NSString stringWithFormat:@"%ld %@", (long)number, number == 1 ? singularPhrase : pluralPhrase];
}
+ (NSString *)cardinalityPhraseRelativeToInteger:(NSInteger)number singularPhrase:(NSString *)singularPhrase pluralPhrase:(NSString *)pluralPhrase
{
return [NSString stringWithFormat:@"%@", number == 1 ? singularPhrase : pluralPhrase];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment