Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Array safeValue(at:) behavior for negative index #279

Merged
merged 2 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions CardParts/src/Extensions/Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import Foundation

extension Array {
/// read an array element at a given `index` safely
/// - Parameter index: the index of the element to read
/// - Returns: array element at a given `index`. `nil` if index is out of range
func safeValue(at index: Int) -> Element? {
if index < self.count {
return self[index]
} else {
guard index >= 0, index < count else {
return nil
}
return self[index]
}
}

12 changes: 12 additions & 0 deletions Example/CardParts.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
80F3C768234ADA7900F5D271 /* CardPartMapViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 80F3C767234ADA7900F5D271 /* CardPartMapViewTests.swift */; };
9A33961F2410208D00167DD5 /* CardPartsBottomSheetTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A33961E2410208D00167DD5 /* CardPartsBottomSheetTests.swift */; };
9A9AE5C323FBA72D0006E1EC /* CardPartBottomSheetCardController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A9AE5C223FBA72D0006E1EC /* CardPartBottomSheetCardController.swift */; };
A6A6D2CD272463EE00DA453A /* ArrayTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6A6D2CC272463EE00DA453A /* ArrayTests.swift */; };
EB16E38A2396E8FD003F70A2 /* CardParthCustomMarginsCardController.swift in Sources */ = {isa = PBXBuildFile; fileRef = EB16E3892396E8FD003F70A2 /* CardParthCustomMarginsCardController.swift */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -136,6 +137,7 @@
80F3C767234ADA7900F5D271 /* CardPartMapViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CardPartMapViewTests.swift; sourceTree = "<group>"; };
9A33961E2410208D00167DD5 /* CardPartsBottomSheetTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CardPartsBottomSheetTests.swift; sourceTree = "<group>"; };
9A9AE5C223FBA72D0006E1EC /* CardPartBottomSheetCardController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CardPartBottomSheetCardController.swift; sourceTree = "<group>"; };
A6A6D2CC272463EE00DA453A /* ArrayTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArrayTests.swift; sourceTree = "<group>"; };
B21177744B384387575CB614 /* Pods-CardParts_Tests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CardParts_Tests.debug.xcconfig"; path = "Target Support Files/Pods-CardParts_Tests/Pods-CardParts_Tests.debug.xcconfig"; sourceTree = "<group>"; };
E768E0B2422010080EDB1A52 /* Pods-CardParts_Tests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CardParts_Tests.release.xcconfig"; path = "Pods/Target Support Files/Pods-CardParts_Tests/Pods-CardParts_Tests.release.xcconfig"; sourceTree = "<group>"; };
EB16E3892396E8FD003F70A2 /* CardParthCustomMarginsCardController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CardParthCustomMarginsCardController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -214,6 +216,7 @@
607FACE81AFB9204008FA782 /* Tests */ = {
isa = PBXGroup;
children = (
A6A6D2C92724500F00DA453A /* Extensions */,
61D4706E1FDA709B00F451F0 /* CardPartButtonViewTests.swift */,
61D4706A1FDA709B00F451F0 /* CardPartImageViewTests.swift */,
9A33961E2410208D00167DD5 /* CardPartsBottomSheetTests.swift */,
Expand Down Expand Up @@ -298,6 +301,14 @@
name = Frameworks;
sourceTree = "<group>";
};
A6A6D2C92724500F00DA453A /* Extensions */ = {
isa = PBXGroup;
children = (
A6A6D2CC272463EE00DA453A /* ArrayTests.swift */,
);
path = Extensions;
sourceTree = "<group>";
};
BABC66A8CBE4FFFB18E62FFE /* Pods */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -574,6 +585,7 @@
buildActionMask = 2147483647;
files = (
80F3C768234ADA7900F5D271 /* CardPartMapViewTests.swift in Sources */,
A6A6D2CD272463EE00DA453A /* ArrayTests.swift in Sources */,
61D470751FDA709B00F451F0 /* CardPartButtonViewTests.swift in Sources */,
61D470721FDA709B00F451F0 /* CardPartTitleViewTests.swift in Sources */,
55AB80CF20B61E1700B5994B /* CardUtilsTests.swift in Sources */,
Expand Down
35 changes: 35 additions & 0 deletions Example/Tests/Extensions/ArrayTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// ArrayTests.swift
// CardParts_Tests
//

import XCTest
@testable import CardParts

class ArrayExtesionTests: XCTestCase {

func testSafeValue() {
var array: [Int]

// when array is empty
array = []

XCTAssertNil(array.safeValue(at: -1), "Should be nil for negative index")
XCTAssertNil(array.safeValue(at: 0), "Should be nil for positive index if array is empty")
XCTAssertNil(array.safeValue(at: Int.random(in: 1...100)), "Should be nil for positive index if array is empty")

// when array is not empty
array = [
Int.random(in: 0...100),
Int.random(in: 0...100),
Int.random(in: 0...100)
]

XCTAssertNil(array.safeValue(at: -1), "Should be nil for negative index")
XCTAssertNil(array.safeValue(at: array.count), "Should be nil for positive index out of range")

for index in 0..<array.count {
XCTAssertEqual(array.safeValue(at: index), array[index])
}
}
}