-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathTestHelpers.swift
More file actions
48 lines (41 loc) · 1.24 KB
/
TestHelpers.swift
File metadata and controls
48 lines (41 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//
// TestHelpers.swift
//
//
// Created by Mathew Polzin on 6/23/19.
//
import XCTest
let testEncoder = { () -> JSONEncoder in
let encoder = JSONEncoder()
if #available(macOS 10.13, *) {
encoder.dateEncodingStrategy = .iso8601
encoder.keyEncodingStrategy = .useDefaultKeys
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
}
#if os(Linux)
encoder.dateEncodingStrategy = .iso8601
encoder.keyEncodingStrategy = .useDefaultKeys
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
#endif
return encoder
}()
func testStringFromEncoding<T: Encodable>(of entity: T) throws -> String? {
return String(data: try testEncoder.encode(entity), encoding: .utf8)
}
func assertJSONEquivalent(_ str1: String?, _ str2: String?, file: StaticString = #filePath, line: UInt = #line) {
// when testing on Linux, pretty printing has slightly different
// meaning so the tests pass on OS X as written but need whitespace
// stripped to pass on Linux
#if os(Linux)
var str1 = str1
var str2 = str2
str1?.removeAll { $0.isWhitespace }
str2?.removeAll { $0.isWhitespace }
#endif
XCTAssertEqual(
str1,
str2,
file: file,
line: line
)
}