Skip to content

Commit aa785ad

Browse files
authored
Make it work on Linux (#5)
* Make Combine optional. ProtocolTransportTests.testSendNotification(Async) are failing, as the response has differently sorted keys in the JSON object testDecodeFloatOverflow, testDecodeNotDouble and testDecodeOverflow are failing due to different error messages for invalid input. These messages are functionally the same * Fix tests on Linux.
1 parent 7253f94 commit aa785ad

File tree

3 files changed

+56
-15
lines changed

3 files changed

+56
-15
lines changed

Sources/JSONRPC/JSONValueDecoder.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
import Foundation
2-
import Combine
2+
#if canImport(Combine)
3+
import Combine
4+
#else
5+
public protocol TopLevelDecoder {
6+
associatedtype Input
7+
8+
func decode<T>(
9+
_ type: T.Type,
10+
from: JSONValueDecoder.Input
11+
) throws -> T where T: Decodable
12+
}
13+
#endif
314

415
/// An object that decodes instances of a data type from `JSONValue` objects.
516
public class JSONValueDecoder: TopLevelDecoder {

Tests/JSONRPCTests/JSONValueDecoderTests.swift

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,17 @@ final class JSONValueDecoderTests: XCTestCase {
108108
from: JSONValue.hash(["double": "string"])
109109
)
110110
) { error in
111-
XCTAssertEqual(
112-
error.localizedDescription,
113-
"The data couldn’t be read because it isn’t in the correct format."
114-
)
111+
#if os(Linux)
112+
XCTAssertEqual(
113+
error.localizedDescription,
114+
"The operation could not be completed. The data isn’t in the correct format."
115+
)
116+
#else
117+
XCTAssertEqual(
118+
error.localizedDescription,
119+
"The data couldn’t be read because it isn’t in the correct format."
120+
)
121+
#endif
115122
}
116123
}
117124

@@ -122,10 +129,17 @@ final class JSONValueDecoderTests: XCTestCase {
122129
from: JSONValue.hash(["int8": 300])
123130
)
124131
) { error in
125-
XCTAssertEqual(
126-
error.localizedDescription,
127-
"The data couldn’t be read because it isn’t in the correct format."
128-
)
132+
#if os(Linux)
133+
XCTAssertEqual(
134+
error.localizedDescription,
135+
"The operation could not be completed. The data isn’t in the correct format."
136+
)
137+
#else
138+
XCTAssertEqual(
139+
error.localizedDescription,
140+
"The data couldn’t be read because it isn’t in the correct format."
141+
)
142+
#endif
129143
}
130144
}
131145

@@ -136,10 +150,17 @@ final class JSONValueDecoderTests: XCTestCase {
136150
from: JSONValue.hash(["float": 1e300])
137151
)
138152
) { error in
139-
XCTAssertEqual(
140-
error.localizedDescription,
141-
"The data couldn’t be read because it isn’t in the correct format."
142-
)
153+
#if os(Linux)
154+
XCTAssertEqual(
155+
error.localizedDescription,
156+
"The operation could not be completed. The data isn’t in the correct format."
157+
)
158+
#else
159+
XCTAssertEqual(
160+
error.localizedDescription,
161+
"The data couldn’t be read because it isn’t in the correct format."
162+
)
163+
#endif
143164
}
144165
}
145166
}

Tests/JSONRPCTests/ProtocolTransportTests.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ final class ProtocolTransportTests: XCTestCase {
7979
let result = JSONRPCNotification(method: "mynotification", params: params)
8080
let resultData = try JSONEncoder().encode(result)
8181

82-
XCTAssertEqual(dataTransport.writtenData, [resultData])
82+
assertDataArraysEquals(dataTransport.writtenData, [resultData])
8383
}
8484

8585
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
@@ -94,7 +94,16 @@ final class ProtocolTransportTests: XCTestCase {
9494
let result = JSONRPCNotification(method: "mynotification", params: params)
9595
let resultData = try JSONEncoder().encode(result)
9696

97-
XCTAssertEqual(dataTransport.writtenData, [resultData])
97+
assertDataArraysEquals(dataTransport.writtenData, [resultData])
98+
}
99+
100+
func assertDataArraysEquals(_ expr1: [Data], _ expr2: [Data]) {
101+
XCTAssertEqual(expr1.count, expr2.count)
102+
for idx in 0..<expr1.count {
103+
let sorted1 = expr1[idx].sorted()
104+
let sorted2 = expr2[idx].sorted()
105+
XCTAssertEqual(sorted1, sorted2)
106+
}
98107
}
99108

100109
func testServerToClientNotification() throws {

0 commit comments

Comments
 (0)