Skip to content

Commit 35396d0

Browse files
committed
test(libexpr-tests): add tests for nix_get_external
Regression test for the check_value_out → check_value_in fix: nix_get_external reads from an already-initialized value and must use check_value_in, not check_value_out (which asserts the value is NOT initialized). Added: - nix_get_external_roundtrip: create external → init → force → get back - nix_get_external_invalid: null and uninitialized value error paths - nix_get_external_value_content_null: null ExternalValue* gracefully
1 parent dac89e4 commit 35396d0

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

src/libexpr-tests/nix_api_external.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,34 @@ TEST_F(nix_api_expr_test, nix_external_printValueAsJSON_can_use_state)
104104
nix_gc_decref(ctx, toJsonFn);
105105
}
106106

107+
TEST_F(nix_api_expr_test, nix_get_external_roundtrip)
108+
{
109+
int content = 42;
110+
NixCExternalValueDesc desc{};
111+
ExternalValue * ext = nix_create_external_value(ctx, &desc, &content);
112+
assert_ctx_ok();
113+
ASSERT_NE(nullptr, ext);
114+
115+
nix_init_external(ctx, value, ext);
116+
assert_ctx_ok();
117+
118+
nix_value_force(ctx, state, value);
119+
assert_ctx_ok();
120+
121+
ExternalValue * retrieved = nix_get_external(ctx, value);
122+
assert_ctx_ok();
123+
ASSERT_NE(nullptr, retrieved);
124+
125+
void * content_ptr = nix_get_external_value_content(ctx, retrieved);
126+
assert_ctx_ok();
127+
ASSERT_EQ(&content, content_ptr);
128+
129+
nix_gc_decref(ctx, ext);
130+
}
131+
132+
TEST_F(nix_api_expr_test, nix_get_external_value_content_null)
133+
{
134+
ASSERT_EQ(nullptr, nix_get_external_value_content(ctx, nullptr));
135+
}
136+
107137
} // namespace nixC

src/libexpr-tests/nix_api_value.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ TEST_F(nix_api_expr_test, nix_value_set_get_path)
119119
ASSERT_EQ(NIX_TYPE_PATH, nix_get_type(ctx, value));
120120
}
121121

122+
TEST_F(nix_api_expr_test, nix_get_external_invalid)
123+
{
124+
ASSERT_EQ(nullptr, nix_get_external(ctx, nullptr));
125+
assert_ctx_err();
126+
ASSERT_EQ(nullptr, nix_get_external(ctx, value));
127+
assert_ctx_err();
128+
}
129+
122130
TEST_F(nix_api_expr_test, nix_build_and_init_list_invalid)
123131
{
124132
ASSERT_EQ(nullptr, nix_get_list_byidx(ctx, nullptr, state, 0));

0 commit comments

Comments
 (0)