Skip to content

Commit a87ce04

Browse files
committed
Restructured RedisDataDecoder parsing tests
1 parent bde4eda commit a87ce04

File tree

2 files changed

+99
-93
lines changed

2 files changed

+99
-93
lines changed

Tests/NIORedisTests/RedisDataDecoderTests.swift renamed to Tests/NIORedisTests/RedisDataDecoder+ParsingTests.swift

Lines changed: 98 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,94 @@ import NIO
33
@testable import NIORedis
44
import XCTest
55

6-
final class RedisDataDecoderTests: XCTestCase {
6+
final class RedisDataDecoderParsingTests: XCTestCase {
77
private let allocator = ByteBufferAllocator()
8+
9+
func testParsing_with_simpleString() throws {
10+
try parseTest_singleValue(input: "+OK\r\n")
11+
}
12+
13+
func testParsing_with_simpleString_recursively() throws {
14+
try parseTest_recursive(withChunks: ["+OK\r", "\n+OTHER STRING\r", "\n+&t®in§³¾\r", "\n"])
15+
}
16+
17+
func testParsing_with_integer() throws {
18+
try parseTest_singleValue(input: ":300\r\n")
19+
}
20+
21+
func testParsing_with_integer_recursively() throws {
22+
try parseTest_recursive(withChunks: [":300\r", "\n:-10135135\r", "\n:1\r", "\n"])
23+
}
24+
25+
func testParsing_with_bulkString() throws {
26+
try parseTest_singleValue(input: "$-1\r\n")
27+
try parseTest_singleValue(input: "$0\r\n\r\n")
28+
try parseTest_singleValue(input: "$1\r\n!\r\n")
29+
try parseTest_singleValue(input: "$1\r\n".convertedToData() + Data(bytes: [0xff] + "\r\n".convertedToData()))
30+
31+
let data = "$1\r\n".convertedToData() + Data(bytes: [0xba]) + "\r\n".convertedToData()
32+
try parseTest_singleValue(input: data)
33+
}
34+
35+
func testParsing_with_bulkString_recursively() throws {
36+
try parseTest_recursive(withChunks: ["$3\r", "\naaa\r\n$", "4\r\nnio!\r\n"])
37+
try parseTest_recursive(withChunks: [
38+
"$3\r".convertedToData(),
39+
"\n".convertedToData() + Data(bytes: [0xAA, 0xA3, 0xFF]) + "\r\n$".convertedToData(),
40+
"4\r\n".convertedToData() + Data(bytes: [0xbb, 0x3a, 0xba, 0xFF]) + "\r\n".convertedToData()
41+
])
42+
}
43+
44+
/// See parse_Test_singleValue(input:) String
45+
private func parseTest_singleValue(input: String) throws {
46+
try parseTest_singleValue(input: input.convertedToData())
47+
}
48+
49+
/// Takes a collection of bytes representing a complete message
50+
private func parseTest_singleValue(input: Data) throws {
51+
let decoder = RedisDataDecoder()
52+
var buffer = allocator.buffer(capacity: input.count + 1)
53+
buffer.write(bytes: input)
54+
55+
var position = 0
56+
57+
XCTAssertEqual(try decoder._parse(at: &position, from: buffer), .parsed)
58+
}
59+
60+
/// See parseTest_recursive(withCunks:) [Data]
61+
private func parseTest_recursive(withChunks messageChunks: [String]) throws {
62+
try parseTest_recursive(withChunks: messageChunks.map({ $0.convertedToData() }))
63+
}
64+
65+
/// Takes a collection of byte streams to write to a buffer and assert decoding states in between
66+
/// buffer writes.
67+
/// The expected pattern of messages should be [incomplete, remaining, incomplete, remaining]
68+
private func parseTest_recursive(withChunks messageChunks: [Data]) throws {
69+
let decoder = RedisDataDecoder()
70+
var buffer = allocator.buffer(capacity: messageChunks.joined().count)
71+
buffer.write(bytes: messageChunks[0])
72+
73+
var position = 0
74+
75+
XCTAssertEqual(try decoder._parse(at: &position, from: buffer), .notYetParsed)
76+
77+
for index in 1..<messageChunks.count {
78+
position = 0
79+
80+
buffer.write(bytes: messageChunks[index])
81+
82+
XCTAssertEqual(try decoder._parse(at: &position, from: buffer), .parsed)
83+
84+
_ = buffer.readBytes(length: position)
85+
86+
XCTAssertEqual(try decoder._parse(at: &position, from: buffer), .notYetParsed)
87+
}
88+
}
889
}
990

1091
// MARK: Simple String Parsing
1192

12-
extension RedisDataDecoderTests {
93+
extension RedisDataDecoderParsingTests {
1394
func testParsing_simpleString_missingEndings_returnsNil() throws {
1495
XCTAssertNil(try parseTestSimpleString("+OK"))
1596
XCTAssertNil(try parseTestSimpleString("+OK\r"))
@@ -56,7 +137,7 @@ extension RedisDataDecoderTests {
56137

57138
// MARK: Integer Parsing
58139

59-
extension RedisDataDecoderTests {
140+
extension RedisDataDecoderParsingTests {
60141
func testParsing_integer_missingEndings_returnsNil() throws {
61142
XCTAssertNil(try parseTestInteger("+OK"))
62143
XCTAssertNil(try parseTestInteger(":\r"))
@@ -101,7 +182,7 @@ extension RedisDataDecoderTests {
101182

102183
// MARK: BulkString Parsing
103184

104-
extension RedisDataDecoderTests {
185+
extension RedisDataDecoderParsingTests {
105186
func testParsing_bulkString_handlesMissingEndings() throws {
106187
XCTAssertEqual(try parseTestBulkString("$6"), .notYetParsed)
107188
XCTAssertEqual(try parseTestBulkString("$6\r\n"), .notYetParsed)
@@ -146,101 +227,25 @@ extension RedisDataDecoderTests {
146227
}
147228
}
148229

149-
// MARK: Message Parsing
150-
151-
extension RedisDataDecoderTests {
152-
func testParsing_with_simpleString() throws {
153-
try parseTest_singleValue(input: "+OK\r")
154-
}
155-
156-
func testParsing_with_simpleString_recursively() throws {
157-
try parseTest_recursive(withChunks: ["+OK\r", "\n+OTHER STRING\r", "\n+&t®in§³¾\r", "\n"])
158-
}
159-
160-
func testParsing_with_integer() throws {
161-
try parseTest_singleValue(input: ":300\r")
162-
}
163-
164-
func testParsing_with_integer_recursively() throws {
165-
try parseTest_recursive(withChunks: [":300\r", "\n:-10135135\r", "\n:1\r", "\n"])
166-
}
167-
168-
func testParsing_with_bulkString() throws {
169-
try parseTest_singleValue(input: "$-1\r")
170-
try parseTest_singleValue(input: "$0\r")
171-
try parseTest_singleValue(input: "$1\r\n!\r")
172-
try parseTest_singleValue(input: "$1\r\n".convertedToData() + Data(bytes: [0xff] + "\r".convertedToData()))
173-
}
174-
175-
func testParsing_with_bulkString_recursively() throws {
176-
try parseTest_recursive(withChunks: ["$3\r", "\naaa\r\n$", "4\r\nnio!\r\n"])
177-
}
178-
179-
/// See parse_Test_singleValue(input:) String
180-
private func parseTest_singleValue(input: String) throws {
181-
try parseTest_singleValue(input: input.convertedToData())
182-
}
183-
184-
/// Takes a collection of bytes representing an incomplete message to assert decoding states
185-
/// This method will add the appropriate \n terminator to the end of the byte stream
186-
private func parseTest_singleValue(input: Data) throws {
187-
let decoder = RedisDataDecoder()
188-
var buffer = allocator.buffer(capacity: input.count + 1)
189-
buffer.write(bytes: input)
190-
191-
var position = 0
192-
193-
XCTAssertEqual(try decoder._parse(at: &position, from: buffer), .notYetParsed)
194-
195-
buffer.write(string: "\n")
196-
position = 0 // reset
197-
198-
XCTAssertEqual(try decoder._parse(at: &position, from: buffer), .parsed)
199-
}
200-
201-
/// See parseTest_recursive(withCunks:) [Data]
202-
private func parseTest_recursive(withChunks messageChunks: [String]) throws {
203-
try parseTest_recursive(withChunks: messageChunks.map({ $0.convertedToData() }))
204-
}
205-
206-
/// Takes a collection of byte streams to write to a buffer and assert decoding states in between
207-
/// buffer writes.
208-
/// The expected pattern of messages should be [incomplete, remaining, incomplete, remaining]
209-
private func parseTest_recursive(withChunks messageChunks: [Data]) throws {
210-
let decoder = RedisDataDecoder()
211-
var buffer = allocator.buffer(capacity: messageChunks.joined().count)
212-
buffer.write(bytes: messageChunks[0])
213-
214-
var position = 0
215-
216-
XCTAssertEqual(try decoder._parse(at: &position, from: buffer), .notYetParsed)
217-
218-
for index in 1..<messageChunks.count {
219-
position = 0
220-
221-
buffer.write(bytes: messageChunks[index])
222-
223-
XCTAssertEqual(try decoder._parse(at: &position, from: buffer), .parsed)
224-
225-
_ = buffer.readBytes(length: position)
226-
227-
XCTAssertEqual(try decoder._parse(at: &position, from: buffer), .notYetParsed)
228-
}
229-
}
230-
}
231-
232-
extension RedisDataDecoderTests {
230+
extension RedisDataDecoderParsingTests {
233231
static var allTests = [
232+
("testParsing_with_simpleString", testParsing_with_simpleString),
233+
("testParsing_with_simpleString_recursively", testParsing_with_simpleString_recursively),
234+
("testParsing_with_integer", testParsing_with_integer),
235+
("testParsing_with_integer_recursively", testParsing_with_integer_recursively),
236+
("testParsing_with_bulkString", testParsing_with_bulkString),
237+
("testParsing_with_bulkString_recursively", testParsing_with_bulkString_recursively),
234238
("testParsing_simpleString_missingEndings_returnsNil", testParsing_simpleString_missingEndings_returnsNil),
235239
("testParsing_simpleString_withNoContent_returnsEmpty", testParsing_simpleString_withNoContent_returnsEmpty),
236240
("testParsing_simpleString_withContent_returnsExpectedContent", testParsing_simpleString_withContent_returnsExpectedContent),
237-
("testParsing_simpleString_handlesRecursion", testParsing_simpleString_handlesRecursion),
238241
("testParsing_integer_missingEndings_returnsNil", testParsing_integer_missingEndings_returnsNil),
239242
("testParsing_integer_withContent_returnsExpectedContent", testParsing_integer_withContent_returnsExpectedContent),
240243
("testParsing_integer_handlesRecursion", testParsing_integer_handlesRecursion),
241-
("testParsing_with_simpleString", testParsing_with_simpleString),
242-
("testParsing_with_simpleString_recursively", testParsing_with_simpleString_recursively),
243-
("testParsing_with_integer", testParsing_with_integer),
244-
("testParsing_with_integer_recursively", testParsing_with_integer_recursively),
244+
("testParsing_bulkString_handlesMissingEndings", testParsing_bulkString_handlesMissingEndings),
245+
("testParsing_bulkString_withNoSize_returnsEmpty", testParsing_bulkString_withNoSize_returnsEmpty),
246+
("testParsing_bulkString_withSize_returnsContent", testParsing_bulkString_withSize_returnsContent),
247+
("testParsing_bulkString_withNull_returnsNil", testParsing_bulkString_withNull_returnsNil),
248+
("testParsing_bulkString_handlesRawBytes", testParsing_bulkString_handlesRawBytes),
249+
("testParsing_bulkString_handlesLargeSizes", testParsing_bulkString_handlesLargeSizes),
245250
]
246251
}

Tests/NIORedisTests/XCTestManifests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import XCTest
44
public func allTests() -> [XCTestCaseEntry] {
55
return [
66
testCase(NIORedisTests.allTests),
7+
testCase(RedisDataDecoderParsingTests.allTests)
78
]
89
}
910
#endif

0 commit comments

Comments
 (0)