|
| 1 | +import NIO |
| 2 | +@testable import NIORedis |
| 3 | +import XCTest |
| 4 | + |
| 5 | +final class RedisDataEncoderTests: XCTestCase { |
| 6 | + private var encoder: RedisDataEncoder! |
| 7 | + private var allocator: ByteBufferAllocator! |
| 8 | + private var channel: EmbeddedChannel! |
| 9 | + |
| 10 | + override func setUp() { |
| 11 | + super.setUp() |
| 12 | + |
| 13 | + encoder = RedisDataEncoder() |
| 14 | + allocator = ByteBufferAllocator() |
| 15 | + channel = EmbeddedChannel() |
| 16 | + _ = try? channel.pipeline.add(handler: encoder).wait() |
| 17 | + } |
| 18 | + |
| 19 | + override func tearDown() { |
| 20 | + super.tearDown() |
| 21 | + |
| 22 | + _ = try? channel.finish() |
| 23 | + } |
| 24 | + |
| 25 | + func testSimpleStrings() throws { |
| 26 | + let simpleString1 = RedisData.simpleString("Test1") |
| 27 | + try runEncodePass(with: simpleString1) { XCTAssertEqual($0.readableBytes, 8) } |
| 28 | + XCTAssertNoThrow(try channel.writeOutbound(simpleString1)) |
| 29 | + |
| 30 | + let simpleString2 = RedisData.simpleString("®in§³¾") |
| 31 | + try runEncodePass(with: simpleString2) { XCTAssertEqual($0.readableBytes, 13) } |
| 32 | + XCTAssertNoThrow(try channel.writeOutbound(simpleString2)) |
| 33 | + } |
| 34 | + |
| 35 | + func testBulkStrings() throws { |
| 36 | + let bs1 = RedisData.bulkString(Data(bytes: [0x01, 0x02, 0x0a, 0x1b, 0xaa])) |
| 37 | + try runEncodePass(with: bs1) { XCTAssertEqual($0.readableBytes, 11) } |
| 38 | + XCTAssertNoThrow(try channel.writeOutbound(bs1)) |
| 39 | + |
| 40 | + let bs2 = RedisData.bulkString("®in§³¾".convertedToData()) |
| 41 | + try runEncodePass(with: bs2) { XCTAssertEqual($0.readableBytes, 17) } |
| 42 | + XCTAssertNoThrow(try channel.writeOutbound(bs2)) |
| 43 | + |
| 44 | + let bs3 = RedisData.bulkString("".convertedToData()) |
| 45 | + try runEncodePass(with: bs3) { XCTAssertEqual($0.readableBytes, 6) } |
| 46 | + XCTAssertNoThrow(try channel.writeOutbound(bs3)) |
| 47 | + } |
| 48 | + |
| 49 | + func testIntegers() throws { |
| 50 | + let i1 = RedisData.integer(Int.min) |
| 51 | + try runEncodePass(with: i1) { XCTAssertEqual($0.readableBytes, 23) } |
| 52 | + XCTAssertNoThrow(try channel.writeOutbound(i1)) |
| 53 | + |
| 54 | + let i2 = RedisData.integer(0) |
| 55 | + try runEncodePass(with: i2) { XCTAssertEqual($0.readableBytes, 4) } |
| 56 | + XCTAssertNoThrow(try channel.writeOutbound(i2)) |
| 57 | + } |
| 58 | + |
| 59 | + func testArrays() throws { |
| 60 | + let a1 = RedisData.array([]) |
| 61 | + try runEncodePass(with: a1) { XCTAssertEqual($0.readableBytes, 4) } |
| 62 | + XCTAssertNoThrow(try channel.writeOutbound(a1)) |
| 63 | + |
| 64 | + let a2: RedisData = .array([.integer(3), .simpleString("foo")]) |
| 65 | + try runEncodePass(with: a2) { XCTAssertEqual($0.readableBytes, 14) } |
| 66 | + XCTAssertNoThrow(try channel.writeOutbound(a2)) |
| 67 | + |
| 68 | + let bytes = Data(bytes: [ 0x0a, 0x1a, 0x1b, 0xff ]) |
| 69 | + let a3: RedisData = .array([.array([ |
| 70 | + .integer(3), |
| 71 | + .bulkString(bytes) |
| 72 | + ])]) |
| 73 | + try runEncodePass(with: a3) { XCTAssertEqual($0.readableBytes, 22) } |
| 74 | + XCTAssertNoThrow(try channel.writeOutbound(a3)) |
| 75 | + } |
| 76 | + |
| 77 | + func testError() throws { |
| 78 | + let error = RedisError(identifier: "testError", reason: "Manual error") |
| 79 | + let data = RedisData.error(error) |
| 80 | + try runEncodePass(with: data) { |
| 81 | + XCTAssertEqual($0.readableBytes, "-\(error.description)\r\n".convertedToData().count) |
| 82 | + } |
| 83 | + XCTAssertNoThrow(try channel.writeOutbound(data)) |
| 84 | + } |
| 85 | + |
| 86 | + func testNull() throws { |
| 87 | + let null = RedisData.null |
| 88 | + try runEncodePass(with: null) { XCTAssertEqual($0.readableBytes, 5) } |
| 89 | + XCTAssertNoThrow(try channel.writeOutbound(null)) |
| 90 | + } |
| 91 | + |
| 92 | + private func runEncodePass(with input: RedisData, _ validation: (ByteBuffer) -> Void) throws { |
| 93 | + let context = try channel.pipeline.context(handler: encoder).wait() |
| 94 | + |
| 95 | + var buffer = allocator.buffer(capacity: 256) |
| 96 | + try encoder.encode(ctx: context, data: input, out: &buffer) |
| 97 | + validation(buffer) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +extension RedisDataEncoderTests { |
| 102 | + static var allTests = [ |
| 103 | + ("testSimpleStrings", testSimpleStrings), |
| 104 | + ("testBulkStrings", testBulkStrings), |
| 105 | + ("testIntegers", testIntegers), |
| 106 | + ("testArrays", testArrays), |
| 107 | + ("testError", testError), |
| 108 | + ("testNull", testNull), |
| 109 | + ] |
| 110 | +} |
0 commit comments