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!
This commit is contained in:
John Ericson 2023-01-30 10:55:08 -05:00
parent ecd3e4ebd7
commit 560142fec0
4 changed files with 67 additions and 17 deletions

View file

@ -78,22 +78,37 @@ TEST_F(NixStringContextElemTest, built) {
namespace rc { namespace rc {
using namespace nix; using namespace nix;
Gen<NixStringContextElem::Opaque> Arbitrary<NixStringContextElem::Opaque>::arbitrary()
{
return gen::just(NixStringContextElem::Opaque {
.path = *gen::arbitrary<StorePath>(),
});
}
Gen<NixStringContextElem::DrvDeep> Arbitrary<NixStringContextElem::DrvDeep>::arbitrary()
{
return gen::just(NixStringContextElem::DrvDeep {
.drvPath = *gen::arbitrary<StorePath>(),
});
}
Gen<NixStringContextElem::Built> Arbitrary<NixStringContextElem::Built>::arbitrary()
{
return gen::just(NixStringContextElem::Built {
.drvPath = *gen::arbitrary<StorePath>(),
.output = (*gen::arbitrary<StorePathName>()).name,
});
}
Gen<NixStringContextElem> Arbitrary<NixStringContextElem>::arbitrary() Gen<NixStringContextElem> Arbitrary<NixStringContextElem>::arbitrary()
{ {
switch (*gen::inRange<uint8_t>(0, 2)) { switch (*gen::inRange<uint8_t>(0, 2)) {
case 0: case 0:
return gen::just((NixStringContextElem) NixStringContextElem::Opaque { return gen::just<NixStringContextElem>(*gen::arbitrary<NixStringContextElem::Opaque>());
.path = *gen::arbitrary<StorePath>(),
});
case 1: case 1:
return gen::just((NixStringContextElem) NixStringContextElem::DrvDeep { return gen::just<NixStringContextElem>(*gen::arbitrary<NixStringContextElem::DrvDeep>());
.drvPath = *gen::arbitrary<StorePath>(),
});
default: default:
return gen::just((NixStringContextElem) NixStringContextElem::Built { return gen::just<NixStringContextElem>(*gen::arbitrary<NixStringContextElem::Built>());
.drvPath = *gen::arbitrary<StorePath>(),
.output = (*gen::arbitrary<StorePathName>()).name,
});
} }
} }

View file

@ -7,6 +7,21 @@
namespace rc { namespace rc {
using namespace nix; using namespace nix;
template<>
struct Arbitrary<NixStringContextElem::Opaque> {
static Gen<NixStringContextElem::Opaque> arbitrary();
};
template<>
struct Arbitrary<NixStringContextElem::Built> {
static Gen<NixStringContextElem::Built> arbitrary();
};
template<>
struct Arbitrary<NixStringContextElem::DrvDeep> {
static Gen<NixStringContextElem::DrvDeep> arbitrary();
};
template<> template<>
struct Arbitrary<NixStringContextElem> { struct Arbitrary<NixStringContextElem> {
static Gen<NixStringContextElem> arbitrary(); static Gen<NixStringContextElem> arbitrary();

View file

@ -10,18 +10,28 @@
namespace rc { namespace rc {
using namespace nix; using namespace nix;
Gen<DerivedPath::Opaque> Arbitrary<DerivedPath::Opaque>::arbitrary()
{
return gen::just(DerivedPath::Opaque {
.path = *gen::arbitrary<StorePath>(),
});
}
Gen<DerivedPath::Built> Arbitrary<DerivedPath::Built>::arbitrary()
{
return gen::just(DerivedPath::Built {
.drvPath = *gen::arbitrary<StorePath>(),
.outputs = *gen::arbitrary<OutputsSpec>(),
});
}
Gen<DerivedPath> Arbitrary<DerivedPath>::arbitrary() Gen<DerivedPath> Arbitrary<DerivedPath>::arbitrary()
{ {
switch (*gen::inRange<uint8_t>(0, 1)) { switch (*gen::inRange<uint8_t>(0, 1)) {
case 0: case 0:
return gen::just((DerivedPath) DerivedPath::Opaque { return gen::just<DerivedPath>(*gen::arbitrary<DerivedPath::Opaque>());
.path = *gen::arbitrary<StorePath>(),
});
default: default:
return gen::just((DerivedPath) DerivedPath::Built { return gen::just<DerivedPath>(*gen::arbitrary<DerivedPath::Built>());
.drvPath = *gen::arbitrary<StorePath>(),
.outputs = *gen::arbitrary<OutputsSpec>(),
});
} }
} }

View file

@ -10,6 +10,16 @@
namespace rc { namespace rc {
using namespace nix; using namespace nix;
template<>
struct Arbitrary<DerivedPath::Opaque> {
static Gen<DerivedPath::Opaque> arbitrary();
};
template<>
struct Arbitrary<DerivedPath::Built> {
static Gen<DerivedPath::Built> arbitrary();
};
template<> template<>
struct Arbitrary<DerivedPath> { struct Arbitrary<DerivedPath> {
static Gen<DerivedPath> arbitrary(); static Gen<DerivedPath> arbitrary();