forked from cucumber/cucumber-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQtTestDriver.cpp
More file actions
67 lines (55 loc) · 1.5 KB
/
QtTestDriver.cpp
File metadata and controls
67 lines (55 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "cucumber-cpp/internal/drivers/QtTestDriver.hpp"
#include <QTest>
#include <QTemporaryFile>
#include <QTextStream>
namespace cucumber {
namespace internal {
/**
* Wraps the QTemporaryFile for Windows.
*
* On Windows. the file can not be written as long as QTemporaryFile keeps it open.
*/
class TemporaryFileWrapper {
public:
~TemporaryFileWrapper() {
QFile::remove(filename);
}
bool exists() const {
return !filename.isEmpty();
}
QString name() const {
return filename;
}
QString read() const {
QFile file{filename};
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return QString();
QTextStream in(&file);
return in.readAll();
}
private:
const QString filename{getTmpFileName()};
static QString getTmpFileName() {
QTemporaryFile tempFile{};
if (!tempFile.open()) {
return {};
}
return tempFile.fileName() + ".txt";
}
};
const InvokeResult QtTestStep::invokeStepBody() {
const TemporaryFileWrapper file{};
if (!file.exists()) {
return InvokeResult::failure("Unable to open temporary file needed for this test");
}
QtTestObject testObject{this};
const QStringList args{"test", "-o", file.name() + ",tap"};
const int returnValue = QTest::qExec(&testObject, args);
if (returnValue == 0) {
return InvokeResult::success();
} else {
return InvokeResult::failure(file.read().toLocal8Bit());
}
}
}
}