@@ -41,6 +41,18 @@ final class RedisDataDecoderParsingTests: XCTestCase {
41
41
] )
42
42
}
43
43
44
+ func testParsing_with_arrays( ) throws {
45
+ try parseTest_singleValue ( input: " *1 \r \n +! \r \n " )
46
+ try parseTest_singleValue ( input: " *2 \r \n *1 \r \n :1 \r \n :3 \r \n " )
47
+ try parseTest_singleValue ( input: " *0 \r \n " . convertedToData ( ) )
48
+ try parseTest_singleValue ( input: " *-1 \r \n " . convertedToData ( ) )
49
+ }
50
+
51
+ func testParsing_with_arrays_recursively( ) throws {
52
+ try parseTest_recursive ( withChunks: [ " *2 \r " , " \n +a \r \n +a \r \n * " , " 0 \r \n " ] )
53
+ try parseTest_recursive ( withChunks: [ " *-1 \r " . convertedToData ( ) , " \n " . convertedToData ( ) ] )
54
+ }
55
+
44
56
/// See parse_Test_singleValue(input:) String
45
57
private func parseTest_singleValue( input: String ) throws {
46
58
try parseTest_singleValue ( input: input. convertedToData ( ) )
@@ -208,7 +220,7 @@ extension RedisDataDecoderParsingTests {
208
220
}
209
221
210
222
func testParsing_bulkString_handlesLargeSizes( ) throws {
211
- let bytes = [ UInt8 ] . init ( repeating: . dollar, count: 10_000_000 )
223
+ let bytes = [ UInt8 ] . init ( repeating: . dollar, count: 1_000_000 )
212
224
let data = " $ \( bytes. count) \r \n " . convertedToData ( ) + Data( bytes: bytes) + " \r \n " . convertedToData ( )
213
225
XCTAssertEqual ( try parseTestBulkString ( data) , . parsed)
214
226
}
@@ -227,6 +239,55 @@ extension RedisDataDecoderParsingTests {
227
239
}
228
240
}
229
241
242
+ // MARK: Array Parsing
243
+
244
+ extension RedisDataDecoderParsingTests {
245
+ func testParsing_array_whenNull_returnsNil( ) {
246
+ XCTAssertEqual ( try parseTestArray ( " *-1 \r \n " ) , . parsed)
247
+ }
248
+
249
+ func testParsing_array_whenEmpty_returnsEmpty( ) {
250
+ XCTAssertEqual ( try parseTestArray ( " *0 \r \n " ) , . parsed)
251
+ }
252
+
253
+ func testParsing_array_handlesLargeSizes( ) {
254
+ let range = 0 ..< 1000
255
+ var data = " * \( range. endIndex) \r \n " . convertedToData ( )
256
+ range. forEach { _ in
257
+ data += " $5 \r \n " . convertedToData ( )
258
+ data += Data ( bytes: [ 0xaa , 0xbb , 0xcc , 0xab , 0xff ] )
259
+ data += " \r \n " . convertedToData ( )
260
+ }
261
+
262
+ XCTAssertEqual ( try parseTestArray ( data) , . parsed)
263
+ }
264
+
265
+ func testParsing_array_handlesMixedTypes( ) {
266
+ XCTAssertEqual ( try parseTestArray ( " *3 \r \n :3 \r \n +OK \r \n $1 \r \n a \r \n " ) , . parsed)
267
+ }
268
+
269
+ func testParsing_array_handlesNullElements( ) {
270
+ XCTAssertEqual ( try parseTestArray ( " *3 \r \n :3 \r \n $-1 \r \n :30 \r \n " ) , . parsed)
271
+ }
272
+
273
+ func testParsing_array_handlesNestedArrays( ) {
274
+ XCTAssertEqual ( try parseTestArray ( " *2 \r \n :3 \r \n *2 \r \n :30 \r \n :15 \r \n " ) , . parsed)
275
+ }
276
+
277
+ private func parseTestArray( _ input: String ) throws -> RedisDataDecoder . _RedisDataDecodingState {
278
+ return try parseTestArray ( input. convertedToData ( ) )
279
+ }
280
+
281
+ private func parseTestArray( _ input: Data ) throws -> RedisDataDecoder . _RedisDataDecodingState {
282
+ var buffer = allocator. buffer ( capacity: input. count)
283
+ buffer. write ( bytes: input)
284
+
285
+ var position = 1 // "trim" token
286
+
287
+ return try RedisDataDecoder ( ) . _parseArray ( at: & position, from: buffer)
288
+ }
289
+ }
290
+
230
291
extension RedisDataDecoderParsingTests {
231
292
static var allTests = [
232
293
( " testParsing_with_simpleString " , testParsing_with_simpleString) ,
@@ -235,6 +296,8 @@ extension RedisDataDecoderParsingTests {
235
296
( " testParsing_with_integer_recursively " , testParsing_with_integer_recursively) ,
236
297
( " testParsing_with_bulkString " , testParsing_with_bulkString) ,
237
298
( " testParsing_with_bulkString_recursively " , testParsing_with_bulkString_recursively) ,
299
+ ( " testParsing_with_arrays " , testParsing_with_arrays) ,
300
+ ( " testParsing_with_arrays_recursively " , testParsing_with_arrays_recursively) ,
238
301
( " testParsing_simpleString_missingEndings_returnsNil " , testParsing_simpleString_missingEndings_returnsNil) ,
239
302
( " testParsing_simpleString_withNoContent_returnsEmpty " , testParsing_simpleString_withNoContent_returnsEmpty) ,
240
303
( " testParsing_simpleString_withContent_returnsExpectedContent " , testParsing_simpleString_withContent_returnsExpectedContent) ,
@@ -247,5 +310,11 @@ extension RedisDataDecoderParsingTests {
247
310
( " testParsing_bulkString_withNull_returnsNil " , testParsing_bulkString_withNull_returnsNil) ,
248
311
( " testParsing_bulkString_handlesRawBytes " , testParsing_bulkString_handlesRawBytes) ,
249
312
( " testParsing_bulkString_handlesLargeSizes " , testParsing_bulkString_handlesLargeSizes) ,
313
+ ( " testParsing_array_whenNull_returnsNil " , testParsing_array_whenNull_returnsNil) ,
314
+ ( " testParsing_array_whenEmpty_returnsEmpty " , testParsing_array_whenEmpty_returnsEmpty) ,
315
+ ( " testParsing_array_handlesLargeSizes " , testParsing_array_handlesLargeSizes) ,
316
+ ( " testParsing_array_handlesMixedTypes " , testParsing_array_handlesMixedTypes) ,
317
+ ( " testParsing_array_handlesNullElements " , testParsing_array_handlesNullElements) ,
318
+ ( " testParsing_array_handlesNestedArrays " , testParsing_array_handlesNestedArrays) ,
250
319
]
251
320
}
0 commit comments