-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add helper protocols for the properties included in the dump
The current method of using the CustomDumpReflectable can work to produce this but it requires a lot more work to implement for each type that needs it.
- Loading branch information
Showing
3 changed files
with
212 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/// Implement this protocol to exclude superclass nodes | ||
public protocol CustomDumpExcludeSuperclass {} | ||
|
||
/// Implement this protocol to ignore dumping child nodes | ||
/// | ||
/// Using this protocol will make the type ignore the children completly and only print out the type name. | ||
/// This can be useful when something is irrelevant | ||
public protocol CustomDumpIgnoreChildNodes {} | ||
|
||
/// Properties to include child nodes. By default all child nodes are included but when this is implemented, only the values passed will be included in the dump | ||
/// | ||
/// ``` | ||
/// struct Human { | ||
/// let name = "Jimmy" | ||
/// } | ||
/// | ||
/// struct User: CustomDumpIncludedChildNodesProvider { | ||
/// static var includedNodes: [String]? { | ||
/// [ | ||
/// "name", | ||
/// "email", | ||
/// "friends", | ||
/// ] | ||
/// } | ||
/// let name = "John" | ||
/// let email = "[email protected]" | ||
/// let age = 97 | ||
/// let friends = [ | ||
/// "James", | ||
/// "Lilly", | ||
/// "Peter", | ||
/// "Remus", | ||
/// ] | ||
/// let human = Human() | ||
/// } | ||
/// ``` | ||
/// The dump for this will produce | ||
/// ``` | ||
/// User( | ||
/// name: "John", | ||
/// email: "[email protected]", | ||
/// friends: [ | ||
/// [0]: "James", | ||
/// [1]: "Lilly", | ||
/// [2]: "Peter", | ||
/// [3]: "Remus" | ||
/// ] | ||
/// ) | ||
/// ``` | ||
public protocol CustomDumpIncludedChildNodesProvider { | ||
/// Which nodes to include in the dump | ||
static var includedNodes: [String]? { get } | ||
} | ||
|
||
/// Properties to exclude child nodes. This can be helpful when one or more fields are not relevant | ||
/// | ||
/// ``` | ||
/// struct Human { | ||
/// let name = "Jimmy" | ||
/// } | ||
/// | ||
/// struct User: CustomDumpExcludedChildNodesProvider { | ||
/// static var excludedNodes: [String] { | ||
/// [ | ||
/// "age", | ||
/// "friends" | ||
/// ] | ||
/// } | ||
/// let name = "John" | ||
/// let email = "[email protected]" | ||
/// let age = 97 | ||
/// let friends = [ | ||
/// "James", | ||
/// "Lilly", | ||
/// "Peter", | ||
/// "Remus", | ||
/// ] | ||
/// let human = Human() | ||
/// } | ||
/// ``` | ||
/// The dump for this will produce | ||
/// ``` | ||
/// User( | ||
/// name: "John", | ||
/// email: "[email protected]", | ||
/// friends: [ | ||
/// [0]: "James", | ||
/// [1]: "Lilly", | ||
/// [2]: "Peter", | ||
/// [3]: "Remus" | ||
/// ] | ||
/// ) | ||
/// ``` | ||
public protocol CustomDumpExcludedChildNodesProvider { | ||
/// Which nodes to exclude from the dump | ||
static var excludedNodes: [String] { get } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -850,6 +850,77 @@ final class DumpTests: XCTestCase { | |
) | ||
} | ||
|
||
func testIncludedNodes() { | ||
var dump = "" | ||
class Human: CustomDumpIncludedChildNodesProvider { | ||
static var includedNodes: [String]? { | ||
[ | ||
"name", | ||
"email", | ||
] | ||
} | ||
let name = "John" | ||
let email = "[email protected]" | ||
let age = 97 | ||
} | ||
|
||
customDump(Human(), to: &dump) | ||
|
||
XCTAssertNoDifference( | ||
dump, | ||
""" | ||
DumpTests.Human( | ||
name: "John", | ||
email: "[email protected]" | ||
) | ||
""" | ||
) | ||
} | ||
|
||
func testExcludedNodes() { | ||
var dump = "" | ||
class Human: CustomDumpExcludedChildNodesProvider { | ||
static var excludedNodes: [String] { | ||
[ | ||
"name", | ||
] | ||
} | ||
let name = "John" | ||
let email = "[email protected]" | ||
let age = 97 | ||
} | ||
|
||
customDump(Human(), to: &dump) | ||
|
||
XCTAssertNoDifference( | ||
dump, | ||
""" | ||
DumpTests.Human( | ||
email: "[email protected]", | ||
age: 97 | ||
) | ||
""" | ||
) | ||
} | ||
|
||
func testIgnoreChildNodes() { | ||
var dump = "" | ||
class Human: CustomDumpIgnoreChildNodes { | ||
let name = "John" | ||
let email = "[email protected]" | ||
let age = 97 | ||
} | ||
|
||
customDump(Human(), to: &dump) | ||
|
||
XCTAssertNoDifference( | ||
dump, | ||
""" | ||
DumpTests.Human(...) | ||
""" | ||
) | ||
} | ||
|
||
func testRepeatition() { | ||
class Human { | ||
let name = "John" | ||
|
@@ -871,25 +942,26 @@ final class DumpTests: XCTestCase { | |
|
||
XCTAssertNoDifference( | ||
dump, | ||
""" | ||
[ | ||
[0]: DumpTests.Human( | ||
name: "John", | ||
email: "[email protected]", | ||
age: 97 | ||
), | ||
[1]: DumpTests.Human(↩︎), | ||
[2]: DumpTests.Human( | ||
name: "John", | ||
email: "[email protected]", | ||
age: 97 | ||
), | ||
[3]: DumpTests.Human(↩︎) | ||
] | ||
""" | ||
""" | ||
[ | ||
[0]: DumpTests.Human( | ||
name: "John", | ||
email: "[email protected]", | ||
age: 97 | ||
), | ||
[1]: DumpTests.Human(↩︎), | ||
[2]: DumpTests.Human( | ||
name: "John", | ||
email: "[email protected]", | ||
age: 97 | ||
), | ||
[3]: DumpTests.Human(↩︎) | ||
] | ||
""" | ||
) | ||
} | ||
|
||
|
||
#if canImport(CoreGraphics) | ||
func testCoreGraphics() { | ||
var dump = "" | ||
|