1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2024-09-19 10:50:24 -04:00
nix/tests/unit/libexpr/nix_api_external.cc

69 lines
2 KiB
C++
Raw Normal View History

2024-02-24 18:28:04 -05: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_expr_internal.h"
#include "nix_api_value.h"
#include "nix_api_external.h"
2024-02-25 12:14:32 -05:00
#include "tests/nix_api_expr.hh"
#include "tests/string_callback.hh"
2024-02-24 18:28:04 -05:00
#include <gtest/gtest.h>
namespace nixC {
class MyExternalValueDesc : public NixCExternalValueDesc
{
public:
MyExternalValueDesc(int x)
: _x(x)
{
print = print_function;
showType = show_type_function;
typeOf = type_of_function;
}
private:
int _x;
static void print_function(void * self, nix_printer * printer) {}
static void show_type_function(void * self, nix_string_return * res) {}
static void type_of_function(void * self, nix_string_return * res)
{
MyExternalValueDesc * obj = static_cast<MyExternalValueDesc *>(self);
std::string type_string = "nix-external<MyExternalValueDesc( ";
type_string += std::to_string(obj->_x);
type_string += " )>";
res->str = &*type_string.begin();
}
};
2024-02-25 12:14:32 -05:00
TEST_F(nix_api_expr_test, nix_expr_eval_external)
2024-02-24 18:28:04 -05:00
{
MyExternalValueDesc * external = new MyExternalValueDesc(42);
ExternalValue * val = nix_create_external_value(ctx, external, external);
2024-03-29 09:00:19 -04:00
nix_init_external(ctx, value, val);
2024-02-24 18:28:04 -05:00
EvalState * stateResult = nix_state_create(nullptr, nullptr, store);
nix_value * valueResult = nix_alloc_value(nullptr, stateResult);
2024-02-24 18:28:04 -05:00
EvalState * stateFn = nix_state_create(nullptr, nullptr, store);
nix_value * valueFn = nix_alloc_value(nullptr, stateFn);
2024-02-24 18:28:04 -05:00
nix_expr_eval_from_string(nullptr, state, "builtins.typeOf", ".", valueFn);
ASSERT_EQ(NIX_TYPE_EXTERNAL, nix_get_type(nullptr, value));
nix_value_call(ctx, state, valueFn, value, valueResult);
std::string string_value;
nix_get_string(nullptr, valueResult, OBSERVE_STRING(string_value));
ASSERT_STREQ("nix-external<MyExternalValueDesc( 42 )>", string_value.c_str());
2024-02-24 18:28:04 -05:00
}
2024-02-24 18:28:04 -05:00
}