Skip to content

Commit

Permalink
First working Lua unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandlo committed Sep 3, 2023
1 parent 1a30b61 commit 6ce7069
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/core/gui/GladeGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ GladeGui::GladeGui(GladeSearchpath* gladeSearchPath, const std::string& glade, c

GladeGui::~GladeGui() {
if (!gtk_widget_get_parent(window)) {
gtk_widget_destroy(window);
// gtk_widget_destroy(window);
}
}

Expand Down
79 changes: 79 additions & 0 deletions test/unit_tests/lua/LuaTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Xournal++
*
* This file is part of the Xournal UnitTests
*
* @author Xournal++ Team
* https://github.com/xournalpp/xournalpp
*
* @license GNU GPLv2 or later
*/

#include <cstdint>

#include <config-test.h>
#include <gio/gio.h> // for GApplication, G_APPLICATION
#include <glib-object.h> // for G_CALLBACK, g_signal_con...
#include <glib.h>
#include <gtest/gtest.h>

#include "control/Control.h"
#include "control/jobs/XournalScheduler.h" // for XournalScheduler
#include "gui/GladeSearchpath.h" // for GladeSearchpath

extern "C" {
#include <lauxlib.h> // for luaL_Reg, luaL_newstate, luaL_requiref
#include <lua.h> // for lua_getglobal, lua_getfield, lua_setf...
#include <lualib.h> // for luaL_openlibs
}

#include "plugin/luapi_application.h" // for luaopen_app


struct Data {
Data() = default;
Data(Data&&) = delete;
Data(Data const&) = delete;
auto operator=(Data&&) -> Data = delete;
auto operator=(Data const&) -> Data = delete;

~Data() {}

std::unique_ptr<GladeSearchpath> gladePath;
std::unique_ptr<Control> control;
std::unique_ptr<MainWindow> win;
};

void on_activate(GtkApplication* app, Data* data) {
auto uiPath = fs::path(PROJECT_SOURCE_DIR) / "ui";
data->gladePath = std::make_unique<GladeSearchpath>();
data->gladePath->addSearchDirectory(uiPath);

data->control = std::make_unique<Control>(G_APPLICATION(app), data->gladePath.get(), true);
data->win = std::make_unique<MainWindow>(data->gladePath.get(), data->control.get(), app);
data->control->initWindow(data->win.get());
data->control->getScheduler()->start();
Util::execInUiThread([=]() { data->control->getWindow()->getXournal()->layoutPages(); });
gtk_application_add_window(app, GTK_WINDOW(data->win->getWindow()));

data->win->show(nullptr);
data->control->addDefaultPage("");

auto pluginPath = fs::path(PROJECT_SOURCE_DIR) / "test" / "unit_tests" / "lua";
auto plugin = std::make_unique<Plugin>(data->control.get(), pluginPath.filename().string(), pluginPath);
ASSERT_TRUE(plugin->isValid());
plugin->loadScript();

g_application_quit(G_APPLICATION(app));
}

void on_shutdown(GApplication*, Data* data) { data->control->getScheduler()->stop(); }

TEST(LuaTest, testPage) {
Data data;
GtkApplication* app = gtk_application_new("com.github.xournalpp.xournalpp", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(&on_activate), &data);
g_signal_connect(app, "shutdown", G_CALLBACK(&on_shutdown), &data);
g_application_run(G_APPLICATION(app), 0, NULL);
g_object_unref(app);
}
15 changes: 15 additions & 0 deletions test/unit_tests/lua/plugin.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[about]
## Author / Copyright notice
author=Xournal++ Team

description=This is used for unit tests

## If the plugin is packed with Xournal++, use
## <xournalpp> then it gets the same version number
version=<xournalpp>

[default]
enabled=true

[plugin]
mainfile=test.lua
66 changes: 66 additions & 0 deletions test/unit_tests/lua/test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
local lunatest = require "lunatest"

local assert_true, assert_false = lunatest.assert_true, lunatest.assert_false
local assert_diffvars = lunatest.assert_diffvars
local assert_boolean, assert_not_boolean = lunatest.assert_boolean, lunatest.assert_not_boolean
local assert_len, assert_not_len = lunatest.assert_len, lunatest.assert_not_len
local assert_match, assert_not_match = lunatest.assert_match, lunatest.assert_not_match
local assert_error = lunatest.assert_error
local assert_lt, assert_lte = lunatest.assert_lt, lunatest.assert_lte
local assert_gt, assert_gte = lunatest.assert_gt, lunatest.assert_gte
local assert_nil, assert_not_nil = lunatest.assert_nil, lunatest.assert_not_nil
local assert_equal, assert_not_equal = lunatest.assert_equal, lunatest.assert_not_equal
local assert_string, assert_not_string = lunatest.assert_string, lunatest.assert_not_string
local assert_metatable, assert_not_metatable = lunatest.assert_metatable, lunatest.assert_not_metatable
local assert_userdata, assert_not_userdata = lunatest.assert_userdata, lunatest.assert_not_userdata
local assert_thread, assert_not_thread = lunatest.assert_thread, lunatest.assert_not_thread
local assert_function, assert_not_function = lunatest.assert_function, lunatest.assert_not_function
local assert_table, assert_not_table = lunatest.assert_table, lunatest.assert_not_table
local assert_number, assert_not_number = lunatest.assert_number, lunatest.assert_not_number
local skip, fail = lunatest.skip, lunatest.fail


local sep = package.config:sub(1,1) -- "/" on Linux and MacOS, "\\" (escaped backslash) on Windows
local sourceDir = debug.getinfo(1).source:sub(2):match("(.*[/\\])"):gsub("/", sep) -- directory containing the Lua script
local testDoc = sourceDir .. "testDoc.xopp" -- path of the test document

function test_docStructure()
local success = app.openFile(testDoc)
assert_true(success)

app.setCurrentPage(3)
app.setCurrentLayer(2)
local doc = app.getDocumentStructure()

assert_equal(doc["xoppFilename"], testDoc)
assert_equal(doc["pdfBackgroundFilename"], "")
assert_equal(doc["currentPage"], 3)

local pages = doc["pages"]
assert_equal(#pages, 3)

assert_equal(pages[3]["currentLayer"], 2)

assert_true(pages[1]["isAnnotated"])
assert_false(pages[2]["isAnnotated"])
assert_true(pages[3]["isAnnotated"])

assert_equal(#pages[1]["layers"], 1)
assert_equal(#pages[3]["layers"], 3)
assert_true(pages[3]["layers"][1]["isAnnotated"])
assert_true(pages[3]["layers"][2]["isAnnotated"])
assert_false(pages[3]["layers"][3]["isAnnotated"])

assert_equal(pages[1]["pageTypeFormat"], "graph")
assert_equal(pages[2]["pageTypeFormat"], "plain")
assert_equal(pages[3]["pageTypeFormat"], "ruled")
end

function test_sidebarPage()
app.setSidebarPageNo(1)
assert_equal(app.getSidebarPageNo(), 1)
app.setSidebarPageNo(2)
assert_equal(app.getSidebarPageNo(), 2)
end

lunatest.run()
Binary file added test/unit_tests/lua/testDoc.xopp
Binary file not shown.

0 comments on commit 6ce7069

Please sign in to comment.