mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-10 00:08:07 +02:00
test/libutil: Add OBSERVE_STRING macro
Makes string callback easier to pass, without mistakes.
This commit is contained in:
parent
876e70bc9a
commit
a512f4eebc
4 changed files with 33 additions and 20 deletions
|
@ -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);
|
||||
|
|
9
tests/unit/libutil-support/tests/string_callback.cc
Normal file
9
tests/unit/libutil-support/tests/string_callback.cc
Normal file
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
12
tests/unit/libutil-support/tests/string_callback.hh
Normal file
12
tests/unit/libutil-support/tests/string_callback.hh
Normal file
|
@ -0,0 +1,12 @@
|
|||
#pragma once
|
||||
#include <string>
|
||||
|
||||
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)
|
||||
|
||||
}
|
|
@ -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 <gtest/gtest.h>
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue