Skip to content

Commit

Permalink
C API: refactor test support
Browse files Browse the repository at this point in the history
  • Loading branch information
jlesquembre committed Feb 25, 2024
1 parent abf14c5 commit 39074eb
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 74 deletions.
29 changes: 29 additions & 0 deletions tests/unit/libexpr-support/tests/nix_api_expr.hh
Original file line number Diff line number Diff line change
@@ -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;
};
}
20 changes: 1 addition & 19 deletions tests/unit/libexpr/nix_api_expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,12 @@
#include "nix_api_expr.h"
#include "nix_api_value.h"

#include "tests/nix_api_store.hh"
#include "tests/nix_api_expr.hh"

#include <gtest/gtest.h>

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)
{
nix_expr_eval_from_string(nullptr, state, "builtins.nixVersion", ".", value);
Expand Down
23 changes: 2 additions & 21 deletions tests/unit/libexpr/nix_api_external.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
#include "nix_api_expr_internal.h"
#include "nix_api_value.h"
#include "nix_api_external.h"
#include "tests/nix_api_store.hh"
#include "tests/nix_api_expr.hh"

#include <string>
#include <gtest/gtest.h>

namespace nixC {
Expand Down Expand Up @@ -42,25 +41,7 @@ class MyExternalValueDesc : public NixCExternalValueDesc
}
};

class nix_api_external_test : public nix_api_store_test
{
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)
TEST_F(nix_api_expr_test, nix_expr_eval_external)
{
MyExternalValueDesc * external = new MyExternalValueDesc(42);
ExternalValue * val = nix_create_external_value(ctx, external, external);
Expand Down
26 changes: 4 additions & 22 deletions tests/unit/libexpr/nix_api_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,22 @@
#include "nix_api_expr.h"
#include "nix_api_value.h"

#include "tests/nix_api_store.hh"
#include "tests/nix_api_expr.hh"

#include <cstdlib>
#include <gtest/gtest.h>

namespace nixC {

class nix_api_value_test : public nix_api_store_test
{
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)
TEST_F(nix_api_expr_test, nix_value_set_get_int)
{
int myInt = 1;
nix_init_int(nullptr, value, myInt);

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;
ListBuilder * builder = nix_make_list_builder(nullptr, state, size);
Expand All @@ -56,7 +38,7 @@ TEST_F(nix_api_value_test, nix_make_and_set_list)
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;
const char ** out_name = (const char **) malloc(sizeof(char *));
Expand Down
24 changes: 16 additions & 8 deletions tests/unit/libstore-support/tests/nix_api_store.hh
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,16 @@

namespace fs = std::filesystem;

namespace nixC {
class nix_api_store_test : public nix_api_util_context
{
public:
nix_api_store_test()
{
nix_libstore_init(ctx);

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);
init_local_store();
};

~nix_api_store_test() override
{
nix_store_free(store);
Expand All @@ -37,4 +32,17 @@ public:

Store * store;
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);
}
};
}
7 changes: 3 additions & 4 deletions tests/unit/libutil-support/tests/nix_api_util.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@

#include <gtest/gtest.h>

namespace nixC {
class nix_api_util_context : public ::testing::Test
{
protected:
static void SetUpTestSuite()
{
nix_libutil_init(NULL);
}

nix_api_util_context()
{
ctx = nix_c_context_create();
nix_libutil_init(ctx);
};

~nix_api_util_context() override
Expand All @@ -25,3 +23,4 @@ protected:

nix_c_context * ctx;
};
}

0 comments on commit 39074eb

Please sign in to comment.