Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(chore) Add integration testing. #76

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions qml/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ ApplicationWindow {

StackView {
id: stackView
objectName: "stackView"
anchors.fill: parent
// initialItem: LoginPage{
// }
Expand All @@ -34,6 +35,7 @@ ApplicationWindow {
Component {
id: login
LoginPage{
objectName: "loginPage"
}
}

Expand Down
33 changes: 33 additions & 0 deletions tests/includes/documenthandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef DOCUMENTHANDLER_H
#define DOCUMENTHANDLER_H

#include <QQuickTextDocument>

class DocumentHandler : public QObject {
Q_OBJECT
Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)

public:
explicit DocumentHandler(QObject* parent = 0);

~DocumentHandler() {}

Q_INVOKABLE bool isDarkMode();
Q_INVOKABLE void openFile(const QString& path);
Q_INVOKABLE void setDocument(QQuickTextDocument* document, const QString& theme);
Q_INVOKABLE bool saveToFile(const QString& filename, const QString& content);
Q_INVOKABLE QString loadFromFile(const QString& filename);

QString text() const;

public slots:
void setText(QString text);

signals:
void textChanged(QString text);

private:
QString m_text;
};

#endif // DOCUMENTHANDLER_H
33 changes: 33 additions & 0 deletions tests/includes/highlighter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef HIGHLIGHTER_H
#define HIGHLIGHTER_H

#include <QSyntaxHighlighter>
#include <QTextCharFormat>

class Highlighter : public QSyntaxHighlighter {
Q_OBJECT

public:
explicit Highlighter(QTextDocument *parent = 0, const QString &theme = "light");

protected:
void highlightBlock(const QString &text);

private:
struct HighlightingRule {
QRegExp pattern;
QTextCharFormat format;
};
QVector<HighlightingRule> highlightingRules;

QRegExp commentStartExpression;
QRegExp commentEndExpression;

QTextCharFormat keywordFormat;
QTextCharFormat classFormat;
QTextCharFormat singleLineCommentFormat;
QTextCharFormat multiLineCommentFormat;
QTextCharFormat quotationFormat;
QTextCharFormat functionFormat;
};
#endif // HIGHLIGHTER_H
17 changes: 17 additions & 0 deletions tests/includes/qmltests.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <QCoreApplication>
#include <QQmlApplicationEngine>
#include <QtTest>

class QmlTests : public QObject {
Q_OBJECT

private slots:
void initTestCase();
void cleanupTestCase();

void testInit();
void testWelcomePage();

private:
QQmlApplicationEngine engine;
};
85 changes: 85 additions & 0 deletions tests/resources.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<RCC>
<qresource prefix="/">
<file alias="main.qml">../qml/main.qml</file>
<file alias="CustomButton.qml">../qml/CustomButton.qml</file>
<file alias="StatsPage.qml">../qml/StatsPage.qml</file>
<file alias="ReviewPage.qml">../qml/ReviewPage.qml</file>
<file alias="Theme.qml">../qml/Theme.qml</file>
<file alias="SectionPage.qml">../qml/SectionPage.qml</file>
<file alias="WelcomePage.qml">../qml/WelcomePage.qml</file>
<file alias="PracticePage.qml">../qml/PracticePage.qml</file>
<file alias="AboutPage.qml">../qml/AboutPage.qml</file>
<file alias="CategoryPage.qml">../qml/CategoryPage.qml</file>
<file alias="Session.qml">../qml/Session.qml</file>
<file alias="LoginPage.qml">../qml/LoginPage.qml</file>
<file alias="SettingsPage.qml">../qml/SettingsPage.qml</file>
<file alias="handler.js">../logic/handler.js</file>

<file alias="bfs-recursive.js">../logic/graph/bfs-recursive.js</file>
<file alias="dfs-recursive.js">../logic/graph/dfs-recursive.js</file>
<file alias="bfs-iterative.js">../logic/graph/bfs-iterative.js</file>
<file alias="dfs-iterative.js">../logic/graph/dfs-iterative.js</file>
<file alias="dijkstra.js">../logic/graph/dijkstra.js</file>

<file alias="backtracking.js">../logic/backtracking/nqueens.js</file>

<file alias="dsu.js">../logic/disjoint-set/dsu.js</file>

<file alias="kadane.js">../logic/dp/kadane.js</file>

<file alias="topological-sorting.js">../logic/topological-sorting/topological-sorting.js</file>

<file alias="merge-interval.js">../logic/intervals/merge-interval.js</file>
<file alias="insert-interval.js">../logic/intervals/insert-interval.js</file>
<file alias="min-meeting-room.js">../logic/intervals/min-meeting-room.js</file>

<file alias="binary-search.js">../logic/binary-search/binary-search.js</file>

<file alias="linear-search.js">../logic/searching/linear-search.js</file>
<file alias="jump-search.js">../logic/searching/jump-search.js</file>
<file alias="interpolation-search.js">../logic/searching/interpolation-search.js</file>

<file alias="trie.js">../logic/trie/trie.js</file>

<file alias="kmp.js">../logic/string/kmp.js</file>
<file alias="lcp.js">../logic/string/lcp.js</file>
<file alias="rabin-karp.js">../logic/string/rabin-karp.js</file>

<file alias="factorial.js">../logic/recursion/factorial.js</file>

<file alias="fractional-knapsack.js">../logic/greedy/fractional-knapsack.js</file>

<file alias="check-power-of-two.js">../logic/bit-manipulation/check-power-of-two.js</file>
<file alias="count-set-bit.js">../logic/bit-manipulation/count-set-bit.js</file>
<file alias="reverse-bits.js">../logic/bit-manipulation/reverse-bits.js</file>

<file alias="max-sub-array.js">../logic/sliding-window/max-sub-array.js</file>

<file alias="heap-sort.js">../logic/heap/heap-sort.js</file>
<file alias="insert-heap.js">../logic/heap/insert-heap.js</file>

<file alias="insert-at-end.js">../logic/linked-list/insert-at-end.js</file>
<file alias="remove-element.js">../logic/linked-list/remove-element.js</file>
<file alias="ancestor.js">../logic/linked-list/ancestor.js</file>

<file alias="sieve.js">../logic/mathematics/sieve.js</file>
<file alias="gcd.js">../logic/mathematics/gcd.js</file>
<file alias="fast-exponentiation.js">../logic/mathematics/fast-exponentiation.js</file>

<file alias="convex-hull.js">../logic/geometry/convex-hull.js</file>
<file alias="line-intersection.js">../logic/geometry/line-intersection.js</file>
<file alias="area-triangle-heron.js">../logic/geometry/area-triangle-heron.js</file>

<file alias="lazy-propagation.js">../logic/segment-tree/lazy-propagation.js</file>
<file alias="range-sum-queries.js">../logic/segment-tree/range-sum-queries.js</file>

<file alias="sessionData.json">sessionData.json</file>

<file alias="Hedgehog-Telegram.jpeg">../images/Hedgehog-Telegram.jpeg</file>
<file alias="buy-me-a-coffee.png">../images/buy-me-a-coffee.png</file>
<file alias="appearance-dark.svg">../images/appearance-dark.svg</file>
<file alias="font-dark.svg">../images/font-dark.svg</file>
<file alias="appearance-light.svg">../images/appearance-light.svg</file>
<file alias="font-light.svg">../images/font-light.svg</file>
</qresource>
</RCC>
18 changes: 18 additions & 0 deletions tests/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/sh

function replaceDestinationBinary() {
file='Makefile'
string_to_replace='tests.app\/Contents\/MacOS\/'
replacement_string=''

# Use sed to replace the string
sed -i '' "s/$string_to_replace/$replacement_string/g" "$file"

echo "Replaced '$string_to_replace' with '$replacement_string' in '$file'."
}

make clean
qmake
replaceDestinationBinary
make
./tests
Empty file added tests/sessionData.json
Empty file.
80 changes: 80 additions & 0 deletions tests/src/documenthandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include "includes/documenthandler.h"

#include <QFile>
#include <QFileDialog>
#include <QOperatingSystemVersion>
#include <QQmlEngine>
#include <QSettings>
#include <QTextStream>
#include <QUrl>

#include "includes/highlighter.h"

DocumentHandler::DocumentHandler(QObject* parent)
: QObject(parent) {
}

bool DocumentHandler::isDarkMode() {
if (QOperatingSystemVersion::currentType() != QOperatingSystemVersion::MacOS) {
return false;
}

QSettings settings("Apple", "General");
QString theme = settings.value("AppleInterfaceStyle", "").toString();
return theme == "Dark";
}

void DocumentHandler::openFile(const QString& path) {
QUrl url(path);
QFile file(url.toLocalFile());
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream stream(&file);
auto text = stream.readAll();
setText(text);
file.close();
}

void DocumentHandler::setDocument(QQuickTextDocument* document, const QString& theme) {
auto highlighter = new Highlighter(document->textDocument(), theme);
Q_UNUSED(highlighter)
}

QString DocumentHandler::text() const {
return m_text;
}

void DocumentHandler::setText(QString text) {
if (m_text != text) {
m_text = text;
emit textChanged(text);
}
}

bool DocumentHandler::saveToFile(const QString& filename, const QString& content) {
QFile file(filename);

if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
qWarning() << "Could not open file for writing:" << filename;
return false;
}

QTextStream out(&file);
out << content;
file.close();
return true;
}

QString DocumentHandler::loadFromFile(const QString& filename) {
QFile file(filename);

if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qWarning() << "Could not open file for reading:" << filename;
return QString();
}

QTextStream in(&file);
QString content = in.readAll();
file.close();
return content;
}
Loading