Skip to content
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
14 changes: 11 additions & 3 deletions Sources/OpenSwiftUICore/Util/StandardLibraryAdditions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,17 @@ package enum IndirectOptional<Wrapped>: ExpressibleByNilLiteral {
}

package var wrappedValue: Wrapped? {
switch self {
case .none: nil
case let .some(wrapped): wrapped
get {
switch self {
case .none: nil
case .some(let wrapped): wrapped
}
}
set {
switch newValue {
case .none: self = .none
case .some(let wrapped): self = .some(wrapped)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -618,3 +618,89 @@ struct BidirectionalCollectionInsertionSortTests {
#expect(array == [.value(4), .invalid, .value(5), .value(2), .value(1)])
}
}

// MARK: - IndirectOptionalTests

struct IndirectOptionalTests {
@Test
func initWithValue() {
let opt = IndirectOptional<Int>(42)
#expect(opt.wrappedValue == 42)
}

@Test
func initWithNilLiteral() {
let opt: IndirectOptional<Int> = nil
#expect(opt.wrappedValue == nil)
}

@Test
func initWithWrappedValue() {
let opt = IndirectOptional<String>(wrappedValue: "hello")
#expect(opt.wrappedValue == "hello")

let nilOpt = IndirectOptional<String>(wrappedValue: nil)
#expect(nilOpt.wrappedValue == nil)
}

@Test
func wrappedValueGetter() {
let some: IndirectOptional<Int> = .some(10)
let none: IndirectOptional<Int> = .none

#expect(some.wrappedValue == 10)
#expect(none.wrappedValue == nil)
}

@Test
func wrappedValueSetter() {
var opt: IndirectOptional<Int> = .none

opt.wrappedValue = 42
#expect(opt.wrappedValue == 42)

opt.wrappedValue = 99
#expect(opt.wrappedValue == 99)

opt.wrappedValue = nil
#expect(opt.wrappedValue == nil)
}

@Test
func wrappedValueSetterFromSomeToNone() {
var opt: IndirectOptional<String> = .some("initial")
#expect(opt.wrappedValue == "initial")

opt.wrappedValue = nil
#expect(opt.wrappedValue == nil)
}

@Test
func wrappedValueSetterFromNoneToSome() {
var opt: IndirectOptional<String> = .none
#expect(opt.wrappedValue == nil)

opt.wrappedValue = "assigned"
#expect(opt.wrappedValue == "assigned")
}

@Test
func equatable() {
let a: IndirectOptional<Int> = .some(5)
let b: IndirectOptional<Int> = .some(5)
let c: IndirectOptional<Int> = .some(10)
let d: IndirectOptional<Int> = .none

#expect(a == b)
#expect(a != c)
#expect(a != d)
#expect(d == IndirectOptional<Int>.none)
}

@Test
func hashable() {
let a: IndirectOptional<Int> = .some(5)
let b: IndirectOptional<Int> = .some(5)
#expect(a.hashValue == b.hashValue)
}
}
Loading