Repository: apple/swift-nio
File: Tests/NIOCoreTests/ByteBufferTest.swift (Swift)

Code before:
        00000000  79 65 2c 20 77 6f 72 6c  64 21 20 49 74 20 77 61  |ye, world! It wa|
        00000010  73 20 6e 69 63 65 20 6b  6e 6f 77 69 6e 67 20 79  |s nice knowing y|
        00000020  6f 75 2e 0a                                       |ou..|
        00000024
        """
        let actual = buf.hexDumpLong()
        XCTAssertEqual(expected, actual)
    }

    func testHexDumpSwitcher() {
        let buf = ByteBuffer(string: "Goodbye, world! It was nice knowing you.\n")
        let expected = """
        00000000  47 6f 6f 64 62 79 65 2c  20 77 6f 72 6c 64 21 20  |Goodbye, world! |
        00000010  49 74 20 77 61 73 20 6e  69 63 65 20 6b 6e 6f 77  |It was nice know|
        00000020  69 6e 67 20 79 6f 75 2e  0a                       |ing you..|
        00000029
        """
        let actual = buf.hexDump(format: .hexDumpCompatible)
        XCTAssertEqual(expected, actual)
    }

    func testHexDumpLongEmptyBuffer() {
        let buf = ByteBuffer()
        let expected = "00000000"
        let actual = buf.hexDump(format: .hexDumpCompatible)
        XCTAssertEqual(expected, actual)
    }

    func testReadableBytesView() throws {
        self.buf.clear()
        self.buf.writeString("hello world 012345678")
        XCTAssertEqual("hello ", self.buf.readString(length: 6))
        self.buf.moveWriterIndex(to: self.buf.writerIndex - 10)
        XCTAssertEqual("world", String(decoding: self.buf.readableBytesView, as: Unicode.UTF8.self))
        XCTAssertEqual("world", self.buf.readString(length: self.buf.readableBytes))
    }

    func testReadableBytesViewNoReadableBytes() throws {
        self.buf.clear()
        let view = self.buf.readableBytesView
        XCTAssertEqual(0, view.count)
    }

    func testBytesView() throws {
        self.buf.clear()
        self.buf.writeString("hello world 012345678")
        XCTAssertEqual(self.buf.viewBytes(at: self.buf.readerIndex,
                                          length: self.buf.writerIndex - self.buf.readerIndex).map { (view: ByteBufferView) -> String in
                                            String(decoding: view, as: Unicode.UTF8.self)
                                          },
                       self.buf.getString(at: self.buf.readerIndex, length: self.buf.readableBytes))

Review comment (suggestion):
Interestingly `echo -n '' | hexdump -C` doesn't print any output: we should do the same.

Code after:
        """)

        let expected = """
        00000000  47 6f 6f 64 62 79 65 2c  20 77 6f 72 6c 64 21 20  |Goodbye, world! |
        00000010  49 74                                             |It              |
        ........  .. .. .. .. .. .. .. ..  .. .. .. .. .. .. .. ..  ..................
        00000050           69 74 27 73 20  39 34 2b 20 63 6f 6d 6d  |   it's 94+ comm|
        00000060  65 6e 74 73 2e                                    |ents.|
        00000065
        """
        let actual = buf.hexDump(format: .detailed(maxBytes: 36))
        XCTAssertEqual(expected, actual)
    }

    func testHexDumpDetailedWithOffset() {
        var buf = ByteBuffer(string: "Goodbye, world! It was nice knowing you.\n")
        let _ = buf.readBytes(length: 5)
        let expected = """
        00000000  79 65 2c 20 77 6f 72 6c  64 21 20 49 74 20 77 61  |ye, world! It wa|
        00000010  73 20 6e 69 63 65 20 6b  6e 6f 77 69 6e 67 20 79  |s nice knowing y|
        00000020  6f 75 2e 0a                                       |ou..|
        00000024
        """
        let actual = buf.hexDump(format: .detailed)
        XCTAssertEqual(expected, actual)
    }

    func testHexDumpLongEmptyBuffer() {
        let buf = ByteBuffer()
        let expected = ""
        let actual = buf.hexDump(format: .detailed)
        XCTAssertEqual(expected, actual)
    }

    func testReadableBytesView() throws {
        self.buf.clear()
        self.buf.writeString("hello world 012345678")
        XCTAssertEqual("hello ", self.buf.readString(length: 6))
        self.buf.moveWriterIndex(to: self.buf.writerIndex - 10)
        XCTAssertEqual("world", String(decoding: self.buf.readableBytesView, as: Unicode.UTF8.self))
        XCTAssertEqual("world", self.buf.readString(length: self.buf.readableBytes))
    }

    func testReadableBytesViewNoReadableBytes() throws {
        self.buf.clear()
        let view = self.buf.readableBytesView
        XCTAssertEqual(0, view.count)
    }

    func testBytesView() throws {
        self.buf.clear()