mirror of
https://github.com/NixOS/nix.git
synced 2025-11-09 12:06:01 +01:00
Today, with the tests inside a `tests` intermingled with the
corresponding library's source code, we have a few problems:
- We have to be careful that wildcards don't end up with tests being
built as part of Nix proper, or test headers being installed as part
of Nix proper.
- Tests in libraries but not executables is not right:
- It means each executable runs the previous unit tests again, because
it needs the libraries.
- It doesn't work right on Windows, which doesn't want you to load a
DLL just for the side global variable . It could be made to work
with the dlopen equivalent, but that's gross!
This reorg solves these problems.
There is a remaining problem which is that sibbling headers (like
`hash.hh` the test header vs `hash.hh` the main `libnixutil` header) end
up shadowing each other. This PR doesn't solve that. That is left as
future work for a future PR.
Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
58 lines
1.6 KiB
C++
58 lines
1.6 KiB
C++
#include <vector>
|
|
#include <optional>
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include "json-utils.hh"
|
|
|
|
namespace nix {
|
|
|
|
/* Test `to_json` and `from_json` with `std::optional` types.
|
|
* We are specifically interested in whether we can _nest_ optionals in STL
|
|
* containers so we that we can leverage existing adl_serializer templates. */
|
|
|
|
TEST(to_json, optionalInt) {
|
|
std::optional<int> val = std::make_optional(420);
|
|
ASSERT_EQ(nlohmann::json(val), nlohmann::json(420));
|
|
val = std::nullopt;
|
|
ASSERT_EQ(nlohmann::json(val), nlohmann::json(nullptr));
|
|
}
|
|
|
|
TEST(to_json, vectorOfOptionalInts) {
|
|
std::vector<std::optional<int>> vals = {
|
|
std::make_optional(420),
|
|
std::nullopt,
|
|
};
|
|
ASSERT_EQ(nlohmann::json(vals), nlohmann::json::parse("[420,null]"));
|
|
}
|
|
|
|
TEST(to_json, optionalVectorOfInts) {
|
|
std::optional<std::vector<int>> val = std::make_optional(std::vector<int> {
|
|
-420,
|
|
420,
|
|
});
|
|
ASSERT_EQ(nlohmann::json(val), nlohmann::json::parse("[-420,420]"));
|
|
val = std::nullopt;
|
|
ASSERT_EQ(nlohmann::json(val), nlohmann::json(nullptr));
|
|
}
|
|
|
|
TEST(from_json, optionalInt) {
|
|
nlohmann::json json = 420;
|
|
std::optional<int> val = json;
|
|
ASSERT_TRUE(val.has_value());
|
|
ASSERT_EQ(*val, 420);
|
|
json = nullptr;
|
|
json.get_to(val);
|
|
ASSERT_FALSE(val.has_value());
|
|
}
|
|
|
|
TEST(from_json, vectorOfOptionalInts) {
|
|
nlohmann::json json = { 420, nullptr };
|
|
std::vector<std::optional<int>> vals = json;
|
|
ASSERT_EQ(vals.size(), 2);
|
|
ASSERT_TRUE(vals.at(0).has_value());
|
|
ASSERT_EQ(*vals.at(0), 420);
|
|
ASSERT_FALSE(vals.at(1).has_value());
|
|
}
|
|
|
|
} /* namespace nix */
|