Skip to content

Commit 250e87c

Browse files
Consolidate lazy and computed property tests into main PropertyHolder test block
- Moved lazy property, computed property, and property observer tests from separate ph3 instance to main ph test block - Eliminated need for ph3 instance by reusing existing ph instance - Updated test expectations to match the actual test flow - All property tests now use consistent PropertyHolder instance for better code organization
1 parent bab3d56 commit 250e87c

File tree

4 files changed

+130
-36
lines changed

4 files changed

+130
-36
lines changed

Tests/BridgeJSRuntimeTests/ExportAPITests.swift

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,25 @@ struct TestError: Error {
181181
// Property with property observers
182182
@JS var observedProperty: Int {
183183
willSet {
184-
// Note: print won't show in WebAssembly tests, but the observers will still execute
184+
Self.willSetCallCount += 1
185+
Self.lastWillSetOldValue = self.observedProperty
186+
Self.lastWillSetNewValue = newValue
185187
}
186188
didSet {
187-
// Note: print won't show in WebAssembly tests, but the observers will still execute
189+
Self.didSetCallCount += 1
190+
Self.lastDidSetOldValue = oldValue
191+
Self.lastDidSetNewValue = self.observedProperty
188192
}
189193
}
190194

195+
// Static properties to track observer calls
196+
nonisolated(unsafe) static var willSetCallCount: Int = 0
197+
nonisolated(unsafe) static var didSetCallCount: Int = 0
198+
nonisolated(unsafe) static var lastWillSetOldValue: Int = 0
199+
nonisolated(unsafe) static var lastWillSetNewValue: Int = 0
200+
nonisolated(unsafe) static var lastDidSetOldValue: Int = 0
201+
nonisolated(unsafe) static var lastDidSetNewValue: Int = 0
202+
191203
@JS init(
192204
intValue: Int,
193205
floatValue: Float,
@@ -236,6 +248,20 @@ struct TestError: Error {
236248
return holder.getAllValues()
237249
}
238250

251+
@JS func resetObserverCounts() {
252+
PropertyHolder.willSetCallCount = 0
253+
PropertyHolder.didSetCallCount = 0
254+
PropertyHolder.lastWillSetOldValue = 0
255+
PropertyHolder.lastWillSetNewValue = 0
256+
PropertyHolder.lastDidSetOldValue = 0
257+
PropertyHolder.lastDidSetNewValue = 0
258+
}
259+
260+
@JS func getObserverStats() -> String {
261+
return
262+
"willSet:\(PropertyHolder.willSetCallCount),didSet:\(PropertyHolder.didSetCallCount),willSetOld:\(PropertyHolder.lastWillSetOldValue),willSetNew:\(PropertyHolder.lastWillSetNewValue),didSetOld:\(PropertyHolder.lastDidSetOldValue),didSetNew:\(PropertyHolder.lastDidSetNewValue)"
263+
}
264+
239265
class ExportAPITests: XCTestCase {
240266
func testAll() {
241267
var hasDeinitGreeter = false

Tests/BridgeJSRuntimeTests/Generated/BridgeJS.ExportSwift.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,29 @@ public func _bjs_testPropertyHolder(holder: UnsafeMutableRawPointer) -> Void {
505505
#endif
506506
}
507507

508+
@_expose(wasm, "bjs_resetObserverCounts")
509+
@_cdecl("bjs_resetObserverCounts")
510+
public func _bjs_resetObserverCounts() -> Void {
511+
#if arch(wasm32)
512+
resetObserverCounts()
513+
#else
514+
fatalError("Only available on WebAssembly")
515+
#endif
516+
}
517+
518+
@_expose(wasm, "bjs_getObserverStats")
519+
@_cdecl("bjs_getObserverStats")
520+
public func _bjs_getObserverStats() -> Void {
521+
#if arch(wasm32)
522+
var ret = getObserverStats()
523+
return ret.withUTF8 { ptr in
524+
_swift_js_return_string(ptr.baseAddress, Int32(ptr.count))
525+
}
526+
#else
527+
fatalError("Only available on WebAssembly")
528+
#endif
529+
}
530+
508531
@_expose(wasm, "bjs_Greeter_init")
509532
@_cdecl("bjs_Greeter_init")
510533
public func _bjs_Greeter_init(nameBytes: Int32, nameLen: Int32) -> UnsafeMutableRawPointer {

Tests/BridgeJSRuntimeTests/Generated/JavaScript/BridgeJS.ExportSwift.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,6 +1167,38 @@
11671167

11681168
}
11691169
}
1170+
},
1171+
{
1172+
"abiName" : "bjs_resetObserverCounts",
1173+
"effects" : {
1174+
"isAsync" : false,
1175+
"isThrows" : false
1176+
},
1177+
"name" : "resetObserverCounts",
1178+
"parameters" : [
1179+
1180+
],
1181+
"returnType" : {
1182+
"void" : {
1183+
1184+
}
1185+
}
1186+
},
1187+
{
1188+
"abiName" : "bjs_getObserverStats",
1189+
"effects" : {
1190+
"isAsync" : false,
1191+
"isThrows" : false
1192+
},
1193+
"name" : "getObserverStats",
1194+
"parameters" : [
1195+
1196+
],
1197+
"returnType" : {
1198+
"string" : {
1199+
1200+
}
1201+
}
11701202
}
11711203
],
11721204
"moduleName" : "BridgeJSRuntimeTests"

Tests/prelude.mjs

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,53 @@ function BridgeJSRuntimeTests_runJsWorks(instance, exports) {
221221
ph.sibling = newSibling;
222222
assert.equal(ph.sibling.value, 123);
223223

224+
// Test lazy property
225+
assert.equal(ph.lazyValue, "computed lazily");
226+
ph.lazyValue = "modified lazy";
227+
assert.equal(ph.lazyValue, "modified lazy");
228+
229+
// Test computed read-write property
230+
assert.equal(ph.computedReadWrite, "Value: 456");
231+
ph.computedReadWrite = "Value: 777";
232+
assert.equal(ph.intValue, 777); // Should have parsed and set intValue
233+
assert.equal(ph.computedReadWrite, "Value: 777");
234+
235+
// Test computed readonly property
236+
assert.equal(ph.computedReadonly, 1554); // intValue * 2 = 777 * 2
237+
238+
// Test property with observers
239+
// First, reset observer counts to start fresh
240+
exports.resetObserverCounts();
241+
let initialStats = exports.getObserverStats();
242+
assert.equal(initialStats, "willSet:0,didSet:0,willSetOld:0,willSetNew:0,didSetOld:0,didSetNew:0");
243+
244+
// Set property from JavaScript and verify observers are called
245+
ph.observedProperty = 100;
246+
assert.equal(ph.observedProperty, 100);
247+
let afterSetStats = exports.getObserverStats();
248+
249+
// Verify willSet and didSet were called
250+
// The stats should show: willSet:1,didSet:1,willSetOld:777,willSetNew:100,didSetOld:777,didSetNew:100
251+
assert(afterSetStats.includes("willSet:1"), `willSet should be called once, got: ${afterSetStats}`);
252+
assert(afterSetStats.includes("didSet:1"), `didSet should be called once, got: ${afterSetStats}`);
253+
assert(afterSetStats.includes("willSetOld:777"), `willSet should see old value 777, got: ${afterSetStats}`);
254+
assert(afterSetStats.includes("willSetNew:100"), `willSet should see new value 100, got: ${afterSetStats}`);
255+
assert(afterSetStats.includes("didSetOld:777"), `didSet should see old value 777, got: ${afterSetStats}`);
256+
assert(afterSetStats.includes("didSetNew:100"), `didSet should see new value 100, got: ${afterSetStats}`);
257+
258+
// Set property to a different value and verify observers are called again
259+
ph.observedProperty = 200;
260+
assert.equal(ph.observedProperty, 200);
261+
let afterSecondSetStats = exports.getObserverStats();
262+
263+
// Now should be: willSet:2,didSet:2,willSetOld:100,willSetNew:200,didSetOld:100,didSetNew:200
264+
assert(afterSecondSetStats.includes("willSet:2"), `willSet should be called twice, got: ${afterSecondSetStats}`);
265+
assert(afterSecondSetStats.includes("didSet:2"), `didSet should be called twice, got: ${afterSecondSetStats}`);
266+
assert(afterSecondSetStats.includes("willSetOld:100"), `willSet should see old value 100 on second call, got: ${afterSecondSetStats}`);
267+
assert(afterSecondSetStats.includes("willSetNew:200"), `willSet should see new value 200 on second call, got: ${afterSecondSetStats}`);
268+
assert(afterSecondSetStats.includes("didSetOld:100"), `didSet should see old value 100 on second call, got: ${afterSecondSetStats}`);
269+
assert(afterSecondSetStats.includes("didSetNew:200"), `didSet should see new value 200 on second call, got: ${afterSecondSetStats}`);
270+
224271
ph.release();
225272
ph2.release();
226273

@@ -235,40 +282,6 @@ function BridgeJSRuntimeTests_runJsWorks(instance, exports) {
235282
const anyObject = {};
236283
assert.equal(exports.roundTripJSObject(anyObject), anyObject);
237284

238-
// Test PropertyHolder's newly added lazy and computed properties
239-
// Create a new PropertyHolder for testing lazy/computed properties
240-
const testObj2 = { testProp2: "test2" };
241-
const sibling2 = new exports.SimplePropertyHolder(777);
242-
const ph3 = new exports.PropertyHolder(
243-
456, // intValue
244-
6.28, // floatValue
245-
1.414, // doubleValue
246-
false, // boolValue
247-
"test2", // stringValue
248-
testObj2, // jsObject
249-
sibling2 // sibling
250-
);
251-
252-
// Test lazy property
253-
assert.equal(ph3.lazyValue, "computed lazily");
254-
ph3.lazyValue = "modified lazy";
255-
assert.equal(ph3.lazyValue, "modified lazy");
256-
257-
// Test computed readonly property
258-
assert.equal(ph3.computedReadonly, 912); // intValue * 2 = 456 * 2
259-
260-
// Test computed read-write property
261-
assert.equal(ph3.computedReadWrite, "Value: 456");
262-
ph3.computedReadWrite = "Value: 555";
263-
assert.equal(ph3.intValue, 555); // Should have parsed and set intValue
264-
assert.equal(ph3.computedReadWrite, "Value: 555");
265-
266-
// Test property with observers
267-
ph3.observedProperty = 100;
268-
assert.equal(ph3.observedProperty, 100);
269-
270-
ph3.release();
271-
272285
try {
273286
exports.throwsSwiftError(true);
274287
assert.fail("Expected error");

0 commit comments

Comments
 (0)