Skip to content

Commit c686708

Browse files
Fixed an issue where optional comparisons would not be ordered correctly
1 parent a877838 commit c686708

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

Sources/CodableDatastore/Indexes/Indexable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ extension UInt64: DiscreteIndexable, RangedIndexable {}
8080
extension Optional: Comparable where Wrapped: Comparable {
8181
public static func < (lhs: Self, rhs: Self) -> Bool {
8282
if let lhs, let rhs { return lhs < rhs }
83-
return lhs == nil
83+
return lhs == nil && rhs != nil
8484
}
8585
}
8686
extension Optional: DiscreteIndexable where Wrapped: DiscreteIndexable {}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// OptionalTests.swift
3+
// CodableDatastore
4+
//
5+
// Created by Dimitri Bouniol on 2024-04-20.
6+
// Copyright © 2023-24 Mochi Development, Inc. All rights reserved.
7+
//
8+
9+
import XCTest
10+
@testable import CodableDatastore
11+
12+
final class OptionalTests: XCTestCase {
13+
func testComparable() throws {
14+
XCTAssertTrue(Int?.some(5) < Int?.some(10))
15+
XCTAssertTrue(Int?.none < Int?.some(10))
16+
XCTAssertFalse(Int?.some(5) < Int?.none)
17+
XCTAssertFalse(Int?.none < Int?.none)
18+
}
19+
}

0 commit comments

Comments
 (0)