Skip to content

Commit

Permalink
Prettified description for index sets
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Andrey Mishanin authored and facebook-github-bot committed May 17, 2018
1 parent c4b5cea commit aa537f6
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 13 deletions.
24 changes: 24 additions & 0 deletions ComponentKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,13 @@
D0B47D9E1CBDA9AC00BB33CE /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D0B47D871CBDA24900BB33CE /* UIKit.framework */; };
D0B47D9F1CBDA9B600BB33CE /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D0B47D8B1CBDA25E00BB33CE /* CoreGraphics.framework */; };
D0B47DA01CBDA9C200BB33CE /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D0B47D8B1CBDA25E00BB33CE /* CoreGraphics.framework */; };
D63F9F8120AAF31A003F3887 /* CKIndexSetDescription.h in Headers */ = {isa = PBXBuildFile; fileRef = D63F9F7620AAF31A003F3887 /* CKIndexSetDescription.h */; settings = {ATTRIBUTES = (Private, ); }; };
D63F9F8320AAF487003F3887 /* CKIndexSetDescription.mm in Sources */ = {isa = PBXBuildFile; fileRef = D63F9F8220AAF487003F3887 /* CKIndexSetDescription.mm */; };
D6A2652120AC333F00B7B499 /* CKIndexSetDescription.mm in Sources */ = {isa = PBXBuildFile; fileRef = D63F9F8220AAF487003F3887 /* CKIndexSetDescription.mm */; };
D6A2652F20AC5B2100B7B499 /* NSIndexSetExtensions.mm in Sources */ = {isa = PBXBuildFile; fileRef = D6B0AE0020AAFB1000A31FDF /* NSIndexSetExtensions.mm */; };
D6B0ADF420AAF88C00A31FDF /* CKIndexSetDescriptionTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = D6B0ADF320AAF88C00A31FDF /* CKIndexSetDescriptionTests.mm */; };
D6B0AE0120AAFB1000A31FDF /* NSIndexSetExtensions.h in Headers */ = {isa = PBXBuildFile; fileRef = D6B0ADFF20AAFB1000A31FDF /* NSIndexSetExtensions.h */; };
D6B0AE0220AAFB1000A31FDF /* NSIndexSetExtensions.mm in Sources */ = {isa = PBXBuildFile; fileRef = D6B0AE0020AAFB1000A31FDF /* NSIndexSetExtensions.mm */; };
EB14A3411D8267DF0004BECF /* CKAutoSizedImageComponent.h in Headers */ = {isa = PBXBuildFile; fileRef = EB14A33F1D8267DF0004BECF /* CKAutoSizedImageComponent.h */; settings = {ATTRIBUTES = (Public, ); }; };
EB14A3421D8267DF0004BECF /* CKAutoSizedImageComponent.h in Headers */ = {isa = PBXBuildFile; fileRef = EB14A33F1D8267DF0004BECF /* CKAutoSizedImageComponent.h */; settings = {ATTRIBUTES = (Public, ); }; };
EB14A3431D8267DF0004BECF /* CKAutoSizedImageComponent.mm in Sources */ = {isa = PBXBuildFile; fileRef = EB14A3401D8267DF0004BECF /* CKAutoSizedImageComponent.mm */; };
Expand Down Expand Up @@ -1286,6 +1293,11 @@
D0B47D8B1CBDA25E00BB33CE /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
D0B47D971CBDA97400BB33CE /* CKComponentSnapshotTestCase.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CKComponentSnapshotTestCase.h; sourceTree = "<group>"; };
D0B47D981CBDA97400BB33CE /* CKComponentSnapshotTestCase.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; lineEnding = 0; path = CKComponentSnapshotTestCase.mm; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
D63F9F7620AAF31A003F3887 /* CKIndexSetDescription.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CKIndexSetDescription.h; sourceTree = "<group>"; };
D63F9F8220AAF487003F3887 /* CKIndexSetDescription.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = CKIndexSetDescription.mm; sourceTree = "<group>"; };
D6B0ADF320AAF88C00A31FDF /* CKIndexSetDescriptionTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = CKIndexSetDescriptionTests.mm; sourceTree = "<group>"; };
D6B0ADFF20AAFB1000A31FDF /* NSIndexSetExtensions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NSIndexSetExtensions.h; sourceTree = "<group>"; };
D6B0AE0020AAFB1000A31FDF /* NSIndexSetExtensions.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = NSIndexSetExtensions.mm; sourceTree = "<group>"; };
D6B7E9BF1FCF4C6500FAD4E0 /* CKComponentScopeFrameInternal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CKComponentScopeFrameInternal.h; sourceTree = "<group>"; };
EB14A33F1D8267DF0004BECF /* CKAutoSizedImageComponent.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CKAutoSizedImageComponent.h; sourceTree = "<group>"; };
EB14A3401D8267DF0004BECF /* CKAutoSizedImageComponent.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CKAutoSizedImageComponent.mm; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1466,6 +1478,8 @@
A273801E1AFD144100E6F222 /* CKTestActionComponent.mm */,
035FD04A1D83218100D28351 /* CKTestRunLoopRunning.h */,
035FD04B1D83218100D28351 /* CKTestRunLoopRunning.mm */,
D6B0ADFF20AAFB1000A31FDF /* NSIndexSetExtensions.h */,
D6B0AE0020AAFB1000A31FDF /* NSIndexSetExtensions.mm */,
);
path = ComponentKitTestHelpers;
sourceTree = "<group>";
Expand Down Expand Up @@ -1524,6 +1538,7 @@
B342DC601AC23EA900ACAC53 /* CKComponentViewReuseTests.mm */,
B342DC611AC23EA900ACAC53 /* CKDimensionTests.mm */,
7F80A4E61F63128B00242503 /* CKFlexboxComponentTests.mm */,
D6B0ADF320AAF88C00A31FDF /* CKIndexSetDescriptionTests.mm */,
B342DC621AC23EA900ACAC53 /* CKOptimisticViewMutationsTests.mm */,
A22FE3041AF2CF0C00EC30B8 /* CKStateExposingComponent.h */,
A22FE3051AF2CF0C00EC30B8 /* CKStateExposingComponent.mm */,
Expand Down Expand Up @@ -1839,6 +1854,8 @@
D0B47B361CBD926700BB33CE /* CKComponentDebugController.mm */,
D0B47B371CBD926700BB33CE /* CKComponentHierarchyDebugHelper.h */,
D0B47B381CBD926700BB33CE /* CKComponentHierarchyDebugHelper.mm */,
D63F9F7620AAF31A003F3887 /* CKIndexSetDescription.h */,
D63F9F8220AAF487003F3887 /* CKIndexSetDescription.mm */,
D0B47B391CBD926700BB33CE /* ComponentKit+QuickLook.mm */,
);
path = Debug;
Expand Down Expand Up @@ -2291,6 +2308,7 @@
buildActionMask = 2147483647;
files = (
7F98F58C1F67F2F600436978 /* CKLifecycleTestComponent.h in Headers */,
D6B0AE0120AAFB1000A31FDF /* NSIndexSetExtensions.h in Headers */,
A27380341AFD172500E6F222 /* CKTestActionComponent.h in Headers */,
7FA099141F66F36800B81EED /* CKComponentLifecycleTestHelper.h in Headers */,
);
Expand Down Expand Up @@ -2322,6 +2340,7 @@
D0B47D161CBD948E00BB33CE /* CKComponentAnnouncerBase.h in Headers */,
D0B47CF61CBD948E00BB33CE /* CKComponentLayout.h in Headers */,
D0B47D3F1CBD948E00BB33CE /* CKDataSourceAppliedChanges.h in Headers */,
D63F9F8120AAF31A003F3887 /* CKIndexSetDescription.h in Headers */,
D0B47D3C1CBD948E00BB33CE /* CKStatefulViewComponentController.h in Headers */,
D0B47CEF1CBD948E00BB33CE /* CKComponent.h in Headers */,
D0B47CF31CBD948E00BB33CE /* CKComponentController.h in Headers */,
Expand Down Expand Up @@ -2846,6 +2865,7 @@
7F27E2C11F69CC0F00573E14 /* CKLifecycleTestComponent.mm in Sources */,
03A98A6B1D2B2BFD00C5BAC2 /* CKTestActionComponent.mm in Sources */,
7F27E2CF1F69E3FC00573E14 /* CKComponentLifecycleTestHelper.mm in Sources */,
D6A2652F20AC5B2100B7B499 /* NSIndexSetExtensions.mm in Sources */,
2D8270F71E3F72F1008C1A26 /* CKTestRunLoopRunning.mm in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -2954,6 +2974,7 @@
03B8B4BF1D2A346F00EDFF59 /* CKTextKitAttributes.mm in Sources */,
03B8B4C01D2A346F00EDFF59 /* CKTextKitContext.mm in Sources */,
03B8B4C11D2A346F00EDFF59 /* CKTextKitEntityAttribute.m in Sources */,
D6A2652120AC333F00B7B499 /* CKIndexSetDescription.mm in Sources */,
03B8B4C21D2A346F00EDFF59 /* CKTextKitRenderer+Positioning.mm in Sources */,
03B8B4C31D2A346F00EDFF59 /* CKTextKitRenderer+TextChecking.mm in Sources */,
03B8B4C41D2A346F00EDFF59 /* CKTextKitRenderer.mm in Sources */,
Expand Down Expand Up @@ -3030,6 +3051,7 @@
7F98F58D1F67F2F600436978 /* CKLifecycleTestComponent.mm in Sources */,
A273801F1AFD144100E6F222 /* CKTestActionComponent.mm in Sources */,
2D8270F61E3F72DE008C1A26 /* CKTestRunLoopRunning.mm in Sources */,
D6B0AE0220AAFB1000A31FDF /* NSIndexSetExtensions.mm in Sources */,
7FA099151F66F36800B81EED /* CKComponentLifecycleTestHelper.mm in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -3060,6 +3082,7 @@
A25C02D11AF0767700F4C864 /* CKDataSourceChangesetModificationTests.mm in Sources */,
23FEC5AA203B31230068E09D /* CKTreeNodeTests.mm in Sources */,
A27436FA1AE9589700832359 /* CKDataSourceStateTestHelpers.mm in Sources */,
D6B0ADF420AAF88C00A31FDF /* CKIndexSetDescriptionTests.mm in Sources */,
51EDD905207E79C400892C35 /* CKBuildAndLayoutComponentTests.mm in Sources */,
39B090BF1B71645600A5470B /* CKComponentDataSourceAttachControllerTests.mm in Sources */,
B342DC741AC23EA900ACAC53 /* CKComponentHostingViewTestModel.mm in Sources */,
Expand Down Expand Up @@ -3161,6 +3184,7 @@
D0B47C971CBD943400BB33CE /* ComponentLayoutContext.mm in Sources */,
D0B47C981CBD943400BB33CE /* ComponentViewManager.mm in Sources */,
D0B47C991CBD943400BB33CE /* ComponentViewReuseUtilities.mm in Sources */,
D63F9F8320AAF487003F3887 /* CKIndexSetDescription.mm in Sources */,
D0B47C9A1CBD943400BB33CE /* CKComponentScope.mm in Sources */,
D0B47C9B1CBD943400BB33CE /* CKComponentScopeFrame.mm in Sources */,
D0B47C9C1CBD943400BB33CE /* CKComponentScopeHandle.mm in Sources */,
Expand Down
15 changes: 15 additions & 0 deletions ComponentKit/Debug/CKIndexSetDescription.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

