Skip to content

Commit

Permalink
Initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
getogrand committed Apr 26, 2022
1 parent ce4b762 commit 7a84be6
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
DerivedData/
.swiftpm/config/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
30 changes: 30 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// swift-tools-version: 5.6
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "SwiftUIViewTypeInspection",
platforms: [
.iOS(.v13),
.macCatalyst(.v13),
.macOS(.v10_15),
.watchOS(.v6),
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "SwiftUIViewTypeInspection",
targets: ["SwiftUIViewTypeInspection"]),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "SwiftUIViewTypeInspection",
dependencies: []),
.testTarget(
name: "SwiftUIViewTypeInspectionTests",
dependencies: ["SwiftUIViewTypeInspection"]),
]
)
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# SwiftUIViewTypeInspection

SwiftUIViewTypeInspection provides helpers methods on SwiftUI.View to inspect its real concrete
type.

## View.inspectType() -> Self

Print its real concrete type representation to the standard output stream.

Example:

```swift
import SwiftUIViewTypeInspection

struct MyView: View {
var body: some View {
HStack {
Text("Hello, ")
Text("world!")
}
.inspectType() // prints "HStack<TupleView<(Text, Text)>>"
}
}
```

## View.inspectType<Target: TextOutputStream>(to output: inout Target) -> Self

Store its real concrete type representation to the target stream.

Tip: the `String` type conforms `TextOutputStream`.

Example:

```swift
import SwiftUIViewTypeInspection

var output: String = ""

struct MyView: View {
var body: some View {
HStack {
Text("Hello, ")
Text("world!")
}
.inspectType(to: &output) // stores "HStack<TupleView<(Text, Text)>>\n"
}
}
```
53 changes: 53 additions & 0 deletions Sources/SwiftUIViewTypeInspection/SwiftUIViewTypeInspection.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import SwiftUI

extension View {
/// Print its real concrete type representation to the standard output stream.
///
/// Example:
///
/// ```swift
/// import SwiftUIViewTypeInspection
///
/// struct MyView: View {
/// var body: some View {
/// HStack {
/// Text("Hello, ")
/// Text("world!")
/// }
/// .inspectType() // prints "HStack<TupleView<(Text, Text)>>"
/// }
/// }
/// ```
public func inspectType() -> Self {
var output: String = ""
_ = self.inspectType(to: &output)
print(output)
return self
}

/// Store its real concrete type representation to the target stream.
///
/// Tip: the `String` type conforms `TextOutputStream`.
///
/// Example:
///
/// ```swift
/// import SwiftUIViewTypeInspection
///
/// var output: String = ""
///
/// struct MyView: View {
/// var body: some View {
/// HStack {
/// Text("Hello, ")
/// Text("world!")
/// }
/// .inspectType(to: &output) // stores "HStack<TupleView<(Text, Text)>>\n"
/// }
/// }
/// ```
public func inspectType<Target: TextOutputStream>(to output: inout Target) -> Self {
print(Mirror(reflecting: self).subjectType, to: &output)
return self
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import XCTest
import SwiftUI

@testable import SwiftUIViewTypeInspection

final class SwiftUIViewTypeInspectionTests: XCTestCase {
func test_insepctType() throws {
var output: String = ""
_ = FixtureView().body.inspectType(to: &output)
XCTAssertEqual(output, "HStack<TupleView<(Text, Text)>>\n")
}
}

private struct FixtureView: View {
var body: some View {
HStack {
Text("Hello, ")
Text("world!")
}
}
}

0 comments on commit 7a84be6

Please sign in to comment.