Skip to content

Commit

Permalink
Added constant registration and fixed ending and canceling objects re…
Browse files Browse the repository at this point in the history
…gistrations.
  • Loading branch information
tomaz committed Apr 24, 2012
1 parent bd1686d commit a789509
Show file tree
Hide file tree
Showing 14 changed files with 483 additions and 78 deletions.
36 changes: 24 additions & 12 deletions AppledocTests/Parsing/ObjectiveCStructStateTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ - (void)testParseStreamForParserStoreShouldDetectStructWithSingleItemWithSingleT
[[store expect] setCurrentSourceInfo:OCMOCK_ANY];
[[store expect] beginStruct];
[[store expect] beginConstant];
[[store expect] appendConstantType:@"type"];
[[store expect] beginConstantTypes];
[[store expect] appendType:@"type"];
[[store expect] endCurrentObject]; // types
[[store expect] appendConstantName:@"item"];
[[store expect] endCurrentObject]; // constant
[[store expect] endCurrentObject]; // struct
Expand All @@ -72,9 +74,11 @@ - (void)testParseStreamForParserStoreShouldDetectStructWithSingleItemWithMultipl
[[store expect] setCurrentSourceInfo:OCMOCK_ANY];
[[store expect] beginStruct];
[[store expect] beginConstant];
[[store expect] appendConstantType:@"type1"];
[[store expect] appendConstantType:@"type2"];
[[store expect] appendConstantType:@"type3"];
[[store expect] beginConstantTypes];
[[store expect] appendType:@"type1"];
[[store expect] appendType:@"type2"];
[[store expect] appendType:@"type3"];
[[store expect] endCurrentObject]; // types
[[store expect] appendConstantName:@"item"];
[[store expect] endCurrentObject]; // constant
[[store expect] endCurrentObject]; // struct
Expand All @@ -98,11 +102,15 @@ - (void)testParseStreamForParserStoreShouldDetectStructWithMultipleItemsWithSing
[[store expect] setCurrentSourceInfo:OCMOCK_ANY];
[[store expect] beginStruct];
[[store expect] beginConstant];
[[store expect] appendConstantType:@"type1"];
[[store expect] beginConstantTypes];
[[store expect] appendType:@"type1"];
[[store expect] endCurrentObject]; // types
[[store expect] appendConstantName:@"item1"];
[[store expect] endCurrentObject]; // constant
[[store expect] beginConstant];
[[store expect] appendConstantType:@"type2"];
[[store expect] beginConstantTypes];
[[store expect] appendType:@"type2"];
[[store expect] endCurrentObject]; // types
[[store expect] appendConstantName:@"item2"];
[[store expect] endCurrentObject]; // constant
[[store expect] endCurrentObject]; // struct
Expand All @@ -124,15 +132,19 @@ - (void)testParseStreamForParserStoreShouldDetectStructWithMultipleItemWithMulti
[[store expect] setCurrentSourceInfo:OCMOCK_ANY];
[[store expect] beginStruct];
[[store expect] beginConstant];
[[store expect] appendConstantType:@"typeA1"];
[[store expect] appendConstantType:@"typeA2"];
[[store expect] appendConstantType:@"typeA3"];
[[store expect] beginConstantTypes];
[[store expect] appendType:@"typeA1"];
[[store expect] appendType:@"typeA2"];
[[store expect] appendType:@"typeA3"];
[[store expect] endCurrentObject]; // types
[[store expect] appendConstantName:@"itemA"];
[[store expect] endCurrentObject]; // constant
[[store expect] beginConstant];
[[store expect] appendConstantType:@"typeB1"];
[[store expect] appendConstantType:@"typeB2"];
[[store expect] appendConstantType:@"typeB3"];
[[store expect] beginConstantTypes];
[[store expect] appendType:@"typeB1"];
[[store expect] appendType:@"typeB2"];
[[store expect] appendType:@"typeB3"];
[[store expect] endCurrentObject]; // types
[[store expect] appendConstantName:@"itemB"];
[[store expect] endCurrentObject]; // constant
[[store expect] endCurrentObject]; // struct
Expand Down
70 changes: 70 additions & 0 deletions AppledocTests/Store/ConstantInfoTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//
// ConstantInfoTests.m
// appledoc
//
// Created by Tomaž Kragelj on 4/20/12.
// Copyright (c) 2012 Tomaz Kragelj. All rights reserved.
//

#import "Store.h"
#import "TestCaseBase.h"

@interface ConstantInfoTests : TestCaseBase
@end

@interface ConstantInfoTests (CreationMethods)
- (void)runWithConstantInfo:(void(^)(ConstantInfo *info))handler;
@end

@implementation ConstantInfoTests

#pragma mark - Verify lazy initialization

- (void)testLazyInitializersWork {
[self runWithConstantInfo:^(ConstantInfo *info) {
// execute & verify
assertThat(info.constantTypes, instanceOf([TypeInfo class]));
}];
}


#pragma mark - beginConstantTypes

- (void)testBeginConstantTypesShouldChangeCurrentRegistrationObjectToTypes {
[self runWithConstantInfo:^(ConstantInfo *info) {
// setup
id mock = [OCMockObject mockForClass:[Store class]];
[[mock expect] pushRegistrationObject:[OCMArg checkWithBlock:^BOOL(id obj) {
return [obj isKindOfClass:[TypeInfo class]];
}]];
info.objectRegistrar = mock;
// execute
[info beginConstantTypes];
// verify
STAssertNoThrow([mock verify], nil);
}];
}

#pragma mark - appendConstantName:

- (void)testAppendConstantNameShouldAssignGivenString {
[self runWithConstantInfo:^(ConstantInfo *info) {
// execute
[info appendConstantName:@"value"];
// verify
assertThat(info.constantName, equalTo(@"value"));
}];
}

@end

#pragma mark -

@implementation ConstantInfoTests (CreationMethods)

- (void)runWithConstantInfo:(void(^)(ConstantInfo *info))handler {
ConstantInfo *info = [ConstantInfo new];
handler(info);
}

@end
2 changes: 1 addition & 1 deletion AppledocTests/Store/PropertyInfoTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ - (void)testBeginPropertyAttributesShouldChangeCurrentRegistrationObjectToAttrib

#pragma mark - beginPropertyTypes

