tests: add tests for lookup paths and imports

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I7e54691aa3e81efcb495124d13e8c24a6a6a6964
This commit is contained in:
raf 2026-02-22 20:00:35 +03:00
commit 77aa67c7e0
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF
6 changed files with 156 additions and 7 deletions

3
tests/import_lookup.nix Normal file
View file

@ -0,0 +1,3 @@
# Test import with lookup path
# Common pattern: import <nixpkgs> { }
import <nixpkgs>

11
tests/import_simple.nix Normal file
View file

@ -0,0 +1,11 @@
# Test import expression
# Import evaluates the file and returns its value
# Import a file that returns a simple value (42)
import ./simple.nix
# Can also import lookup paths:
# import <nixpkgs> { }
# Import with path expressions:
# import (./dir + "/file.nix")

9
tests/lookup_path.nix Normal file
View file

@ -0,0 +1,9 @@
# Test lookup path syntax
# Lookup paths resolve via NIX_PATH environment variable
# Example: <nixpkgs> -> /nix/var/nix/profiles/per-user/root/channels/nixpkgs
# Simple lookup path
<nixpkgs>
# Nested lookup path (common pattern)
# <nixpkgs/lib>

View file

@ -0,0 +1,3 @@
# Test nested lookup path
# Common pattern in Nix: <nixpkgs/lib> or <nixpkgs/pkgs/stdenv>
<nixpkgs/lib>

View file

@ -157,6 +157,116 @@ void test_parser_expect_in_speculative_parsing() {
<< std::endl;
}
void test_lookup_path_node() {
std::cout << "> Lookup path serialization..." << std::endl;
auto lookup = std::make_shared<Node>(ConstLookupPathNode("nixpkgs"));
IRModule module;
module.entry = lookup;
Serializer ser;
auto bytes = ser.serialize_to_bytes(module);
Deserializer deser;
auto loaded = deser.deserialize(bytes);
auto *loaded_lookup = loaded.entry->get_if<ConstLookupPathNode>();
TEST_CHECK(loaded_lookup != nullptr, "Deserialized node is ConstLookupPathNode");
TEST_CHECK(loaded_lookup && loaded_lookup->value == "nixpkgs",
"Lookup path value is 'nixpkgs'");
}
void test_import_node() {
std::cout << "> Import node serialization..." << std::endl;
auto path = std::make_shared<Node>(ConstPathNode("./test.nix"));
auto import_node = std::make_shared<Node>(ImportNode(path));
IRModule module;
module.entry = import_node;
Serializer ser;
auto bytes = ser.serialize_to_bytes(module);
Deserializer deser;
auto loaded = deser.deserialize(bytes);
auto *loaded_import = loaded.entry->get_if<ImportNode>();
TEST_CHECK(loaded_import != nullptr, "Deserialized node is ImportNode");
TEST_CHECK(loaded_import && loaded_import->path != nullptr,
"Import node has path");
if (loaded_import && loaded_import->path) {
auto *path_node = loaded_import->path->get_if<ConstPathNode>();
TEST_CHECK(path_node != nullptr, "Import path is ConstPathNode");
TEST_CHECK(path_node && path_node->value == "./test.nix",
"Import path value is './test.nix'");
}
}
void test_import_with_lookup_path() {
std::cout << "> Import with lookup path..." << std::endl;
auto lookup = std::make_shared<Node>(ConstLookupPathNode("nixpkgs"));
auto import_node = std::make_shared<Node>(ImportNode(lookup));
IRModule module;
module.entry = import_node;
Serializer ser;
auto bytes = ser.serialize_to_bytes(module);
Deserializer deser;
auto loaded = deser.deserialize(bytes);
auto *loaded_import = loaded.entry->get_if<ImportNode>();
TEST_CHECK(loaded_import != nullptr, "Deserialized node is ImportNode");
if (loaded_import && loaded_import->path) {
auto *lookup_node = loaded_import->path->get_if<ConstLookupPathNode>();
TEST_CHECK(lookup_node != nullptr, "Import path is ConstLookupPathNode");
TEST_CHECK(lookup_node && lookup_node->value == "nixpkgs",
"Lookup path value is 'nixpkgs'");
}
}
void test_uri_node() {
std::cout << "> URI node serialization..." << std::endl;
auto uri = std::make_shared<Node>(ConstURINode("https://example.com"));
IRModule module;
module.entry = uri;
Serializer ser;
auto bytes = ser.serialize_to_bytes(module);
Deserializer deser;
auto loaded = deser.deserialize(bytes);
auto *loaded_uri = loaded.entry->get_if<ConstURINode>();
TEST_CHECK(loaded_uri != nullptr, "Deserialized node is ConstURINode");
TEST_CHECK(loaded_uri && loaded_uri->value == "https://example.com",
"URI value is 'https://example.com'");
}
void test_float_node() {
std::cout << "> Float node serialization..." << std::endl;
auto float_val = std::make_shared<Node>(ConstFloatNode(3.14159));
IRModule module;
module.entry = float_val;
Serializer ser;
auto bytes = ser.serialize_to_bytes(module);
Deserializer deser;
auto loaded = deser.deserialize(bytes);
auto *loaded_float = loaded.entry->get_if<ConstFloatNode>();
TEST_CHECK(loaded_float != nullptr, "Deserialized node is ConstFloatNode");
TEST_CHECK(loaded_float && loaded_float->value > 3.14 &&
loaded_float->value < 3.15,
"Float value is approximately 3.14159");
}
int main() {
std::cout << "=== Regression Tests for Nixir ===" << std::endl << std::endl;
@ -178,6 +288,21 @@ int main() {
test_parser_expect_in_speculative_parsing();
std::cout << std::endl;
test_lookup_path_node();
std::cout << std::endl;
test_import_node();
std::cout << std::endl;
test_import_with_lookup_path();
std::cout << std::endl;
test_uri_node();
std::cout << std::endl;
test_float_node();
std::cout << std::endl;
std::cout << "=== Tests Complete ===" << std::endl;
std::cout << "Failures: " << failures << std::endl;
return failures > 0 ? 1 : 0;

View file

@ -10,13 +10,11 @@ in {
# Multiple interpolations
multi = "x is ${x} and name is ${name}";
# Nested expression
nested = "Result: ${
if bool_val
then "yes"
else "no"
}";
# Expression evaluation in interpolation
computed = "x + 10 = ${x + 10}";
# Just a string (no interpolation)
bool_check = "${bool_val} is true!";
# Just a string, no interpolation
plain = "plain text";
}