From 560142fec0debefe7f983e60663dc3db4bdd79a9 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Mon, 30 Jan 2023 10:55:08 -0500 Subject: [PATCH] Make per-variant Arbitrary impls too This is a nice idea that @roberth requested. If we could factor our a generic `std::variant` impl as a follow-up it would be even better! --- src/libexpr/tests/value/context.cc | 35 +++++++++++++++++++++--------- src/libexpr/tests/value/context.hh | 15 +++++++++++++ src/libstore/tests/derived-path.cc | 24 ++++++++++++++------ src/libstore/tests/derived-path.hh | 10 +++++++++ 4 files changed, 67 insertions(+), 17 deletions(-) diff --git a/src/libexpr/tests/value/context.cc b/src/libexpr/tests/value/context.cc index 75934ab31..083359b7a 100644 --- a/src/libexpr/tests/value/context.cc +++ b/src/libexpr/tests/value/context.cc @@ -78,22 +78,37 @@ TEST_F(NixStringContextElemTest, built) { namespace rc { using namespace nix; +Gen Arbitrary::arbitrary() +{ + return gen::just(NixStringContextElem::Opaque { + .path = *gen::arbitrary(), + }); +} + +Gen Arbitrary::arbitrary() +{ + return gen::just(NixStringContextElem::DrvDeep { + .drvPath = *gen::arbitrary(), + }); +} + +Gen Arbitrary::arbitrary() +{ + return gen::just(NixStringContextElem::Built { + .drvPath = *gen::arbitrary(), + .output = (*gen::arbitrary()).name, + }); +} + Gen Arbitrary::arbitrary() { switch (*gen::inRange(0, 2)) { case 0: - return gen::just((NixStringContextElem) NixStringContextElem::Opaque { - .path = *gen::arbitrary(), - }); + return gen::just(*gen::arbitrary()); case 1: - return gen::just((NixStringContextElem) NixStringContextElem::DrvDeep { - .drvPath = *gen::arbitrary(), - }); + return gen::just(*gen::arbitrary()); default: - return gen::just((NixStringContextElem) NixStringContextElem::Built { - .drvPath = *gen::arbitrary(), - .output = (*gen::arbitrary()).name, - }); + return gen::just(*gen::arbitrary()); } } diff --git a/src/libexpr/tests/value/context.hh b/src/libexpr/tests/value/context.hh index 83505962f..54d21760e 100644 --- a/src/libexpr/tests/value/context.hh +++ b/src/libexpr/tests/value/context.hh @@ -7,6 +7,21 @@ namespace rc { using namespace nix; +template<> +struct Arbitrary { + static Gen arbitrary(); +}; + +template<> +struct Arbitrary { + static Gen arbitrary(); +}; + +template<> +struct Arbitrary { + static Gen arbitrary(); +}; + template<> struct Arbitrary { static Gen arbitrary(); diff --git a/src/libstore/tests/derived-path.cc b/src/libstore/tests/derived-path.cc index 09887d1f0..d1ac2c5e7 100644 --- a/src/libstore/tests/derived-path.cc +++ b/src/libstore/tests/derived-path.cc @@ -10,18 +10,28 @@ namespace rc { using namespace nix; +Gen Arbitrary::arbitrary() +{ + return gen::just(DerivedPath::Opaque { + .path = *gen::arbitrary(), + }); +} + +Gen Arbitrary::arbitrary() +{ + return gen::just(DerivedPath::Built { + .drvPath = *gen::arbitrary(), + .outputs = *gen::arbitrary(), + }); +} + Gen Arbitrary::arbitrary() { switch (*gen::inRange(0, 1)) { case 0: - return gen::just((DerivedPath) DerivedPath::Opaque { - .path = *gen::arbitrary(), - }); + return gen::just(*gen::arbitrary()); default: - return gen::just((DerivedPath) DerivedPath::Built { - .drvPath = *gen::arbitrary(), - .outputs = *gen::arbitrary(), - }); + return gen::just(*gen::arbitrary()); } } diff --git a/src/libstore/tests/derived-path.hh b/src/libstore/tests/derived-path.hh index 0161449ef..3bc812440 100644 --- a/src/libstore/tests/derived-path.hh +++ b/src/libstore/tests/derived-path.hh @@ -10,6 +10,16 @@ namespace rc { using namespace nix; +template<> +struct Arbitrary { + static Gen arbitrary(); +}; + +template<> +struct Arbitrary { + static Gen arbitrary(); +}; + template<> struct Arbitrary { static Gen arbitrary();