From 6d3ee55638164df6521a60b6392a26f855d5f924 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Sat, 8 Mar 2025 20:28:10 +0530 Subject: Add test setup + spec for command input --- spec/CommandInputSpec.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++++++ spec/TestUtils.h | 20 +++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 spec/CommandInputSpec.cpp create mode 100644 spec/TestUtils.h (limited to 'spec') diff --git a/spec/CommandInputSpec.cpp b/spec/CommandInputSpec.cpp new file mode 100644 index 0000000..7d15f25 --- /dev/null +++ b/spec/CommandInputSpec.cpp @@ -0,0 +1,71 @@ +#include "TestUtils.h" + +#include "CommandInput.hpp" + +class CommandInputSpec : public QObject { + Q_OBJECT + +private slots: + void testWithInitialInput() { + context("when initialized with some text"); + it("sets the initial input text") { + CommandInput inputWidget("Initial content", new QWidget()); + + QCOMPARE(inputWidget.getInputCommand(), QString("Initial content")); + } + } + + void testInputUpdate() { + context("when user types in input"); + it("updates input command") { + CommandInput inputWidget("Initial content", new QWidget()); + + auto input = inputWidget.findChild(); + QTest::keyClicks(input, " updated"); + + QCOMPARE(inputWidget.getInputCommand(), + QString("Initial content updated")); + } + } + + void testSubmitSignalOnReturnKey() { + context("when user hits return key"); + it("emits the submitted signal with the input command") { + CommandInput inputWidget("Initial content", new QWidget()); + QSignalSpy spy(&inputWidget, &CommandInput::submitted); + + auto input = inputWidget.findChild(); + QTest::keyClicks(input, " updated"); + QTest::keyClick(&inputWidget, Qt::Key_Return); + + QCOMPARE(spy.count(), 1); + QCOMPARE(spy.takeFirst().at(0).toString(), + QString("Initial content updated")); + } + } + + void testSetFocus() { + context("when setInputFocus is called with true"); + xit("focusses input field") { + CommandInput inputWidget("Initial content", new QWidget()); + + inputWidget.setInputFocus(true); + QApplication::processEvents(); + + QVERIFY(inputWidget.isInputFocussed()); + } + + context("when setInputFocus is called with false"); + xit("unfocusses input field") { + CommandInput inputWidget("Initial content", new QWidget()); + + inputWidget.setInputFocus(false); + QApplication::processEvents(); + + QVERIFY(!inputWidget.isInputFocussed()); + } + } +}; + +QTEST_MAIN(CommandInputSpec) +#include "CommandInputSpec.moc" diff --git a/spec/TestUtils.h b/spec/TestUtils.h new file mode 100644 index 0000000..b2c1cc6 --- /dev/null +++ b/spec/TestUtils.h @@ -0,0 +1,20 @@ +#include +#include + +#define ANSI_BOLD "\x1b[1m" +#define COLOR_CONTEXT "\x1b[32m" ANSI_BOLD +#define COLOR_IT "\x1b[36m" ANSI_BOLD +#define COLOR_SKIP "\x1b[33m" ANSI_BOLD +#define ANSI_RESET "\x1b[0m" + +#define context(msg) printf("⚪" COLOR_CONTEXT "%s" ANSI_RESET "\n", msg); + +#define it(msg) \ + printf(" ⚪" ANSI_BOLD COLOR_IT "it " ANSI_RESET COLOR_IT "%s" ANSI_RESET \ + "\n", \ + msg); \ + if (1) + +#define xit(msg) \ + printf(" ⚪" COLOR_SKIP "SKIPPED it %s" ANSI_RESET "\n", msg); \ + if (0) -- cgit v1.3.1