#include <QtCore/QCoreApplication>
#include "Widget.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Widget w;
w.setAge(20);
QTest::qExec(&w);
return a.exec();
}
#pragma once
#include <QObject>
#include <qdebug.h>
#include <qtest.h>
class Widget : public QObject
{
Q_OBJECT
public:
explicit Widget(QObject *parent = nullptr);
~Widget();
void setAge(int value);
signals:
public slots:
private slots:
void testFail();
void testAge();
void testWidget();
private:
int age;
};
#include "Widget.h"
Widget::Widget(QObject *parent)
: QObject(parent)
{
age = 0;
}
Widget::~Widget()
{
}
void Widget::setAge(int value)
{
age = value;
}
void Widget::testFail()
{
QFAIL("NO REASON JUST FAIL!");
}
void Widget::testAge()
{
if (!age) QFAIL("Age is not set!");
}
void Widget::testWidget()
{
int value = 45;
// Make sure the age is valid
QVERIFY(age > 0 && age < 100);
// Issue warnings.
if (age > 40) QWARN("Age is over 40");
if (age < 21) QFAIL("Must be an adult!");
// Make sure they are the same
QCOMPARE(age, value);
}
