Commits

Kunal Parmar committed 458b1bb

Fix formatter to not add unwanted spaces.

Comments (0)

Files changed (2)

Sources/KPTASLReader.m

 - (NSString *)stringForMessage:(KPTASLMessage *)message
 {
   NSDictionary *properties = [message properties];
-  NSMutableString *string = [NSMutableString string];
+  NSMutableArray *formattedValues =
+    [NSMutableArray arrayWithCapacity:[properties count]];
   [properties enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
     (void)stop;
-    [string appendFormat:@" %@", [self stringForMessageProperty:key
-                                                          value:obj]];
+    NSString *formattedValue = [self stringForMessageProperty:key
+                                                        value:obj];
+    if ([formattedValue length] > 0)
+    {
+      [formattedValues addObject:formattedValue];
+    }
   }];
-  return string;
+  return [formattedValues componentsJoinedByString:@" "];
 }
 
 - (NSString *)stringForMessageProperty:(NSString *)property
 
 - (NSString *)stringForMessage:(KPTASLMessage *)message
 {
-  NSMutableString *string = [NSMutableString string];
+  NSMutableArray *formattedValues =
+    [NSMutableArray arrayWithCapacity:[properties_ count]];
   for (NSString *property in properties_)
   {
     NSString *value = [message valueForProperty:property];
-    [string appendFormat:@" %@", [self stringForMessageProperty:property
-                                                          value:value]];
+    NSString *formattedValue = [self stringForMessageProperty:property
+                                                        value:value];
+    if ([formattedValue length] > 0)
+    {
+      [formattedValues addObject:formattedValue];
+    }
   }
-  return string;
+  return [formattedValues componentsJoinedByString:@" "];
 }
 
 @end

Sources/KPTASLReaderTest.m

                      forProperty:@"property4"],
                nil);
 
-  NSArray *properties = [NSArray arrayWithObjects:
-                         @"property2", @"property3", @"property1", nil];
+  NSArray *properties = [NSArray array];
   id<KPTASLReaderFormatter> formatter =
     [KPTASLReaderPropertyFormatter propertyFormatterWithProperties:properties];
   STAssertNotNil(formatter, nil);
+  STAssertEqualObjects([formatter stringForMessage:message], @"", nil);
+
+  // Test properties that are not present.
+  properties = [NSArray arrayWithObjects:@"foo", @"bar", @"baz", nil];
+  STAssertNotNil(properties, nil);
+  formatter =
+    [KPTASLReaderPropertyFormatter propertyFormatterWithProperties:properties];
+  STAssertNotNil(formatter, nil);
+  STAssertEqualObjects([formatter stringForMessage:message], @"", nil);
+
+  properties = [NSArray arrayWithObjects:
+                @"property2", @"property3", @"property1", nil];
+  STAssertNotNil(properties, nil);
+  formatter =
+    [KPTASLReaderPropertyFormatter propertyFormatterWithProperties:properties];
+  STAssertNotNil(formatter, nil);
   NSString *string = [formatter stringForMessage:message];
 
-  // Test that all properties are collected.
+  // Test that given properties are collected.
   NSRange property1Range = [string rangeOfString:@"property1Value"];
   STAssertTrue(property1Range.location != NSNotFound, nil);
   NSRange property2Range = [string rangeOfString:@"property2Value"];