-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure correct dependency code-generation ordering (#27)
- Loading branch information
Showing
6 changed files
with
375 additions
and
28 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
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,98 @@ | ||
// Distributed under the MIT License | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
public final class List<Element>: Sequence { | ||
|
||
// MARK: Initialization | ||
|
||
public init(value: Element, previous: List? = nil, next: List? = nil) { | ||
self.value = value | ||
self.previous = previous | ||
self.next = next | ||
} | ||
|
||
public convenience init?(_ collection: some Collection<Element>) { | ||
guard let first = collection.first else { return nil } | ||
self.init(first: first, remaining: collection.dropFirst()) | ||
} | ||
|
||
public convenience init(first: Element, remaining: some Collection<Element>) { | ||
self.init(value: first) | ||
var next = self | ||
for element in remaining { | ||
next = next.insert(element) | ||
} | ||
} | ||
|
||
// MARK: Public | ||
|
||
public let value: Element | ||
|
||
/// Inserts the value after the current element. | ||
/// - Parameter value: The value to insert into the list. | ||
/// - Returns: The inserted element in the list. | ||
@discardableResult | ||
public func insert(_ value: Element) -> List<Element> { | ||
let next = next | ||
|
||
let nextToInsert = List(value: value) | ||
self.next = nextToInsert | ||
|
||
nextToInsert.next = next | ||
nextToInsert.previous = self | ||
|
||
next?.previous = nextToInsert | ||
|
||
return nextToInsert | ||
} | ||
|
||
/// Removes the receiver from the list. | ||
/// - Returns: The next element in the list, if the current element is the head of the list. | ||
@discardableResult | ||
public func remove() -> List<Element>? { | ||
previous?.next = next | ||
next?.previous = previous | ||
return previous == nil ? next : nil | ||
} | ||
|
||
// MARK: Sequence | ||
|
||
public func makeIterator() -> Iterator { | ||
Iterator(node: self) | ||
} | ||
|
||
public struct Iterator: IteratorProtocol { | ||
init(node: List?) { | ||
self.node = node | ||
} | ||
|
||
public mutating func next() -> List? { | ||
defer { node = node?.next } | ||
return node | ||
} | ||
|
||
private var node: List? | ||
} | ||
|
||
// MARK: Private | ||
|
||
private var next: List? = nil | ||
private var previous: List? = nil | ||
} |
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,167 @@ | ||
// Distributed under the MIT License | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
import XCTest | ||
|
||
@testable import SafeDICore | ||
|
||
final class ListTests: XCTestCase { | ||
|
||
func test_nonEmptyInit_createsListFromCollection() throws { | ||
XCTAssertEqual( | ||
try XCTUnwrap(List([1, 2, 3, 4, 5])).map(\.value), | ||
[1, 2, 3, 4, 5] | ||
) | ||
} | ||
|
||
func test_insert_onFirstElementInList_insertsElementAfterFirstElement() throws { | ||
let systemUnderTest = try XCTUnwrap(List([1, 3, 4, 5])) | ||
systemUnderTest.insert(2) | ||
XCTAssertEqual( | ||
systemUnderTest.map(\.value), | ||
[1, 2, 3, 4, 5] | ||
) | ||
} | ||
|
||
func test_insert_onLaterItemsInList_insertsElementAfterCurrentElement() { | ||
let systemUnderTest = List(value: 1) | ||
var last = systemUnderTest.insert(2) | ||
last = last.insert(3) | ||
last = last.insert(4) | ||
last = last.insert(5) | ||
XCTAssertEqual( | ||
systemUnderTest.map(\.value), | ||
[1, 2, 3, 4, 5] | ||
) | ||
} | ||
|
||
func test_remove_onFirstElementInList_removesFirstElementAndReturnsNewFirstElement() throws { | ||
let systemUnderTest = try XCTUnwrap(List([1, 2, 3, 4, 5])) | ||
XCTAssertEqual( | ||
systemUnderTest.remove()?.map(\.value), | ||
[2, 3, 4, 5] | ||
) | ||
} | ||
|
||
func test_remove_onItemThatWasInsertedAfterListCreation_removesItem() { | ||
let systemUnderTest = List(value: 1) | ||
let two = systemUnderTest.insert(2) | ||
let four = two.insert(4) | ||
four.insert(5) | ||
two.insert(3).remove() | ||
|
||
XCTAssertEqual( | ||
systemUnderTest.map(\.value), | ||
[1, 2, 4, 5] | ||
) | ||
} | ||
|
||
func test_remove_onItemBeforeItemInsertedAfterListCreation_removesItem() { | ||
let systemUnderTest = List(value: 1) | ||
let two = systemUnderTest.insert(2) | ||
let four = two.insert(4) | ||
four.insert(5) | ||
two.insert(3) | ||
two.remove() | ||
|
||
XCTAssertEqual( | ||
systemUnderTest.map(\.value), | ||
[1, 3, 4, 5] | ||
) | ||
} | ||
|
||
func test_remove_onItemAfterItemInsertedAfterListCreation_removesItem() { | ||
let systemUnderTest = List(value: 1) | ||
let two = systemUnderTest.insert(2) | ||
let four = two.insert(4) | ||
four.insert(5) | ||
two.insert(3) | ||
four.remove() | ||
|
||
XCTAssertEqual( | ||
systemUnderTest.map(\.value), | ||
[1, 2, 3, 5] | ||
) | ||
} | ||
|
||
func test_remove_onLaterItemsInList_removesElementAndReturnsNil() { | ||
let systemUnderTest = List(value: 1) | ||
let two = systemUnderTest.insert(2) | ||
let three = two.insert(3) | ||
let four = three.insert(4) | ||
four.insert(5) | ||
XCTAssertNil(four.remove()) | ||
XCTAssertEqual( | ||
systemUnderTest.map(\.value), | ||
[1, 2, 3, 5] | ||
) | ||
} | ||
|
||
func test_remove_onLastInList_removesElement() throws { | ||
let systemUnderTest = try XCTUnwrap(List([1, 2, 3, 4])) | ||
let lastElement = systemUnderTest.insert(5) | ||
lastElement.remove() | ||
XCTAssertEqual( | ||
systemUnderTest.map(\.value), | ||
[1, 2, 3, 4] | ||
) | ||
} | ||
|
||
func test_insert_andThenRemoveItemBeforeInsertion_insertsAndThenRemoves() { | ||
let systemUnderTest = List(value: 1) | ||
let two = systemUnderTest.insert(2) | ||
let three = two.insert(3) | ||
let four = three.insert(4) | ||
let secondFour = four.insert(4) | ||
secondFour.insert(5) | ||
four.remove() | ||
XCTAssertEqual( | ||
systemUnderTest.map(\.value), | ||
[1, 2, 3, 4, 5] | ||
) | ||
} | ||
|
||
func test_insert_andThenRemoveItem_insertsAndThenRemoves() { | ||
let systemUnderTest = List(value: 1) | ||
let two = systemUnderTest.insert(2) | ||
let three = two.insert(3) | ||
let four = three.insert(4) | ||
four.remove() | ||
three.insert(5) | ||
XCTAssertEqual( | ||
systemUnderTest.map(\.value), | ||
[1, 2, 3, 5] | ||
) | ||
} | ||
|
||
func test_insert_andThenRemoveItemAfterInsertion_insertsAndThenRemoves() { | ||
let systemUnderTest = List(value: 1) | ||
let two = systemUnderTest.insert(2) | ||
let three = two.insert(3) | ||
let four = three.insert(4) | ||
four.insert(5) | ||
four.remove() | ||
three.insert(4) | ||
XCTAssertEqual( | ||
systemUnderTest.map(\.value), | ||
[1, 2, 3, 4, 5] | ||
) | ||
} | ||
} |
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
Oops, something went wrong.