|
| 1 | +#include <cstdlib> |
| 2 | +#include <gtest/gtest.h> |
| 3 | +#include <cucumber-cpp/defs.hpp> |
| 4 | +#include <QApplication> |
| 5 | +#include <QTest> |
| 6 | + |
| 7 | +#include "CalculatorWidget.h" |
| 8 | + |
| 9 | +static int argc = 0; |
| 10 | +static QApplication app(argc, 0); |
| 11 | +static int milliseconds = -1; |
| 12 | + |
| 13 | +int millisecondsToWait() { |
| 14 | + if (milliseconds < 0) |
| 15 | + { |
| 16 | + char* envVariable = getenv("CALCQT_STEP_DELAY"); |
| 17 | + milliseconds = (0 != envVariable) ? atoi(envVariable) : 0; |
| 18 | + } |
| 19 | + return milliseconds; |
| 20 | +} |
| 21 | + |
| 22 | +std::istream& operator>> (std::istream& in, QString& val) { std::string s; in >> s; val = s.c_str(); return in; } |
| 23 | +std::ostream& operator<< (std::ostream& out, const QString& val) { out << val.toLatin1().data(); return out; } |
| 24 | + |
| 25 | +GIVEN("^I just turned on the calculator$") { |
| 26 | + cucumber::ScenarioScope<CalculatorWidget> calculator; |
| 27 | + calculator->move(0, 0); |
| 28 | + calculator->show(); |
| 29 | +#if QT_VERSION >= 0x050000 |
| 30 | + QTest::qWaitForWindowExposed(calculator.get()); |
| 31 | +#else |
| 32 | + QTest::qWaitForWindowShown(calculator.get()); |
| 33 | +#endif |
| 34 | + QTest::qWait(millisecondsToWait()); |
| 35 | +} |
| 36 | + |
| 37 | +WHEN("^I press (\\d+)$") { |
| 38 | + REGEX_PARAM(unsigned int, n); |
| 39 | + cucumber::ScenarioScope<CalculatorWidget> calculator; |
| 40 | + QTest::keyClick(calculator.get(), Qt::Key_0 + n, Qt::NoModifier, millisecondsToWait()); |
| 41 | +} |
| 42 | + |
| 43 | +WHEN("^I press add") { |
| 44 | + cucumber::ScenarioScope<CalculatorWidget> calculator; |
| 45 | + QTest::keyClick(calculator.get(), Qt::Key_Plus, Qt::NoModifier, millisecondsToWait()); |
| 46 | +} |
| 47 | + |
| 48 | +WHEN("^I press calculate") { |
| 49 | + cucumber::ScenarioScope<CalculatorWidget> calculator; |
| 50 | + QTest::keyClick(calculator.get(), Qt::Key_Return, Qt::NoModifier, millisecondsToWait()); |
| 51 | +} |
| 52 | + |
| 53 | +WHEN("^I press clear") { |
| 54 | + cucumber::ScenarioScope<CalculatorWidget> calculator; |
| 55 | + QTest::keyClick(calculator.get(), Qt::Key_Escape, Qt::NoModifier, millisecondsToWait()); |
| 56 | +} |
| 57 | + |
| 58 | +WHEN("^I press subtract") { |
| 59 | + cucumber::ScenarioScope<CalculatorWidget> calculator; |
| 60 | + QTest::keyClick(calculator.get(), Qt::Key_Minus, Qt::NoModifier, millisecondsToWait()); |
| 61 | +} |
| 62 | + |
| 63 | +THEN("^the display should be empty$") { |
| 64 | + cucumber::ScenarioScope<CalculatorWidget> calculator; |
| 65 | + EXPECT_EQ(calculator->display().size(), 0); |
| 66 | + QTest::qWait(millisecondsToWait()); |
| 67 | +} |
| 68 | + |
| 69 | +THEN("^the display should show (.*)$") { |
| 70 | + REGEX_PARAM(QString, expected); |
| 71 | + cucumber::ScenarioScope<CalculatorWidget> calculator; |
| 72 | + EXPECT_EQ(expected, calculator->display()); |
| 73 | + QTest::qWait(millisecondsToWait()); |
| 74 | +} |
0 commit comments