Skip to content

Commit

Permalink
Merge pull request #177 from atilaneves/fix-176
Browse files Browse the repository at this point in the history
Fix #176 - check is no longer falsely trusted
  • Loading branch information
atilaneves authored Mar 27, 2020
2 parents 88849b7 + 073575c commit b7457d5
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 30 deletions.
5 changes: 2 additions & 3 deletions build/ut.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ dub run -q -c unittest-unthreaded --build=unittest-cov --compiler="$DC"
printf '\n\nLight tests\n--------------------\n\n'
dub run -q -c unittest-light --build=unittest --compiler="$DC"

for dn in `ls -d subpackages/*`
for dn in $(ls -d subpackages/*)
do
printf '\n\n'$dn' tests\n--------------------\n\n'
dub test -q --compiler="$DC" --root=$dn
dub test -q --compiler="$DC" --root="$dn"
done

1 change: 1 addition & 0 deletions dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"targetName": "ut_pass",
"sourcePaths": ["source", "gen/source", "tests/unit_threaded", "tests/examples/pass"],
"mainSourceFile": "example/example_pass.d",
"dflags": ["-dip25", "-dip1000", "-dip1008"],
"versions": ["testing_unit_threaded", "unitUnthreaded"]
},

Expand Down
1 change: 1 addition & 0 deletions subpackages/property/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
property-test-unittest
8 changes: 8 additions & 0 deletions subpackages/property/dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@ license "BSD 3-clause"
targetType "library"
dependency "unit-threaded:from" version="*"
dependency "unit-threaded:exception" version="*"

configuration "default" {

}

configuration "unittest" {
dflags "-preview=dip1000"
}
58 changes: 33 additions & 25 deletions subpackages/property/source/unit_threaded/property.d
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,28 @@ void check(alias F, int numFuncCalls = 100)
(in uint seed = from!"std.random".unpredictableSeed,
in string file = __FILE__,
in size_t line = __LINE__)
@trusted
{

import unit_threaded.randomized.random: RndValueGen;
import unit_threaded.exception: UnitTestException;
import std.conv: text;
import std.traits: ReturnType, Parameters, isSomeString;
import std.array: join;
import std.typecons: Flag, Yes, No;
import std.random: Random;
import std.traits: isSafe;

static assert(is(ReturnType!F == bool),
text("check only accepts functions that return bool, not ", ReturnType!F.stringof));

auto random = Random(seed);
auto gen = RndValueGen!(Parameters!F)(&random);
scope random = Random(seed);
scope gen = RndValueGen!(Parameters!F)(&random);

auto input(Flag!"shrink" shrink = Yes.shrink) {
string[] ret;
scope string[] ret;
static if(Parameters!F.length == 1 && canShrink!(Parameters!F[0])) {
auto val = gen.values[0].value;
auto shrunk = shrink ? val.shrink!F : val;
ret ~= shrunk.text;
ret ~= shrunk.text.idup;
static if(isSomeString!(Parameters!F[0]))
ret[$-1] = `"` ~ ret[$-1] ~ `"`;
} else
Expand All @@ -57,27 +56,36 @@ void check(alias F, int numFuncCalls = 100)
foreach(i; 0 .. numFuncCalls) {
bool pass;

try {
gen.genValues;
} catch(Throwable t) {
throw new PropertyException("Error generating values\n" ~ t.toString, file, line, t);
}

try {
pass = F(gen.values);
} catch(Throwable t) {
// trying to shrink when an exeption is thrown is too much of a bother code-wise
throw new UnitTestException(
text("Property threw. Seed: ", seed, ". Input: ", input(No.shrink), ". Message: ", t.msg),
file,
line,
t,
);
}
() @trusted { // catch throwable
try {
static if(isSafe!({ gen.genValues; }))
() @safe { gen.genValues; }();
else
gen.genValues;
} catch(Throwable t) {
throw new PropertyException("Error generating values\n" ~ t.toString, file, line, t);
}
}();

() @trusted { // catch throwable
try {
static if(isSafe!F)
pass = () @safe { return F(gen.values); }();
else
pass = F(gen.values);
} catch(Throwable t) {
// trying to shrink when an exeption is thrown is too much of a bother code-wise
throw new UnitTestException(
text("Property threw. Seed: ", seed, ". Input: ", input(No.shrink), ". Message: ", t.msg),
file,
line,
t,
);
}
}();

if(!pass) {
if(!pass)
throw new UnitTestException(text("Property failed. Seed: ", seed, ". Input: ", input), file, line);
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions subpackages/property/source/unit_threaded/randomized/random.d
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct RndValueGen(T...)
Params:
rnd = The required random number generator.
*/
this(Random* rnd) @safe
this(Random* rnd)
{
this.rnd = rnd;
}
Expand All @@ -49,8 +49,9 @@ struct RndValueGen(T...)
$(D values) passing $(D the provided) random number generator
*/
void genValues()
in(rnd !is null)
do
{
assert(rnd !is null);
foreach (ref it; this.values)
{
it.gen(*this.rnd);
Expand Down
28 changes: 28 additions & 0 deletions tests/unit_threaded/ut/issues.d
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,31 @@ class CalcController {
` tests/unit_threaded/ut/issues.d:123 - Got : 42000.301000`);
}
}


// should not compile for @safe
@("176")
@safe unittest {
int* ptr;
bool func(int a) {
*(ptr + 256) = 42;
return a % 2 == 0;
}

version(unitThreadedLight) {}
else
static assert(!__traits(compiles, check!func(100)));
}


// should compile for @system
@("176.1")
@system unittest {
int* ptr;
bool func(int a) {
*(ptr + 256) = 42;
return a % 2 == 0;
}

static assert(__traits(compiles, check!func(100)));
}

0 comments on commit b7457d5

Please sign in to comment.