Skip to content

Commit aa537f6

Browse files
Andrey Mishaninfacebook-github-bot
authored andcommitted
Prettified description for index sets
Summary: The relevant code is in `CKIndexSetDescription.[h|mm]`. I also moved `makeIndexSet()` to a separate file because I needed it for `CKIndexSetDescriptionTests`. Reviewed By: kfirapps, kevin0571 Differential Revision: D7992079 fbshipit-source-id: 6fd1290a46244dd926c8dad7ccbe121231b05277
1 parent c4b5cea commit aa537f6

File tree

8 files changed

+163
-13
lines changed

8 files changed

+163
-13
lines changed

ComponentKit.xcodeproj/project.pbxproj

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,13 @@
728728
D0B47D9E1CBDA9AC00BB33CE /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D0B47D871CBDA24900BB33CE /* UIKit.framework */; };
729729
D0B47D9F1CBDA9B600BB33CE /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D0B47D8B1CBDA25E00BB33CE /* CoreGraphics.framework */; };
730730
D0B47DA01CBDA9C200BB33CE /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D0B47D8B1CBDA25E00BB33CE /* CoreGraphics.framework */; };
731+
D63F9F8120AAF31A003F3887 /* CKIndexSetDescription.h in Headers */ = {isa = PBXBuildFile; fileRef = D63F9F7620AAF31A003F3887 /* CKIndexSetDescription.h */; settings = {ATTRIBUTES = (Private, ); }; };
732+
D63F9F8320AAF487003F3887 /* CKIndexSetDescription.mm in Sources */ = {isa = PBXBuildFile; fileRef = D63F9F8220AAF487003F3887 /* CKIndexSetDescription.mm */; };
733+
D6A2652120AC333F00B7B499 /* CKIndexSetDescription.mm in Sources */ = {isa = PBXBuildFile; fileRef = D63F9F8220AAF487003F3887 /* CKIndexSetDescription.mm */; };
734+
D6A2652F20AC5B2100B7B499 /* NSIndexSetExtensions.mm in Sources */ = {isa = PBXBuildFile; fileRef = D6B0AE0020AAFB1000A31FDF /* NSIndexSetExtensions.mm */; };
735+
D6B0ADF420AAF88C00A31FDF /* CKIndexSetDescriptionTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = D6B0ADF320AAF88C00A31FDF /* CKIndexSetDescriptionTests.mm */; };
736+
D6B0AE0120AAFB1000A31FDF /* NSIndexSetExtensions.h in Headers */ = {isa = PBXBuildFile; fileRef = D6B0ADFF20AAFB1000A31FDF /* NSIndexSetExtensions.h */; };
737+
D6B0AE0220AAFB1000A31FDF /* NSIndexSetExtensions.mm in Sources */ = {isa = PBXBuildFile; fileRef = D6B0AE0020AAFB1000A31FDF /* NSIndexSetExtensions.mm */; };
731738
EB14A3411D8267DF0004BECF /* CKAutoSizedImageComponent.h in Headers */ = {isa = PBXBuildFile; fileRef = EB14A33F1D8267DF0004BECF /* CKAutoSizedImageComponent.h */; settings = {ATTRIBUTES = (Public, ); }; };
732739
EB14A3421D8267DF0004BECF /* CKAutoSizedImageComponent.h in Headers */ = {isa = PBXBuildFile; fileRef = EB14A33F1D8267DF0004BECF /* CKAutoSizedImageComponent.h */; settings = {ATTRIBUTES = (Public, ); }; };
733740
EB14A3431D8267DF0004BECF /* CKAutoSizedImageComponent.mm in Sources */ = {isa = PBXBuildFile; fileRef = EB14A3401D8267DF0004BECF /* CKAutoSizedImageComponent.mm */; };
@@ -1286,6 +1293,11 @@
12861293
D0B47D8B1CBDA25E00BB33CE /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
12871294
D0B47D971CBDA97400BB33CE /* CKComponentSnapshotTestCase.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CKComponentSnapshotTestCase.h; sourceTree = "<group>"; };
12881295
D0B47D981CBDA97400BB33CE /* CKComponentSnapshotTestCase.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; lineEnding = 0; path = CKComponentSnapshotTestCase.mm; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
1296+
D63F9F7620AAF31A003F3887 /* CKIndexSetDescription.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CKIndexSetDescription.h; sourceTree = "<group>"; };
1297+
D63F9F8220AAF487003F3887 /* CKIndexSetDescription.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = CKIndexSetDescription.mm; sourceTree = "<group>"; };
1298+
D6B0ADF320AAF88C00A31FDF /* CKIndexSetDescriptionTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = CKIndexSetDescriptionTests.mm; sourceTree = "<group>"; };
1299+
D6B0ADFF20AAFB1000A31FDF /* NSIndexSetExtensions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NSIndexSetExtensions.h; sourceTree = "<group>"; };
1300+
D6B0AE0020AAFB1000A31FDF /* NSIndexSetExtensions.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = NSIndexSetExtensions.mm; sourceTree = "<group>"; };
12891301
D6B7E9BF1FCF4C6500FAD4E0 /* CKComponentScopeFrameInternal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CKComponentScopeFrameInternal.h; sourceTree = "<group>"; };
12901302
EB14A33F1D8267DF0004BECF /* CKAutoSizedImageComponent.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CKAutoSizedImageComponent.h; sourceTree = "<group>"; };
12911303
EB14A3401D8267DF0004BECF /* CKAutoSizedImageComponent.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CKAutoSizedImageComponent.mm; sourceTree = "<group>"; };
@@ -1466,6 +1478,8 @@
14661478
A273801E1AFD144100E6F222 /* CKTestActionComponent.mm */,
14671479
035FD04A1D83218100D28351 /* CKTestRunLoopRunning.h */,
14681480
035FD04B1D83218100D28351 /* CKTestRunLoopRunning.mm */,
1481+
D6B0ADFF20AAFB1000A31FDF /* NSIndexSetExtensions.h */,
1482+
D6B0AE0020AAFB1000A31FDF /* NSIndexSetExtensions.mm */,
14691483
);
14701484
path = ComponentKitTestHelpers;
14711485
sourceTree = "<group>";
@@ -1524,6 +1538,7 @@
15241538
B342DC601AC23EA900ACAC53 /* CKComponentViewReuseTests.mm */,
15251539
B342DC611AC23EA900ACAC53 /* CKDimensionTests.mm */,
15261540
7F80A4E61F63128B00242503 /* CKFlexboxComponentTests.mm */,
1541+
D6B0ADF320AAF88C00A31FDF /* CKIndexSetDescriptionTests.mm */,
15271542
B342DC621AC23EA900ACAC53 /* CKOptimisticViewMutationsTests.mm */,
15281543
A22FE3041AF2CF0C00EC30B8 /* CKStateExposingComponent.h */,
15291544
A22FE3051AF2CF0C00EC30B8 /* CKStateExposingComponent.mm */,
@@ -1839,6 +1854,8 @@
18391854
D0B47B361CBD926700BB33CE /* CKComponentDebugController.mm */,
18401855
D0B47B371CBD926700BB33CE /* CKComponentHierarchyDebugHelper.h */,
18411856
D0B47B381CBD926700BB33CE /* CKComponentHierarchyDebugHelper.mm */,
1857+
D63F9F7620AAF31A003F3887 /* CKIndexSetDescription.h */,
1858+
D63F9F8220AAF487003F3887 /* CKIndexSetDescription.mm */,
18421859
D0B47B391CBD926700BB33CE /* ComponentKit+QuickLook.mm */,
18431860
);
18441861
path = Debug;
@@ -2291,6 +2308,7 @@
22912308
buildActionMask = 2147483647;
22922309
files = (
22932310
7F98F58C1F67F2F600436978 /* CKLifecycleTestComponent.h in Headers */,
2311+
D6B0AE0120AAFB1000A31FDF /* NSIndexSetExtensions.h in Headers */,
22942312
A27380341AFD172500E6F222 /* CKTestActionComponent.h in Headers */,
22952313
7FA099141F66F36800B81EED /* CKComponentLifecycleTestHelper.h in Headers */,
22962314
);
@@ -2322,6 +2340,7 @@
23222340
D0B47D161CBD948E00BB33CE /* CKComponentAnnouncerBase.h in Headers */,
23232341
D0B47CF61CBD948E00BB33CE /* CKComponentLayout.h in Headers */,
23242342
D0B47D3F1CBD948E00BB33CE /* CKDataSourceAppliedChanges.h in Headers */,
2343+
D63F9F8120AAF31A003F3887 /* CKIndexSetDescription.h in Headers */,
23252344
D0B47D3C1CBD948E00BB33CE /* CKStatefulViewComponentController.h in Headers */,
23262345
D0B47CEF1CBD948E00BB33CE /* CKComponent.h in Headers */,
23272346
D0B47CF31CBD948E00BB33CE /* CKComponentController.h in Headers */,
@@ -2846,6 +2865,7 @@
28462865
7F27E2C11F69CC0F00573E14 /* CKLifecycleTestComponent.mm in Sources */,
28472866
03A98A6B1D2B2BFD00C5BAC2 /* CKTestActionComponent.mm in Sources */,
28482867
7F27E2CF1F69E3FC00573E14 /* CKComponentLifecycleTestHelper.mm in Sources */,
2868+
D6A2652F20AC5B2100B7B499 /* NSIndexSetExtensions.mm in Sources */,
28492869
2D8270F71E3F72F1008C1A26 /* CKTestRunLoopRunning.mm in Sources */,
28502870
);
28512871
runOnlyForDeploymentPostprocessing = 0;
@@ -2954,6 +2974,7 @@
29542974
03B8B4BF1D2A346F00EDFF59 /* CKTextKitAttributes.mm in Sources */,
29552975
03B8B4C01D2A346F00EDFF59 /* CKTextKitContext.mm in Sources */,
29562976
03B8B4C11D2A346F00EDFF59 /* CKTextKitEntityAttribute.m in Sources */,
2977+
D6A2652120AC333F00B7B499 /* CKIndexSetDescription.mm in Sources */,
29572978
03B8B4C21D2A346F00EDFF59 /* CKTextKitRenderer+Positioning.mm in Sources */,
29582979
03B8B4C31D2A346F00EDFF59 /* CKTextKitRenderer+TextChecking.mm in Sources */,
29592980
03B8B4C41D2A346F00EDFF59 /* CKTextKitRenderer.mm in Sources */,
@@ -3030,6 +3051,7 @@
30303051
7F98F58D1F67F2F600436978 /* CKLifecycleTestComponent.mm in Sources */,
30313052
A273801F1AFD144100E6F222 /* CKTestActionComponent.mm in Sources */,
30323053
2D8270F61E3F72DE008C1A26 /* CKTestRunLoopRunning.mm in Sources */,
3054+
D6B0AE0220AAFB1000A31FDF /* NSIndexSetExtensions.mm in Sources */,
30333055
7FA099151F66F36800B81EED /* CKComponentLifecycleTestHelper.mm in Sources */,
30343056
);
30353057
runOnlyForDeploymentPostprocessing = 0;
@@ -3060,6 +3082,7 @@
30603082
A25C02D11AF0767700F4C864 /* CKDataSourceChangesetModificationTests.mm in Sources */,
30613083
23FEC5AA203B31230068E09D /* CKTreeNodeTests.mm in Sources */,
30623084
A27436FA1AE9589700832359 /* CKDataSourceStateTestHelpers.mm in Sources */,
3085+
D6B0ADF420AAF88C00A31FDF /* CKIndexSetDescriptionTests.mm in Sources */,
30633086
51EDD905207E79C400892C35 /* CKBuildAndLayoutComponentTests.mm in Sources */,
30643087
39B090BF1B71645600A5470B /* CKComponentDataSourceAttachControllerTests.mm in Sources */,
30653088
B342DC741AC23EA900ACAC53 /* CKComponentHostingViewTestModel.mm in Sources */,
@@ -3161,6 +3184,7 @@
31613184
D0B47C971CBD943400BB33CE /* ComponentLayoutContext.mm in Sources */,
31623185
D0B47C981CBD943400BB33CE /* ComponentViewManager.mm in Sources */,
31633186
D0B47C991CBD943400BB33CE /* ComponentViewReuseUtilities.mm in Sources */,
3187+
D63F9F8320AAF487003F3887 /* CKIndexSetDescription.mm in Sources */,
31643188
D0B47C9A1CBD943400BB33CE /* CKComponentScope.mm in Sources */,
31653189
D0B47C9B1CBD943400BB33CE /* CKComponentScopeFrame.mm in Sources */,
31663190
D0B47C9C1CBD943400BB33CE /* CKComponentScopeHandle.mm in Sources */,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright (c) 2014-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
*/
10+
11+
#import <Foundation/Foundation.h>
12+
13+
namespace CK {
14+
auto indexSetDescription(NSIndexSet *const is, NSString *const title = @"", const int indent = 0) -> NSString *;
15+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) 2014-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
*/
10+
11+
#import "CKIndexSetDescription.h"
12+
13+
auto CK::indexSetDescription(NSIndexSet *const is, NSString *const title, const int indent) -> NSString *
14+
{
15+
auto rangeStrings = static_cast<NSMutableArray<NSString *> *>([NSMutableArray array]);
16+
[is enumerateRangesUsingBlock:^(NSRange range, BOOL * _Nonnull) {
17+
const auto rangeStr = range.length > 1 ?
18+
[NSString stringWithFormat:@"%lu%lu", (unsigned long)range.location, (unsigned long)NSMaxRange(range) - 1] :
19+
[NSString stringWithFormat:@"%lu", (unsigned long)range.location];
20+
[rangeStrings addObject:rangeStr];
21+
}];
22+
const auto description = [rangeStrings componentsJoinedByString:@", "];
23+
const auto titleIndentStr = [@"" stringByPaddingToLength:indent withString:@" " startingAtIndex:0];
24+
return title.length > 0 ? [NSString stringWithFormat:@"%@%@: %@", titleIndentStr, title, description] : description;
25+
}

