From 77aa67c7e0411d031e03078b86d3c3271cbed193 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Sun, 22 Feb 2026 20:00:35 +0300 Subject: [PATCH] tests: add tests for lookup paths and imports Signed-off-by: NotAShelf Change-Id: I7e54691aa3e81efcb495124d13e8c24a6a6a6964 --- tests/import_lookup.nix | 3 + tests/import_simple.nix | 11 +++ tests/lookup_path.nix | 9 +++ tests/lookup_path_nested.nix | 3 + tests/regression_test.cpp | 125 +++++++++++++++++++++++++++++++++++ tests/string_interp.nix | 12 ++-- 6 files changed, 156 insertions(+), 7 deletions(-) create mode 100644 tests/import_lookup.nix create mode 100644 tests/import_simple.nix create mode 100644 tests/lookup_path.nix create mode 100644 tests/lookup_path_nested.nix diff --git a/tests/import_lookup.nix b/tests/import_lookup.nix new file mode 100644 index 0000000..448b1ea --- /dev/null +++ b/tests/import_lookup.nix @@ -0,0 +1,3 @@ +# Test import with lookup path +# Common pattern: import { } +import diff --git a/tests/import_simple.nix b/tests/import_simple.nix new file mode 100644 index 0000000..023b49d --- /dev/null +++ b/tests/import_simple.nix @@ -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 { } + +# Import with path expressions: +# import (./dir + "/file.nix") diff --git a/tests/lookup_path.nix b/tests/lookup_path.nix new file mode 100644 index 0000000..e8bb4ca --- /dev/null +++ b/tests/lookup_path.nix @@ -0,0 +1,9 @@ +# Test lookup path syntax +# Lookup paths resolve via NIX_PATH environment variable +# Example: -> /nix/var/nix/profiles/per-user/root/channels/nixpkgs + +# Simple lookup path + + +# Nested lookup path (common pattern) +# diff --git a/tests/lookup_path_nested.nix b/tests/lookup_path_nested.nix new file mode 100644 index 0000000..0478b00 --- /dev/null +++ b/tests/lookup_path_nested.nix @@ -0,0 +1,3 @@ +# Test nested lookup path +# Common pattern in Nix: or + diff --git a/tests/regression_test.cpp b/tests/regression_test.cpp index 10123a0..1b4c4ce 100644 --- a/tests/regression_test.cpp +++ b/tests/regression_test.cpp @@ -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(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(); + 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(ConstPathNode("./test.nix")); + auto import_node = std::make_shared(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(); + 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(); + 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(ConstLookupPathNode("nixpkgs")); + auto import_node = std::make_shared(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(); + TEST_CHECK(loaded_import != nullptr, "Deserialized node is ImportNode"); + + if (loaded_import && loaded_import->path) { + auto *lookup_node = loaded_import->path->get_if(); + 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(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(); + 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(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(); + 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; diff --git a/tests/string_interp.nix b/tests/string_interp.nix index af7b42d..9edc11b 100644 --- a/tests/string_interp.nix +++ b/tests/string_interp.nix @@ -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"; }