#import <Foundation/Foundation.h>

namespace CK {
auto indexSetDescription(NSIndexSet *const is, NSString *const title = @"", const int indent = 0) -> NSString *;
}
25 changes: 25 additions & 0 deletions ComponentKit/Debug/CKIndexSetDescription.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

#import "CKIndexSetDescription.h"

auto CK::indexSetDescription(NSIndexSet *const is, NSString *const title, const int indent) -> NSString *
{
auto rangeStrings = static_cast<NSMutableArray<NSString *> *>([NSMutableArray array]);
[is enumerateRangesUsingBlock:^(NSRange range, BOOL * _Nonnull) {
const auto rangeStr = range.length > 1 ?
[NSString stringWithFormat:@"%lu%lu", (unsigned long)range.location, (unsigned long)NSMaxRange(range) - 1] :
[NSString stringWithFormat:@"%lu", (unsigned long)range.location];
[rangeStrings addObject:rangeStr];
}];
const auto description = [rangeStrings componentsJoinedByString:@", "];
const auto titleIndentStr = [@"" stringByPaddingToLength:indent withString:@" " startingAtIndex:0];
return title.length > 0 ? [NSString stringWithFormat:@"%@%@: %@", titleIndentStr, title, description] : description;
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#import "CKComponentScopeRoot.h"
#import "CKComponentScopeRootFactory.h"
#import "CKDataSourceModificationHelper.h"
#import "CKIndexSetDescription.h"

@implementation CKDataSourceChangesetModification
{
Expand Down Expand Up @@ -177,8 +178,8 @@ - (CKDataSourceChange *)changeFromState:(CKDataSourceState *)oldState
const auto sectionItems = (NSArray *)[newSections objectAtIndex:sectionIt.first];
const auto invalidIndexes = CK::invalidIndexesForInsertionInArray(sectionItems, indexes);
if (invalidIndexes.count > 0) {
CKCFatal(@"Invalid indexes: %@ for range: %@ in section: %lu. Changeset: %@, user info: %@",
invalidIndexes,
CKCFatal(@"%@ for range: %@ in section: %lu. Changeset: %@, user info: %@",
CK::indexSetDescription(invalidIndexes, @"Invalid indexes", 0),
NSStringFromRange({0, sectionItems.count}),
(unsigned long)sectionIt.first,
_changeset,
Expand Down
17 changes: 17 additions & 0 deletions ComponentKitTestHelpers/NSIndexSetExtensions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

#import <Foundation/Foundation.h>

#import <vector>

namespace CK {
auto makeIndexSet(const std::vector<NSUInteger> v) -> NSIndexSet *;
}
20 changes: 20 additions & 0 deletions ComponentKitTestHelpers/NSIndexSetExtensions.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

#import "NSIndexSetExtensions.h"

auto CK::makeIndexSet(const std::vector<NSUInteger> v) -> NSIndexSet *
{
auto r = [NSMutableIndexSet new];
for (const auto &i : v) {
[r addIndex:i];
}
return r;
}
56 changes: 56 additions & 0 deletions ComponentKitTests/CKIndexSetDescriptionTests.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2004-present Facebook. All Rights Reserved.

#import <XCTest/XCTest.h>

#import <ComponentKit/CKIndexSetDescription.h>
#import <ComponentKitTestHelpers/NSIndexSetExtensions.h>

@interface CKIndexSetDescriptionTests : XCTestCase
@end

@implementation CKIndexSetDescriptionTests

- (void)test_WhenIndexSetIsEmpty_ReturnsEmptyString
{
XCTAssertEqualObjects(CK::indexSetDescription([NSIndexSet new]), @"");
}

- (void)test_WhenIndexSetHasOneIndex_ReturnsTheIndexAsString
{
XCTAssertEqualObjects(CK::indexSetDescription(CK::makeIndexSet({1})), @"1");
}

- (void)test_WhenIndexSetHasDisjointIndexes_ReturnsIndexesSeparatedByComma
{
XCTAssertEqualObjects(CK::indexSetDescription(CK::makeIndexSet({1, 3})), @"1, 3");
}

- (void)test_WhenIndexSetHasAdjacentIndexes_CombinesThemIntoOneRange
{
XCTAssertEqualObjects(CK::indexSetDescription(CK::makeIndexSet({1, 2, 3})), @"1–3");
}

@end

@interface CKIndexSetDescriptionTests_WithTitleAndIndent : XCTestCase
@end

@implementation CKIndexSetDescriptionTests_WithTitleAndIndent

- (void)test_WhenIndentIsNonZero_AddsIndentBeforeTitle
{
const auto is = CK::makeIndexSet({1, 2, 4});
const auto expectedDescription = @" Removed Sections: 1–2, 4";

XCTAssertEqualObjects(CK::indexSetDescription(is, @"Removed Sections", 2), expectedDescription);
}

- (void)test_WhenIndentIsZero_AddsOnlyTitle
{
const auto is = CK::makeIndexSet({1, 2, 4});
const auto expectedDescription = @"Removed Sections: 1–2, 4";

XCTAssertEqualObjects(CK::indexSetDescription(is, @"Removed Sections"), expectedDescription);
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#import <ComponentKit/CKDataSourceItem.h>
#import <ComponentKit/CKDataSourceChangesetModification.h>
#import <ComponentKit/CKDataSourceState.h>
#import <ComponentKitTestHelpers/NSIndexSetExtensions.h>

#import "CKDataSourceStateTestHelpers.h"

Expand Down Expand Up @@ -284,18 +285,9 @@ - (void)test_WhenFirstInsertionLocationIsGreaterThanCount_IsNotValid
- (void)test_WhenOtherInsertionLocationIsGreaterThanCount_IsNotValid
{
const auto array = @[@"one"];
const auto indexes = makeIndexSet({1, 3});
const auto indexes = CK::makeIndexSet({1, 3});

XCTAssertEqualObjects(CK::invalidIndexesForInsertionInArray(array, indexes), makeIndexSet({3}));
}

static auto makeIndexSet(const std::vector<NSUInteger> v) -> NSIndexSet *
{
auto r = [NSMutableIndexSet new];
for (const auto &i : v) {
[r addIndex:i];
}
return r;
XCTAssertEqualObjects(CK::invalidIndexesForInsertionInArray(array, indexes), CK::makeIndexSet({3}));
}

@end

0 comments on commit aa537f6

Please sign in to comment.