From 1093ab64a24aec3032a0642b27522520c970d898 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Sun, 25 Feb 2024 21:21:05 +0100 Subject: [PATCH] C API: add more tests --- src/libexpr/c/nix_api_value.cc | 4 +- src/libexpr/c/nix_api_value.h | 9 +++-- src/libexpr/eval.cc | 5 --- tests/unit/libexpr/nix_api_external.cc | 1 - tests/unit/libexpr/nix_api_value.cc | 56 ++++++++++++++++++++++++++ tests/unit/libutil/nix_api_util.cc | 37 ++++++++++------- 6 files changed, 86 insertions(+), 26 deletions(-) diff --git a/src/libexpr/c/nix_api_value.cc b/src/libexpr/c/nix_api_value.cc index e63d13f7a..740751beb 100644 --- a/src/libexpr/c/nix_api_value.cc +++ b/src/libexpr/c/nix_api_value.cc @@ -382,13 +382,13 @@ nix_err nix_init_string(nix_c_context * context, Value * value, const char * str NIXC_CATCH_ERRS } -nix_err nix_init_path_string(nix_c_context * context, Value * value, const char * str) +nix_err nix_init_path_string(nix_c_context * context, EvalState * s, Value * value, const char * str) { if (context) context->last_err_code = NIX_OK; try { auto & v = check_value_not_null(value); - v.mkPath(std::string_view(str)); + v.mkPath(s->state.rootPath(nix::CanonPath(str))); } NIXC_CATCH_ERRS } diff --git a/src/libexpr/c/nix_api_value.h b/src/libexpr/c/nix_api_value.h index 64c0c367a..e3e937e37 100644 --- a/src/libexpr/c/nix_api_value.h +++ b/src/libexpr/c/nix_api_value.h @@ -129,6 +129,7 @@ nix_err nix_register_primop(nix_c_context * context, PrimOp * primOp); * @return value, or null in case of errors * */ + Value * nix_alloc_value(nix_c_context * context, EvalState * state); /** @addtogroup value_manip Manipulating values * @brief Functions to inspect and change Nix language values, represented by Value. @@ -142,6 +143,7 @@ Value * nix_alloc_value(nix_c_context * context, EvalState * state); * @param[in] value Nix value to inspect * @return type of nix value */ + ValueType nix_get_type(nix_c_context * context, const Value * value); /** @brief Get type name of value as defined in the evaluator * @param[out] context Optional, stores error information @@ -149,6 +151,7 @@ ValueType nix_get_type(nix_c_context * context, const Value * value); * @return type name, owned string * @todo way to free the result */ + const char * nix_get_typename(nix_c_context * context, const Value * value); /** @brief Get boolean value @@ -290,24 +293,24 @@ nix_err nix_init_bool(nix_c_context * context, Value * value, bool b); * @param[in] str the string, copied * @return error code, NIX_OK on success. */ - nix_err nix_init_string(nix_c_context * context, Value * value, const char * str); + /** @brief Set a path * @param[out] context Optional, stores error information * @param[out] value Nix value to modify * @param[in] str the path string, copied * @return error code, NIX_OK on success. */ +nix_err nix_init_path_string(nix_c_context * context, EvalState * s, Value * value, const char * str); -nix_err nix_init_path_string(nix_c_context * context, Value * value, const char * str); /** @brief Set a float * @param[out] context Optional, stores error information * @param[out] value Nix value to modify * @param[in] d the float, 64-bits * @return error code, NIX_OK on success. */ - nix_err nix_init_float(nix_c_context * context, Value * value, double d); + /** @brief Set an int * @param[out] context Optional, stores error information * @param[out] value Nix value to modify diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index a93e531b6..794451d82 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -897,11 +897,6 @@ void Value::mkStringMove(const char * s, const NixStringContext & context) copyContextToValue(*this, context); } -void Value::mkPath(std::string_view path) -{ - mkPath(makeImmutableString(path)); -} - void Value::mkPath(const SourcePath & path) { mkPath(&*path.accessor, makeImmutableString(path.path.abs())); diff --git a/tests/unit/libexpr/nix_api_external.cc b/tests/unit/libexpr/nix_api_external.cc index 1f190d9c8..68766fd47 100644 --- a/tests/unit/libexpr/nix_api_external.cc +++ b/tests/unit/libexpr/nix_api_external.cc @@ -31,7 +31,6 @@ private: static void type_of_function(void * self, nix_string_return * res) { - std::cout << self << std::endl; MyExternalValueDesc * obj = static_cast(self); std::string type_string = "nix-externallast_err, err_msg_ref); } -TEST_F(nix_api_util_context, nix_set_err_msg) { +TEST_F(nix_api_util_context, nix_set_err_msg) +{ ASSERT_EQ(ctx->last_err_code, NIX_OK); nix_set_err_msg(ctx, NIX_ERR_UNKNOWN, "unknown test error"); ASSERT_EQ(ctx->last_err_code, NIX_ERR_UNKNOWN); ASSERT_EQ(*ctx->last_err, "unknown test error"); } -TEST(nix_api_util, nix_version_get) { +TEST(nix_api_util, nix_version_get) +{ ASSERT_EQ(std::string(nix_version_get()), PACKAGE_VERSION); } @@ -77,9 +80,10 @@ TEST_F(nix_api_util_context, nix_setting_set) ASSERT_STREQ("new-value", value); } -TEST_F(nix_api_util_context, nix_err_msg) { +TEST_F(nix_api_util_context, nix_err_msg) +{ // no error - EXPECT_THROW(nix_err_msg(NULL, ctx, NULL), nix::Error); + EXPECT_THROW(nix_err_msg(nullptr, ctx, NULL), nix::Error); // set error nix_set_err_msg(ctx, NIX_ERR_UNKNOWN, "unknown test error"); @@ -90,46 +94,49 @@ TEST_F(nix_api_util_context, nix_err_msg) { // advanced usage unsigned int sz; - err_msg = nix_err_msg(NULL, ctx, &sz); + err_msg = nix_err_msg(nix_c_context_create(), ctx, &sz); ASSERT_EQ(sz, err_msg.size()); } -TEST_F(nix_api_util_context, nix_err_info_msg) { +TEST_F(nix_api_util_context, nix_err_info_msg) +{ // no error EXPECT_THROW(nix_err_info_msg(NULL, ctx, NULL, 256), nix::Error); try { throw nix::Error("testing error"); - } catch(...) { + } catch (...) { nix_context_error(ctx); } char buf[256]; - nix_err_info_msg(NULL, ctx, buf, 256); + nix_err_info_msg(nix_c_context_create(), ctx, buf, 256); ASSERT_EQ(std::string(buf), "testing error"); // should overflow EXPECT_THROW(nix_err_info_msg(NULL, ctx, buf, 1), nix::Error); } -TEST_F(nix_api_util_context, nix_err_name) { +TEST_F(nix_api_util_context, nix_err_name) +{ // no error EXPECT_THROW(nix_err_name(NULL, ctx, NULL, 256), nix::Error); std::string err_msg_ref; try { throw nix::Error("testing error"); - } catch(...) { + } catch (...) { nix_context_error(ctx); } char err_name[32]; - nix_err_name(NULL, ctx, err_name, 32); + nix_err_name(nix_c_context_create(), ctx, err_name, 32); ASSERT_EQ(std::string(err_name), "nix::Error"); // overflow EXPECT_THROW(nix_err_name(NULL, ctx, err_name, 1), nix::Error); } -TEST_F(nix_api_util_context, nix_err_code) { +TEST_F(nix_api_util_context, nix_err_code) +{ ASSERT_EQ(nix_err_code(ctx), NIX_OK); nix_set_err_msg(ctx, NIX_ERR_UNKNOWN, "unknown test error"); ASSERT_EQ(nix_err_code(ctx), NIX_ERR_UNKNOWN);