From db33eece150165c6c63341a4fcb1e37754e17787 Mon Sep 17 00:00:00 2001 From: Sandro Bonazzola Date: Tue, 22 Aug 2023 17:49:40 +0000 Subject: [PATCH] tests: add scaffolding for unit tests with coverage --- README.md | 15 ++++++++++++ meson.build | 7 ++++++ meson_options.txt | 6 +++++ tests/meson.build | 5 ++++ tests/unit-tests.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 90 insertions(+) create mode 100644 tests/meson.build create mode 100644 tests/unit-tests.c diff --git a/README.md b/README.md index e17830e..00ac66c 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,21 @@ D-Bus device support in QEMU and GTK 4. Nightly documentation can be found [here](https://gnome.pages.gitlab.gnome.org/libmks/libmks1). +# Unit testing + +Be sure you have `lcov` package installed on your system if you want coverage data. + +```bash +meson setup builddir +meson configure -Db_coverage=true builddir # if you want to collect coverage data +meson compile -C builddir +meson test -C builddir --suit "libmks" +rm -rf builddir/subprojects # if you don't want subprojects coverage in the report +ninja coverage-html -C builddir # if you want to generate coverage report +``` + +If generated, coverage report will be in `builddir/meson-logs/coveragereport/index.html` + # Testing By default, QEMU will connect to your user session D-Bus if you do not diff --git a/meson.build b/meson.build index 615e1f2..cf7d113 100644 --- a/meson.build +++ b/meson.build @@ -158,8 +158,15 @@ add_project_link_arguments(project_link_args, language: 'c') subdir('lib') subdir('tools') +have_tests = get_option('tests') +if have_tests +subdir('tests') +endif + if get_option('docs') and get_option('introspection').allowed() subdir('docs') endif configure_file(output: 'config.h', configuration: config_h) + +summary('Enabled', have_tests, section: 'Tests') diff --git a/meson_options.txt b/meson_options.txt index 0eacbbe..6f4bb9c 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -14,3 +14,9 @@ option('tracing', type: 'boolean', value: false, description: 'Enable developer tracing information in logs') + +option('tests', + type: 'boolean', + value: true, + description: 'Enable tests globally.' +) \ No newline at end of file diff --git a/tests/meson.build b/tests/meson.build new file mode 100644 index 0000000..fa2ff5e --- /dev/null +++ b/tests/meson.build @@ -0,0 +1,5 @@ +unit_tests = executable('unit-tests', 'unit-tests.c', + dependencies: libmks_dep, +) + +test('Unit tests', unit_tests) diff --git a/tests/unit-tests.c b/tests/unit-tests.c new file mode 100644 index 0000000..4f1389b --- /dev/null +++ b/tests/unit-tests.c @@ -0,0 +1,57 @@ +/* + * unit-tests.c + * + * Copyright 2023 Sandro Bonazzola + * + * This library is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of the + * License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: LGPL-2.1-or-later + */ + + +#include +#include + + +static void test_mks_get_major_version (void) { + int version = mks_get_major_version(); + g_assert_cmpint (version, ==, MKS_MAJOR_VERSION); +} + +static void test_mks_get_minor_version (void) { + int version = mks_get_minor_version(); + g_assert_cmpint (version, ==, MKS_MINOR_VERSION); +} + +static void test_mks_get_micro_version (void) { + int version = mks_get_micro_version(); + g_assert_cmpint (version, ==, MKS_MICRO_VERSION); +} + +static void +init_tests (void) +{ + g_test_add_func ("/lib/init", mks_init); + g_test_add_func ("/lib/get_major_version", test_mks_get_major_version); + g_test_add_func ("/lib/get_minor_version", test_mks_get_minor_version); + g_test_add_func ("/lib/get_micro_version", test_mks_get_micro_version); +} + +int main (int argc, char *argv[]) +{ + g_test_init (&argc, &argv, NULL); + init_tests (); + + return g_test_run (); +}