Add assert for ASCollectionDataSource implementing collectionView:constrainedSizeForNodeAtIndexPath: instead of ASCollectionDelegate (#2165)

This commit is contained in:
Michael Schneider
2016-08-29 16:44:11 -07:00
committed by Adlai Holler
parent 8d86bb6db3
commit 1849c41b03
2 changed files with 74 additions and 0 deletions

View File

@@ -10,6 +10,7 @@
#import <XCTest/XCTest.h>
#import <UIKit/UIKit.h>
#import <OCMock/OCMock.h>
#import "ASCollectionView.h"
#import "ASCollectionViewFlowLayoutInspector.h"
@@ -45,6 +46,27 @@
@end
@protocol InspectorTestDataSourceDelegateProtocol <ASCollectionDataSource, ASCollectionDelegate>
@end
@interface InspectorTestDataSourceDelegateWithoutNodeConstrainedSize : NSObject <InspectorTestDataSourceDelegateProtocol>
@end
@implementation InspectorTestDataSourceDelegateWithoutNodeConstrainedSize
- (ASCellNodeBlock)collectionView:(ASCollectionView *)collectionView nodeBlockForItemAtIndexPath:(NSIndexPath *)indexPath
{
return ^{ return [[ASCellNode alloc] init]; };
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 0;
}
@end
@interface ASCollectionViewFlowLayoutInspectorTests : XCTestCase
@end
@@ -345,4 +367,50 @@
collectionView.asyncDelegate = nil;
}
- (void)testThatItThrowsIfNodeConstrainedSizeIsImplementedOnDataSourceButNotOnDelegateLayoutInspector
{
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
ASCollectionView *collectionView = [[ASCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
id dataSourceAndDelegate = [OCMockObject mockForProtocol:@protocol(InspectorTestDataSourceDelegateProtocol)];
ASSizeRange constrainedSize = ASSizeRangeMake(CGSizeZero, CGSizeZero);
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
NSValue *value = [NSValue value:&constrainedSize withObjCType:@encode(ASSizeRange)];
[[[dataSourceAndDelegate stub] andReturnValue:value] collectionView:collectionView constrainedSizeForNodeAtIndexPath:indexPath];
collectionView.asyncDataSource = dataSourceAndDelegate;
id delegate = [InspectorTestDataSourceDelegateWithoutNodeConstrainedSize new];
collectionView.asyncDelegate = delegate;
ASCollectionViewLayoutInspector *inspector = [[ASCollectionViewLayoutInspector alloc] initWithCollectionView:collectionView];
collectionView.layoutInspector = inspector;
XCTAssertThrows([inspector collectionView:collectionView constrainedSizeForNodeAtIndexPath:indexPath]);
collectionView.asyncDelegate = dataSourceAndDelegate;
XCTAssertNoThrow([inspector collectionView:collectionView constrainedSizeForNodeAtIndexPath:indexPath]);
}
- (void)testThatItThrowsIfNodeConstrainedSizeIsImplementedOnDataSourceButNotOnDelegateFlowLayoutInspector
{
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
ASCollectionView *collectionView = [[ASCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
id dataSourceAndDelegate = [OCMockObject mockForProtocol:@protocol(InspectorTestDataSourceDelegateProtocol)];
ASSizeRange constrainedSize = ASSizeRangeMake(CGSizeZero, CGSizeZero);
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
NSValue *value = [NSValue value:&constrainedSize withObjCType:@encode(ASSizeRange)];
[[[dataSourceAndDelegate stub] andReturnValue:value] collectionView:collectionView constrainedSizeForNodeAtIndexPath:indexPath];
collectionView.asyncDataSource = dataSourceAndDelegate;
id delegate = [InspectorTestDataSourceDelegateWithoutNodeConstrainedSize new];
collectionView.asyncDelegate = delegate;
ASCollectionViewFlowLayoutInspector *inspector = collectionView.layoutInspector;
XCTAssertThrows([inspector collectionView:collectionView constrainedSizeForNodeAtIndexPath:indexPath]);
collectionView.asyncDelegate = dataSourceAndDelegate;
XCTAssertNoThrow([inspector collectionView:collectionView constrainedSizeForNodeAtIndexPath:indexPath]);
}
@end