Skip to content

Commit

Permalink
Implement dump
Browse files Browse the repository at this point in the history
  • Loading branch information
tahirmt committed Sep 22, 2022
1 parent 8d67542 commit 55ee61a
Show file tree
Hide file tree
Showing 13 changed files with 289 additions and 1 deletion.
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
32 changes: 32 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"pins" : [
{
"identity" : "swift-custom-dump",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-custom-dump.git",
"state" : {
"revision" : "c9b6b940d95c0a925c63f6858943415714d8a981",
"version" : "0.5.2"
}
},
{
"identity" : "swift-snapshot-testing",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-snapshot-testing.git",
"state" : {
"revision" : "f29e2014f6230cf7d5138fc899da51c7f513d467",
"version" : "1.10.0"
}
},
{
"identity" : "xctest-dynamic-overlay",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/xctest-dynamic-overlay",
"state" : {
"revision" : "30314f1ece684dd60679d598a9b89107557b67d9",
"version" : "0.4.1"
}
}
],
"version" : 2
}
37 changes: 37 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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: "swift-snapshot-testing-dump",
platforms: [
.iOS(.v13),
.macOS(.v10_15),
.tvOS(.v13)
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "SnapshotTestingDump",
targets: ["SnapshotTestingDump"]),
],
dependencies: [
.package(url: "https://github.com/pointfreeco/swift-snapshot-testing.git", from: "1.10.0"),
.package(url: "https://github.com/pointfreeco/swift-custom-dump.git", from: "0.5.2"),
],
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: "SnapshotTestingDump",
dependencies: [
.product(name: "SnapshotTesting", package: "swift-snapshot-testing"),
.product(name: "CustomDump", package: "swift-custom-dump"),
]),
.testTarget(
name: "SnapshotTestingDumpTests",
dependencies: ["SnapshotTestingDump"],
exclude: ["__Snapshots__"]),
]
)
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# swift-snapshot-testing-dump
A plugin to use swift-custom-dump with swift-snapshot-testing

A plugin for `swift-snapshot-testing` that combines it with `swift-custom-dump` to be able to produce textual snapshots of model objects.
19 changes: 19 additions & 0 deletions Sources/SnapshotTestingDump/SnapshotTestingDump.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import SnapshotTesting
import CustomDump

extension Snapshotting where Format == String {
/// A snapshot strategy for comparing any structure based on a sanitized text dump.
public static var customDump: Snapshotting {
.customDump()
}

public static func customDump(name: String? = nil,
indent: Int = 0,
maxDepth: Int = .max) -> Snapshotting {
SimplySnapshotting.lines.pullback {
var output = ""
CustomDump.customDump($0, to: &output, name: name, indent: indent, maxDepth: maxDepth)
return output
}
}
}
137 changes: 137 additions & 0 deletions Tests/SnapshotTestingDumpTests/SnapshotTestingDumpTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import XCTest
import SnapshotTesting
import CustomDump
@testable import SnapshotTestingDump

final class SnapshotTestingDumpTests: XCTestCase {
func testStruct() throws {
struct User {
let name = "John"
let email = "john@me.com"
let age = 97
}

assertSnapshot(matching: User(), as: .customDump)
}

func testClass() throws {
class User {
let name = "John"
let email = "john@me.com"
let age = 97
}

assertSnapshot(matching: User(), as: .customDump)
}

func testClassWithSuperclass() throws {
class User {
let name = "John"
let email = "john@me.com"
let age = 97
}

class Doctor: User {
let field = "Podiatry"
let level = "Expert"
}

customDump(Doctor())
assertSnapshot(matching: Doctor(), as: .customDump)
}

func testNSObjectClass() throws {
class User: NSObject {
let name = "John"
let email = "john@me.com"
let age = 97
}

customDump(User())
assertSnapshot(matching: User(), as: .customDump)
}

func testComplexStructure() throws {
struct User {
let name = "John"
let email = "john@me.com"
let age = 97
let friends = [
"James",
"Lilly",
"Peter",
"Remus",
]
}

assertSnapshot(matching: User(), as: .customDump)
}

func testRecursion() throws {
class Human {
let name: String

init(name: String) {
self.name = name
}
}

class Child: Human {
weak var parent: Parent?
}

class Parent: Human {
let children: [Human]

init(name: String, children: [Child]) {
self.children = children
super.init(name: name)

children.forEach {
$0.parent = self
}
}
}

let subject = Parent(name: "Arthur", children: [
Child(name: "Virginia"),
Child(name: "Ronald"),
Child(name: "Fred"),
Child(name: "George"),
Child(name: "Percy"),
Child(name: "Charles"),
])

assertSnapshot(matching: subject, as: .customDump)
}

func testLevelsOfInheritance() throws {
class Human {
let name: String

init(name: String) {
self.name = name
}
}

class Driver: Human {
let wheels: Int

init(name: String, wheels: Int) {
self.wheels = wheels
super.init(name: name)
}
}

class TruckDriver: Driver {
let truckType: String

init(name: String, wheels: Int, truckType: String) {
self.truckType = truckType
super.init(name: name, wheels: wheels)
}
}

assertSnapshot(matching: TruckDriver(name: "Jimmy", wheels: 6, truckType: "Loader"), as: .customDump)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SnapshotTestingDumpTests.User(
name: "John",
email: "john@me.com",
age: 97
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SnapshotTestingDumpTests.Doctor(
field: "Podiatry",
level: "Expert"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
SnapshotTestingDumpTests.User(
name: "John",
email: "john@me.com",
age: 97,
friends: [
[0]: "James",
[1]: "Lilly",
[2]: "Peter",
[3]: "Remus"
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SnapshotTestingDumpTests.TruckDriver(truckType: "Loader")
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SnapshotTestingDumpTests.User(
name: "John",
email: "john@me.com",
age: 97
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
SnapshotTestingDumpTests.Parent(
children: [
[0]: SnapshotTestingDumpTests.Child(
parent: SnapshotTestingDumpTests.Parent(↩︎)
),
[1]: SnapshotTestingDumpTests.Child(
parent: SnapshotTestingDumpTests.Parent(↩︎)
),
[2]: SnapshotTestingDumpTests.Child(
parent: SnapshotTestingDumpTests.Parent(↩︎)
),
[3]: SnapshotTestingDumpTests.Child(
parent: SnapshotTestingDumpTests.Parent(↩︎)
),
[4]: SnapshotTestingDumpTests.Child(
parent: SnapshotTestingDumpTests.Parent(↩︎)
),
[5]: SnapshotTestingDumpTests.Child(
parent: SnapshotTestingDumpTests.Parent(↩︎)
)
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SnapshotTestingDumpTests.User(
name: "John",
email: "john@me.com",
age: 97
)

0 comments on commit 55ee61a

Please sign in to comment.