Skip to content

Commit

Permalink
Adds a plist strategy (pointfreeco#73)
Browse files Browse the repository at this point in the history
* Support plist snapshots.

* update snaps

* revert snaps

* guard for linux

* better guard for linux
  • Loading branch information
mbrandonw authored Nov 2, 2018
1 parent 00bdf92 commit ff2d53f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
22 changes: 20 additions & 2 deletions Sources/SnapshotTesting/Strategy/Codable.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

extension Strategy where A: Encodable {
extension Strategy where A: Encodable, B == String {
@available(OSX 10.13, *)
public static var json: Strategy<A, String> {
let encoder = JSONEncoder()
Expand All @@ -9,8 +9,26 @@ extension Strategy where A: Encodable {
}

public static func json(_ encoder: JSONEncoder) -> Strategy<A, String> {
return Strategy.lines.pullback { encodable in
var strategy = Strategy.lines.pullback { (encodable: A) in
try! String(decoding: encoder.encode(encodable), as: UTF8.self)
}
strategy.pathExtension = "json"
return strategy
}

#if !os(Linux)
public static var plist: Strategy<A, String> {
let encoder = PropertyListEncoder()
encoder.outputFormat = .xml
return .plist(encoder)
}

public static func plist(_ encoder: PropertyListEncoder) -> Strategy<A, String> {
var strategy = Strategy.lines.pullback { (encodable: A) in
try! String(decoding: encoder.encode(encodable), as: UTF8.self)
}
strategy.pathExtension = "plist"
return strategy
}
#endif
}
7 changes: 6 additions & 1 deletion Tests/SnapshotTestingTests/SnapshotTestingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@ class SnapshotTestingTests: SnapshotTestCase {

func testWithEncodable() {
struct User: Encodable { let id: Int, name: String, bio: String }
let user = User(id: 1, name: "Blobby", bio: "Blobbed around the world.")

if #available(OSX 10.13, *) {
assertSnapshot(of: .json, matching: User(id: 1, name: "Blobby", bio: "Blobbed around the world."))
assertSnapshot(of: .json, matching: user)

#if !os(Linux)
assertSnapshot(of: .plist, matching: user)
#endif
}
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>bio</key>
<string>Blobbed around the world.</string>
<key>id</key>
<integer>1</integer>
<key>name</key>
<string>Blobby</string>
</dict>
</plist>

0 comments on commit ff2d53f

Please sign in to comment.