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
3 changes: 2 additions & 1 deletion Sources/OpenSwiftUICore/Data/Binding/Location.swift
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,10 @@ private struct ProjectedLocation<L: Location, P: Projection>: Location where P.B
projection.get(base: location.get())
}

func set(_ value: Value, transaction _: Transaction) {
func set(_ value: Value, transaction: Transaction) {
var base = location.get()
projection.set(base: &base, newValue: value)
location.set(base, transaction: transaction)
}

func update() -> (Value, Bool) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// Author: Claude Code with Claude Sonnet 4.5

import Foundation
import Testing
@testable import OpenSwiftUICore
import Testing

struct BindingOperationsTests {

Expand Down Expand Up @@ -37,10 +37,8 @@ struct BindingOperationsTests {

let optionalBinding: Binding<Int?> = Binding(baseBinding)
optionalBinding.wrappedValue = 20
#expect(optionalBinding.wrappedValue == 10)

storage = 20
#expect(optionalBinding.wrappedValue == 20)
#expect(storage == 20)
}

@Test
Expand Down Expand Up @@ -86,10 +84,8 @@ struct BindingOperationsTests {

let anyHashableBinding: Binding<AnyHashable> = Binding(baseBinding)
anyHashableBinding.wrappedValue = AnyHashable(20)
#expect(anyHashableBinding.wrappedValue == AnyHashable(10))

storage = 20
#expect(anyHashableBinding.wrappedValue == AnyHashable(20))
#expect(storage == 20)
}

@Test
Expand All @@ -105,10 +101,8 @@ struct BindingOperationsTests {
#expect(anyHashableBinding.wrappedValue == AnyHashable("hello"))

anyHashableBinding.wrappedValue = AnyHashable("world")
#expect(anyHashableBinding.wrappedValue == AnyHashable("hello"))

storage = "world"
#expect(anyHashableBinding.wrappedValue == AnyHashable("world"))
#expect(storage == "world")
}
}

Expand Down Expand Up @@ -157,10 +151,8 @@ struct BindingOperationsTests {
}

unwrappedBinding.wrappedValue = 20
#expect(unwrappedBinding.wrappedValue == 10)

storage = 20
#expect(unwrappedBinding.wrappedValue == 20)
#expect(storage == 20)
}
}

Expand Down Expand Up @@ -218,10 +210,8 @@ struct BindingOperationsTests {
#expect(nilCoalescingBinding.wrappedValue == 100)

nilCoalescingBinding.wrappedValue = 50
#expect(nilCoalescingBinding.wrappedValue == 100)

storage.value = 50
#expect(nilCoalescingBinding.wrappedValue == 50)
#expect(storage.value == 50)
}

@Test
Expand Down Expand Up @@ -272,10 +262,8 @@ struct BindingOperationsTests {

let doubleBinding: Binding<Double> = Binding(baseBinding)
doubleBinding.wrappedValue = 2.5
#expect(doubleBinding.wrappedValue.isApproximatelyEqual(to: 1.0, absoluteTolerance: 0.001))

storage = 2.5
#expect(doubleBinding.wrappedValue.isApproximatelyEqual(to: 2.5, absoluteTolerance: 0.001))
#expect(storage == 2.5)
}

@Test
Expand All @@ -302,10 +290,8 @@ struct BindingOperationsTests {

let doubleBinding: Binding<Double> = Binding(baseBinding)
doubleBinding.wrappedValue = 2.71828
#expect(doubleBinding.wrappedValue.isApproximatelyEqual(to: 1.0))

storage = 2.71828
#expect(doubleBinding.wrappedValue.isApproximatelyEqual(to: 2.71828))
#expect(storage == 2.71828)
}
}

Expand Down Expand Up @@ -336,10 +322,8 @@ struct BindingOperationsTests {

let doubleBinding: Binding<Double> = Binding(baseBinding)
doubleBinding.wrappedValue = 25.7
#expect(doubleBinding.wrappedValue == 10.0)

storage = 25
#expect(doubleBinding.wrappedValue == 25.0)
#expect(storage == 25)
}

@Test
Expand All @@ -355,10 +339,8 @@ struct BindingOperationsTests {
#expect(doubleBinding.wrappedValue == 100.0)

doubleBinding.wrappedValue = 200.5
#expect(doubleBinding.wrappedValue == 100.0)

storage = 200
#expect(doubleBinding.wrappedValue == 200.0)
#expect(storage == 200)
}
}

Expand Down Expand Up @@ -404,10 +386,8 @@ struct BindingOperationsTests {
#expect(resultBinding.wrappedValue == false)

resultBinding.wrappedValue = true
#expect(resultBinding.wrappedValue == false)

storage = 42
#expect(resultBinding.wrappedValue == true)
#expect(storage == 42)
}

@Test
Expand Down Expand Up @@ -443,10 +423,8 @@ struct BindingOperationsTests {
#expect(resultBinding2.wrappedValue == false)

resultBinding2.wrappedValue = true
#expect(resultBinding2.wrappedValue == false)

storage = "world"
#expect(resultBinding2.wrappedValue == true)
#expect(storage == "world")

resultBinding2.wrappedValue = false
#expect(resultBinding2.wrappedValue == true)
Expand Down
6 changes: 4 additions & 2 deletions Tests/OpenSwiftUICoreTests/Data/Binding/BindingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ struct BindingTests {
#expect(optionalBinding.wrappedValue == 0)

optionalBinding.wrappedValue = 10
#expect(optionalBinding.wrappedValue == 0)
#expect(optionalBinding.wrappedValue == 10)
#expect(storage == 10)
storage = 20
#expect(optionalBinding.wrappedValue == 20)
optionalBinding.wrappedValue = nil
Expand Down Expand Up @@ -233,7 +234,8 @@ struct BindingTests {
let anyHashableBinding: Binding<AnyHashable> = Binding(baseBinding)
#expect(anyHashableBinding.wrappedValue == AnyHashable(0))
anyHashableBinding.wrappedValue = 10
#expect(anyHashableBinding.wrappedValue == AnyHashable(0))
#expect(anyHashableBinding.wrappedValue == AnyHashable(10))
#expect(storage == 10)
storage = 20
#expect(anyHashableBinding.wrappedValue == AnyHashable(20))
// #expectCrash(optionalBinding.wrappedValue = 0.0)
Expand Down
28 changes: 28 additions & 0 deletions Tests/OpenSwiftUICoreTests/Data/Binding/LocationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,34 @@ struct LocationTests {
}
#endif

@Test
func projectedWriteForwardsValueAndTransaction() {
struct Value: Equatable {
var count = 0
var name = "value"
}

var value = Value()
var receivedTransaction: Transaction?
var writes = 0
let location = LocationBox(FunctionalLocation {
value
} setValue: { newValue, transaction in
value = newValue
receivedTransaction = transaction
writes += 1
})
let projection = location.projecting(\Value.count)
var transaction = Transaction()
transaction.disablesAnimations = true

projection.set(3, transaction: transaction)

#expect(value == Value(count: 3))
#expect(receivedTransaction?.disablesAnimations == true)
#expect(writes == 1)
}

@Test
func constantLocation() throws {
let location = ConstantLocation(value: 0)
Expand Down
Loading