Skip to content

Update OGTypeID layoutDescription #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2024
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
11 changes: 11 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ let openGraphShimsTarget = Target.target(
swiftSettings: sharedSwiftSettings
)

let openGraphShimsTestTarget = Target.testTarget(
name: "OpenGraphShimsTests",
dependencies: [
"OpenGraphShims",
],
exclude: ["README.md"],
swiftSettings: sharedSwiftSettings
)

let openGraphTestTarget = Target.testTarget(
name: "OpenGraphTests",
dependencies: [
Expand Down Expand Up @@ -146,6 +155,8 @@ if swiftTestingCondition {
package.targets.append(openGraphCompatibilityTestTarget)
addTestDependency(openGraphTempTestTarget)
package.targets.append(openGraphTempTestTarget)
addTestDependency(openGraphShimsTestTarget)
package.targets.append(openGraphShimsTestTarget)
}

let compatibilityTestCondition = envEnable("OPENGRAPH_COMPATIBILITY_TEST")
Expand Down
15 changes: 11 additions & 4 deletions Sources/OpenGraphShims/OGTypeID+Debug.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ extension OGTypeID {
write(&result, string: "enum \(type) {", level: level)
_ = forEachField(options: [._4]) { name, offset, type in // anything contains ._4 will work here
let fieldName = String(cString: name)
write(&result, string: "case \(fieldName)(\(type)) // offset = \(offset)", level: level+1)
write(&result, string: "case \(fieldName)(\(type)) // offset = \(offset.hex)", level: level+1)
if recursive {
OGTypeID(type)._layoutDescription(&result, recursive: true, level: level+1)
}
Expand All @@ -53,7 +53,7 @@ extension OGTypeID {
case .optional:
_ = forEachField(options: [._4]) { name, offset, type in // anything contains ._4 will work here
let fieldName = String(cString: name)
write(&result, string: "case \(fieldName)(\(type)) // offset = \(offset)", level: level+1)
write(&result, string: "case \(fieldName)(\(type)) // offset = \(offset.hex)", level: level+1)
if recursive {
OGTypeID(type)._layoutDescription(&result, recursive: true, level: level+1)
}
Expand All @@ -63,7 +63,7 @@ extension OGTypeID {
write(&result, string: "struct \(type) {", level: level)
_ = forEachField(options: []) { name, offset, type in // only [] and [._2] will work here
let fieldName = String(cString: name)
write(&result, string: "var \(fieldName): \(type) // offset = \(offset)", level: level+1)
write(&result, string: "var \(fieldName): \(type) // offset = \(offset.hex)", level: level+1)
if recursive {
OGTypeID(type)._layoutDescription(&result, recursive: true, level: level+1)
}
Expand All @@ -75,7 +75,8 @@ extension OGTypeID {
write(&result, string: "class \(type) {", level: level)
_ = forEachField(options: [._1]) { name, offset, type in // anything contains ._1 will work here
let fieldName = String(cString: name)
write(&result, string: "var \(fieldName): \(type) // offset = \(offset)", level: level+1)

write(&result, string: "var \(fieldName): \(type) // offset = \(offset.hex)", level: level+1)
if recursive {
OGTypeID(type)._layoutDescription(&result, recursive: true, level: level+1)
}
Expand All @@ -86,3 +87,9 @@ extension OGTypeID {
}
}
}

extension Int {
fileprivate var hex: String {
"0x\(String(format:"%X", self))"
}
}
39 changes: 39 additions & 0 deletions Tests/OpenGraphShimsTests/OGTypeIDDebugTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// OGTypeIDDebugTests.swift
// OpenGraphTests

@_spi(Debug) import OpenGraphShims
import Testing

@Suite(.disabled(if: !attributeGraphEnabled, "forEachField is not implemented for OG"))
struct OGTypeIDDebugTests {
struct Demo1 {
var a: Int = .zero
var b: Double = .zero
}

class Demo2 {
var a: Int = .zero
var b: Double = .zero
}

@Test
func layout() {
#expect(OGTypeID(Demo1.self).layoutDescription == #"""
struct Demo1 {
\#tvar a: Int // offset = 0x0
\#tvar b: Double // offset = 0x8
}

"""#)

#expect(OGTypeID(Demo2.self).layoutDescription == #"""
class Demo2 {
\#tvar a: Int // offset = 0x10
\#tvar b: Double // offset = 0x18
}

"""#)
}

}
7 changes: 7 additions & 0 deletions Tests/OpenGraphShimsTests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## OpenGraphShimsTests

Test OpenGraphShims Debug API

```swift
@_spi(Debug) import OpenGraphShims
```
13 changes: 13 additions & 0 deletions Tests/OpenGraphShimsTests/Scaffolding.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// Scaffolding.swift
// OpenGraphShimsTests

import Testing
import XCTest

// See https://github.com/apple/swift-testing/issues/329
//final class AllTests: XCTestCase {
// func testAll() async {
// await XCTestScaffold.runAllTests(hostedBy: self)
// }
//}