Skip to content

Commit

Permalink
Merge pull request cedarbdd#172 from PaulTaykalo/junit-xml-reporter-g…
Browse files Browse the repository at this point in the history
…rouping

Junit xml reporter grouping by classname.

Reverted memory management semantics in CDRSpy.
  • Loading branch information
jeffh committed Jan 18, 2014
2 parents fc9a85f + be9b966 commit 61e81bf
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 9 deletions.
32 changes: 32 additions & 0 deletions Source/Doubles/CDRSpy.mm
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,38 @@ + (void)stopInterceptingMessagesForInstance:(id)instance {

#pragma mark - Emulating the original object

- (id)retain {
__block id that = self;
[self as_spied_class:^{
[that retain];
}];
return self;
}

- (oneway void)release {
__block id that = self;
[self as_spied_class:^{
[that release];
}];
}

- (id)autorelease {
__block id that = self;
[self as_spied_class:^{
[that autorelease];
}];
return self;
}

- (NSUInteger)retainCount {
__block id that = self;
__block NSUInteger count;
[self as_spied_class:^{
count = [that retainCount];
}];
return count;
}

- (NSString *)description {
__block id that = self;
__block NSString *description = nil;
Expand Down
26 changes: 21 additions & 5 deletions Source/ReporterHelpers/CDROTestNamer.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,25 @@
#import "CDRExample.h"
#import "CDRExampleBase.h"

@interface CDROTestNamer ()
@property (nonatomic, retain) NSMutableCharacterSet *allowedCharacterSet;
@end


@implementation CDROTestNamer

- (id)init {
self = [super init];
if (self) {
self.allowedCharacterSet = [[[NSCharacterSet alphanumericCharacterSet] mutableCopy] autorelease];
[self.allowedCharacterSet addCharactersInString:@"_"];
}
return self;
}

- (NSString *)classNameForExample:(CDRExampleBase *)example {
NSString *className = NSStringFromClass([example.spec class]);
className = className ?: @"Cedar";
return [self sanitizeNameFromString:className];
}

Expand All @@ -28,18 +43,19 @@ - (NSString *)sanitizeNameFromString:(NSString *)string {
NSMutableString *mutableString = [string mutableCopy];
[mutableString replaceOccurrencesOfString:@" " withString:@"_" options:0 range:NSMakeRange(0, mutableString.length)];

NSMutableCharacterSet *allowedCharacterSet = [[NSCharacterSet alphanumericCharacterSet] mutableCopy];
[allowedCharacterSet addCharactersInString:@"_"];

for (NSUInteger i=0; i<mutableString.length; i++) {
if (![allowedCharacterSet characterIsMember:[mutableString characterAtIndex:i]]) {
if (![self.allowedCharacterSet characterIsMember:[mutableString characterAtIndex:i]]) {
[mutableString deleteCharactersInRange:NSMakeRange(i, 1)];
i--;
}
}

[allowedCharacterSet release];
return [mutableString autorelease];
}

- (void)dealloc {
self.allowedCharacterSet = nil;
[super dealloc];
}

@end
15 changes: 13 additions & 2 deletions Source/Reporters/CDRJUnitXMLReporter.m
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
#import "CDRJUnitXMLReporter.h"
#import "CDRExample.h"
#import "CDRSpec.h"
#import "CDROTestNamer.h"


@interface CDRJUnitXMLReporter ()
@property (nonatomic, retain) CDROTestNamer * namer;
@end

@implementation CDRJUnitXMLReporter

- (id)init {
if (self = [super init]) {
successExamples_ = [[NSMutableArray alloc] init];
failureExamples_ = [[NSMutableArray alloc] init];
self.namer = [[[CDROTestNamer alloc] init] autorelease];
}
return self;
}

- (void)dealloc {
[successExamples_ release];
[failureExamples_ release];
self.namer = nil;
[super dealloc];
}

Expand Down Expand Up @@ -46,16 +55,18 @@ - (void)runDidComplete {
[xml appendString:@"<testsuite>\n"];

for (CDRExample *example in successExamples_) {
[xml appendFormat:@"\t<testcase classname=\"Cedar\" name=\"%@\" time=\"%f\" />\n", [self escapeString:example.fullText], example.runTime];
NSString *className = [self.namer classNameForExample:example];
[xml appendFormat:@"\t<testcase classname=\"%@\" name=\"%@\" time=\"%f\" />\n", [self escapeString:className], [self escapeString:example.fullText], example.runTime];
}

for (CDRExample *example in failureExamples_) {
NSString *failureMessage = [self failureMessageForExample:example];
NSArray *parts = [failureMessage componentsSeparatedByString:@"\n"];
NSString *testCaseName = [parts objectAtIndex:0];
NSString *failureDescription = [parts objectAtIndex:1];
NSString *className = [self.namer classNameForExample:example];

[xml appendFormat:@"\t<testcase classname=\"Cedar\" name=\"%@\" time=\"%f\" >\n", [self escapeString:testCaseName], example.runTime];
[xml appendFormat:@"\t<testcase classname=\"%@\" name=\"%@\" time=\"%f\" >\n", [self escapeString:className], [self escapeString:testCaseName], example.runTime];
[xml appendFormat:@"\t\t<failure type=\"Failure\">%@</failure>\n", [self escapeString:failureDescription]];
[xml appendString:@"\t</testcase>\n"];
}
Expand Down
52 changes: 50 additions & 2 deletions Spec/Reporters/CDRJUnitXMLReporterSpec.mm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#import "CDRExample.h"
#import "CDRJUnitXMLReporter.h"
#import "CDRSpecFailure.h"
#import "GDataXMLNode.h"
#import "ExampleWithPublicRunDates.h"

Expand Down Expand Up @@ -143,7 +142,32 @@ + (id)exampleWithText:(NSString *)text andState:(CDRExampleState)state {
GDataXMLElement *exampleXML = [[reporter.xmlRootElement elementsForName:@"testcase"] objectAtIndex:0];
expect([[exampleXML attributeForName:@"time"] stringValue]).to_not(be_nil);
expect([[[exampleXML attributeForName:@"time"] stringValue] floatValue]).to(be_close_to(5));
});

it(@"should have it's classname", ^{
CDRExample *example = [CDRExample exampleWithText:@"Spec" andState:CDRExampleStatePassed];
example.spec = [[CDRSpec new] autorelease];
[reporter reportOnExample:example];

CDRExample *junitExample = [CDRExample exampleWithText:@"JUnitExample" andState:CDRExampleStatePassed];
junitExample.spec = [[CDRJUnitXMLReporterSpec new] autorelease];
[reporter reportOnExample:junitExample];

[reporter runDidComplete];
GDataXMLElement *exampleXML = [[reporter.xmlRootElement elementsForName:@"testcase"] objectAtIndex:0];
expect([[exampleXML attributeForName:@"classname"] stringValue]).to(equal(@"CDRSpec"));
GDataXMLElement *junitExampleXML = [[reporter.xmlRootElement elementsForName:@"testcase"] objectAtIndex:1];
expect([[junitExampleXML attributeForName:@"classname"] stringValue]).to(equal(@"CDRJUnitXMLReporterSpec"));
});

it(@"should have it's classname to default value if spec filename is empty", ^{
CDRExample *example = [CDRExample exampleWithText:@"Spec" andState:CDRExampleStatePassed];

[reporter reportOnExample:example];

[reporter runDidComplete];
GDataXMLElement *exampleXML = [[reporter.xmlRootElement elementsForName:@"testcase"] objectAtIndex:0];
expect([[exampleXML attributeForName:@"classname"] stringValue]).to(equal(@"Cedar"));
});
});

Expand Down Expand Up @@ -187,7 +211,6 @@ + (id)exampleWithText:(NSString *)text andState:(CDRExampleState)state {
});

