Skip to content

Commit

Permalink
Refactored objects assertions to a separate class to be used as a bas…
Browse files Browse the repository at this point in the history
…e class for unit cases which need to do these assertions.

Note that we couldn't use class methods as we do with GBTestObjectsRegistry as we use SenTest macros which require failWithException: method be declared in the test case class.
  • Loading branch information
tomaz committed Jul 27, 2010
1 parent 0c0d07f commit 7fb7216
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 86 deletions.
1 change: 1 addition & 0 deletions AppledocTests_prefix.pch
Expand Up @@ -18,4 +18,5 @@
#import <OCMock/OCMock.h>
#import <SenTestingKit/SenTestingKit.h>
#import "GBTestObjectsRegistry.h"
#import "GBObjectsAssertor.h"
#endif
84 changes: 1 addition & 83 deletions Testing/GBObjectiveCParser-ClassParsingTesting.m
Expand Up @@ -9,14 +9,7 @@
#import "GBStore.h"
#import "GBObjectiveCParser.h"

@interface GBObjectiveCParserClassParsingTesting : SenTestCase

- (void)assertIvar:(GBIvarData *)ivar matches:(NSString *)firstType,... NS_REQUIRES_NIL_TERMINATION;
- (void)assertMethod:(GBMethodData *)method matchesInstanceComponents:(NSString *)firstItem,... NS_REQUIRES_NIL_TERMINATION;
- (void)assertMethod:(GBMethodData *)method matchesClassComponents:(NSString *)firstItem,... NS_REQUIRES_NIL_TERMINATION;
- (void)assertMethod:(GBMethodData *)method matchesPropertyComponents:(NSString *)firstItem,... NS_REQUIRES_NIL_TERMINATION;
- (void)assertMethod:(GBMethodData *)method matchesType:(GBMethodType)type start:(NSString *)first components:(va_list)args;

@interface GBObjectiveCParserClassParsingTesting : GBObjectsAssertor
@end

@implementation GBObjectiveCParserClassParsingTesting
Expand Down Expand Up @@ -244,79 +237,4 @@ - (void)testParseObjectsFromString_shouldRegisterClassFromRealLifeInput {
// setup
}

#pragma mark Assertion methods

- (void)assertIvar:(GBIvarData *)ivar matches:(NSString *)firstType,... {
NSMutableArray *arguments = [NSMutableArray array];
va_list args;
va_start(args, firstType);
for (NSString *arg=firstType; arg != nil; arg=va_arg(args, NSString*)) {
[arguments addObject:arg];
}
va_end(args);

assertThatInteger([[ivar ivarTypes] count], equalToInteger([arguments count] - 1));
for (NSUInteger i=0; i<[arguments count] - 1; i++)
assertThat([ivar.ivarTypes objectAtIndex:i], is([arguments objectAtIndex:i]));

assertThat(ivar.ivarName, is([arguments lastObject]));
}

- (void)assertMethod:(GBMethodData *)method matchesInstanceComponents:(NSString *)firstItem,... {
va_list args;
va_start(args,firstItem);
[self assertMethod:method matchesType:GBMethodTypeInstance start:firstItem components:args];
va_end(args);
}
- (void)assertMethod:(GBMethodData *)method matchesClassComponents:(NSString *)firstItem,... {
va_list args;
va_start(args,firstItem);
[self assertMethod:method matchesType:GBMethodTypeClass start:firstItem components:args];
va_end(args);
}

- (void)assertMethod:(GBMethodData *)method matchesPropertyComponents:(NSString *)firstItem,... {
va_list args;
va_start(args,firstItem);
[self assertMethod:method matchesType:GBMethodTypeProperty start:firstItem components:args];
va_end(args);
}

//- (void)assertMethod:(GBMethodData *)method matchesType:(GBMethodType)type components:(NSString *)firstItem,... {
- (void)assertMethod:(GBMethodData *)method matchesType:(GBMethodType)type start:(NSString *)first components:(va_list)args {
// Note that we flatten all the arguments to make assertion methods simpler; nice trick but we do need to
// use ST macros instead of hamcrest to get more meaningful description in case of failure :(
STAssertEquals(method.methodType, type, @"Method type doesn't match!");

NSMutableArray *arguments = [NSMutableArray arrayWithObject:first];
NSString *arg;
while ((arg = va_arg(args, NSString*))) {
[arguments addObject:arg];
}

NSUInteger i=0;

for (NSString *attribute in method.methodAttributes) {
STAssertEqualObjects(attribute, [arguments objectAtIndex:i++], @"Property attribute doesn't match at flat idx %ld!", i-1);
}

for (NSString *type in method.methodResultTypes) {
STAssertEqualObjects(type, [arguments objectAtIndex:i++], @"Method result doesn't match at flat idx %ld!", i-1);
}

for (GBMethodArgument *argument in method.methodArguments) {
STAssertEqualObjects(argument.argumentName, [arguments objectAtIndex:i++], @"Method argument name doesn't match at flat idx %ld!", i-1);
if (argument.argumentTypes) {
for (NSString *type in argument.argumentTypes) {
STAssertEqualObjects(type, [arguments objectAtIndex:i++], @"Method argument type doesn't match at flat idx %ld!", i-1);
}
}
if (argument.argumentVar) {
STAssertEqualObjects(argument.argumentVar, [arguments objectAtIndex:i++], @"Method argument var doesn't match at flat idx %ld!", i-1);
}
}

STAssertEquals(i, [arguments count], @"Flattened method has %ld components, expected %ld!", i, [arguments count]);
}

