blob: ccc32aa5c78d142f5e71029ada9754fb4b694108 [file] [log] [blame]
Chris Simpsona9b6c702018-04-08 07:11:37 -04001//
2// TCompactProtocolTests.swift
3// Thrift
4//
5// Created by Christopher Simpson on 8/19/16.
6//
7//
8
9import XCTest
10import Foundation
11@testable import Thrift
12
13
14/// Testing Binary protocol read/write against itself
15/// Uses separate read/write transport/protocols
16class TCompactProtocolTests: XCTestCase {
17 var transport: TMemoryBufferTransport = TMemoryBufferTransport(flushHandler: {
18 $0.reset(readBuffer: $1)
19 })
20 var proto: TCompactProtocol!
21
22 override func setUp() {
23 super.setUp()
24 proto = TCompactProtocol(on: transport)
25 transport.reset()
26 }
27
28 override func tearDown() {
29 super.tearDown()
30 transport.reset()
31 }
32
33 func testInt8WriteRead() {
34 let writeVal: UInt8 = 250
35 try? proto.write(writeVal)
36 try? transport.flush()
37 let readVal: UInt8 = (try? proto.read()) ?? 0
38 XCTAssertEqual(writeVal, readVal, "Error with UInt8, wrote \(writeVal) but read \(readVal)")
39 }
40
41 func testInt16WriteRead() {
42 let writeVal: Int16 = 12312
43 try? proto.write(writeVal)
44 try? transport.flush()
45
46 let readVal: Int16 = (try? proto.read()) ?? 0
47 XCTAssertEqual(writeVal, readVal, "Error with Int16, wrote \(writeVal) but read \(readVal)")
48 }
49
50 func testInt32WriteRead() {
51 let writeVal: Int32 = 2029234
52 try? proto.write(writeVal)
53 try? transport.flush()
54
55 let readVal: Int32 = (try? proto.read()) ?? 0
56 XCTAssertEqual(writeVal, readVal, "Error with Int32, wrote \(writeVal) but read \(readVal)")
57 }
58
59 func testInt64WriteRead() {
60 let writeVal: Int64 = 234234981374134
61 try? proto.write(writeVal)
62 try? transport.flush()
63
64 let readVal: Int64 = (try? proto.read()) ?? 0
65 XCTAssertEqual(writeVal, readVal, "Error with Int64, wrote \(writeVal) but read \(readVal)")
66 }
67
68 func testDoubleWriteRead() {
69 let writeVal: Double = 3.1415926
70 try? proto.write(writeVal)
71 try? transport.flush()
72
73 let readVal: Double = (try? proto.read()) ?? 0.0
74 XCTAssertEqual(writeVal, readVal, "Error with Double, wrote \(writeVal) but read \(readVal)")
75 }
76
77 func testBoolWriteRead() {
78 let writeVal: Bool = true
79 try? proto.write(writeVal)
80 try? transport.flush()
81
82 let readVal: Bool = (try? proto.read()) ?? false
83 XCTAssertEqual(writeVal, readVal, "Error with Bool, wrote \(writeVal) but read \(readVal)")
84 }
85
86 func testStringWriteRead() {
87 let writeVal: String = "Hello World"
88 try? proto.write(writeVal)
89 try? transport.flush()
90
91 let readVal: String!
92 do {
93 readVal = try proto.read()
94 } catch let error {
95 XCTAssertFalse(true, "Error reading \(error)")
96 return
97 }
98
99 XCTAssertEqual(writeVal, readVal, "Error with String, wrote \(writeVal) but read \(readVal)")
100 }
101
102 func testDataWriteRead() {
103 let writeVal: Data = "Data World".data(using: .utf8)!
104 try? proto.write(writeVal)
105 try? transport.flush()
106
107 let readVal: Data = (try? proto.read()) ?? "Goodbye World".data(using: .utf8)!
108 XCTAssertEqual(writeVal, readVal, "Error with Data, wrote \(writeVal) but read \(readVal)")
109 }
110
111 func testStructWriteRead() {
112 let msg = "Test Protocol Error"
113 let writeVal = TApplicationError(error: .protocolError, message: msg)
114 do {
115 try writeVal.write(to: proto)
116 try transport.flush()
117
118 } catch let error {
119 XCTAssertFalse(true, "Caught Error attempting to write \(error)")
120 }
121
122 do {
123 let readVal = try TApplicationError.read(from: proto)
124 XCTAssertEqual(readVal.error.thriftErrorCode, writeVal.error.thriftErrorCode, "Error case mismatch, expected \(readVal.error) got \(writeVal.error)")
125 XCTAssertEqual(readVal.message, writeVal.message, "Error message mismatch, expected \(readVal.message) got \(writeVal.message)")
126 } catch let error {
127 XCTAssertFalse(true, "Caught Error attempting to read \(error)")
128 }
129 }
130
131 static var allTests : [(String, (TCompactProtocolTests) -> () throws -> Void)] {
132 return [
133 ("testInt8WriteRead", testInt8WriteRead),
134 ("testInt16WriteRead", testInt16WriteRead),
135 ("testInt32WriteRead", testInt32WriteRead),
136 ("testInt64WriteRead", testInt64WriteRead),
137 ("testDoubleWriteRead", testDoubleWriteRead),
138 ("testBoolWriteRead", testBoolWriteRead),
139 ("testStringWriteRead", testStringWriteRead),
140 ("testDataWriteRead", testDataWriteRead),
141 ("testStructWriteRead", testStructWriteRead)
142 ]
143 }
144}