ComponentKit/TransactionalDataSources/Common/Internal/CKDataSourceChangesetModification.mm

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#import "CKComponentScopeRoot.h"
2929
#import "CKComponentScopeRootFactory.h"
3030
#import "CKDataSourceModificationHelper.h"
31+
#import "CKIndexSetDescription.h"
3132

3233
@implementation CKDataSourceChangesetModification
3334
{
@@ -177,8 +178,8 @@ - (CKDataSourceChange *)changeFromState:(CKDataSourceState *)oldState
177178
const auto sectionItems = (NSArray *)[newSections objectAtIndex:sectionIt.first];
178179
const auto invalidIndexes = CK::invalidIndexesForInsertionInArray(sectionItems, indexes);
179180
if (invalidIndexes.count > 0) {
180-
CKCFatal(@"Invalid indexes: %@ for range: %@ in section: %lu. Changeset: %@, user info: %@",
181-
invalidIndexes,
181+
CKCFatal(@"%@ for range: %@ in section: %lu. Changeset: %@, user info: %@",
182+
CK::indexSetDescription(invalidIndexes, @"Invalid indexes", 0),
182183
NSStringFromRange({0, sectionItems.count}),
183184
(unsigned long)sectionIt.first,
184185
_changeset,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright (c) 2014-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
*/
10+
11+
#import <Foundation/Foundation.h>
12+
13+
#import <vector>
14+
15+
namespace CK {
16+
auto makeIndexSet(const std::vector<NSUInteger> v) -> NSIndexSet *;
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (c) 2014-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
*/
10+
11+
#import "NSIndexSetExtensions.h"
12+
13+
auto CK::makeIndexSet(const std::vector<NSUInteger> v) -> NSIndexSet *
14+
{
15+
auto r = [NSMutableIndexSet new];
16+
for (const auto &i : v) {
17+
[r addIndex:i];
18+
}
19+
return r;
20+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2004-present Facebook. All Rights Reserved.
2+
3+
#import <XCTest/XCTest.h>
4+
5+
#import <ComponentKit/CKIndexSetDescription.h>
6+
#import <ComponentKitTestHelpers/NSIndexSetExtensions.h>
7+
8+
@interface CKIndexSetDescriptionTests : XCTestCase
9+
@end
10+
11+
@implementation CKIndexSetDescriptionTests
12+
13+
- (void)test_WhenIndexSetIsEmpty_ReturnsEmptyString
14+
{
15+
XCTAssertEqualObjects(CK::indexSetDescription([NSIndexSet new]), @"");
16+
}
17+
18+
- (void)test_WhenIndexSetHasOneIndex_ReturnsTheIndexAsString
19+
{
20+
XCTAssertEqualObjects(CK::indexSetDescription(CK::makeIndexSet({1})), @"1");
21+
}
22+
23+
- (void)test_WhenIndexSetHasDisjointIndexes_ReturnsIndexesSeparatedByComma
24+
{
25+
XCTAssertEqualObjects(CK::indexSetDescription(CK::makeIndexSet({1, 3})), @"1, 3");
26+
}
27+
28+
- (void)test_WhenIndexSetHasAdjacentIndexes_CombinesThemIntoOneRange
29+
{
30+
XCTAssertEqualObjects(CK::indexSetDescription(CK::makeIndexSet({1, 2, 3})), @"1–3");
31+
}
32+
33+
@end
34+
35+
@interface CKIndexSetDescriptionTests_WithTitleAndIndent : XCTestCase
36+
@end
37+
38+
@implementation CKIndexSetDescriptionTests_WithTitleAndIndent
39+
40+
- (void)test_WhenIndentIsNonZero_AddsIndentBeforeTitle
41+
{
42+
const auto is = CK::makeIndexSet({1, 2, 4});
43+
const auto expectedDescription = @" Removed Sections: 1–2, 4";
44+
45+
XCTAssertEqualObjects(CK::indexSetDescription(is, @"Removed Sections", 2), expectedDescription);
46+
}
47+
48+
- (void)test_WhenIndentIsZero_AddsOnlyTitle
49+
{
50+
const auto is = CK::makeIndexSet({1, 2, 4});
51+
const auto expectedDescription = @"Removed Sections: 1–2, 4";
52+
53+
XCTAssertEqualObjects(CK::indexSetDescription(is, @"Removed Sections"), expectedDescription);
54+
}
55+
56+
@end

ComponentKitTests/TransactionalDataSource/CKDataSourceChangesetModificationTests.mm

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#import <ComponentKit/CKDataSourceItem.h>
2222
#import <ComponentKit/CKDataSourceChangesetModification.h>
2323
#import <ComponentKit/CKDataSourceState.h>
24+
#import <ComponentKitTestHelpers/NSIndexSetExtensions.h>
2425

2526
#import "CKDataSourceStateTestHelpers.h"
2627

@@ -284,18 +285,9 @@ - (void)test_WhenFirstInsertionLocationIsGreaterThanCount_IsNotValid
284285
- (void)test_WhenOtherInsertionLocationIsGreaterThanCount_IsNotValid
285286
{
286287
const auto array = @[@"one"];
287-
const auto indexes = makeIndexSet({1, 3});
288+
const auto indexes = CK::makeIndexSet({1, 3});
288289

289-
XCTAssertEqualObjects(CK::invalidIndexesForInsertionInArray(array, indexes), makeIndexSet({3}));
290-
}
291-
292-
static auto makeIndexSet(const std::vector<NSUInteger> v) -> NSIndexSet *
293-
{
294-
auto r = [NSMutableIndexSet new];
295-
for (const auto &i : v) {
296-
[r addIndex:i];
297-
}
298-
return r;
290+
XCTAssertEqualObjects(CK::invalidIndexesForInsertionInArray(array, indexes), CK::makeIndexSet({3}));
299291
}
300292

301293
@end

0 commit comments

Comments
 (0)