From 363dbeae95ce46be2b716506de90717998391a57 Mon Sep 17 00:00:00 2001 From: Griffin Smith Date: Fri, 27 Nov 2020 19:38:07 -0500 Subject: refactor(tvix): Centralize arbitrary definitions To make it easier for future tests to use the arbitrary specialisations we've defined for some nix types, centralize them all in a single arbitrary.hh header file. Change-Id: I767f27949cfe7ec55c79901f7d7aa538d2f98c6b Reviewed-on: https://cl.tvl.fyi/c/depot/+/2182 Tested-by: BuildkiteCI Reviewed-by: kanepyork --- third_party/nix/src/tests/attr-set.cc | 88 +---------------------------------- 1 file changed, 2 insertions(+), 86 deletions(-) (limited to 'third_party/nix/src/tests/attr-set.cc') diff --git a/third_party/nix/src/tests/attr-set.cc b/third_party/nix/src/tests/attr-set.cc index 84756c60940e..35932bbeff65 100644 --- a/third_party/nix/src/tests/attr-set.cc +++ b/third_party/nix/src/tests/attr-set.cc @@ -20,93 +20,9 @@ #include "libexpr/nixexpr.hh" #include "libexpr/symbol-table.hh" #include "libexpr/value.hh" +#include "tests/arbitrary.hh" #include "tests/dummy-store.hh" -static nix::SymbolTable* symbol_table; - -namespace rc { -using nix::Pos; -using nix::Value; - -// TODO(grfn): These arbitrary implementations should be pulled out to a util -// file sooner rather than later - -template <> -struct Arbitrary { - static Gen arbitrary() { - return gen::map(gen::arbitrary(), - [](std::string s) { return symbol_table->Create(s); }); - } -}; - -template <> -struct Arbitrary { - static Gen arbitrary() { - return gen::build(gen::construct(), - // TODO(grfn) generalize to more types - gen::set(&Value::type, gen::just(nix::ValueType::tInt)), - gen::set(&Value::integer, gen::arbitrary())); - } -}; - -template <> -struct Arbitrary { - static Gen arbitrary() { - return gen::apply( - [](nix::ValueType typ, int i) { - auto ret = new Value(); - ret->type = typ; - ret->integer = i; - return ret; - }, - gen::just(nix::ValueType::tInt), gen::arbitrary()); - } -}; - -template <> -struct Arbitrary { - static Gen arbitrary() { - return gen::construct(gen::arbitrary(), - gen::arbitrary(), - gen::arbitrary()); - } -}; - -template <> -struct Arbitrary { - static Gen arbitrary() { - return gen::apply( - [](unsigned int line, unsigned int column) { - return new Pos({}, line, column); - }, - gen::arbitrary(), gen::arbitrary()); - } -}; - -template <> -struct Arbitrary { - static Gen arbitrary() { - return gen::construct(gen::arbitrary(), - gen::arbitrary(), - gen::arbitrary()); - } -}; - -template <> -struct Arbitrary { - static Gen arbitrary() { - return gen::map(gen::arbitrary>(), [](auto attrs) { - nix::Bindings res; - for (const auto& attr : attrs) { - res.push_back(attr); - } - return res; - }); - } -}; - -} // namespace rc - namespace nix { using nix::tests::DummyStore; @@ -118,7 +34,7 @@ class AttrSetTest : public ::testing::Test { nix::expr::InitGC(); auto store = std::make_shared(); eval_state_ = new EvalState({"."}, ref(store)); - symbol_table = &eval_state_->symbols; + tests::symbol_table = &eval_state_->symbols; } void assert_bindings_equal(nix::Bindings* lhs, nix::Bindings* rhs) { -- cgit 1.4.1