Skip to content

Commit

Permalink
enabling php7.2 refs giorgiosironi#116
Browse files Browse the repository at this point in the history
replacing some configuration with annotations refs giorgiosironi#105
moving rand and mt_rand into Source-Implementations
removing unused use statements
replacing os specific tmp-dir with function calls
fixing env-setting on windows
handling int overflow for seeds on 32bit systems
adjusting documentation
  • Loading branch information
Idrinth committed Jul 4, 2018
1 parent d921bbe commit f5ffcdc
Show file tree
Hide file tree
Showing 69 changed files with 413 additions and 360 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ matrix:
env: WITH_CS=true
- php: 7 # PHPUnit 6.x
- php: 7.1 # PHPUnit 7.x
- php: 7.2 # PHPUnit 7.x
- php: hhvm
env: SKIP_LINT_TEST_CASES=1
sudo: required
Expand Down
6 changes: 3 additions & 3 deletions docs/limits.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ Time and iterations

By default Eris extracts a sample of 100 values for each ``forAll()`` call, and runs the ``then()`` callback over each of them.

For tests which take very long to run, it is possible to either limit the number of elements in the sample, or to specify a time limit the test should not exceed. For this purpose, the ``limitTo()`` method accepts either:
For tests which take very long to run, it is possible to limit the number of elements in the sample, or to specify a time limit the test should not exceed. For this purpose, the you can use the following annotations:

* an integer requesting a fixed number of iterations;
* a `DateInterval`_ object from the standard PHP library.
* `@eris-repeat {number}` with an integer requesting a fixed number of iterations;
* `@eris-duration {definition}` with a `DateInterval`_ compatible definition.

.. literalinclude:: ../examples/LimitToTest.php
:language: php
Expand Down
2 changes: 1 addition & 1 deletion docs/listeners.rst
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ It is not advised to rely on this format for parsing, being it only oriented to
Minimum Evaluations
---
The ``minimumEvaluations($ratio)`` API method instantiates and wires in a Listener that checks that at least ``$ratio`` of the total number of inputs being generated is actually evaluated. This Listener is only needed in case of an aggressive use of ``when()``.
The ``@eris-ratio {ratio}`` annotation instantiates and wires in a Listener that checks that at least ``ratio``% of the total number of inputs being generated is actually evaluated. This Listener is only needed in case of an aggressive use of ``when()``.
Management of this Listener is provided through this method instead of explicitly adding a Listener object, as there is a default Listener instantiated with a threshold of 0.5 that has to be replaced in case a new minimum is chosen.
Expand Down
2 changes: 1 addition & 1 deletion docs/shrinking.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ The shrinking for this test will not run for more than 2 seconds (although the t
1) ShrinkingTimeLimitTest::testLengthPreservation
RuntimeException: Eris has reached the time limit for shrinking (2s elapsed of 2s), here it is presenting the simplest failure case.
If you can afford to spend more time to find a simpler failing input, increase it with $this->shrinkingTimeLimit($seconds).
If you can afford to spend more time to find a simpler failing input, increase it with the annotation '@eris-shrink {seconds}'.
/home/giorgio/code/eris/src/Eris/Shrinker/Random.php:71
/home/giorgio/code/eris/src/Eris/Quantifier/ForAll.php:128
Expand Down
4 changes: 3 additions & 1 deletion examples/CharacterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ public function testLengthOfPrintableAsciiCharacters()
});
}

