Skip to content

Commit

Permalink
updates to 021_cef_cvotes.t (WIP), +3 test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ikluft committed Sep 4, 2023
1 parent d2a7e21 commit 405f76d
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 34 deletions.
79 changes: 45 additions & 34 deletions src/perl/prefvote/t/021_cef_cvotes.t
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,59 @@ use PrefVote::Core::Input;
Readonly::Scalar my $debug_mode => ( ( $ENV{PREFVOTE_DEBUG} // 0 ) or ( $ENV{CEF_PARSER_DEBUG} // 0 ) ) and 1;
Readonly::Scalar my $input_dir => getcwd() . "/t/test-inputs/" . basename( $0, ".t" );
Readonly::Scalar my $tests_per_good_file => 1;
Readonly::Scalar my $tests_per_bad_file => 1;
Readonly::Scalar my $tests_per_bad_file => 2;

# compute number of tests: (test case data are read from 000-test-metadata.yml)
# 1 test per good or bad file, but preserve option to change multipliers per type
sub count_tests
{
my ( $metadata, @files ) = @_;

# count files by good and bad CEF syntax
my $good_total = 0;
my $bad_total = 0;
foreach my $file (@files) {
my $test_case = {};
if ( $metadata and exists $metadata->{$file} ) {
if ( ref $metadata->{$file} eq "HASH" ) {
$test_case = $metadata->{$file};
}
}
if ( exists $test_case->{error} ) {
$bad_total++;
} else {
$good_total++;
}
}
return $tests_per_bad_file * $bad_total + $tests_per_good_file * $good_total;
}

# run tests per CEF test file
sub cef_file_tests
{
my $filepath = shift;
my $flags = shift;
my $filepath = shift;
my $test_case = shift;
my $test_group = shift;
$debug_mode and say STDERR "debug: cef_file_tests($filepath)";

# use file basename as test name
my $test_name = basename($filepath);

# check flags: if neither good or bad are set, default to good
if ( not exists $flags->{bad} and not exists $flags->{good} ) {
$flags->{good} = 1; # if not bad, add good flag so it shows up on the flag summary string
}
my $flag_str = join " ", sort keys %$flags;
# stringify test case data for test name
my $flag_str = join " ", sort keys %$test_case;

# run tests
my $input_doc;
if ( $flags->{good} // 0 ) {
if ( not exists $test_case->{error} ) {
lives_ok( sub { $input_doc = PrefVote::Core::Input->new( filepath => $filepath ); },
"$test_name good as expected" );
"$test_group: $test_name / good as expected" );
} else {
my $err_regex = $test_case->{error};
dies_ok( sub { $input_doc = PrefVote::Core::Input->new( filepath => $filepath ); },
"$test_name bad as expected" );
"$test_group: $test_name / bad as expected" );
my $err_result = $@;
$debug_mode and say STDERR "$test_group: $test_name / result: error $err_result";
like( $err_result, qr($err_regex), "$test_group: $test_name / expected error: $err_regex" );
}
}

Expand All @@ -63,34 +90,18 @@ if ( ref $test_metadata[0] eq "HASH" ) {
$metadata = $test_metadata[0];
}

# count files by good and bad CEF syntax
my $good_total = 0;
my $bad_total = 0;
foreach my $file (@files) {
my $flags = {};
if ( $metadata and exists $metadata->{$file} ) {
if ( ref $metadata->{$file} eq "HASH" ) {
$flags = $metadata->{$file};
}
}
if ( exists $flags->{bad} ) {
$bad_total++;
} else {
$good_total++;
}
}

# compute number of tests: (flags are read from 000-test-metadata.yml)
# 1 test per good or bad file, but preserve option to change multipliers per type
plan tests => $tests_per_bad_file * $bad_total + $tests_per_good_file * $good_total;
# declare test count
plan tests => count_tests( $metadata, @files );

# run cef_file_tests() for each file
my $test_group = 1;
foreach my $file (@files) {
my $flags = {};
my $test_case = {};
if ( $metadata and exists $metadata->{$file} ) {
if ( ref $metadata->{$file} eq "HASH" ) {
$flags = $metadata->{$file};
$test_case = $metadata->{$file};
}
}
cef_file_tests( "$input_dir/$file", $flags );
cef_file_tests( "$input_dir/$file", $test_case, $test_group );
$test_group++;
}
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
---
boudry-test1.cvotes:
error: "^weight not permitted without weight_allowed flag"
boudry-test2.cvotes:
error: "^weight not permitted without weight_allowed flag"
cef-doc-example1.cvotes:
error: "^weight not permitted without weight_allowed flag"
cef-doc-example2.cvotes:
error: "^weight not permitted without weight_allowed flag"
cef-doc-invalid3.cvotes:
error: "^Syntax error at position 12, expected EMPTY_RANKING INT WORD"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# imported from https://github.com/julien-boudry/Condorcet/Tests/src/Tools/Converters/CondorcetElectionFormatData/
#/Candidates:A;B;C
#/Implicit Ranking: true
#/Weight Allowed: true

A>B ^2
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# imported from https://github.com/julien-boudry/Condorcet/Tests/src/Tools/Converters/CondorcetElectionFormatData/
#/Candidates:A;B;C
#/Implicit Ranking: false
#/Weight Allowed: true
#Number of Seats: 42

B>A ^2
B>C
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# My beautiful election
#/Candidates: Candidate A;Candidate B;Candidate C

Candidate A < Candidate B > Candidate C

0 comments on commit 405f76d

Please sign in to comment.