Files
cappuccino/Tests/Objective-J/sprintfTest.j
T
Brian Donovan 7be6352531 Add format parameter index support to sprintf.
This is primarily useful in cases when the format string can vary, but the argument order will not, as is often the case with format strings in Localized.strings files.

From http://perldoc.perl.org/functions/sprintf.html:

An explicit format parameter index, such as 2$. By default sprintf will format the next unused argument in the list, but this allows you to take the arguments out of order.

See also this Cocoa example: http://stackoverflow.com/questions/1063843/is-there-a-way-to-specify-argument-position-index-in-nsstring-stringwithformat
2010-10-21 09:54:51 -07:00

38 lines
832 B
Plaintext

var sprintf = ObjectiveJ.sprintf;
@implementation sprintfTest : OJTestCase
// TODO: add many many more of these...
- (void)testObjectWithPrefixAndSuffix
{
[self assert:@"[hello world]" equals:sprintf(@"[%@]", @"hello world")];
}
- (void)testDecimalWithPrefixAndSuffix
{
[self assert:@"[123]" equals:sprintf(@"[%d]", 123)];
}
- (void)testFloatWithPrefixAndSuffix
{
[self assert:@"[123.1234]" equals:sprintf(@"[%f]", 123.1234)];
}
- (void)testZeroPaddingWithWidthAndPercentEscaping
{
[self assert:@"099%" equals:sprintf(@"%03d%%", 99)];
}
- (void)testOutOfOrderExplicitFormatParameterIndexes
{
[self assert:@"2 > 1" equals:sprintf(@"%2$d > %1$d", 1, 2)];
}
- (void)testMixingImplicitAndExplicitFormatParameterIndexes
{
[self assert:@"a < b && b > a" equals:sprintf(@"%@ < %2$@ && %@ > %1$@", @"a", @"b")];
}
@end