@end
21 changes: 21 additions & 0 deletions Testing/GBObjectsAssertor.h
@@ -0,0 +1,21 @@
//
// GBObjectsAssertor.h
// appledoc
//
// Created by Tomaz Kragelj on 27.7.10.
// Copyright (C) 2010, Gentle Bytes. All rights reserved.
//

#import "GBIvarData.h"
#import "GBMethodData.h"

// Need to derive from SenTestCase otherwise ST macros used wouldn't work...
@interface GBObjectsAssertor : SenTestCase

- (void)assertIvar:(GBIvarData *)ivar matches:(NSString *)firstType,... NS_REQUIRES_NIL_TERMINATION;
- (void)assertMethod:(GBMethodData *)method matchesInstanceComponents:(NSString *)firstItem,... NS_REQUIRES_NIL_TERMINATION;
- (void)assertMethod:(GBMethodData *)method matchesClassComponents:(NSString *)firstItem,... NS_REQUIRES_NIL_TERMINATION;
- (void)assertMethod:(GBMethodData *)method matchesPropertyComponents:(NSString *)firstItem,... NS_REQUIRES_NIL_TERMINATION;
- (void)assertMethod:(GBMethodData *)method matchesType:(GBMethodType)type start:(NSString *)first components:(va_list)args;

@end
86 changes: 86 additions & 0 deletions Testing/GBObjectsAssertor.m
@@ -0,0 +1,86 @@
//
// GBObjectsAssertor.m
// appledoc
//
// Created by Tomaz Kragelj on 27.7.10.
// Copyright (C) 2010, Gentle Bytes. All rights reserved.
//

#import "GBObjectsAssertor.h"

@implementation GBObjectsAssertor

- (void)assertIvar:(GBIvarData *)ivar matches:(NSString *)firstType,... {
NSMutableArray *arguments = [NSMutableArray array];
va_list args;
va_start(args, firstType);
for (NSString *arg=firstType; arg != nil; arg=va_arg(args, NSString*)) {
[arguments addObject:arg];
}
va_end(args);

assertThatInteger([[ivar ivarTypes] count], equalToInteger([arguments count] - 1));
for (NSUInteger i=0; i<[arguments count] - 1; i++)
assertThat([ivar.ivarTypes objectAtIndex:i], is([arguments objectAtIndex:i]));

assertThat(ivar.ivarName, is([arguments lastObject]));
}

- (void)assertMethod:(GBMethodData *)method matchesInstanceComponents:(NSString *)firstItem,... {
va_list args;
va_start(args,firstItem);
[self assertMethod:method matchesType:GBMethodTypeInstance start:firstItem components:args];
va_end(args);
}

- (void)assertMethod:(GBMethodData *)method matchesClassComponents:(NSString *)firstItem,... {
va_list args;
va_start(args,firstItem);
[self assertMethod:method matchesType:GBMethodTypeClass start:firstItem components:args];
va_end(args);
}

- (void)assertMethod:(GBMethodData *)method matchesPropertyComponents:(NSString *)firstItem,... {
va_list args;
va_start(args,firstItem);
[self assertMethod:method matchesType:GBMethodTypeProperty start:firstItem components:args];
va_end(args);
}

- (void)assertMethod:(GBMethodData *)method matchesType:(GBMethodType)type start:(NSString *)first components:(va_list)args {
// Note that we flatten all the arguments to make assertion methods simpler; nice trick but we do need to
// use custom macros instead of hamcrest to get more meaningful description in case of failure :(
STAssertEquals(method.methodType, type, @"Method type doesn't match!");

NSMutableArray *arguments = [NSMutableArray arrayWithObject:first];
NSString *arg;
while ((arg = va_arg(args, NSString*))) {
[arguments addObject:arg];
}

NSUInteger i=0;

for (NSString *attribute in method.methodAttributes) {
STAssertEqualObjects(attribute, [arguments objectAtIndex:i++], @"Property attribute doesn't match at flat idx %ld!", i-1);
}

for (NSString *type in method.methodResultTypes) {
STAssertEqualObjects(type, [arguments objectAtIndex:i++], @"Method result doesn't match at flat idx %ld!", i-1);
}

for (GBMethodArgument *argument in method.methodArguments) {
STAssertEqualObjects(argument.argumentName, [arguments objectAtIndex:i++], @"Method argument name doesn't match at flat idx %ld!", i-1);
if (argument.argumentTypes) {
for (NSString *type in argument.argumentTypes) {
STAssertEqualObjects(type, [arguments objectAtIndex:i++], @"Method argument type doesn't match at flat idx %ld!", i-1);
}
}
if (argument.argumentVar) {
STAssertEqualObjects(argument.argumentVar, [arguments objectAtIndex:i++], @"Method argument var doesn't match at flat idx %ld!", i-1);
}
}

STAssertEquals(i, [arguments count], @"Flattened method has %ld components, expected %ld!", i, [arguments count]);
}

