Skip to content

Commit c53fcc6

Browse files
oSoMoNmisyltoad
authored andcommitted
Tests: add basic unit tests exercising ConVar and scripting
1 parent 8b3d95d commit c53fcc6

5 files changed

Lines changed: 84 additions & 1 deletion

File tree

.github/workflows/main.yml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ jobs:
1919
libxtst libxkbcommon libdrm libinput wayland-protocols benchmark \
2020
xorg-xwayland pipewire cmake \
2121
libavif libheif aom rav1e libdecor libxdamage \
22-
luajit
22+
luajit \
23+
catch2 gcovr
2324
- uses: actions/checkout@v6
2425
with:
2526
submodules: recursive
@@ -38,3 +39,14 @@ jobs:
3839
# export CC=clang CXX=clang++
3940
# meson setup build-clang/ -Dinput_emulation=disabled --werror --auto-features=enabled
4041
# ninja -C build-clang/
42+
- name: Run unit tests and collect coverage
43+
run: |
44+
export CC=gcc CXX=g++
45+
meson configure -Db_coverage=true build-gcc
46+
meson test -C build-gcc --suite gamescope -v
47+
gcovr -f src --xml -o coverage.xml -s build-gcc
48+
- name: Upload coverage report
49+
uses: actions/upload-artifact@v7
50+
with:
51+
name: code coverage
52+
path: coverage.xml

meson.build

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ if get_option('enable_gamescope')
105105
subdir('src')
106106
endif
107107

108+
if get_option('enable_tests')
109+
subdir('tests')
110+
endif
111+
112+
108113
# Handle default script/config/looks stuff
109114
meson.add_install_script('default_extras_install.sh')
110115

meson_options.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ option('input_emulation' , type: 'feature', description: 'Support for
77
option('enable_gamescope', type : 'boolean', value : true, description: 'Build Gamescope executable')
88
option('enable_gamescope_wsi_layer', type : 'boolean', value : true, description: 'Build Gamescope layer')
99
option('enable_openvr_support', type : 'boolean', value : true, description: 'OpenVR Integrations')
10+
option('enable_tests', type : 'boolean', value : true, description: 'Build unit tests')
1011
option('benchmark', type: 'feature', description: 'Benchmark tools')

tests/meson.build

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
catch2_dep = dependency('catch2-with-main', required: true)
2+
3+
srcdir = '../src'
4+
unittest_src = []
5+
foreach src : gamescope_core_src + ['Script/Script.cpp', 'convar_script.cpp']
6+
unittest_src += srcdir / src
7+
endforeach
8+
9+
test_convar = executable(
10+
'test_convar',
11+
unittest_src + ['test_convar.cpp'],
12+
include_directories: [srcdir, sol2_include],
13+
dependencies: [catch2_dep, luajit_dep, cap_dep, drm_dep, glm_dep, wlroots_dep],
14+
cpp_args: gamescope_cpp_args,
15+
)
16+
test('convar', test_convar)

tests/test_convar.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <catch2/catch_test_macros.hpp>
2+
3+
#include "convar.h"
4+
#include "Script/Script.h"
5+
6+
using namespace gamescope;
7+
8+
TEST_CASE("ConVar", "[convar]") {
9+
ConVar<bool> cv_bool_var {"foobar", true, "switch on and off"};
10+
REQUIRE(cv_bool_var.GetName() == "foobar");
11+
REQUIRE(cv_bool_var.GetDescription() == "switch on and off");
12+
REQUIRE(cv_bool_var.Get() == true);
13+
cv_bool_var.SetValue(false);
14+
REQUIRE(cv_bool_var.Get() == false);
15+
16+
{
17+
auto proxy = CScriptScopedLock().Manager().Gamescope().Convars.Base["foobar"];
18+
REQUIRE(proxy.valid() == true);
19+
ConVar<bool>* cv = proxy;
20+
REQUIRE(cv->GetName() == "foobar");
21+
REQUIRE(cv->GetDescription() == "switch on and off");
22+
REQUIRE(cv->Get() == false);
23+
cv->SetValue(true);
24+
REQUIRE(cv_bool_var.Get() == true);
25+
}
26+
27+
{
28+
auto proxy = CScriptScopedLock().Manager().Gamescope().Convars.Base["nobar"];
29+
REQUIRE(proxy.valid() == false);
30+
}
31+
32+
{
33+
auto proxy = CScriptScopedLock()->script("return gamescope.convars.foobar");
34+
REQUIRE(proxy.valid() == true);
35+
ConVar<bool>* cv = proxy;
36+
REQUIRE(cv->GetName() == "foobar");
37+
REQUIRE(cv->GetDescription() == "switch on and off");
38+
REQUIRE(cv->Get() == true);
39+
cv->SetValue(false);
40+
REQUIRE(cv_bool_var.Get() == false);
41+
}
42+
43+
{
44+
auto proxy = CScriptScopedLock()->script("return gamescope.convars.nobar");
45+
REQUIRE(proxy.valid() == true);
46+
ConVar<bool>* cv = proxy;
47+
REQUIRE(cv == nullptr);
48+
}
49+
}

0 commit comments

Comments
 (0)