Skip to content

Commit

Permalink
Make per-variant Arbitrary impls too
Browse files Browse the repository at this point in the history
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!
  • Loading branch information
Ericson2314 committed Jan 30, 2023
1 parent ecd3e4e commit 560142f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 17 deletions.
35 changes: 25 additions & 10 deletions src/libexpr/tests/value/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,37 @@ TEST_F(NixStringContextElemTest, built) {
namespace rc {
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()
{
switch (*gen::inRange<uint8_t>(0, 2)) {
case 0:
return gen::just((NixStringContextElem) NixStringContextElem::Opaque {
.path = *gen::arbitrary<StorePath>(),
});
return gen::just<NixStringContextElem>(*gen::arbitrary<NixStringContextElem::Opaque>());
case 1:
return gen::just((NixStringContextElem) NixStringContextElem::DrvDeep {
.drvPath = *gen::arbitrary<StorePath>(),
});
return gen::just<NixStringContextElem>(*gen::arbitrary<NixStringContextElem::DrvDeep>());
default:
return gen::just((NixStringContextElem) NixStringContextElem::Built {
.drvPath = *gen::arbitrary<StorePath>(),
.output = (*gen::arbitrary<StorePathName>()).name,
});
return gen::just<NixStringContextElem>(*gen::arbitrary<NixStringContextElem::Built>());
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/libexpr/tests/value/context.hh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@
namespace rc {
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<>
struct Arbitrary<NixStringContextElem> {
static Gen<NixStringContextElem> arbitrary();
Expand Down
24 changes: 17 additions & 7 deletions src/libstore/tests/derived-path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,28 @@
namespace rc {
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()
{
switch (*gen::inRange<uint8_t>(0, 1)) {
case 0:
return gen::just((DerivedPath) DerivedPath::Opaque {
.path = *gen::arbitrary<StorePath>(),
});
return gen::just<DerivedPath>(*gen::arbitrary<DerivedPath::Opaque>());
default:
return gen::just((DerivedPath) DerivedPath::Built {
.drvPath = *gen::arbitrary<StorePath>(),
.outputs = *gen::arbitrary<OutputsSpec>(),
});
return gen::just<DerivedPath>(*gen::arbitrary<DerivedPath::Built>());
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/libstore/tests/derived-path.hh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
namespace rc {
using namespace nix;

template<>
struct Arbitrary<DerivedPath::Opaque> {
static Gen<DerivedPath::Opaque> arbitrary();
};

template<>
struct Arbitrary<DerivedPath::Built> {
static Gen<DerivedPath::Built> arbitrary();
};

template<>
struct Arbitrary<DerivedPath> {
static Gen<DerivedPath> arbitrary();
Expand Down

0 comments on commit 560142f

Please sign in to comment.