QBuffer: test and document open() behavior

QBuffer::open() was only documented as \reimp, so its behavior
regarding WriteOnly was never actually described.

Add a test and document the outcome.

Pick-to: 6.5 6.4 6.2 5.15
Change-Id: I75c49cd3f6a1961bcaece4a92a4e479bb3300d36
Reviewed-by: Mate Barany <mate.barany@qt.io>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Marc Mutz 2022-12-12 15:09:25 +01:00
parent ed623e6375
commit c262a1805a
2 changed files with 28 additions and 0 deletions

View File

@ -288,6 +288,10 @@ void QBuffer::setData(const char *data, qsizetype size)
/*!
\reimp
Unlike QFile, opening a QBuffer QIODevice::WriteOnly does not truncate it.
However, pos() is set to 0. Use QIODevice::Append or QIODevice::Truncate to
change either behavior.
*/
bool QBuffer::open(OpenMode flags)
{

View File

@ -15,6 +15,7 @@ class tst_QBuffer : public QObject
Q_OBJECT
private slots:
void open();
void openWriteOnlyDoesNotTruncate();
void getSetCheck();
void readBlock();
void readBlockPastEnd();
@ -111,6 +112,29 @@ void tst_QBuffer::open()
b.close();
}
void tst_QBuffer::openWriteOnlyDoesNotTruncate()
{
QBuffer b;
const auto data = QByteArrayLiteral("Hey, presto!");
{
QVERIFY(b.open(QIODevice::WriteOnly));
b.write(data);
b.close();
}
{
QVERIFY(b.open(QIODevice::ReadOnly));
QCOMPARE(b.readAll(), data);
b.close();
}
{
QVERIFY(b.open(QIODevice::WriteOnly));
QCOMPARE(b.size(), data.size());
QCOMPARE(b.pos(), 0);
b.close();
}
}
// some status() tests, too
void tst_QBuffer::readBlock()
{