C API: add more tests

This commit is contained in:
José Luis Lafuente 2024-02-25 21:21:05 +01:00 committed by José Luis Lafuente
parent 2e1dbbe307
commit 1093ab64a2
No known key found for this signature in database
GPG key ID: 8A3455EBE455489A
6 changed files with 86 additions and 26 deletions

View file

@ -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
}

View file

@ -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

View file

@ -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()));

View file

@ -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<MyExternalValueDesc *>(self);
std::string type_string = "nix-external<MyExternalValueDesc( ";

View file

@ -18,6 +18,56 @@ TEST_F(nix_api_expr_test, nix_value_set_get_int)
nix_init_int(nullptr, value, myInt);
ASSERT_EQ(myInt, nix_get_int(nullptr, value));
ASSERT_STREQ("an integer", nix_get_typename(nullptr, value));
ASSERT_EQ(NIX_TYPE_INT, nix_get_type(nullptr, value));
}
TEST_F(nix_api_expr_test, nix_value_set_get_float)
{
float myDouble = 1.0;
nix_init_float(nullptr, value, myDouble);
ASSERT_EQ(myDouble, nix_get_float(nullptr, value));
ASSERT_STREQ("a float", nix_get_typename(nullptr, value));
ASSERT_EQ(NIX_TYPE_FLOAT, nix_get_type(nullptr, value));
}
TEST_F(nix_api_expr_test, nix_value_set_get_bool)
{
bool myBool = true;
nix_init_bool(nullptr, value, myBool);
ASSERT_EQ(myBool, nix_get_bool(nullptr, value));
ASSERT_STREQ("a Boolean", nix_get_typename(nullptr, value));
ASSERT_EQ(NIX_TYPE_BOOL, nix_get_type(nullptr, value));
}
TEST_F(nix_api_expr_test, nix_value_set_get_string)
{
const char * myString = "some string";
nix_init_string(nullptr, value, myString);
ASSERT_STREQ(myString, nix_get_string(nullptr, value));
ASSERT_STREQ("a string", nix_get_typename(nullptr, value));
ASSERT_EQ(NIX_TYPE_STRING, nix_get_type(nullptr, value));
}
TEST_F(nix_api_expr_test, nix_value_set_get_null)
{
nix_init_null(nullptr, value);
ASSERT_STREQ("null", nix_get_typename(nullptr, value));
ASSERT_EQ(NIX_TYPE_NULL, nix_get_type(nullptr, value));
}
TEST_F(nix_api_expr_test, nix_value_set_get_path)
{
const char * p = "/nix/store/40s0qmrfb45vlh6610rk29ym318dswdr-myname";
nix_init_path_string(nullptr, state, value, p);
ASSERT_STREQ(p, nix_get_path_string(nullptr, value));
ASSERT_STREQ("a path", nix_get_typename(nullptr, value));
ASSERT_EQ(NIX_TYPE_PATH, nix_get_type(nullptr, value));
}
TEST_F(nix_api_expr_test, nix_build_and_init_list)
@ -34,6 +84,9 @@ TEST_F(nix_api_expr_test, nix_build_and_init_list)
ASSERT_EQ(42, nix_get_int(nullptr, nix_get_list_byidx(nullptr, value, state, 0)));
ASSERT_EQ(1, nix_get_list_size(nullptr, value));
ASSERT_STREQ("a list", nix_get_typename(nullptr, value));
ASSERT_EQ(NIX_TYPE_LIST, nix_get_type(nullptr, value));
// Clean up
nix_gc_decref(nullptr, intValue);
}
@ -80,6 +133,9 @@ TEST_F(nix_api_expr_test, nix_build_and_init_attr)
ASSERT_STREQ("b", nix_get_attr_name_byidx(nullptr, value, state, 1));
ASSERT_STREQ("a set", nix_get_typename(nullptr, value));
ASSERT_EQ(NIX_TYPE_ATTRS, nix_get_type(nullptr, value));
// Clean up
nix_gc_decref(nullptr, intValue);
nix_gc_decref(nullptr, stringValue);

View file

@ -9,11 +9,12 @@
namespace nixC {
TEST_F(nix_api_util_context, nix_context_error) {
TEST_F(nix_api_util_context, nix_context_error)
{
std::string err_msg_ref;
try {
throw nix::Error("testing error");
} catch(nix::Error &e) {
} catch (nix::Error & e) {
err_msg_ref = e.what();
nix_context_error(ctx);
}
@ -24,7 +25,7 @@ TEST_F(nix_api_util_context, nix_context_error) {
try {
throw std::runtime_error("testing exception");
} catch(std::exception &e) {
} catch (std::exception & e) {
err_msg_ref = e.what();
nix_context_error(ctx);
}
@ -32,14 +33,16 @@ TEST_F(nix_api_util_context, nix_context_error) {
ASSERT_EQ(*ctx->last_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);