diff --git a/lib/utils/meson.build b/lib/utils/meson.build index b45a87a5..2181e615 100644 --- a/lib/utils/meson.build +++ b/lib/utils/meson.build @@ -105,5 +105,11 @@ test('test-html-to-text', cpp_args: ['-DBUILD_TESTS'], dependencies: [glib_dep, lib_mu_utils_dep])) +test('test-error', + executable('test-error', 'mu-error.cc', + install: false, + cpp_args: ['-DBUILD_TESTS'], + dependencies: [glib_dep, lib_mu_utils_dep])) + subdir('tests') diff --git a/lib/utils/mu-error.cc b/lib/utils/mu-error.cc new file mode 100644 index 00000000..1d098fc8 --- /dev/null +++ b/lib/utils/mu-error.cc @@ -0,0 +1,64 @@ +/* +** Copyright (C) 2023 Dirk-Jan C. Binnema +** +** This program is free software; you can redistribute it and/or modify it +** under the terms of the GNU General Public License as published by the +** Free Software Foundation; either version 3, or (at your option) any +** later version. +** +** This program 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 General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software Foundation, +** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +** +*/ + + +#if BUILD_TESTS + +#include "mu-error.hh" +#include "mu-test-utils.hh" + +using namespace Mu; + +static void +test_fill_error() +{ + const Error err{Error::Code::Internal, "boo!"}; + GError *gerr{}; + + err.fill_g_error(&gerr); + + assert_equal(gerr->message, "boo!"); + g_assert_cmpint(gerr->code, ==, static_cast(err.code())); + + g_clear_error(&gerr); +} + +static void +test_add_hint() +{ + Error err(Error::Code::Internal, "baa!"); + err.add_hint("hello"); + + assert_equal(err.hint(), "hello"); +} + + +int +main(int argc, char* argv[]) +{ + mu_test_init(&argc, &argv); + + g_test_add_func("/error/fill-error", test_fill_error); + g_test_add_func("/error/add-hint", test_add_hint); + + return g_test_run(); + +} + +#endif /*BUILD_TESTS*/ diff --git a/lib/utils/mu-error.hh b/lib/utils/mu-error.hh index 8bf332e1..36b4178b 100644 --- a/lib/utils/mu-error.hh +++ b/lib/utils/mu-error.hh @@ -44,8 +44,8 @@ constexpr uint32_t err_enum(uint8_t code, uint8_t rv, uint8_t cat) { struct Error final : public std::exception { - // 16 lower bits are for the error code the next 8 bits is for the return code - // upper byte is for flags + // 16 lower bits are for the error code;the next 8 bits are for the return code; the upper + // byte is for flags static constexpr uint8_t SoftError = 1; enum struct Code: uint32_t {