Skip to content

Commit f5dde40

Browse files
committed
make codable test more robust
1 parent fc88bc5 commit f5dde40

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

Tests/SwiftGraphTests/SwiftGraphCodableTests.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,12 @@ class SwiftGraphCodableTests: XCTestCase {
3939
func testEncodableDecodable() {
4040
let g = expectedUnweightedGraph
4141

42-
let jsonData: Data
4342
do {
44-
jsonData = try JSONEncoder().encode(g)
43+
_ = try JSONEncoder().encode(g)
4544
} catch {
4645
XCTFail("JSONEncoder().encode(g) threw: \(error)")
4746
return
4847
}
49-
let jsonString = String(data: jsonData, encoding: .utf8)
5048

5149
guard let jsonData2 = expectedString.data(using: .utf8) else {
5250
XCTFail("Unable to serialize expected JSON string into Data")
@@ -57,14 +55,20 @@ class SwiftGraphCodableTests: XCTestCase {
5755
do {
5856
g2 = try JSONDecoder().decode(UnweightedGraph<String>.self, from: jsonData2)
5957
} catch {
60-
XCTFail("JSONDecoder().decode(UnweightedGraph<String>.self, from: jsonData) threw: \(error)")
58+
XCTFail("JSONDecoder().decode(UnweightedGraph<String>.self, from: jsonData2) threw: \(error)")
6159
return
6260
}
6361
XCTAssertEqual(g2.neighborsForVertex("Miami")!, g2.neighborsForVertex(g.neighborsForVertex("New York")![0])!, "Miami and New York Connected bi-directionally")
6462
// XCTAssertEqual(g, expectedUnweightedGraph)
6563
let jsonData3 = try! JSONEncoder().encode(g2)
66-
let jsonString2: String = String(data: jsonData3, encoding: .utf8)!
67-
XCTAssertEqual(jsonString, jsonString2)
64+
let g3: UnweightedGraph<String>
65+
do {
66+
g3 = try JSONDecoder().decode(UnweightedGraph<String>.self, from: jsonData3)
67+
} catch {
68+
XCTFail("JSONDecoder().decode(UnweightedGraph<String>.self, from: jsonData3) threw: \(error)")
69+
return
70+
}
71+
XCTAssertEqual(g3.neighborsForVertex("Miami")!, g3.neighborsForVertex(g.neighborsForVertex("New York")![0])!, "Miami and New York Connected bi-directionally")
6872
}
6973
}
7074

0 commit comments

Comments
 (0)