2024-01-16 00:44:57 +02:00
|
|
|
#include "nix_api_store.h"
|
|
|
|
#include "nix_api_store_internal.h"
|
|
|
|
#include "nix_api_util.h"
|
|
|
|
#include "nix_api_util_internal.h"
|
|
|
|
#include "nix_api_expr.h"
|
|
|
|
#include "nix_api_value.h"
|
|
|
|
|
2024-02-25 19:14:32 +02:00
|
|
|
#include "tests/nix_api_expr.hh"
|
2024-01-16 00:44:57 +02:00
|
|
|
|
2024-02-22 01:09:15 +02:00
|
|
|
#include <cstdlib>
|
2024-01-16 00:44:57 +02:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
namespace nixC {
|
|
|
|
|
2024-02-25 19:14:32 +02:00
|
|
|
TEST_F(nix_api_expr_test, nix_value_set_get_int)
|
2024-01-16 00:44:57 +02:00
|
|
|
{
|
2024-03-29 15:00:19 +02:00
|
|
|
ASSERT_EQ(0, nix_get_int(ctx, nullptr));
|
|
|
|
ASSERT_DEATH(nix_get_int(ctx, value), "");
|
|
|
|
|
2024-01-16 00:44:57 +02:00
|
|
|
int myInt = 1;
|
2024-03-29 15:00:19 +02:00
|
|
|
nix_init_int(ctx, value, myInt);
|
2024-01-16 00:44:57 +02:00
|
|
|
|
2024-03-29 15:00:19 +02:00
|
|
|
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));
|
2024-02-25 22:21:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(nix_api_expr_test, nix_value_set_get_float)
|
|
|
|
{
|
2024-03-29 15:00:19 +02:00
|
|
|
ASSERT_FLOAT_EQ(0.0, nix_get_float(ctx, nullptr));
|
|
|
|
ASSERT_DEATH(nix_get_float(ctx, value), "");
|
|
|
|
|
2024-02-25 22:21:05 +02:00
|
|
|
float myDouble = 1.0;
|
2024-03-29 15:00:19 +02:00
|
|
|
nix_init_float(ctx, value, myDouble);
|
2024-02-25 22:21:05 +02:00
|
|
|
|
2024-03-29 15:00:19 +02:00
|
|
|
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));
|
2024-02-25 22:21:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(nix_api_expr_test, nix_value_set_get_bool)
|
|
|
|
{
|
2024-03-29 15:00:19 +02:00
|
|
|
ASSERT_EQ(false, nix_get_bool(ctx, nullptr));
|
|
|
|
ASSERT_DEATH(nix_get_bool(ctx, value), "");
|
|
|
|
|
2024-02-25 22:21:05 +02:00
|
|
|
bool myBool = true;
|
2024-03-29 15:00:19 +02:00
|
|
|
nix_init_bool(ctx, value, myBool);
|
2024-02-25 22:21:05 +02:00
|
|
|
|
2024-03-29 15:00:19 +02:00
|
|
|
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));
|
2024-02-25 22:21:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(nix_api_expr_test, nix_value_set_get_string)
|
|
|
|
{
|
2024-03-29 15:00:19 +02:00
|
|
|
ASSERT_EQ(nullptr, nix_get_string(ctx, nullptr));
|
|
|
|
ASSERT_DEATH(nix_get_string(ctx, value), "");
|
|
|
|
|
2024-02-25 22:21:05 +02:00
|
|
|
const char * myString = "some string";
|
2024-03-29 15:00:19 +02:00
|
|
|
nix_init_string(ctx, value, myString);
|
2024-02-25 22:21:05 +02:00
|
|
|
|
2024-03-29 15:00:19 +02:00
|
|
|
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));
|
2024-02-25 22:21:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(nix_api_expr_test, nix_value_set_get_null)
|
|
|
|
{
|
2024-03-29 15:00:19 +02:00
|
|
|
ASSERT_DEATH(nix_get_typename(ctx, value), "");
|
|
|
|
|
|
|
|
nix_init_null(ctx, value);
|
2024-02-25 22:21:05 +02:00
|
|
|
|
2024-03-29 15:00:19 +02:00
|
|
|
ASSERT_STREQ("null", nix_get_typename(ctx, value));
|
|
|
|
ASSERT_EQ(NIX_TYPE_NULL, nix_get_type(ctx, value));
|
2024-02-25 22:21:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(nix_api_expr_test, nix_value_set_get_path)
|
|
|
|
{
|
2024-03-29 15:00:19 +02:00
|
|
|
ASSERT_EQ(nullptr, nix_get_path_string(ctx, nullptr));
|
|
|
|
ASSERT_DEATH(nix_get_path_string(ctx, value), "");
|
|
|
|
|
2024-02-25 22:21:05 +02:00
|
|
|
const char * p = "/nix/store/40s0qmrfb45vlh6610rk29ym318dswdr-myname";
|
2024-03-29 15:00:19 +02:00
|
|
|
nix_init_path_string(ctx, state, value, p);
|
2024-02-25 22:21:05 +02:00
|
|
|
|
2024-03-29 15:00:19 +02:00
|
|
|
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));
|
2024-01-16 00:44:57 +02:00
|
|
|
}
|
|
|
|
|
2024-02-25 19:14:32 +02:00
|
|
|
TEST_F(nix_api_expr_test, nix_build_and_init_list)
|
2024-01-16 00:44:57 +02:00
|
|
|
{
|
2024-03-29 15:00:19 +02:00
|
|
|
ASSERT_EQ(nullptr, nix_get_list_byidx(ctx, nullptr, state, 0));
|
|
|
|
ASSERT_EQ(0, nix_get_list_size(ctx, nullptr));
|
|
|
|
|
|
|
|
ASSERT_DEATH(nix_get_list_byidx(ctx, value, state, 0), "");
|
|
|
|
ASSERT_DEATH(nix_get_list_size(ctx, value), "");
|
|
|
|
|
2024-01-16 00:44:57 +02:00
|
|
|
int size = 10;
|
2024-03-29 15:00:19 +02:00
|
|
|
ListBuilder * builder = nix_make_list_builder(ctx, state, size);
|
2024-02-22 01:09:15 +02:00
|
|
|
|
2024-03-29 15:00:19 +02:00
|
|
|
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);
|
2024-02-22 01:09:15 +02:00
|
|
|
nix_list_builder_free(builder);
|
|
|
|
|
2024-03-29 15:00:19 +02:00
|
|
|
ASSERT_EQ(42, nix_get_int(ctx, nix_get_list_byidx(ctx, value, state, 0)));
|
|
|
|
ASSERT_EQ(nullptr, nix_get_list_byidx(ctx, value, state, 1));
|
|
|
|
ASSERT_EQ(10, nix_get_list_size(ctx, value));
|
2024-02-22 01:09:15 +02:00
|
|
|
|
2024-03-29 15:00:19 +02:00
|
|
|
ASSERT_STREQ("a list", nix_get_typename(ctx, value));
|
|
|
|
ASSERT_EQ(NIX_TYPE_LIST, nix_get_type(ctx, value));
|
2024-02-25 22:21:05 +02:00
|
|
|
|
2024-02-22 01:09:15 +02:00
|
|
|
// Clean up
|
2024-03-29 15:00:19 +02:00
|
|
|
nix_gc_decref(ctx, intValue);
|
2024-01-16 00:44:57 +02:00
|
|
|
}
|
|
|
|
|
2024-02-25 19:14:32 +02:00
|
|
|
TEST_F(nix_api_expr_test, nix_build_and_init_attr)
|
2024-01-16 00:44:57 +02:00
|
|
|
{
|
2024-03-29 15:00:19 +02:00
|
|
|
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"), "");
|
|
|
|
|
2024-01-16 00:44:57 +02:00
|
|
|
int size = 10;
|
2024-02-22 01:09:15 +02:00
|
|
|
const char ** out_name = (const char **) malloc(sizeof(char *));
|
|
|
|
|
2024-03-29 15:00:19 +02:00
|
|
|
BindingsBuilder * builder = nix_make_bindings_builder(ctx, state, size);
|
2024-01-16 00:44:57 +02:00
|
|
|
|
2024-03-29 15:00:19 +02:00
|
|
|
Value * intValue = nix_alloc_value(ctx, state);
|
|
|
|
nix_init_int(ctx, intValue, 42);
|
2024-01-16 00:44:57 +02:00
|
|
|
|
2024-03-29 15:00:19 +02:00
|
|
|
Value * stringValue = nix_alloc_value(ctx, state);
|
|
|
|
nix_init_string(ctx, stringValue, "foo");
|
2024-02-22 01:09:15 +02:00
|
|
|
|
2024-03-29 15:00:19 +02:00
|
|
|
nix_bindings_builder_insert(ctx, builder, "a", intValue);
|
|
|
|
nix_bindings_builder_insert(ctx, builder, "b", stringValue);
|
|
|
|
nix_make_attrs(ctx, value, builder);
|
2024-02-22 01:09:15 +02:00
|
|
|
nix_bindings_builder_free(builder);
|
|
|
|
|
2024-03-29 15:00:19 +02:00
|
|
|
ASSERT_EQ(2, nix_get_attrs_size(ctx, value));
|
2024-02-22 01:09:15 +02:00
|
|
|
|
2024-03-29 15:00:19 +02:00
|
|
|
Value * out_value = nix_get_attr_byname(ctx, value, state, "a");
|
|
|
|
ASSERT_EQ(42, nix_get_int(ctx, out_value));
|
|
|
|
nix_gc_decref(ctx, out_value);
|
2024-02-22 01:09:15 +02:00
|
|
|
|
2024-03-29 15:00:19 +02:00
|
|
|
out_value = nix_get_attr_byidx(ctx, value, state, 0, out_name);
|
|
|
|
ASSERT_EQ(42, nix_get_int(ctx, out_value));
|
2024-02-22 01:09:15 +02:00
|
|
|
ASSERT_STREQ("a", *out_name);
|
2024-03-29 15:00:19 +02:00
|
|
|
nix_gc_decref(ctx, out_value);
|
|
|
|
|
|
|
|
ASSERT_STREQ("a", nix_get_attr_name_byidx(ctx, value, state, 0));
|
2024-02-22 01:09:15 +02:00
|
|
|
|
2024-03-29 15:00:19 +02:00
|
|
|
ASSERT_EQ(true, nix_has_attr_byname(ctx, value, state, "b"));
|
|
|
|
ASSERT_EQ(false, nix_has_attr_byname(ctx, value, state, "no-value"));
|
2024-02-22 01:09:15 +02:00
|
|
|
|
2024-03-29 15:00:19 +02:00
|
|
|
out_value = nix_get_attr_byname(ctx, value, state, "b");
|
|
|
|
ASSERT_STREQ("foo", nix_get_string(ctx, out_value));
|
2024-02-22 01:09:15 +02:00
|
|
|
nix_gc_decref(nullptr, out_value);
|
|
|
|
|
2024-03-29 15:00:19 +02:00
|
|
|
out_value = nix_get_attr_byidx(ctx, value, state, 1, out_name);
|
|
|
|
ASSERT_STREQ("foo", nix_get_string(ctx, out_value));
|
2024-02-22 01:09:15 +02:00
|
|
|
ASSERT_STREQ("b", *out_name);
|
|
|
|
nix_gc_decref(nullptr, out_value);
|
|
|
|
|
2024-03-29 15:00:19 +02:00
|
|
|
ASSERT_STREQ("b", nix_get_attr_name_byidx(ctx, value, state, 1));
|
2024-01-16 00:44:57 +02:00
|
|
|
|
2024-03-29 15:00:19 +02:00
|
|
|
ASSERT_STREQ("a set", nix_get_typename(ctx, value));
|
|
|
|
ASSERT_EQ(NIX_TYPE_ATTRS, nix_get_type(ctx, value));
|
2024-02-25 22:21:05 +02:00
|
|
|
|
2024-01-16 00:44:57 +02:00
|
|
|
// Clean up
|
2024-03-29 15:00:19 +02:00
|
|
|
nix_gc_decref(ctx, intValue);
|
|
|
|
nix_gc_decref(ctx, stringValue);
|
2024-02-22 01:09:15 +02:00
|
|
|
free(out_name);
|
2024-01-16 00:44:57 +02:00
|
|
|
}
|
2024-02-22 01:09:15 +02:00
|
|
|
|
2024-01-16 00:44:57 +02:00
|
|
|
}
|