Assert on construction that OutputsSpec::Names is non-empty

This commit is contained in:
John Ericson 2023-01-12 20:41:29 -05:00
parent e947aa5401
commit d29eb08563
2 changed files with 12 additions and 3 deletions

View file

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <cassert>
#include <optional> #include <optional>
#include <set> #include <set>
#include <variant> #include <variant>
@ -11,13 +12,15 @@ namespace nix {
struct OutputNames : std::set<std::string> { struct OutputNames : std::set<std::string> {
using std::set<std::string>::set; using std::set<std::string>::set;
// These need to be "inherited manually" /* These need to be "inherited manually" */
OutputNames(const std::set<std::string> & s) OutputNames(const std::set<std::string> & s)
: std::set<std::string>(s) : std::set<std::string>(s)
{ } { assert(!empty()); }
OutputNames(std::set<std::string> && s) OutputNames(std::set<std::string> && s)
: std::set<std::string>(s) : std::set<std::string>(s)
{ } { assert(!empty()); }
/* This set should always be non-empty, so we delete this /* This set should always be non-empty, so we delete this
constructor in order make creating empty ones by mistake harder. constructor in order make creating empty ones by mistake harder.

View file

@ -4,6 +4,12 @@
namespace nix { namespace nix {
#ifndef NDEBUG
TEST(OutputsSpec, no_empty_names) {
ASSERT_DEATH(OutputsSpec::Names { std::set<std::string> { } }, "");
}
#endif
#define TEST_DONT_PARSE(NAME, STR) \ #define TEST_DONT_PARSE(NAME, STR) \
TEST(OutputsSpec, bad_ ## NAME) { \ TEST(OutputsSpec, bad_ ## NAME) { \
std::optional OutputsSpecOpt = \ std::optional OutputsSpecOpt = \