-
Notifications
You must be signed in to change notification settings - Fork 586
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
c4b5cea
commit aa537f6
Showing
8 changed files
with
163 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 *; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 *; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters