Skip to content

Commit

Permalink
Pintufy tests
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-boudry committed May 28, 2024
1 parent 109829a commit e645d20
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 19 deletions.
2 changes: 1 addition & 1 deletion tests/DriversTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use PHPUnit\Framework\Attributes\RequiresPhpExtension;

#[RequiresPhpExtension('pdo_sqlite')]
class DriversTestCase extends CondorcetTestCase
abstract class DriversTestCase extends CondorcetTestCase
{
protected function hashVotesList(Election $elec): string
{
Expand Down
18 changes: 12 additions & 6 deletions tests/src/Console/Commands/ConverterCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,24 @@
use Symfony\Component\Console\Tester\CommandTester;
use Tests\src\Console\ConsoleTestCase;

beforeEach(function () {
beforeEach(function (): void {
CondorcetApplication::create();

$this->converterCommand = new CommandTester(CondorcetApplication::$SymfonyConsoleApplication->find('convert'));
});
afterEach(function () {

afterEach(function (): void {
file_exists(self::OUTPUT_FILE) && unlink(self::OUTPUT_FILE);
});

dataset('conversionsProvider', function () {
return [
'fromDebianToCondorcet' => ['--from-debian-format', '--to-condorcet-election-format', ConsoleTestCase::DEBIAN_INPUT_FILE, __DIR__ . '/files/fromDebianExpectedFile.cvotes'],
'fromDavidHillToCondorcet' => ['--from-david-hill-format', '--to-condorcet-election-format', ConsoleTestCase::DAVIDHILL_INPUT_FILE, __DIR__ . '/files/fromDavidHillExpectedFile.cvotes'],
];
});
test('successfull conversions', function (string $from, string $to, string $input, string $comparaison) {

test('successfull conversions', function (string $from, string $to, string $input, string $comparaison): void {
$this->converterCommand->execute(
[
$from => true,
Expand All @@ -42,7 +45,8 @@

expect(file_get_contents(self::OUTPUT_FILE))->toBe(file_get_contents($comparaison));
})->with('conversionsProvider');
test('lacks an option', function () {

test('lacks an option', function (): void {
$this->expectException(ConsoleInputException::class);
$this->expectExceptionMessageMatches('/output/');

Expand All @@ -58,7 +62,8 @@
]
);
});
test('lacks an argument', function () {

test('lacks an argument', function (): void {
$this->expectException(ConsoleInputException::class);
$this->expectExceptionMessageMatches('/output/');

Expand All @@ -74,7 +79,8 @@
]
);
});
test('wrong input', function () {

test('wrong input', function (): void {
$this->expectException(CondorcetInternalException::class);
$this->expectExceptionMessage('The input file does not exist');

Expand Down
2 changes: 1 addition & 1 deletion tests/src/Console/ConsoleTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Tests\CondorcetTestCase;

class ConsoleTestCase extends CondorcetTestCase
abstract class ConsoleTestCase extends CondorcetTestCase
{
public const DEBIAN_INPUT_FILE = __DIR__ . '/../Tools/Converters/DebianData/leader2020_tally.txt';
public const DAVIDHILL_INPUT_FILE = __DIR__ . '/../Tools/Converters/TidemanData/A77.HIL';
Expand Down
2 changes: 1 addition & 1 deletion tests/src/ElectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@
);

unserialize(
file_get_contents(__DIR__.'/ElectionData/serialized_election_v3.2.0.txt')
file_get_contents(__DIR__ . '/ElectionData/serialized_election_v3.2.0.txt')
);
});

Expand Down
25 changes: 16 additions & 9 deletions tests/src/Tools/Randomizers/ArrayRandomizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use CondorcetPHP\Condorcet\Tools\Randomizers\ArrayRandomizer;

test('default random votes', function () {
test('default random votes', function (): void {
$votesRandomizer = new ArrayRandomizer(self::CANDIDATE_SET_1, self::SEED);

for ($i = 0; $i < 10; $i++) {
Expand All @@ -14,7 +14,8 @@
expect($nv)->toHaveCount(\count(self::CANDIDATE_SET_1));
}
});
test('max candidates ranked', function () {

test('max candidates ranked', function (): void {
$votesRandomizer = new ArrayRandomizer(self::CANDIDATE_SET_1, self::SEED);
$votesRandomizer->maxCandidatesRanked = 3;

Expand All @@ -27,7 +28,8 @@
expect($nv)->toHaveCount($votesRandomizer->maxCandidatesRanked);
}
});
test('min candidates ranked', function () {

test('min candidates ranked', function (): void {
$votesRandomizer = new ArrayRandomizer(self::CANDIDATE_SET_1, self::SEED);
$votesRandomizer->minCandidatesRanked = 3;

Expand All @@ -47,7 +49,8 @@
$variationsCount = \count(array_unique($variations));
expect($variationsCount)->toBe(\count(self::CANDIDATE_SET_1) - $votesRandomizer->minCandidatesRanked + 1);
});
test('min and max candidates ranked', function () {

test('min and max candidates ranked', function (): void {
$votesRandomizer = new ArrayRandomizer(self::CANDIDATE_SET_1, self::SEED);
$votesRandomizer->minCandidatesRanked = 3;
$votesRandomizer->maxCandidatesRanked = 6;
Expand All @@ -67,7 +70,8 @@
$variationsCount = \count(array_unique($variations));
expect($variationsCount)->toBe($votesRandomizer->maxCandidatesRanked - $votesRandomizer->minCandidatesRanked + 1);
});
test('added ties', function () {

test('added ties', function (): void {
$votesRandomizer = new ArrayRandomizer(self::CANDIDATE_SET_2, self::SEED);

$votesRandomizer->tiesProbability = 100;
Expand All @@ -91,7 +95,8 @@
expect($nv)->toHaveCount(1);
expect($nv[0])->toHaveCount(3);
});
test('added ties with array1', function () {

test('added ties with array1', function (): void {
$votesRandomizer = new ArrayRandomizer(self::CANDIDATE_SET_3, self::SEED);

$votesRandomizer->tiesProbability = 100;
Expand All @@ -100,7 +105,8 @@
expect($nv)->toHaveCount(3);
expect($nv[2])->toHaveCount(2);
});
test('added ties with array2', function () {

test('added ties with array2', function (): void {
$votesRandomizer = new ArrayRandomizer(self::CANDIDATE_SET_1, self::SEED);

$votesRandomizer->tiesProbability = 500;
Expand All @@ -109,7 +115,8 @@
expect($nv)->toHaveCount(4);
expect($nv[2])->toHaveCount(6);
});
test('seeds', function () {

test('seeds', function (): void {
// Test low seed
$votesRandomizer = new ArrayRandomizer(self::CANDIDATE_SET_1, '42');
expect($votesRandomizer->shuffle()[0])->toBe(self::CANDIDATE_SET_1[5]);
Expand All @@ -121,7 +128,7 @@
expect($votesRandomizer->shuffle()[0])->toBe(self::CANDIDATE_SET_1[6]);

// Test custom Randomizer
$r = new \Random\Randomizer(new \Random\Engine\PcgOneseq128XslRr64('abcdefghijklmnop'));
$r = new Random\Randomizer(new Random\Engine\PcgOneseq128XslRr64('abcdefghijklmnop'));
$votesRandomizer = new ArrayRandomizer(self::CANDIDATE_SET_1, $r);
expect($votesRandomizer->shuffle()[0])->toBe(self::CANDIDATE_SET_1[4]);

Expand Down
2 changes: 1 addition & 1 deletion tests/src/Tools/Randomizers/RandomizerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Tests\CondorcetTestCase;

class RandomizerTestCase extends CondorcetTestCase
abstract class RandomizerTestCase extends CondorcetTestCase
{
public const SEED = 'CondorcetSeed';

Expand Down

0 comments on commit e645d20

Please sign in to comment.