-
Notifications
You must be signed in to change notification settings - Fork 652
Restore rotating index for multicolor #102
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // | ||
| // File.swift | ||
| // | ||
| // | ||
| // Created by Nicolas Savoini on 2020-05-25. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| extension Array where Element == ColorGradient { | ||
| func rotate(for index: Int) -> ColorGradient { | ||
| if self.isEmpty { | ||
| return ColorGradient.orangeBright | ||
| } | ||
|
|
||
| if self.count <= index { | ||
| return self[index % self.count] | ||
| } | ||
|
|
||
| return self[index] | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // | ||
| // File.swift | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| // | ||
| // | ||
| // Created by Nicolas Savoini on 2020-05-25. | ||
| // | ||
|
|
||
| @testable import SwiftUICharts | ||
| import XCTest | ||
|
|
||
| class ArrayExtensionTests: XCTestCase { | ||
|
|
||
| func testArrayRotatingIndexEmpty() { | ||
| let colors = [ColorGradient]() | ||
| XCTAssertEqual(colors.rotate(for: 0), ColorGradient.orangeBright) | ||
| } | ||
|
|
||
| func testArrayRotatingIndexOneValue() { | ||
| let colors = [ColorGradient.greenRed] | ||
|
|
||
| XCTAssertEqual(colors.rotate(for: 0), ColorGradient.greenRed) | ||
| XCTAssertEqual(colors.rotate(for: 1), ColorGradient.greenRed) | ||
| XCTAssertEqual(colors.rotate(for: 2), ColorGradient.greenRed) | ||
| } | ||
|
|
||
| func testArrayRotatingIndexLessValues() { | ||
| let colors = [ColorGradient.greenRed, ColorGradient.whiteBlack] | ||
|
|
||
| XCTAssertEqual(colors.rotate(for: 0), ColorGradient.greenRed) | ||
| XCTAssertEqual(colors.rotate(for: 1), ColorGradient.whiteBlack) | ||
| XCTAssertEqual(colors.rotate(for: 2), ColorGradient.greenRed) | ||
| XCTAssertEqual(colors.rotate(for: 3), ColorGradient.whiteBlack) | ||
| XCTAssertEqual(colors.rotate(for: 4), ColorGradient.greenRed) | ||
| } | ||
|
|
||
| func testArrayRotatingIndexMoreValues() { | ||
| let colors = [ColorGradient.greenRed, ColorGradient.whiteBlack, ColorGradient.orangeBright] | ||
|
|
||
| XCTAssertEqual(colors.rotate(for: 0), ColorGradient.greenRed) | ||
| XCTAssertEqual(colors.rotate(for: 1), ColorGradient.whiteBlack) | ||
|
|
||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💯 on unit tests. That's what was sorely lacking on
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! Working on my TDD skills;) Small functions like that are perfect for it. |
||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Array+Extension.swift