@end
12 changes: 9 additions & 3 deletions appledoc.xcodeproj/project.pbxproj
Expand Up @@ -11,6 +11,7 @@
7340F02811FCC63100E712A4 /* NSFileManager+GBFileManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 7340F02511FCC63100E712A4 /* NSFileManager+GBFileManager.m */; };
7340F02911FCC63100E712A4 /* NSObject+GBObject.m in Sources */ = {isa = PBXBuildFile; fileRef = 7340F02711FCC63100E712A4 /* NSObject+GBObject.m */; };
7367B6EC11FE3788005ED6CD /* GBTestObjectsRegistry.m in Sources */ = {isa = PBXBuildFile; fileRef = 7367B6EB11FE3788005ED6CD /* GBTestObjectsRegistry.m */; };
7367B82311FEEB52005ED6CD /* GBObjectsAssertor.m in Sources */ = {isa = PBXBuildFile; fileRef = 7367B82211FEEB52005ED6CD /* GBObjectsAssertor.m */; };
73D54C6211F8CE6D00CCDDB0 /* appledoc.m in Sources */ = {isa = PBXBuildFile; fileRef = 73D54C6111F8CE6D00CCDDB0 /* appledoc.m */; };
73D54C9E11F8D22B00CCDDB0 /* DDConsoleLogger.m in Sources */ = {isa = PBXBuildFile; fileRef = 73D54C9911F8D22B00CCDDB0 /* DDConsoleLogger.m */; };
73D54C9F11F8D22B00CCDDB0 /* DDFileLogger.m in Sources */ = {isa = PBXBuildFile; fileRef = 73D54C9B11F8D22B00CCDDB0 /* DDFileLogger.m */; };
Expand Down Expand Up @@ -81,6 +82,8 @@
7340F02711FCC63100E712A4 /* NSObject+GBObject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+GBObject.m"; sourceTree = "<group>"; };
7367B6EA11FE3788005ED6CD /* GBTestObjectsRegistry.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GBTestObjectsRegistry.h; sourceTree = "<group>"; };
7367B6EB11FE3788005ED6CD /* GBTestObjectsRegistry.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GBTestObjectsRegistry.m; sourceTree = "<group>"; };
7367B82111FEEB52005ED6CD /* GBObjectsAssertor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GBObjectsAssertor.h; sourceTree = "<group>"; };
7367B82211FEEB52005ED6CD /* GBObjectsAssertor.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GBObjectsAssertor.m; sourceTree = "<group>"; };
73D54C6111F8CE6D00CCDDB0 /* appledoc.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = appledoc.m; sourceTree = "<group>"; };
73D54C6911F8CEEF00CCDDB0 /* appledoc_prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = appledoc_prefix.pch; sourceTree = "<group>"; };
73D54C9811F8D22B00CCDDB0 /* DDConsoleLogger.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DDConsoleLogger.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -270,19 +273,21 @@
73FC741911FE210100AAD0B9 /* GBMethodsProviderTesting.m */,
73FC743511FE2CEB00AAD0B9 /* GBModelDataTesting.m */,
73FC702C11FCEDDD00AAD0B9 /* GBTokenizerTesting.m */,
7367B6C611FE3750005ED6CD /* Registry */,
7367B6C611FE3750005ED6CD /* Creation & Assertion */,
73FC6DFE11FCD2D300AAD0B9 /* Bundle */,
);
path = Testing;
sourceTree = "<group>";
};
7367B6C611FE3750005ED6CD /* Registry */ = {
7367B6C611FE3750005ED6CD /* Creation & Assertion */ = {
isa = PBXGroup;
children = (
7367B6EA11FE3788005ED6CD /* GBTestObjectsRegistry.h */,
7367B6EB11FE3788005ED6CD /* GBTestObjectsRegistry.m */,
7367B82111FEEB52005ED6CD /* GBObjectsAssertor.h */,
7367B82211FEEB52005ED6CD /* GBObjectsAssertor.m */,
);
name = Registry;
name = "Creation & Assertion";
sourceTree = "<group>";
};
73D54BDF11F8CE2300CCDDB0 /* Startup */ = {
Expand Down Expand Up @@ -597,6 +602,7 @@
73FC741A11FE210100AAD0B9 /* GBMethodsProviderTesting.m in Sources */,
73FC743611FE2CEB00AAD0B9 /* GBModelDataTesting.m in Sources */,
7367B6EC11FE3788005ED6CD /* GBTestObjectsRegistry.m in Sources */,
7367B82311FEEB52005ED6CD /* GBObjectsAssertor.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down

0 comments on commit 7fb7216

Please sign in to comment.