it(@"should escape the failure reason", ^{

NSString *exampleName = @"Failing spec 1";
NSString *failureReason = @" Special ' characters \" should < be & escaped > ";
NSString *fullExampleText = [NSString stringWithFormat:@"%@\n%@", exampleName, failureReason];
Expand All @@ -212,7 +235,32 @@ + (id)exampleWithText:(NSString *)text andState:(CDRExampleState)state {
GDataXMLElement *exampleXML = [[reporter.xmlRootElement elementsForName:@"testcase"] objectAtIndex:0];
expect([[exampleXML attributeForName:@"time"] stringValue]).to_not(be_nil);
expect([[[exampleXML attributeForName:@"time"] stringValue] floatValue]).to(be_close_to(5));
});

it(@"should have it's classname", ^{
CDRExample *example = [CDRExample exampleWithText:@"Spec" andState:CDRExampleStateFailed];
example.spec = [[CDRSpec new] autorelease];
[reporter reportOnExample:example];

CDRExample *junitExample = [CDRExample exampleWithText:@"JUnitExample" andState:CDRExampleStateFailed];
junitExample.spec = [[CDRJUnitXMLReporterSpec new] autorelease];
[reporter reportOnExample:junitExample];

[reporter runDidComplete];
GDataXMLElement *exampleXML = [[reporter.xmlRootElement elementsForName:@"testcase"] objectAtIndex:0];
expect([[exampleXML attributeForName:@"classname"] stringValue]).to(equal(@"CDRSpec"));
GDataXMLElement *junitExampleXML = [[reporter.xmlRootElement elementsForName:@"testcase"] objectAtIndex:1];
expect([[junitExampleXML attributeForName:@"classname"] stringValue]).to(equal(@"CDRJUnitXMLReporterSpec"));
});

it(@"should have it's classname to default value if spec filename is empty", ^{
CDRExample *example = [CDRExample exampleWithText:@"Spec" andState:CDRExampleStateFailed];

[reporter reportOnExample:example];

[reporter runDidComplete];
GDataXMLElement *exampleXML = [[reporter.xmlRootElement elementsForName:@"testcase"] objectAtIndex:0];
expect([[exampleXML attributeForName:@"classname"] stringValue]).to(equal(@"Cedar"));
});

});
Expand Down

0 comments on commit 61e81bf

Please sign in to comment.