First Commit
This commit is contained in:
commit
3796ec5cd2
146 files changed
+4825
No files matched your search
@@ -0,0 +1,90 @@
|
||||
#include <QtTest>
|
||||
#include <QComboBox>
|
||||
#include <QLineEdit>
|
||||
#include <QLabel>
|
||||
#include <QPushButton>
|
||||
#include "conversion.h"
|
||||
#include "mainwindow.h"
|
||||
#include <QApplication>
|
||||
#include <QDir>
|
||||
#include "appearance.h"
|
||||
|
||||
static void savePreview(QWidget &window, const QString &fileName)
|
||||
{
|
||||
const QString folder = QString::fromLocal8Bit(qgetenv("QT_TEACHING_SCREENSHOTS"));
|
||||
if (folder.isEmpty()) return;
|
||||
QDir().mkpath(folder);
|
||||
window.show();
|
||||
QApplication::processEvents();
|
||||
QVERIFY(window.grab().save(QDir(folder).filePath(fileName)));
|
||||
}
|
||||
|
||||
class ConverterTest : public QObject {
|
||||
Q_OBJECT
|
||||
private slots:
|
||||
void initTestCase() { applyAppearance(*qApp); }
|
||||
void knownValues_data()
|
||||
{
|
||||
QTest::addColumn<int>("category");
|
||||
QTest::addColumn<QString>("input");
|
||||
QTest::addColumn<int>("from");
|
||||
QTest::addColumn<int>("to");
|
||||
QTest::addColumn<double>("expected");
|
||||
QTest::newRow("freezing") << 0 << QStringLiteral("0") << 0 << 1 << 32.0;
|
||||
QTest::newRow("boiling") << 0 << QStringLiteral("212") << 1 << 0 << 100.0;
|
||||
QTest::newRow("minus40") << 0 << QStringLiteral("-40") << 0 << 1 << -40.0;
|
||||
QTest::newRow("kelvin") << 0 << QStringLiteral("273.15") << 2 << 0 << 0.0;
|
||||
QTest::newRow("absolute-zero-C") << 0 << QStringLiteral("-273.15") << 0 << 2 << 0.0;
|
||||
QTest::newRow("absolute-zero-F") << 0 << QStringLiteral("-459.67") << 1 << 2 << 0.0;
|
||||
QTest::newRow("meter-cm") << 1 << QStringLiteral("1") << 2 << 1 << 100.0;
|
||||
QTest::newRow("inch-cm") << 1 << QStringLiteral("1") << 4 << 1 << 2.54;
|
||||
QTest::newRow("foot-meter") << 1 << QStringLiteral("1") << 5 << 2 << 0.3048;
|
||||
QTest::newRow("km-mm") << 1 << QStringLiteral("1") << 3 << 0 << 1000000.0;
|
||||
QTest::newRow("scientific") << 1 << QStringLiteral(" 1e3 ") << 0 << 2 << 1.0;
|
||||
QTest::newRow("same-unit") << 1 << QStringLiteral("12.25") << 2 << 2 << 12.25;
|
||||
}
|
||||
void knownValues()
|
||||
{
|
||||
QFETCH(int, category); QFETCH(QString, input);
|
||||
QFETCH(int, from); QFETCH(int, to); QFETCH(double, expected);
|
||||
auto result = Conversion::convert(input, static_cast<Conversion::Category>(category), from, to);
|
||||
QVERIFY2(result.ok, qPrintable(result.error));
|
||||
QVERIFY(qAbs(result.value - expected) <= 1e-9 * qMax(1.0, qAbs(expected)));
|
||||
}
|
||||
void rejectsInvalid()
|
||||
{
|
||||
for (const QString &text : {QString(), QStringLiteral("12abc"), QStringLiteral("nan"),
|
||||
QStringLiteral("inf"), QStringLiteral("1,000"), QStringLiteral("1e309")})
|
||||
QVERIFY(!Conversion::convert(text, Conversion::Category::Length, 2, 1).ok);
|
||||
QVERIFY(!Conversion::convert(QStringLiteral("-1"), Conversion::Category::Length, 2, 1).ok);
|
||||
QVERIFY(!Conversion::convert(QStringLiteral("-274"), Conversion::Category::Temperature, 0, 2).ok);
|
||||
QVERIFY(!Conversion::convert(QStringLiteral("-0.1"), Conversion::Category::Temperature, 2, 0).ok);
|
||||
QVERIFY(!Conversion::convert(QStringLiteral("1e308"), Conversion::Category::Length, 3, 0).ok);
|
||||
}
|
||||
void uiWorkflow()
|
||||
{
|
||||
MainWindow w; w.show();
|
||||
auto *input = w.findChild<QLineEdit *>(QStringLiteral("inputEdit"));
|
||||
auto *result = w.findChild<QLineEdit *>(QStringLiteral("resultEdit"));
|
||||
auto *convert = w.findChild<QPushButton *>(QStringLiteral("convertButton"));
|
||||
input->setText(QStringLiteral("25"));
|
||||
QTest::mouseClick(convert, Qt::LeftButton);
|
||||
QVERIFY(result->text().startsWith(QStringLiteral("77 ")));
|
||||
savePreview(w, QStringLiteral("01_converter.png"));
|
||||
input->setText(QStringLiteral("bad"));
|
||||
QVERIFY(result->text().isEmpty());
|
||||
QTest::mouseClick(convert, Qt::LeftButton);
|
||||
QVERIFY(!w.findChild<QLabel *>(QStringLiteral("statusLabel"))->text().isEmpty());
|
||||
QVERIFY(!w.findChild<QPushButton *>(QStringLiteral("copyButton"))->isEnabled());
|
||||
w.findChild<QComboBox *>(QStringLiteral("categoryCombo"))->setCurrentIndex(1);
|
||||
input->setText(QStringLiteral("1"));
|
||||
QTest::mouseClick(convert, Qt::LeftButton);
|
||||
QVERIFY(result->text().startsWith(QStringLiteral("100 ")));
|
||||
QTest::mouseClick(w.findChild<QPushButton *>(QStringLiteral("swapButton")), Qt::LeftButton);
|
||||
QTest::mouseClick(convert, Qt::LeftButton);
|
||||
QVERIFY(result->text().startsWith(QStringLiteral("0.01 ")));
|
||||
}
|
||||
};
|
||||
QTEST_MAIN(ConverterTest)
|
||||
#include "test_UnitConverter.moc"
|
||||
|
||||
Reference in new issue
Block a user