portico/Tests/PorticoTests/InterpolatedTextTests.swift

141 lines
4.9 KiB
Swift

import Testing
@_spi(SGTKInternal) import Gtk
@_spi(Portico) import Portico
import Adw
@MainActor @Suite(.serialized) struct InterpolatedTextTests {
// MARK: - Unit tests (no GTK needed)
@Test func plainLiteralHasStaticText() {
let text: InterpolatedText = "plain"
#expect(text.staticText == "plain")
}
@Test func interpolatedLiteralHasNoStaticText() {
let text: InterpolatedText = "n=\(0)"
#expect(text.staticText == nil)
}
@Test func evaluateReturnsConcatenatedString() {
let box = StateBox(42)
let text: InterpolatedText = "Count: \(box.get())"
#expect(text.evaluate() == "Count: 42")
}
@Test func untrackedTextDoesNotSubscribeEnclosingTracker() {
let box = StateBox(0)
let text: InterpolatedText = "n=\(box.get())"
var runs = 0
let outer = DependencyTracker { runs += 1; _ = text.untrackedText }
outer.run()
#expect(runs == 1)
box.set(1)
#expect(runs == 1)
}
// MARK: - GTK-dependent tests
@Test func interpolatedLiteralUpdatesLabelInPlace() {
guard Gtk.initCheck() else { return }
let box = StateBox(0)
let label = AnyView(Portico.Label("Count: \(box.get())")).makeWidget(MountContext()) as! Gtk.Label
defer { box.set(0) }
#expect(label.getText() == "Count: 0")
box.set(1)
#expect(label.getText() == "Count: 1")
box.set(2)
#expect(label.getText() == "Count: 2")
}
@Test func interpolatedModifierUpdatesLabel() {
guard Gtk.initCheck() else { return }
let box = StateBox(0)
let label = AnyView(Portico.Label(str: nil).label("n=\(box.get())")).makeWidget(MountContext()) as! Gtk.Label
defer { box.set(0) }
#expect(label.getText() == "n=0")
box.set(1)
#expect(label.getText() == "n=1")
}
@Test func multipleInterpolationsTrackEveryBox() {
guard Gtk.initCheck() else { return }
let b1 = StateBox(1)
let b2 = StateBox(10)
let label = AnyView(Portico.Label("a=\(b1.get()) b=\(b2.get())")).makeWidget(MountContext()) as! Gtk.Label
defer { b1.set(0); b2.set(0) }
#expect(label.getText() == "a=1 b=10")
b1.set(2)
#expect(label.getText() == "a=2 b=10")
b2.set(20)
#expect(label.getText() == "a=2 b=20")
}
@Test func stringVariableStaysStatic() {
guard Gtk.initCheck() else { return }
let box = StateBox(0)
let s = "n=\(box.peek())"
let label = AnyView(Portico.Label(s)).makeWidget(MountContext()) as! Gtk.Label
#expect(label.getText() == "n=0")
box.set(1)
#expect(label.getText() == "n=0")
}
@Test func optionalPropertyInterpolationUpdates() {
guard Gtk.initCheck() else { return }
let box = StateBox(0)
let label = AnyView(Portico.Label(str: nil).tooltipText("t=\(box.get())")).makeWidget(MountContext()) as! Gtk.Label
defer { box.set(0) }
#expect(label.getTooltipText() == "t=0")
box.set(1)
#expect(label.getTooltipText() == "t=1")
}
@Test func optionalPropertyNilInterpolation() {
guard Gtk.initCheck() else { return }
let label = AnyView(Portico.Label(str: "x").tooltipText(nil)).makeWidget(MountContext()) as! Gtk.Label
#expect(label.getTooltipText() == nil)
}
@Test func interpolationStopsAfterTeardown() {
guard Gtk.initCheck() else { return }
let box = StateBox(0)
let ctx = MountContext()
let label = AnyView(Portico.Label("n=\(box.get())")).makeWidget(ctx) as! Gtk.Label
defer { box.set(0) }
#expect(label.getText() == "n=0")
ctx.registry.teardown()
box.set(1)
#expect(label.getText() == "n=0")
}
@Test func buttonInterpolationUpdatesLabel() {
guard Gtk.initCheck() else { return }
let box = StateBox(0)
let button = AnyView(Portico.Button("n=\(box.get())") {}).makeWidget(MountContext()) as! Gtk.Button
defer { box.set(0) }
#expect(button.getLabel() == "n=0")
box.set(1)
#expect(button.getLabel() == "n=1")
}
@Test func generatedInterpolatedInitUpdatesLabel() {
guard Gtk.initCheck() else { return }
let box = StateBox(0)
let button = AnyView(Portico.Button(label: "n=\(box.get())")).makeWidget(MountContext()) as! Gtk.Button
defer { box.set(0) }
#expect(button.getLabel() == "n=0")
box.set(1)
#expect(button.getLabel() == "n=1")
}
@Test func avatarInterpolatedTextUpdatesLabel() {
guard Gtk.initCheck() else { return }
let box = StateBox(0)
let avatar = AnyView(Portico.Avatar(size: 32, text: "n=\(box.get())", showInitials: true)).makeWidget(MountContext()) as! Adw.Avatar
defer { box.set(0) }
#expect(avatar.getText() == "n=0")
box.set(1)
#expect(avatar.getText() == "n=1")
}
}