[Pitch] Have all adapters in the standard library uniformly expose their `base`

This is the edge case indeed, but still... This is what I am after, and I believe this is BIG: SwiftUI gives you "a promise" of easily testable UI, without the need to resort to snapshot testing, e.g.:

@main
struct HelloWorldApp: App {
    var body: some Scene {
        WindowGroup {
            MyView(param: "hi")
                .onAppear {
                    runTests()
                }
        }
    }
}

struct MyView: View {
    let param: String
    var body: some View {
        Text("Hello, " + param)
            .foregroundColor(.orange)
    }
}

func runTests() {
    let whatItShouldBe = #"....."#
    let s = MyView(param: "World").body
    print(s)
    assert("\(s)" == whatItShouldBe)
}

Text(storage: SwiftUI.Text.Storage.verbatim("Hello, World"), modifiers: [SwiftUI.Text.Modifier.color(Optional(orange))])

So far so good. Setting "whatItShouldBe" to the string above got the UI tested, voila. If some refactoring violates the assert I'd be notified and correct either the code or the test.

And, if you wonder, should the "SwiftUI.Text.Storage.verbatim" or smth private like that change (e.g. next OS version or Xcode) insignificantly, I'd just update my test strings (or select a different string based upon Xcode / OS version combination). But now this:

        Text("Hello, " + param)
            .font(.body)

SwiftUI.Font(provider: SwiftUI.(unknown context at $111426b50).FontBox<SwiftUI.Font.(unknown context at $111453a20).TextStyleProvider>)

oops. In order to match that I'd somehow need to strip $111426b50. Which is of course doable, but then how to distinguish "body" vs, say, "largeTitle" or Font made with UIFont?

ok, let's do it properly and override description:

extension SwiftUI.Font: CustomStringConvertible {
    public var description: String {
        "WOW"
    }
}

SwiftUI.Text.Modifier.font(Optional(WOW))

So far so good. But how do I get the "body" / "bold" / "italic" / "customName" + size / "testStyle", etc out of it? There are no getters whatsoever defined on Font....

To make the thing totally opaque might be good from the ideological perspective, yet it is a major spanner in the works here. In order to do the above I have to essentially reimplement the major portions of SwiftUI and use my custom wrappers instead of built-in ones.

Sorry for SwiftUI-ism on this forum. I appreciate this is not exactly the "standard library exposing base" issue, yet it is a similar enough example how the opaqueness of the API works against those of us who want to go just one small step away from the expected general pathway.

PS. in this particular case I personally wouldn't mind using some back door SPI solution to get the job done. If you know a way - please let me know here or privately.

1 Like