Open lukeredpath opened 3 years ago
As a workaround, appending a new line character does seem to solve the problem for me:
extension Snapshotting where Value: Encodable, Format == String {
/// A snapshot strategy for comparing encodable structures based on their JSON representation.
///
/// - Parameter encoder: A JSON encoder.
public static func newlineJson(_ encoder: JSONEncoder) -> Snapshotting {
var snapshotting = SimplySnapshotting.lines.pullback { (encodable: Value) in
try! String(decoding: encoder.encode(encodable), as: UTF8.self) + "\n"
}
snapshotting.pathExtension = "json"
return snapshotting
}
}
I'm trying to use the
json
snapshotting strategy to test my app's Codable types but I'm hitting an issue with new lines in the file causing the diffing to fail.I have a nice little TDD strategy for new types:
The problem is that the generated file appears to have no new line characters and as soon as you save the JSON file (I've tried in Xcode and Vim) a new line character gets added that causes the test to fail.
I'm not sure of the best solution to this - I guess it would either involve explicitly inserting a new line character when saving the JSON file or stripping any trailing newlines before diffing.