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
8 changes: 4 additions & 4 deletions Sources/OpenAttributeGraphShims/Metadata+Debug.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ extension Metadata {
switch kind {
case .enum:
write(&result, string: "enum \(type) {", level: level)
_ = forEachField(options: [.enumerateEnumCases]) { name, offset, type in // anything contains ._4 will work here
_ = forEachField(options: [.enumerateEnumCases]) { name, offset, type in
let fieldName = String(cString: name)
write(&result, string: "case \(fieldName)(\(type)) // offset = \(offset.hex)", level: level+1)
if recursive {
Expand All @@ -52,7 +52,7 @@ extension Metadata {
}
write(&result, string: "}", level: level)
case .optional:
_ = forEachField(options: [.enumerateEnumCases]) { name, offset, type in // anything contains ._4 will work here
_ = forEachField(options: [.enumerateEnumCases]) { name, offset, type in
let fieldName = String(cString: name)
write(&result, string: "case \(fieldName)(\(type)) // offset = \(offset.hex)", level: level+1)
if recursive {
Expand All @@ -62,7 +62,7 @@ extension Metadata {
}
case .struct:
write(&result, string: "struct \(type) {", level: level)
_ = forEachField(options: []) { name, offset, type in // only [] and [._2] will work here
_ = forEachField(options: [.continueAfterUnknownField]) { name, offset, type in
let fieldName = String(cString: name)
write(&result, string: "var \(fieldName): \(type) // offset = \(offset.hex)", level: level+1)
if recursive {
Expand All @@ -74,7 +74,7 @@ extension Metadata {
case .tuple: break
case .class:
write(&result, string: "class \(type) {", level: level)
_ = forEachField(options: [.enumerateClassFields]) { name, offset, type in // anything contains ._1 will work here
_ = forEachField(options: [.enumerateClassFields, .continueAfterUnknownField]) { name, offset, type in
let fieldName = String(cString: name)

write(&result, string: "var \(fieldName): \(type) // offset = \(offset.hex)", level: level+1)
Expand Down
32 changes: 30 additions & 2 deletions Tests/OpenAttributeGraphShimsTests/MetadataDebugTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
@_spi(Debug) import OpenAttributeGraphShims
import Testing

#if canImport(UIKit)
import UIKit
private typealias PlatformView = UIView
#elseif canImport(AppKit)
import AppKit
private typealias PlatformView = NSView
#endif

@Suite(.disabled(if: !attributeGraphEnabled, "forEachField is not implemented for OAG"))
struct MetadataDebugTests {
struct Demo1 {
Expand All @@ -16,7 +24,7 @@ struct MetadataDebugTests {
var a: Int = .zero
var b: Double = .zero
}

@Test
func layout() {
#expect(Metadata(Demo1.self).layoutDescription == #"""
Expand All @@ -35,5 +43,25 @@ struct MetadataDebugTests {

"""#)
}


#if canImport(UIKit) || canImport(AppKit)
@Test
func unknownFields() {
class DemoView: PlatformView {
var a: Int = .zero
var b: Double = .zero
}
let layoutDescription = Metadata(DemoView.self).layoutDescription.split(separator: "\n").map(String.init)
#expect(layoutDescription[0] == "class DemoView {")
#expect(layoutDescription[1].contains("var a: Int // offset = "))
#expect(layoutDescription[2].contains("var b: Double // offset = "))
#expect(layoutDescription[3] == "}")
}
#endif
}

extension Int {
fileprivate var hex: String {
"0x\(String(format:"%X", self))"
}
}