C API: add more tests

This commit is contained in:
José Luis Lafuente 2024-03-29 14:00:19 +01:00
parent 2d84433a3b
commit 926fbadcc3
No known key found for this signature in database
GPG key ID: 8A3455EBE455489A
5 changed files with 112 additions and 72 deletions

View file

@ -227,7 +227,7 @@ double nix_get_float(nix_c_context * context, const Value * value)
assert(v.type() == nix::nFloat); assert(v.type() == nix::nFloat);
return v.fpoint; return v.fpoint;
} }
NIXC_CATCH_ERRS_RES(NAN); NIXC_CATCH_ERRS_RES(0.0);
} }
int64_t nix_get_int(nix_c_context * context, const Value * value) int64_t nix_get_int(nix_c_context * context, const Value * value)

View file

@ -136,8 +136,8 @@ nix_err nix_register_primop(nix_c_context * context, PrimOp * primOp);
* @return value, or null in case of errors * @return value, or null in case of errors
* *
*/ */
Value * nix_alloc_value(nix_c_context * context, EvalState * state); Value * nix_alloc_value(nix_c_context * context, EvalState * state);
/** @addtogroup value_manip Manipulating values /** @addtogroup value_manip Manipulating values
* @brief Functions to inspect and change Nix language values, represented by Value. * @brief Functions to inspect and change Nix language values, represented by Value.
* @{ * @{
@ -150,15 +150,14 @@ Value * nix_alloc_value(nix_c_context * context, EvalState * state);
* @param[in] value Nix value to inspect * @param[in] value Nix value to inspect
* @return type of nix value * @return type of nix value
*/ */
ValueType nix_get_type(nix_c_context * context, const Value * value); ValueType nix_get_type(nix_c_context * context, const Value * value);
/** @brief Get type name of value as defined in the evaluator /** @brief Get type name of value as defined in the evaluator
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
* @param[in] value Nix value to inspect * @param[in] value Nix value to inspect
* @return type name, owned string * @return type name, owned string
* @todo way to free the result * @todo way to free the result
*/ */
const char * nix_get_typename(nix_c_context * context, const Value * value); const char * nix_get_typename(nix_c_context * context, const Value * value);
/** @brief Get boolean value /** @brief Get boolean value
@ -167,6 +166,7 @@ const char * nix_get_typename(nix_c_context * context, const Value * value);
* @return true or false, error info via context * @return true or false, error info via context
*/ */
bool nix_get_bool(nix_c_context * context, const Value * value); bool nix_get_bool(nix_c_context * context, const Value * value);
/** @brief Get string /** @brief Get string
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
* @param[in] value Nix value to inspect * @param[in] value Nix value to inspect

View file

@ -7,6 +7,7 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
namespace nixC { namespace nixC {
class nix_api_expr_test : public nix_api_store_test class nix_api_expr_test : public nix_api_store_test
{ {
protected: protected:
@ -26,4 +27,5 @@ protected:
EvalState * state; EvalState * state;
Value * value; Value * value;
}; };
} }

View file

@ -44,7 +44,7 @@ TEST_F(nix_api_expr_test, nix_expr_eval_external)
{ {
MyExternalValueDesc * external = new MyExternalValueDesc(42); MyExternalValueDesc * external = new MyExternalValueDesc(42);
ExternalValue * val = nix_create_external_value(ctx, external, external); ExternalValue * val = nix_create_external_value(ctx, external, external);
nix_init_external(nullptr, value, val); nix_init_external(ctx, value, val);
EvalState * stateResult = nix_state_create(nullptr, nullptr, store); EvalState * stateResult = nix_state_create(nullptr, nullptr, store);
Value * valueResult = nix_alloc_value(nullptr, stateResult); Value * valueResult = nix_alloc_value(nullptr, stateResult);

View file

@ -14,132 +14,170 @@ namespace nixC {
TEST_F(nix_api_expr_test, nix_value_set_get_int) TEST_F(nix_api_expr_test, nix_value_set_get_int)
{ {
int myInt = 1; ASSERT_EQ(0, nix_get_int(ctx, nullptr));
nix_init_int(nullptr, value, myInt); ASSERT_DEATH(nix_get_int(ctx, value), "");
ASSERT_EQ(myInt, nix_get_int(nullptr, value)); int myInt = 1;
ASSERT_STREQ("an integer", nix_get_typename(nullptr, value)); nix_init_int(ctx, value, myInt);
ASSERT_EQ(NIX_TYPE_INT, nix_get_type(nullptr, value));
ASSERT_EQ(myInt, nix_get_int(ctx, value));
ASSERT_STREQ("an integer", nix_get_typename(ctx, value));
ASSERT_EQ(NIX_TYPE_INT, nix_get_type(ctx, value));
} }
TEST_F(nix_api_expr_test, nix_value_set_get_float) TEST_F(nix_api_expr_test, nix_value_set_get_float)
{ {
float myDouble = 1.0; ASSERT_FLOAT_EQ(0.0, nix_get_float(ctx, nullptr));
nix_init_float(nullptr, value, myDouble); ASSERT_DEATH(nix_get_float(ctx, value), "");
ASSERT_EQ(myDouble, nix_get_float(nullptr, value)); float myDouble = 1.0;
ASSERT_STREQ("a float", nix_get_typename(nullptr, value)); nix_init_float(ctx, value, myDouble);
ASSERT_EQ(NIX_TYPE_FLOAT, nix_get_type(nullptr, value));
ASSERT_FLOAT_EQ(myDouble, nix_get_float(ctx, value));
ASSERT_STREQ("a float", nix_get_typename(ctx, value));
ASSERT_EQ(NIX_TYPE_FLOAT, nix_get_type(ctx, value));
} }
TEST_F(nix_api_expr_test, nix_value_set_get_bool) TEST_F(nix_api_expr_test, nix_value_set_get_bool)
{ {
bool myBool = true; ASSERT_EQ(false, nix_get_bool(ctx, nullptr));
nix_init_bool(nullptr, value, myBool); ASSERT_DEATH(nix_get_bool(ctx, value), "");
ASSERT_EQ(myBool, nix_get_bool(nullptr, value)); bool myBool = true;
ASSERT_STREQ("a Boolean", nix_get_typename(nullptr, value)); nix_init_bool(ctx, value, myBool);
ASSERT_EQ(NIX_TYPE_BOOL, nix_get_type(nullptr, value));
ASSERT_EQ(myBool, nix_get_bool(ctx, value));
ASSERT_STREQ("a Boolean", nix_get_typename(ctx, value));
ASSERT_EQ(NIX_TYPE_BOOL, nix_get_type(ctx, value));
} }
TEST_F(nix_api_expr_test, nix_value_set_get_string) TEST_F(nix_api_expr_test, nix_value_set_get_string)
{ {
const char * myString = "some string"; ASSERT_EQ(nullptr, nix_get_string(ctx, nullptr));
nix_init_string(nullptr, value, myString); ASSERT_DEATH(nix_get_string(ctx, value), "");
ASSERT_STREQ(myString, nix_get_string(nullptr, value)); const char * myString = "some string";
ASSERT_STREQ("a string", nix_get_typename(nullptr, value)); nix_init_string(ctx, value, myString);
ASSERT_EQ(NIX_TYPE_STRING, nix_get_type(nullptr, value));
ASSERT_STREQ(myString, nix_get_string(ctx, value));
ASSERT_STREQ("a string", nix_get_typename(ctx, value));
ASSERT_EQ(NIX_TYPE_STRING, nix_get_type(ctx, value));
} }
TEST_F(nix_api_expr_test, nix_value_set_get_null) TEST_F(nix_api_expr_test, nix_value_set_get_null)
{ {
nix_init_null(nullptr, value); ASSERT_DEATH(nix_get_typename(ctx, value), "");
ASSERT_STREQ("null", nix_get_typename(nullptr, value)); nix_init_null(ctx, value);
ASSERT_EQ(NIX_TYPE_NULL, nix_get_type(nullptr, value));
ASSERT_STREQ("null", nix_get_typename(ctx, value));
ASSERT_EQ(NIX_TYPE_NULL, nix_get_type(ctx, value));
} }
TEST_F(nix_api_expr_test, nix_value_set_get_path) TEST_F(nix_api_expr_test, nix_value_set_get_path)
{ {
const char * p = "/nix/store/40s0qmrfb45vlh6610rk29ym318dswdr-myname"; ASSERT_EQ(nullptr, nix_get_path_string(ctx, nullptr));
nix_init_path_string(nullptr, state, value, p); ASSERT_DEATH(nix_get_path_string(ctx, value), "");
ASSERT_STREQ(p, nix_get_path_string(nullptr, value)); const char * p = "/nix/store/40s0qmrfb45vlh6610rk29ym318dswdr-myname";
ASSERT_STREQ("a path", nix_get_typename(nullptr, value)); nix_init_path_string(ctx, state, value, p);
ASSERT_EQ(NIX_TYPE_PATH, nix_get_type(nullptr, value));
ASSERT_STREQ(p, nix_get_path_string(ctx, value));
ASSERT_STREQ("a path", nix_get_typename(ctx, value));
ASSERT_EQ(NIX_TYPE_PATH, nix_get_type(ctx, value));
} }
TEST_F(nix_api_expr_test, nix_build_and_init_list) TEST_F(nix_api_expr_test, nix_build_and_init_list)
{ {
int size = 10; ASSERT_EQ(nullptr, nix_get_list_byidx(ctx, nullptr, state, 0));
ListBuilder * builder = nix_make_list_builder(nullptr, state, size); ASSERT_EQ(0, nix_get_list_size(ctx, nullptr));
Value * intValue = nix_alloc_value(nullptr, state); ASSERT_DEATH(nix_get_list_byidx(ctx, value, state, 0), "");
nix_init_int(nullptr, intValue, 42); ASSERT_DEATH(nix_get_list_size(ctx, value), "");
nix_list_builder_insert(nullptr, builder, 0, intValue);
nix_make_list(nullptr, builder, value); int size = 10;
ListBuilder * builder = nix_make_list_builder(ctx, state, size);
Value * intValue = nix_alloc_value(ctx, state);
nix_init_int(ctx, intValue, 42);
nix_list_builder_insert(ctx, builder, 0, intValue);
nix_make_list(ctx, builder, value);
nix_list_builder_free(builder); nix_list_builder_free(builder);
ASSERT_EQ(42, nix_get_int(nullptr, nix_get_list_byidx(nullptr, value, state, 0))); ASSERT_EQ(42, nix_get_int(ctx, nix_get_list_byidx(ctx, value, state, 0)));
ASSERT_EQ(nullptr, nix_get_list_byidx(nullptr, value, state, 1)); ASSERT_EQ(nullptr, nix_get_list_byidx(ctx, value, state, 1));
ASSERT_EQ(10, nix_get_list_size(nullptr, value)); ASSERT_EQ(10, nix_get_list_size(ctx, value));
ASSERT_STREQ("a list", nix_get_typename(nullptr, value)); ASSERT_STREQ("a list", nix_get_typename(ctx, value));
ASSERT_EQ(NIX_TYPE_LIST, nix_get_type(nullptr, value)); ASSERT_EQ(NIX_TYPE_LIST, nix_get_type(ctx, value));
// Clean up // Clean up
nix_gc_decref(nullptr, intValue); nix_gc_decref(ctx, intValue);
} }
TEST_F(nix_api_expr_test, nix_build_and_init_attr) TEST_F(nix_api_expr_test, nix_build_and_init_attr)
{ {
ASSERT_EQ(nullptr, nix_get_attr_byname(ctx, nullptr, state, 0));
ASSERT_EQ(nullptr, nix_get_attr_byidx(ctx, nullptr, state, 0, nullptr));
ASSERT_EQ(nullptr, nix_get_attr_name_byidx(ctx, nullptr, state, 0));
ASSERT_EQ(0, nix_get_attrs_size(ctx, nullptr));
ASSERT_EQ(false, nix_has_attr_byname(ctx, nullptr, state, "no-value"));
ASSERT_DEATH(nix_get_attr_byname(ctx, value, state, 0), "");
ASSERT_DEATH(nix_get_attr_byidx(ctx, value, state, 0, nullptr), "");
ASSERT_DEATH(nix_get_attr_name_byidx(ctx, value, state, 0), "");
ASSERT_DEATH(nix_get_attrs_size(ctx, value), "");
ASSERT_DEATH(nix_has_attr_byname(ctx, value, state, "no-value"), "");
int size = 10; int size = 10;
const char ** out_name = (const char **) malloc(sizeof(char *)); const char ** out_name = (const char **) malloc(sizeof(char *));
BindingsBuilder * builder = nix_make_bindings_builder(nullptr, state, size); BindingsBuilder * builder = nix_make_bindings_builder(ctx, state, size);
Value * intValue = nix_alloc_value(nullptr, state); Value * intValue = nix_alloc_value(ctx, state);
nix_init_int(nullptr, intValue, 42); nix_init_int(ctx, intValue, 42);
Value * stringValue = nix_alloc_value(nullptr, state); Value * stringValue = nix_alloc_value(ctx, state);
nix_init_string(nullptr, stringValue, "foo"); nix_init_string(ctx, stringValue, "foo");
nix_bindings_builder_insert(nullptr, builder, "a", intValue); nix_bindings_builder_insert(ctx, builder, "a", intValue);
nix_bindings_builder_insert(nullptr, builder, "b", stringValue); nix_bindings_builder_insert(ctx, builder, "b", stringValue);
nix_make_attrs(nullptr, value, builder); nix_make_attrs(ctx, value, builder);
nix_bindings_builder_free(builder); nix_bindings_builder_free(builder);
ASSERT_EQ(2, nix_get_attrs_size(nullptr, value)); ASSERT_EQ(2, nix_get_attrs_size(ctx, value));
Value * out_value = nix_get_attr_byname(nullptr, value, state, "a"); Value * out_value = nix_get_attr_byname(ctx, value, state, "a");
ASSERT_EQ(42, nix_get_int(nullptr, out_value)); ASSERT_EQ(42, nix_get_int(ctx, out_value));
nix_gc_decref(nullptr, out_value); nix_gc_decref(ctx, out_value);
out_value = nix_get_attr_byidx(nullptr, value, state, 0, out_name); out_value = nix_get_attr_byidx(ctx, value, state, 0, out_name);
ASSERT_EQ(42, nix_get_int(nullptr, out_value)); ASSERT_EQ(42, nix_get_int(ctx, out_value));
ASSERT_STREQ("a", *out_name); ASSERT_STREQ("a", *out_name);
nix_gc_decref(ctx, out_value);
ASSERT_STREQ("a", nix_get_attr_name_byidx(ctx, value, state, 0));
ASSERT_EQ(true, nix_has_attr_byname(ctx, value, state, "b"));
ASSERT_EQ(false, nix_has_attr_byname(ctx, value, state, "no-value"));
out_value = nix_get_attr_byname(ctx, value, state, "b");
ASSERT_STREQ("foo", nix_get_string(ctx, out_value));
nix_gc_decref(nullptr, out_value); nix_gc_decref(nullptr, out_value);
ASSERT_STREQ("a", nix_get_attr_name_byidx(nullptr, value, state, 0)); out_value = nix_get_attr_byidx(ctx, value, state, 1, out_name);
ASSERT_STREQ("foo", nix_get_string(ctx, out_value));
out_value = nix_get_attr_byname(nullptr, value, state, "b");
ASSERT_STREQ("foo", nix_get_string(nullptr, out_value));
nix_gc_decref(nullptr, out_value);
out_value = nix_get_attr_byidx(nullptr, value, state, 1, out_name);
ASSERT_STREQ("foo", nix_get_string(nullptr, out_value));
ASSERT_STREQ("b", *out_name); ASSERT_STREQ("b", *out_name);
nix_gc_decref(nullptr, out_value); nix_gc_decref(nullptr, out_value);
ASSERT_STREQ("b", nix_get_attr_name_byidx(nullptr, value, state, 1)); ASSERT_STREQ("b", nix_get_attr_name_byidx(ctx, value, state, 1));
ASSERT_STREQ("a set", nix_get_typename(nullptr, value)); ASSERT_STREQ("a set", nix_get_typename(ctx, value));
ASSERT_EQ(NIX_TYPE_ATTRS, nix_get_type(nullptr, value)); ASSERT_EQ(NIX_TYPE_ATTRS, nix_get_type(ctx, value));
// Clean up // Clean up
nix_gc_decref(nullptr, intValue); nix_gc_decref(ctx, intValue);
nix_gc_decref(nullptr, stringValue); nix_gc_decref(ctx, stringValue);
free(out_name); free(out_name);
} }