Fix child-adder code generation

This commit is contained in:
Brendan Szymanski 2026-08-05 21:55:39 -04:00
parent e5f483a7a7
commit 30f84b0aee
67 changed files with 1313 additions and 619 deletions

View file

@ -1,6 +1,6 @@
import Testing
import Adw
import Gtk
@_spi(SGTKInternal) import Gtk
@_spi(Portico) import Portico
@MainActor @Suite(.serialized) struct GeneratedWidgetTests {
@ -183,4 +183,71 @@ import Gtk
box.set(100)
#expect(avatar.getSize() == 48, "size should not change after teardown")
}
// MARK: - Child-adder generated APIs
/// Two rows plus a header suffix, proving children land as rows and the slot still binds.
@Test func preferencesGroupChildrenAndHeaderSuffix() {
guard Gtk.initCheck() else { return }
let w = AnyView(PreferencesGroup {
ActionRow()
ActionRow()
} headerSuffix: {
Label(str: "suffix")
}).makeWidget(MountContext())
guard let group = w as? Adw.PreferencesGroup else {
Issue.record("Expected Adw.PreferencesGroup, got \(type(of: w))")
return
}
#expect(group.getRow(index: 0) != nil)
#expect(group.getRow(index: 1) != nil)
#expect(group.getRow(index: 2) == nil)
#expect(group.getHeaderSuffix() != nil)
}
/// The generated `children:` builder adds children in closure order.
@Test func stackChildrenInitAddsInOrder() {
guard Gtk.initCheck() else { return }
let w = AnyView(Stack {
Label(str: "first")
Label(str: "second")
}).makeWidget(MountContext())
guard let stack = w as? Gtk.Stack else {
Issue.record("Expected Gtk.Stack, got \(type(of: w))")
return
}
// getFirstChild returns a base Widget; reconstruct Label from the pointer.
guard let a = stack.getFirstChild(), let b = a.getNextSibling() else {
Issue.record("Expected two children")
return
}
#expect(Gtk.Label(retaining: a.pointer).getText() == "first")
#expect(Gtk.Label(retaining: b.pointer).getText() == "second")
#expect(b.getNextSibling() == nil)
}
/// `Adw.Squeezer` has no `Widget` slot, so its whole content API is generated from
/// `add(child:)`; this also exercises the static `Gtk.Widget` modifier overload.
@Test func squeezerStaticAddModifierAddsChild() {
guard Gtk.initCheck() else { return }
let child = Gtk.Button(label: "a")
let w = AnyView(Squeezer().add(child)).makeWidget(MountContext())
guard let squeezer = w as? Adw.Squeezer else {
Issue.record("Expected Adw.Squeezer, got \(type(of: w))")
return
}
#expect(squeezer.getVisibleChild()?.pointer == child.pointer)
}
/// `Adw.ViewStack.visibleChild` is deliberately not a slot; the children builder is
/// what makes a page visible.
@Test func viewStackChildrenInitMakesChildVisible() {
guard Gtk.initCheck() else { return }
let w = AnyView(ViewStack { Label(str: "x") }).makeWidget(MountContext())
guard let stack = w as? Adw.ViewStack else {
Issue.record("Expected Adw.ViewStack, got \(type(of: w))")
return
}
#expect(stack.getVisibleChild() != nil)
}
}