C API: refactor test support

This commit is contained in:
José Luis Lafuente 2024-02-25 18:14:32 +01:00 committed by José Luis Lafuente
parent 693e8ec8fe
commit 2e1dbbe307
No known key found for this signature in database
GPG key ID: 8A3455EBE455489A
6 changed files with 55 additions and 74 deletions

View file

@ -0,0 +1,29 @@
#pragma once
///@file
#include "nix_api_expr.h"
#include "nix_api_value.h"
#include "tests/nix_api_store.hh"
#include <gtest/gtest.h>
namespace nixC {
class nix_api_expr_test : public nix_api_store_test
{
protected:
nix_api_expr_test()
{
nix_libexpr_init(ctx);
state = nix_state_create(nullptr, nullptr, store);
value = nix_alloc_value(nullptr, state);
}
~nix_api_expr_test()
{
nix_gc_decref(nullptr, value);
nix_state_free(state);
}
EvalState * state;
Value * value;
};
}

View file

@ -5,30 +5,12 @@
#include "nix_api_expr.h" #include "nix_api_expr.h"
#include "nix_api_value.h" #include "nix_api_value.h"
#include "tests/nix_api_store.hh" #include "tests/nix_api_expr.hh"
#include <gtest/gtest.h> #include <gtest/gtest.h>
namespace nixC { namespace nixC {
class nix_api_expr_test : public nix_api_store_test
{
public:
nix_api_expr_test()
{
state = nix_state_create(nullptr, nullptr, store);
value = nix_alloc_value(nullptr, state);
}
~nix_api_expr_test()
{
nix_gc_decref(nullptr, value);
nix_state_free(state);
}
EvalState * state;
Value * value;
};
TEST_F(nix_api_expr_test, nix_expr_eval_from_string) TEST_F(nix_api_expr_test, nix_expr_eval_from_string)
{ {
nix_expr_eval_from_string(nullptr, state, "builtins.nixVersion", ".", value); nix_expr_eval_from_string(nullptr, state, "builtins.nixVersion", ".", value);

View file

@ -6,9 +6,8 @@
#include "nix_api_expr_internal.h" #include "nix_api_expr_internal.h"
#include "nix_api_value.h" #include "nix_api_value.h"
#include "nix_api_external.h" #include "nix_api_external.h"
#include "tests/nix_api_store.hh" #include "tests/nix_api_expr.hh"
#include <string>
#include <gtest/gtest.h> #include <gtest/gtest.h>
namespace nixC { namespace nixC {
@ -42,25 +41,7 @@ private:
} }
}; };
class nix_api_external_test : public nix_api_store_test TEST_F(nix_api_expr_test, nix_expr_eval_external)
{
public:
nix_api_external_test()
{
state = nix_state_create(nullptr, nullptr, store);
value = nix_alloc_value(nullptr, state);
}
~nix_api_external_test()
{
nix_gc_decref(nullptr, value);
nix_state_free(state);
}
EvalState * state;
Value * value;
};
TEST_F(nix_api_external_test, nix_expr_eval_from_string)
{ {
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);

View file

@ -5,32 +5,14 @@
#include "nix_api_expr.h" #include "nix_api_expr.h"
#include "nix_api_value.h" #include "nix_api_value.h"
#include "tests/nix_api_store.hh" #include "tests/nix_api_expr.hh"
#include <cstdlib> #include <cstdlib>
#include <gtest/gtest.h> #include <gtest/gtest.h>
namespace nixC { namespace nixC {
class nix_api_value_test : public nix_api_store_test TEST_F(nix_api_expr_test, nix_value_set_get_int)
{
public:
nix_api_value_test()
{
state = nix_state_create(nullptr, nullptr, store);
value = nix_alloc_value(nullptr, state);
}
~nix_api_value_test()
{
nix_gc_decref(nullptr, value);
nix_state_free(state);
}
EvalState * state;
Value * value;
};
TEST_F(nix_api_value_test, nix_value_set_get_int)
{ {
int myInt = 1; int myInt = 1;
nix_init_int(nullptr, value, myInt); nix_init_int(nullptr, value, myInt);
@ -38,7 +20,7 @@ TEST_F(nix_api_value_test, nix_value_set_get_int)
ASSERT_EQ(myInt, nix_get_int(nullptr, value)); ASSERT_EQ(myInt, nix_get_int(nullptr, value));
} }
TEST_F(nix_api_value_test, nix_make_and_set_list) TEST_F(nix_api_expr_test, nix_build_and_init_list)
{ {
int size = 10; int size = 10;
ListBuilder * builder = nix_make_list_builder(nullptr, state, size); ListBuilder * builder = nix_make_list_builder(nullptr, state, size);
@ -56,7 +38,7 @@ TEST_F(nix_api_value_test, nix_make_and_set_list)
nix_gc_decref(nullptr, intValue); nix_gc_decref(nullptr, intValue);
} }
TEST_F(nix_api_value_test, nix_make_attrs_t) TEST_F(nix_api_expr_test, nix_build_and_init_attr)
{ {
int size = 10; int size = 10;
const char ** out_name = (const char **) malloc(sizeof(char *)); const char ** out_name = (const char **) malloc(sizeof(char *));

View file

@ -10,21 +10,16 @@
namespace fs = std::filesystem; namespace fs = std::filesystem;
namespace nixC {
class nix_api_store_test : public nix_api_util_context class nix_api_store_test : public nix_api_util_context
{ {
public: public:
nix_api_store_test() nix_api_store_test()
{ {
nix_libstore_init(ctx); nix_libstore_init(ctx);
init_local_store();
auto tmpl = nix::getEnv("TMPDIR").value_or("/tmp") + "/tests_nix-store.XXXXXX";
nixStoreDir = mkdtemp((char *) tmpl.c_str());
// Options documented in `nix help-stores`
const char * p1[] = {"root", nixStoreDir.c_str()};
const char ** params[] = {p1, nullptr};
store = nix_store_open(ctx, "local", params);
}; };
~nix_api_store_test() override ~nix_api_store_test() override
{ {
nix_store_free(store); nix_store_free(store);
@ -37,4 +32,17 @@ public:
Store * store; Store * store;
std::string nixStoreDir; std::string nixStoreDir;
protected:
void init_local_store()
{
auto tmpl = nix::getEnv("TMPDIR").value_or("/tmp") + "/tests_nix-store.XXXXXX";
nixStoreDir = mkdtemp((char *) tmpl.c_str());
// Options documented in `nix help-stores`
const char * p1[] = {"root", nixStoreDir.c_str()};
const char ** params[] = {p1, nullptr};
store = nix_store_open(ctx, "local", params);
}
}; };
}

View file

@ -4,17 +4,15 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
namespace nixC {
class nix_api_util_context : public ::testing::Test class nix_api_util_context : public ::testing::Test
{ {
protected: protected:
static void SetUpTestSuite()
{
nix_libutil_init(NULL);
}
nix_api_util_context() nix_api_util_context()
{ {
ctx = nix_c_context_create(); ctx = nix_c_context_create();
nix_libutil_init(ctx);
}; };
~nix_api_util_context() override ~nix_api_util_context() override
@ -25,3 +23,4 @@ protected:
nix_c_context * ctx; nix_c_context * ctx;
}; };
}