2019-12-19 17:32:49 +00:00
|
|
|
/*
|
2023-04-06 09:44:16 +00:00
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
2016-09-07 22:37:59 +00:00
|
|
|
*
|
2019-12-19 17:32:49 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-09-07 22:37:59 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#import <XCTest/XCTest.h>
|
|
|
|
|
|
|
|
|
|
#import "IGListSectionMap.h"
|
|
|
|
|
#import "IGListTestSection.h"
|
|
|
|
|
#import "IGTestObject.h"
|
|
|
|
|
|
|
|
|
|
@interface IGListSectionMapTests : XCTestCase
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation IGListSectionMapTests
|
|
|
|
|
|
|
|
|
|
- (void)test_whenUpdatingItems_thatArraysAreEqual {
|
|
|
|
|
NSArray *objects = @[@0, @1, @2];
|
2017-05-01 14:10:52 +00:00
|
|
|
NSArray *sectionControllers = @[[IGListTestSection new], [IGListTestSection new], [IGListTestSection new]];
|
2016-09-07 22:37:59 +00:00
|
|
|
IGListSectionMap *map = [[IGListSectionMap alloc] initWithMapTable:[NSMapTable strongToStrongObjectsMapTable]];
|
|
|
|
|
[map updateWithObjects:objects sectionControllers:sectionControllers];
|
|
|
|
|
XCTAssertEqualObjects(objects, map.objects);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)test_whenUpdatingItems_thatSectionControllersAreMappedForSection {
|
|
|
|
|
NSArray *objects = @[@0, @1, @2];
|
2017-05-01 14:10:52 +00:00
|
|
|
NSArray *sectionControllers = @[[IGListTestSection new], [IGListTestSection new], [IGListTestSection new]];
|
2016-09-07 22:37:59 +00:00
|
|
|
IGListSectionMap *map = [[IGListSectionMap alloc] initWithMapTable:[NSMapTable strongToStrongObjectsMapTable]];
|
|
|
|
|
[map updateWithObjects:objects sectionControllers:sectionControllers];
|
|
|
|
|
XCTAssertEqualObjects([map sectionControllerForSection:1], sectionControllers[1]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)test_whenUpdatingItems_thatSectionControllersAreMappedForItem {
|
|
|
|
|
NSArray *objects = @[@0, @1, @2];
|
2017-05-01 14:10:52 +00:00
|
|
|
NSArray *sectionControllers = @[[IGListTestSection new], [IGListTestSection new], [IGListTestSection new]];
|
2016-09-07 22:37:59 +00:00
|
|
|
IGListSectionMap *map = [[IGListSectionMap alloc] initWithMapTable:[NSMapTable strongToStrongObjectsMapTable]];
|
|
|
|
|
[map updateWithObjects:objects sectionControllers:sectionControllers];
|
|
|
|
|
XCTAssertEqual([map sectionControllerForObject:objects[1]], sectionControllers[1]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)test_whenUpdatingItems_thatSectionsAreMappedForSectionController {
|
|
|
|
|
NSArray *objects = @[@0, @1, @2];
|
2017-05-01 14:10:52 +00:00
|
|
|
NSArray *sectionControllers = @[[IGListTestSection new], [IGListTestSection new], [IGListTestSection new]];
|
2016-09-07 22:37:59 +00:00
|
|
|
IGListSectionMap *map = [[IGListSectionMap alloc] initWithMapTable:[NSMapTable strongToStrongObjectsMapTable]];
|
|
|
|
|
[map updateWithObjects:objects sectionControllers:sectionControllers];
|
|
|
|
|
XCTAssertEqual([map sectionForSectionController:sectionControllers[1]], 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)test_whenUpdatingItems_withUnknownItem_thatSectionControllerIsNil {
|
|
|
|
|
NSArray *objects = @[@0, @1, @2];
|
2017-05-01 14:10:52 +00:00
|
|
|
NSArray *sectionControllers = @[[IGListTestSection new], [IGListTestSection new], [IGListTestSection new]];
|
2016-09-07 22:37:59 +00:00
|
|
|
IGListSectionMap *map = [[IGListSectionMap alloc] initWithMapTable:[NSMapTable strongToStrongObjectsMapTable]];
|
|
|
|
|
[map updateWithObjects:objects sectionControllers:sectionControllers];
|
|
|
|
|
XCTAssertNil([map sectionControllerForObject:@4]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)test_whenUpdatingItems_withSectionController_thatSectionIsNotFound {
|
|
|
|
|
NSArray *objects = @[@0, @1, @2];
|
|
|
|
|
NSArray *sectionControllers = @[[IGListTestSection new], [IGListTestSection new], [IGListTestSection new]];
|
|
|
|
|
IGListSectionMap *map = [[IGListSectionMap alloc] initWithMapTable:[NSMapTable strongToStrongObjectsMapTable]];
|
|
|
|
|
[map updateWithObjects:objects sectionControllers:sectionControllers];
|
|
|
|
|
XCTAssertEqual([map sectionForSectionController:[IGListTestSection new]], NSNotFound);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)test_whenEnumeratingMap_withStopFlagSet_thatEnumerationEndsEarly {
|
|
|
|
|
NSArray *objects = @[@0, @1, @2];
|
2017-05-01 14:10:52 +00:00
|
|
|
NSArray *sectionControllers = @[[IGListTestSection new], [IGListTestSection new], [IGListTestSection new]];
|
2016-09-07 22:37:59 +00:00
|
|
|
IGListSectionMap *map = [[IGListSectionMap alloc] initWithMapTable:[NSMapTable strongToStrongObjectsMapTable]];
|
|
|
|
|
[map updateWithObjects:objects sectionControllers:sectionControllers];
|
|
|
|
|
__block NSInteger counter = 0;
|
2017-04-19 15:17:56 +00:00
|
|
|
[map enumerateUsingBlock:^(id item, IGListSectionController * sectionController, NSInteger section, BOOL *stop) {
|
2016-09-07 22:37:59 +00:00
|
|
|
counter++;
|
|
|
|
|
*stop = section == 1;
|
|
|
|
|
}];
|
|
|
|
|
XCTAssertEqual(counter, 2);
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-07 23:31:08 +00:00
|
|
|
- (void)test_whenAccessingOOBSection_thatNilIsReturned {
|
|
|
|
|
NSArray *objects = @[@0, @1, @2];
|
2017-05-01 14:10:52 +00:00
|
|
|
NSArray *sectionControllers = @[[IGListTestSection new], [IGListTestSection new], [IGListTestSection new]];
|
2016-11-07 23:31:08 +00:00
|
|
|
IGListSectionMap *map = [[IGListSectionMap alloc] initWithMapTable:[NSMapTable strongToStrongObjectsMapTable]];
|
|
|
|
|
[map updateWithObjects:objects sectionControllers:sectionControllers];
|
|
|
|
|
XCTAssertNil([map objectForSection:4]);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-01 14:10:52 +00:00
|
|
|
- (void)test_whenUpdatingItems_thatSectionControllerIndexesAreUpdated {
|
|
|
|
|
NSArray *objects = @[@0, @1, @2];
|
|
|
|
|
|
|
|
|
|
IGListTestSection *one = [IGListTestSection new];
|
2017-05-11 21:46:22 +00:00
|
|
|
XCTAssertEqual(one.section, NSNotFound);
|
2017-05-01 14:10:52 +00:00
|
|
|
|
|
|
|
|
NSArray *sectionControllers = @[[IGListTestSection new], one, [IGListTestSection new]];
|
|
|
|
|
IGListSectionMap *map = [[IGListSectionMap alloc] initWithMapTable:[NSMapTable strongToStrongObjectsMapTable]];
|
|
|
|
|
[map updateWithObjects:objects sectionControllers:sectionControllers];
|
|
|
|
|
|
2017-05-11 21:46:22 +00:00
|
|
|
XCTAssertEqual(one.section, 1);
|
2017-05-01 14:10:52 +00:00
|
|
|
XCTAssertFalse(one.isFirstSection);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 03:41:49 +00:00
|
|
|
- (void)test_whenQueryingItems_thatNilReturnsNotFound {
|
|
|
|
|
NSObject *object = [NSObject new];
|
|
|
|
|
object = nil;
|
|
|
|
|
IGListSectionMap *map = [[IGListSectionMap alloc] initWithMapTable:[NSMapTable strongToStrongObjectsMapTable]];
|
|
|
|
|
XCTAssertEqual([map sectionForObject:object], NSNotFound);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 22:37:59 +00:00
|
|
|
@end
|