- (void)testBeginPropertyTypesShouldChangeCurrentRegistrationObjectToResults {
- (void)testBeginPropertyTypesShouldChangeCurrentRegistrationObjectToTypes {
[self runWithPropertyInfo:^(PropertyInfo *info) {
// setup
id mock = [OCMockObject mockForClass:[Store class]];
Expand Down
145 changes: 114 additions & 31 deletions AppledocTests/Store/StoreTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ - (void)testLazyInitializationWorks {
assertThat(store.storeProtocols, instanceOf([NSMutableArray class]));
assertThat(store.storeEnumerations, instanceOf([NSMutableArray class]));
assertThat(store.storeStructs, instanceOf([NSMutableArray class]));
assertThat(store.storeConstants, instanceOf([NSMutableArray class]));
assertThat(store.registrationStack, instanceOf([NSMutableArray class]));
}];
}
Expand Down Expand Up @@ -372,31 +373,58 @@ - (void)testBeginStructShouldAddStructInfoToStructuresArray {

- (void)testBeginConstantShouldRegisterConstantInfoIfRegistrationStackIsEmpty {
[self runWithStore:^(Store *store) {
// TODO!!! STFail(@"not implemented!");
// execute
[store beginConstant];
// verify
assertThat(store.currentRegistrationObject, instanceOf([ConstantInfo class]));
assertThat([store.currentRegistrationObject objectRegistrar], equalTo(store));
}];
}

- (void)testBeginConstantShouldAddConstantInfoToConstantsArray {
[self runWithStore:^(Store *store) {
// execute
[store beginConstant];
// verify
assertThatInt(store.storeConstants.count, equalToInt(1));
assertThat([store.storeConstants lastObject], equalTo(store.currentRegistrationObject));
}];
}

- (void)testBeginConstantShouldRegisterConstantInfoIfCurrentObjectIsAvailableButDoesntRespondToSelector {
[self runWithStore:^(Store *store) {
// setup
[store pushRegistrationObject:[NSObject new]];
// execute
[store beginConstant];
// verify
assertThatInt(store.storeConstants.count, equalToInt(1));
assertThat([store.storeConstants lastObject], equalTo(store.currentRegistrationObject));
}];
}

- (void)testBeginConstantShouldForwardToCurrentObjectIfAvailable {
- (void)testBeginConstantShouldForwardToCurrentObjectIfAvailableAndRespondsToSelector {
[self runWithStore:^(Store *store) {
// setup
id mock = [OCMockObject mockForClass:[Store class]];
[[mock expect] beginConstant];
[store pushRegistrationObject:mock];
// execute
// TODO!!! [store beginConstant];
[store beginConstant];
// verify
// TODO!!! STAssertNoThrow([mock verify], nil);
STAssertNoThrow([mock verify], nil);
assertThatInt(store.storeConstants.count, equalToInt(0));
}];
}

- (void)testAppendConstantTypeShouldForwardToCurrentObject {
- (void)testBeginConstantTypesShouldForwardToCurrentObject {
[self runWithStore:^(Store *store) {
// setup
id mock = [OCMockObject mockForClass:[Store class]];
[[mock expect] appendConstantType:@"value"];
[[mock expect] beginConstantTypes];
[store pushRegistrationObject:mock];
// execute
[store appendConstantType:@"value"];
[store beginConstantTypes];
// verify
STAssertNoThrow([mock verify], nil);
}];
Expand Down Expand Up @@ -445,31 +473,53 @@ - (void)testAppendAttributeShouldForwardToCurrentObject {

#pragma mark - endCurrentObject

- (void)testEndCurrentObjectShouldForwardToCurrentObjectIfItRespondsToEndMessageThenRemoveObjectFromRegistrationStack {
- (void)testEndCurrentObjectShouldRemoveLastObjectFromRegistrationStack {
[self runWithStore:^(Store *store) {
// setup
id mock = [OCMockObject mockForClass:[Store class]];
[[mock expect] endCurrentObject];
[store pushRegistrationObject:mock];
[store pushRegistrationObject:[OCMockObject niceMockForClass:[Store class]]];
// execute
[store endCurrentObject];
// verify
STAssertNoThrow([mock verify], nil);
assertThatInt(store.registrationStack.count, equalToInt(0));
assertThat(store.currentRegistrationObject, equalTo(nil));
}];
}

- (void)testEndCurrentObjectShouldNotForwardToCurrentObjectIfItDoesntRespondToEndMessageThenRemoveObjectFromRegistrationStack {
- (void)testEndCurrentObjectShouldForwardToSemilastObjectIfItRespondsToEndMessageThenRemoveLastObjectFromRegistrationStack {
[self runWithStore:^(Store *store) {
// setup
id mock = @"object";
[store pushRegistrationObject:mock];
// execute - note that in case we send endCurrentObject, this should fail due to NSString not implementing the method!
id first = [OCMockObject mockForClass:[Store class]];
[[first expect] endCurrentObject];
[store pushRegistrationObject:first];
id second = [OCMockObject mockForClass:[Store class]];
[store pushRegistrationObject:second];
// execute
[store endCurrentObject];
// verify
assertThatInt(store.registrationStack.count, equalToInt(0));
assertThat(store.currentRegistrationObject, equalTo(nil));
STAssertNoThrow([first verify], nil);
STAssertNoThrow([second verify], nil);
assertThatInt(store.registrationStack.count, equalToInt(1));
assertThat(store.registrationStack.lastObject, equalTo(first));
assertThat(store.currentRegistrationObject, equalTo(first));
}];
}

- (void)testEndCurrentObjectShouldNotForwardToSemilastObjectIfItDoesntRespondToEndMessageButShouldRemoveLastObjectFromRegistrationStack {
[self runWithStore:^(Store *store) {
// setup
id first = [OCMockObject mockForClass:[NSObject class]];
[store pushRegistrationObject:first];
id second = [OCMockObject mockForClass:[Store class]];
[[second stub] isKindOfClass:OCMOCK_ANY];
[store pushRegistrationObject:second];
// execute
[store endCurrentObject];
// verify
STAssertNoThrow([first verify], nil);
STAssertNoThrow([second verify], nil);
assertThatInt(store.registrationStack.count, equalToInt(1));
assertThat(store.registrationStack.lastObject, equalTo(first));
assertThat(store.currentRegistrationObject, equalTo(first));
}];
}

Expand All @@ -485,40 +535,61 @@ - (void)testEndCurrentObjectShouldIgnoreIfRegistrationStackIsEmpty {

#pragma mark - cancelCurrentObject

- (void)testCancelCurrentObjectShouldForwardToCurrentObjectIfItRespondsToEndMessageThenRemoveObjectFromRegistrationStack {
- (void)testCancelCurrentObjectShouldRemoveLastObjectFromRegistrationStack {
[self runWithStore:^(Store *store) {
// setup
id mock = [OCMockObject mockForClass:[Store class]];
[[mock stub] isKindOfClass:OCMOCK_ANY]; // store checks if it's one of the top level objects and removes it from array if so
[[mock expect] cancelCurrentObject];
[store pushRegistrationObject:mock];
[store pushRegistrationObject:[OCMockObject niceMockForClass:[Store class]]];
// execute
[store cancelCurrentObject];
// verify
STAssertNoThrow([mock verify], nil);
assertThatInt(store.registrationStack.count, equalToInt(0));
assertThat(store.currentRegistrationObject, equalTo(nil));
}];
}

- (void)testCancelCurrentObjectShouldNotForwardToCurrentObjectIfItDoesntRespondToEndMessageThenRemoveObjectFromRegistrationStack {
- (void)testCancelCurrentObjectShouldForwardToSemilastObjectIfItRespondsToCancelMessageThenRemoveLastObjectFromRegistrationStack {
[self runWithStore:^(Store *store) {
// setup
id mock = @"object";
[store pushRegistrationObject:mock];
// execute - note that in case we send endCurrentObject, this should fail due to NSString not implementing the method!
id first = [OCMockObject mockForClass:[Store class]];
[[first expect] cancelCurrentObject];
[store pushRegistrationObject:first];
id second = [OCMockObject mockForClass:[Store class]];
[store pushRegistrationObject:second];
// execute
[store cancelCurrentObject];
// verify
assertThatInt(store.registrationStack.count, equalToInt(0));
assertThat(store.currentRegistrationObject, equalTo(nil));
STAssertNoThrow([first verify], nil);
STAssertNoThrow([second verify], nil);
assertThatInt(store.registrationStack.count, equalToInt(1));
assertThat(store.registrationStack.lastObject, equalTo(first));
assertThat(store.currentRegistrationObject, equalTo(first));
}];
}

- (void)testCancelCurrentObjectShouldNotForwardToSemilastObjectIfItDoesntRespondToCancelMessageButShouldRemoveLastObjectFromRegistrationStack {
[self runWithStore:^(Store *store) {
// setup
id first = [OCMockObject mockForClass:[NSObject class]];
[store pushRegistrationObject:first];
id second = [OCMockObject mockForClass:[Store class]];
[[second stub] isKindOfClass:OCMOCK_ANY];
[store pushRegistrationObject:second];
// execute
[store cancelCurrentObject];
// verify
STAssertNoThrow([first verify], nil);
STAssertNoThrow([second verify], nil);
assertThatInt(store.registrationStack.count, equalToInt(1));
assertThat(store.registrationStack.lastObject, equalTo(first));
assertThat(store.currentRegistrationObject, equalTo(first));
}];
}

- (void)testCancelCurrentObjectShouldIgnoreIfRegistrationStackIsEmpty {
[self runWithStore:^(Store *store) {
// execute
[store cancelCurrentObject];
// verify - real code logs a warning, but we don't test that here
// verify - real code logs a warning, but we don't test that here, just verify no exception is thrown
assertThatInt(store.registrationStack.count, equalToInt(0));
assertThat(store.currentRegistrationObject, equalTo(nil));
}];
Expand Down Expand Up @@ -596,6 +667,18 @@ - (void)testCancelCurrentObjectShouldRemoveLastStruct {
}];
}

- (void)testCancelCurrentObjectShouldRemoveLastConstant {
[self runWithStore:^(Store *store) {
// setup
[store beginConstant];
// execute
[store cancelCurrentObject];
// verify
assertThatInt(store.storeConstants.count, equalToInt(0));
assertThat(store.currentRegistrationObject, equalTo(nil));
}];
}

#pragma mark - Verify registration stack handling

- (void)testPushRegistrationObjectShouldAddObjectToRegistrationStack {
Expand Down
Loading

0 comments on commit a789509

Please sign in to comment.