/**
* @eris-ratio 10
*/
public function testMultiplePrintableCharacters()
{
$this
->minimumEvaluationRatio(0.1)
->forAll(
Generator\char(['basic-latin']),
Generator\char(['basic-latin'])
Expand Down
14 changes: 9 additions & 5 deletions examples/LimitToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ class LimitToTest extends PHPUnit_Framework_TestCase
{
use TestTrait;

/**
* @eris-repeat 5
*/
public function testNumberOfIterationsCanBeConfigured()
{
$this->limitTo(5)
->forAll(
$this->forAll(
Generator\int()
)
->then(function ($value) {
Expand All @@ -33,11 +35,13 @@ public function testTimeIntervalToRunForCanBeConfiguredButItNeedsToProduceAtLeas
}
*/

/**
* @eris-ratio 0
* @eris-duration PT2S
*/
public function testTimeIntervalToRunForCanBeConfiguredAndAVeryLowNumberOfIterationsCanBeIgnored()
{
$this->minimumEvaluationRatio(0.0)
->limitTo(new DateInterval("PT2S"))
->forAll(
$this->forAll(
Generator\int()
)
->then(function ($value) {
Expand Down
4 changes: 2 additions & 2 deletions examples/LogFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function testWritingIterationsOnALogFile()
->forAll(
Generator\int()
)
->hook(Listener\log('/tmp/eris-log-file-test.log'))
->hook(Listener\log(sys_get_temp_dir().'/eris-log-file-test.log'))
->then(function ($number) {
$this->assertInternalType('integer', $number);
});
Expand All @@ -25,7 +25,7 @@ public function testLogOfFailuresAndShrinking()
->forAll(
Generator\int()
)
->hook(Listener\log('/tmp/eris-log-file-shrinking.log'))
->hook(Listener\log(sys_get_temp_dir().'/eris-log-file-shrinking.log'))
->then(function ($number) {
$this->assertLessThanOrEqual(42, $number);
});
Expand Down
4 changes: 3 additions & 1 deletion examples/MinimumEvaluationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ public function testFailsBecauseOfTheLowEvaluationRatio()
});
}

/**
* @eris-ratio 1
*/
public function testPassesBecauseOfTheArtificiallyLowMinimumEvaluationRatio()
{
$this
->minimumEvaluationRatio(0.01)
->forAll(
Generator\choose(0, 100)
)
Expand Down
8 changes: 6 additions & 2 deletions examples/RandConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,25 @@ class RandConfigurationTest extends PHPUnit_Framework_TestCase
{
use TestTrait;

/**
* @eris-method rand
*/
public function testUsingTheDefaultRandFunction()
{
$this
->withRand('rand')
->forAll(
Generator\int()
)
->withMaxSize(1000 * 1000* 1000)
->then($this->isInteger());
}

/**
* @eris-method mt_rand
*/
public function testUsingTheDefaultMtRandFunction()
{
$this
->withRand('mt_rand')
->forAll(
Generator\int()
)
Expand Down
1 change: 0 additions & 1 deletion examples/SequenceTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php
use Eris\Generator;
use Eris\Listener;

class SequenceTest extends PHPUnit_Framework_TestCase
{
Expand Down
4 changes: 3 additions & 1 deletion examples/ShrinkingTimeLimitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ class ShrinkingTimeLimitTest extends PHPUnit_Framework_TestCase
{
use Eris\TestTrait;

/**
* @eris-shrink 2
*/
public function testLengthPreservation()
{
$this
->shrinkingTimeLimit(2)
->forAll(
Generator\string(),
Generator\string()
Expand Down
2 changes: 1 addition & 1 deletion examples/StringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testLengthPreservation()
Generator\string(),
Generator\string()
)
->hook(Listener\log('/tmp/eris-string-shrinking.log'))
->hook(Listener\log(sys_get_temp_dir().'/eris-string-shrinking.log'))
->then(function ($first, $second) {
$result = string_concatenation($first, $second);
$this->assertEquals(
Expand Down
2 changes: 1 addition & 1 deletion examples/SuchThatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function testSuchThatAcceptsPHPUnitConstraints()
)
)
)
->hook(Listener\log('/tmp/eris-such-that.log'))
->hook(Listener\log(sys_get_temp_dir().'/eris-such-that.log'))
->then($this->allNumbersAreBiggerThan(42));
}

Expand Down
9 changes: 9 additions & 0 deletions src/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,13 @@ public function __construct()
$this->erisSetupBeforeClass();
$this->erisSetup();
}

/**
* sadly this facade has no option to retrieve annotations of testcases
* @return array
*/
protected function getAnnotations()
{
return array();
}
}
4 changes: 2 additions & 2 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ interface Generator
{
/**
* @param int The generation size
* @param callable a rand() function
* @param Random\RandomRange
* @return GeneratedValueSingle<T>
*/
public function __invoke($size, $rand);
public function __invoke($size, Random\RandomRange $rand);

/**
* The conditions for terminating are either:
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/AssociativeArrayGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(array $generators)
$this->tupleGenerator = new TupleGenerator(array_values($generators));
}

public function __invoke($size, $rand)
public function __invoke($size, \Eris\Random\RandomRange $rand)
{
$tuple = $this->tupleGenerator->__invoke($size, $rand);
return $this->mapToAssociativeArray($tuple);
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/BindGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct($innerGenerator, $outerGeneratorFactory)
$this->outerGeneratorFactory = $outerGeneratorFactory;
}

public function __invoke($size, $rand)
public function __invoke($size, \Eris\Random\RandomRange $rand)
{
$innerGeneratorValue = $this->innerGenerator->__invoke($size, $rand);
$outerGenerator = call_user_func($this->outerGeneratorFactory, $innerGeneratorValue->unbox());
Expand Down
5 changes: 2 additions & 3 deletions src/Generator/BooleanGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
namespace Eris\Generator;

use Eris\Generator;
use DomainException;

function bool()
{
Expand All @@ -11,10 +10,10 @@ function bool()

class BooleanGenerator implements Generator
{
public function __invoke($_size, $rand)
public function __invoke($_size, \Eris\Random\RandomRange $rand)
{
$booleanValues = [true, false];
$randomIndex = $rand(0, count($booleanValues) - 1);
$randomIndex = $rand->rand(0, count($booleanValues) - 1);

return GeneratedValueSingle::fromJustValue($booleanValues[$randomIndex], 'boolean');
}
Expand Down
5 changes: 2 additions & 3 deletions src/Generator/CharacterGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,13 @@ public function __construct($lowerLimit, $upperLimit)
$this->shrinkingProgression = ArithmeticProgression::discrete($this->lowerLimit);
}

public function __invoke($_size, $rand)
public function __invoke($_size, \Eris\Random\RandomRange $rand)
{
return GeneratedValueSingle::fromJustValue(chr($rand($this->lowerLimit, $this->upperLimit)), 'character');
return GeneratedValueSingle::fromJustValue(chr($rand->rand($this->lowerLimit, $this->upperLimit)), 'character');
}

public function shrink(GeneratedValueSingle $element)
{
$codePoint = ord($element->unbox());
$shrinkedValue = chr($this->shrinkingProgression->next(ord($element->unbox())));
return GeneratedValueSingle::fromJustValue($shrinkedValue, 'character');
}
Expand Down
5 changes: 2 additions & 3 deletions src/Generator/ChooseGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use Eris\Generator;
use InvalidArgumentException;
use DomainException;

if (!defined('ERIS_PHP_INT_MIN')) {
define('ERIS_PHP_INT_MIN', ~PHP_INT_MAX);
Expand Down Expand Up @@ -42,9 +41,9 @@ public function __construct($x, $y)
);
}

public function __invoke($_size, $rand)
public function __invoke($_size, \Eris\Random\RandomRange $rand)
{
$value = $rand($this->lowerLimit, $this->upperLimit);
$value = $rand->rand($this->lowerLimit, $this->upperLimit);

return GeneratedValueSingle::fromJustValue($value, 'choose');
}
Expand Down
3 changes: 1 addition & 2 deletions src/Generator/ConstantGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
namespace Eris\Generator;

use Eris\Generator;
use DomainException;

/**
* @param mixed $value the only value to generate
Expand All @@ -27,7 +26,7 @@ public function __construct($value)
$this->value = $value;
}

public function __invoke($_size, $rand)
public function __invoke($_size, \Eris\Random\RandomRange $rand)
{
return GeneratedValueSingle::fromJustValue($this->value, 'constant');
}
Expand Down
5 changes: 2 additions & 3 deletions src/Generator/DateGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use Eris\Generator;
use DateTime;
use DomainException;

function date($lowerLimit = null, $upperLimit = null)
{
Expand Down Expand Up @@ -42,9 +41,9 @@ public function __construct(DateTime $lowerLimit, DateTime $upperLimit)
$this->intervalInSeconds = $upperLimit->getTimestamp() - $lowerLimit->getTimestamp();
}

public function __invoke($_size, $rand)
public function __invoke($_size, \Eris\Random\RandomRange $rand)
{
$generatedOffset = $rand(0, $this->intervalInSeconds);
$generatedOffset = $rand->rand(0, $this->intervalInSeconds);
return GeneratedValueSingle::fromJustValue(
$this->fromOffset($generatedOffset),
'date'
Expand Down
4 changes: 2 additions & 2 deletions src/Generator/ElementsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ private function __construct($domain)
$this->domain = $domain;
}

public function __invoke($_size, $rand)
public function __invoke($_size, \Eris\Random\RandomRange $rand)
{
$index = $rand(0, count($this->domain) - 1);
$index = $rand->rand(0, count($this->domain) - 1);
return GeneratedValueSingle::fromJustValue($this->domain[$index], 'elements');
}

Expand Down
9 changes: 4 additions & 5 deletions src/Generator/FloatGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
namespace Eris\Generator;

use Eris\Generator;
use DomainException;

function float()
{
Expand All @@ -15,13 +14,13 @@ public function __construct()
{
}

public function __invoke($size, $rand)
public function __invoke($size, \Eris\Random\RandomRange $rand)
{
$denominator = $rand(1, $size) ?: 1;
$denominator = $rand->rand(1, $size) ?: 1;

$value = (float) $rand(0, $size) / (float) $denominator;
$value = (float) $rand->rand(0, $size) / (float) $denominator;

$signedValue = $rand(0, 1) === 0
$signedValue = $rand->rand(0, 1) === 0
? $value
: $value * (-1);
return GeneratedValueSingle::fromJustValue($signedValue, 'float');
Expand Down
Loading

0 comments on commit f5ffcdc

Please sign in to comment.