diff --git a/tests/unit/libstore/nix_api_store.cc b/tests/unit/libstore/nix_api_store.cc index a31d66a4c..7c6ec0780 100644 --- a/tests/unit/libstore/nix_api_store.cc +++ b/tests/unit/libstore/nix_api_store.cc @@ -4,14 +4,10 @@ #include "nix_api_store_internal.h" #include "tests/nix_api_store.hh" +#include "tests/string_callback.hh" namespace nixC { -void observe_string_cb(const char * start, unsigned int n, std::string * user_data) -{ - *user_data = std::string(start); -} - std::string PATH_SUFFIX = "/g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-name"; TEST_F(nix_api_util_context, nix_libstore_init) @@ -23,7 +19,7 @@ TEST_F(nix_api_util_context, nix_libstore_init) TEST_F(nix_api_store_test, nix_store_get_uri) { std::string str; - auto ret = nix_store_get_uri(ctx, store, (void *) observe_string_cb, &str); + auto ret = nix_store_get_uri(ctx, store, OBSERVE_STRING(str)); ASSERT_EQ(NIX_OK, ret); ASSERT_STREQ("local", str.c_str()); } @@ -56,7 +52,7 @@ TEST_F(nix_api_store_test, DoesNotCrashWhenContextIsNull) TEST_F(nix_api_store_test, get_version) { std::string str; - auto ret = nix_store_get_version(ctx, store, (void *) observe_string_cb, &str); + auto ret = nix_store_get_version(ctx, store, OBSERVE_STRING(str)); ASSERT_EQ(NIX_OK, ret); ASSERT_STREQ(PACKAGE_VERSION, str.c_str()); } @@ -69,7 +65,7 @@ TEST_F(nix_api_util_context, nix_store_open_dummy) ASSERT_STREQ("dummy", store->ptr->getUri().c_str()); std::string str; - nix_store_get_version(ctx, store, (void *) observe_string_cb, &str); + nix_store_get_version(ctx, store, OBSERVE_STRING(str)); ASSERT_STREQ("", str.c_str()); nix_store_free(store); diff --git a/tests/unit/libutil-support/tests/string_callback.cc b/tests/unit/libutil-support/tests/string_callback.cc new file mode 100644 index 000000000..28ac8b10c --- /dev/null +++ b/tests/unit/libutil-support/tests/string_callback.cc @@ -0,0 +1,9 @@ +#include "string_callback.hh" + +namespace nix::testing { + +void observe_string_cb(const char * start, unsigned int n, std::string * user_data) { + *user_data = std::string(start); +} + +} diff --git a/tests/unit/libutil-support/tests/string_callback.hh b/tests/unit/libutil-support/tests/string_callback.hh new file mode 100644 index 000000000..808fb707b --- /dev/null +++ b/tests/unit/libutil-support/tests/string_callback.hh @@ -0,0 +1,12 @@ +#pragma once +#include + +namespace nix::testing { + +void observe_string_cb(const char * start, unsigned int n, std::string * user_data); +inline void * observe_string_cb_data(std::string & out) { + return (void *) &out; +}; +#define OBSERVE_STRING(str) (void *)nix::testing::observe_string_cb, nix::testing::observe_string_cb_data(str) + +} diff --git a/tests/unit/libutil/nix_api_util.cc b/tests/unit/libutil/nix_api_util.cc index 09f3f3e05..d2999f55b 100644 --- a/tests/unit/libutil/nix_api_util.cc +++ b/tests/unit/libutil/nix_api_util.cc @@ -3,16 +3,12 @@ #include "nix_api_util.h" #include "nix_api_util_internal.h" #include "tests/nix_api_util.hh" +#include "tests/string_callback.hh" #include namespace nixC { -void observe_string_cb(const char * start, unsigned int n, std::string * user_data) -{ - *user_data = std::string(start); -} - TEST_F(nix_api_util_context, nix_context_error) { std::string err_msg_ref; @@ -62,10 +58,10 @@ TEST_F(nix_api_util_context, nix_setting_get) { ASSERT_EQ(ctx->last_err_code, NIX_OK); std::string setting_value; - nix_err result = nix_setting_get(ctx, "invalid-key", (void *) observe_string_cb, &setting_value); + nix_err result = nix_setting_get(ctx, "invalid-key", OBSERVE_STRING(setting_value)); ASSERT_EQ(result, NIX_ERR_KEY); - result = nix_setting_get(ctx, "setting-name", (void *) observe_string_cb, &setting_value); + result = nix_setting_get(ctx, "setting-name", OBSERVE_STRING(setting_value)); ASSERT_EQ(result, NIX_OK); ASSERT_STREQ("empty", setting_value.c_str()); } @@ -79,7 +75,7 @@ TEST_F(nix_api_util_context, nix_setting_set) ASSERT_EQ(result, NIX_OK); std::string setting_value; - result = nix_setting_get(ctx, "setting-name", (void *) observe_string_cb, &setting_value); + result = nix_setting_get(ctx, "setting-name", OBSERVE_STRING(setting_value)); ASSERT_EQ(result, NIX_OK); ASSERT_STREQ("new-value", setting_value.c_str()); } @@ -107,14 +103,14 @@ TEST_F(nix_api_util_context, nix_err_info_msg) std::string err_info; // no error - EXPECT_THROW(nix_err_info_msg(NULL, ctx, (void *) observe_string_cb, &err_info), nix::Error); + EXPECT_THROW(nix_err_info_msg(NULL, ctx, OBSERVE_STRING(err_info)), nix::Error); try { throw nix::Error("testing error"); } catch (...) { nix_context_error(ctx); } - nix_err_info_msg(nix_c_context_create(), ctx, (void *) observe_string_cb, &err_info); + nix_err_info_msg(nix_c_context_create(), ctx, OBSERVE_STRING(err_info)); ASSERT_STREQ("testing error", err_info.c_str()); } @@ -123,7 +119,7 @@ TEST_F(nix_api_util_context, nix_err_name) std::string err_name; // no error - EXPECT_THROW(nix_err_name(NULL, ctx, (void *) observe_string_cb, &err_name), nix::Error); + EXPECT_THROW(nix_err_name(NULL, ctx, OBSERVE_STRING(err_name)), nix::Error); std::string err_msg_ref; try { @@ -131,7 +127,7 @@ TEST_F(nix_api_util_context, nix_err_name) } catch (...) { nix_context_error(ctx); } - nix_err_name(nix_c_context_create(), ctx, (void *) observe_string_cb, &err_name); + nix_err_name(nix_c_context_create(), ctx, OBSERVE_STRING(err_name)); ASSERT_EQ(std::string(err_name), "nix::Error"); }