blob: 1017244f8f0f0210c944b2b9fe5e2b574cd8ffdd [file] [log] [blame]
HojjatK 💾d67e5c22023-06-05 19:17:26 -07001//
2// File.swift
3//
4// Created by Khodabakhsh, Hojjat on 5/18/23.
5//
6
7import XCTest
8import Foundation
9@testable import Thrift
10
11class TJSONProtocolTests: XCTestCase {
12 var transport: TMemoryBufferTransport = TMemoryBufferTransport(flushHandler: {
13 $0.reset(readBuffer: $1)
14 })
15
16 var proto: TJSONProtocol!
17
18 override func setUp() {
19 super.setUp()
20 proto = TJSONProtocol(on: transport)
21 transport.reset()
22 }
23
24 override func tearDown() {
25 super.tearDown()
26 transport.reset()
27 }
28
29 func testUInt8WriteRead() {
30 let writeVal: UInt8 = 250
31 try? proto.write(writeVal)
32 try? transport.flush()
33
34 let readVal: UInt8 = (try? proto.read()) ?? 0
35 XCTAssertEqual(writeVal, readVal, "Error with UInt8, wrote \(writeVal) but read \(readVal)")
36 }
37
38 func testInt8WriteRead() {
39 let writeVal: Int8 = -120
40 try? proto.write(writeVal)
41 try? transport.flush()
42
43 let readVal: Int8 = (try? proto.read()) ?? 0
44 XCTAssertEqual(writeVal, readVal, "Error with UInt8, wrote \(writeVal) but read \(readVal)")
45 }
46
47 func testInt16WriteRead() {
48 let writeVal: Int16 = 12312
49 try? proto.write(writeVal)
50 try? transport.flush()
51 let readVal: Int16 = (try? proto.read()) ?? 0
52 XCTAssertEqual(writeVal, readVal, "Error with Int16, wrote \(writeVal) but read \(readVal)")
53 }
54
55 func testInt32WriteRead() {
56 let writeVal: Int32 = 2029234
57 try? proto.write(writeVal)
58 try? transport.flush()
59
60 let readVal: Int32 = (try? proto.read()) ?? 0
61 XCTAssertEqual(writeVal, readVal, "Error with Int32, wrote \(writeVal) but read \(readVal)")
62 }
63
64 func testInt64WriteRead() {
65 let writeVal: Int64 = 234234981374134
66 try? proto.write(writeVal)
67 try? transport.flush()
68
69 let readVal: Int64 = (try? proto.read()) ?? 0
70 XCTAssertEqual(writeVal, readVal, "Error with Int64, wrote \(writeVal) but read \(readVal)")
71 }
72
73 func testDoubleWriteRead() {
74 let writeVal: Double = 3.1415926
75 try? proto.write(writeVal)
76 try? transport.flush()
77
78 let readVal: Double = (try? proto.read()) ?? 0.0
79 XCTAssertEqual(writeVal, readVal, "Error with Double, wrote \(writeVal) but read \(readVal)")
80 }
81
82 func testBoolWriteRead() {
83 let writeVal: Bool = true
84 try? proto.write(writeVal)
85 try? transport.flush()
86
87 let readVal: Bool = (try? proto.read()) ?? false
88 XCTAssertEqual(writeVal, readVal, "Error with Bool, wrote \(writeVal) but read \(readVal)")
89 }
90
91 func testStringWriteRead() {
92 let writeVal: String = "Hello World"
93 try? proto.write(writeVal)
94 try? transport.flush()
95
96 let readVal: String
97 do {
98 readVal = try proto.read()
99 } catch let error {
100 XCTAssertFalse(true, "Error reading \(error)")
101 return
102 }
103
104 XCTAssertEqual(writeVal, readVal, "Error with String, wrote \(writeVal) but read \(readVal)")
105 }
106
107 func testStringWriteRead2() {
108 let writeVal: String = "你好世界 means hello world!"
109 try? proto.write(writeVal)
110 try? transport.flush()
111
112 let readVal: String
113 do {
114 print(writeVal)
115 readVal = try proto.read()
116 print(readVal)
117 } catch let error {
118 XCTAssertFalse(true, "Error reading \(error)")
119 return
120 }
121
122 XCTAssertEqual(writeVal, readVal, "Error with String, wrote \(writeVal) but read \(readVal)")
123 }
124
125 func testDataWriteRead() {
126 let writeVal: Data = "Data World".data(using: .utf8)!
127 try? proto.write(writeVal)
128 try? transport.flush()
129
130 let readVal: Data = (try? proto.read()) ?? "Goodbye World".data(using: .utf8)!
131 XCTAssertEqual(writeVal, readVal, "Error with Data, wrote \(writeVal) but read \(readVal)")
132 }
133
134 func testUUIDWriteRead() {
135 let writeVal: UUID = UUID()
136 try? proto.write(writeVal)
137 try? transport.flush()
138
139 let newUuid = UUID()
140 let readVal: UUID = (try? proto.read()) ?? newUuid
141 XCTAssertEqual(writeVal, readVal, "Error with Data, wrote \(writeVal) but read \(readVal)")
142 }
143
144 func testStructWriteRead() {
145 let msg = "Test Protocol Error"
146 let writeVal = TApplicationError(error: .protocolError, message: msg)
147 do {
148 try writeVal.write(to: proto)
149 try? transport.flush()
150 } catch let error {
151 XCTAssertFalse(true, "Caught Error attempting to write \(error)")
152 }
153
154 do {
155 let readVal: TApplicationError = try TApplicationError.read(from: proto)
156 XCTAssertEqual(readVal.error.thriftErrorCode, writeVal.error.thriftErrorCode, "Error case mismatch, expected \(readVal.error) got \(writeVal.error)")
157 let readValMessage = readVal.message ?? "", writeValMessage = writeVal.message ?? ""
158 XCTAssertEqual(readVal.message, writeVal.message, "Error message mismatch, expected \(readValMessage) got \(writeValMessage)")
159 } catch let error {
160 XCTAssertFalse(true, "Caught Error attempting to read \(error)")
161 }
162 }
163
164 func testBase64WriteRead() {
165 let writeText = "!testing base64 read and write ..."
166 let writeData = writeText.data(using: .utf8)!
167 let writeVal = [UInt8](writeData)
168 try? proto.writeJsonBase64(bytes: writeVal)
169 try? transport.flush()
170
171 var data = Data()
172 if let readVal = try? proto.readJsonBase64() {
173 data = Data(bytes: readVal, count: readVal.count)
174 }
175 let readText = String(decoding: data, as: UTF8.self)
176 XCTAssertEqual(readText, writeText, "Error message mismatch, expected \(readText) got \(writeText)")
177 }
178
179 func testBase64WriteRead2() {
180 let writeText = "你好世界 means hello world!"
181 let writeData = writeText.data(using: .utf8)!
182 let writeVal = [UInt8](writeData)
183 try? proto.writeJsonBase64(bytes: writeVal)
184 try? transport.flush()
185
186 var data = Data()
187 if let readVal = try? proto.readJsonBase64() {
188 data = Data(bytes: readVal, count: readVal.count)
189 }
190 let readText = String(decoding: data, as: UTF8.self)
191 XCTAssertEqual(readText, writeText, "Error message mismatch, expected \(readText) got \(writeText)")
192 }
193
194 static var allTests : [(String, (TJSONProtocolTests) -> () throws -> Void)] {
195 return [
196 ("testUInt8WriteRead", testUInt8WriteRead),
197 ("testInt8WriteRead", testInt8WriteRead),
198 ("testInt16WriteRead", testInt16WriteRead),
199 ("testInt32WriteRead", testInt32WriteRead),
200 ("testInt64WriteRead", testInt64WriteRead),
201 ("testDoubleWriteRead", testDoubleWriteRead),
202 ("testBoolWriteRead", testBoolWriteRead),
203 ("testStringWriteRead", testStringWriteRead),
204 ("testStringWriteRead2", testStringWriteRead2),
205 ("testDataWriteRead", testDataWriteRead),
206 ("testUUIDWriteRead", testUUIDWriteRead),
207 ("testStructWriteRead", testStructWriteRead),
208 ("testBase64WriteRead", testBase64WriteRead),
209 ("testBase64WriteRead2", testBase64WriteRead2)
210 ]
211 }
212}