Skip to content

Commit

Permalink
pick test fixtures at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
rnbguy committed Nov 24, 2023
1 parent a4e8d3d commit 996007e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 47 deletions.
21 changes: 13 additions & 8 deletions Code/itf/tests/consensus.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
use glob::glob;

use malachite_itf::consensus::State;
use rstest::rstest;
use std::path::PathBuf;

#[rstest]
fn test_itf(#[files("tests/fixtures/consensus/*.json")] json_fixture: PathBuf) {
println!("Parsing {json_fixture:?}");
#[test]
fn test_itf() {
for json_fixture in glob("tests/fixtures/consensus/*.json")
.expect("Failed to read glob pattern")
.flatten()
{
println!("Parsing {json_fixture:?}");

let json = std::fs::read_to_string(&json_fixture).unwrap();
let state = itf::trace_from_str::<State>(&json).unwrap();
let json = std::fs::read_to_string(&json_fixture).unwrap();
let state = itf::trace_from_str::<State>(&json).unwrap();

dbg!(state);
dbg!(state);
}
}
52 changes: 36 additions & 16 deletions Code/itf/tests/votekeeper.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
use std::path::PathBuf;

use malachite_itf::votekeeper::State;

use rstest::rstest;

#[path = "votekeeper/runner.rs"]
pub mod runner;
#[path = "votekeeper/utils.rs"]
pub mod utils;

use runner::{vote_keeper_runner, VoteKeeperRunner};
use glob::glob;
use rand::rngs::StdRng;
use rand::SeedableRng;

use malachite_itf::votekeeper::State;
use malachite_test::{Address, PrivateKey};

use runner::VoteKeeperRunner;
use utils::ADDRESSES;

const RANDOM_SEED: u64 = 0x42;

#[test]
fn test_itf() {
for json_fixture in glob("tests/fixtures/votekeeper/*.itf.json")
.expect("Failed to read glob pattern")
.flatten()
{
println!("Parsing {json_fixture:?}");

let json = std::fs::read_to_string(&json_fixture).unwrap();
let trace = itf::trace_from_str::<State>(&json).unwrap();

let mut rng = StdRng::seed_from_u64(RANDOM_SEED);

#[rstest]
fn test_itf(
#[files("tests/fixtures/votekeeper/*.itf.json")] json_fixture: PathBuf,
vote_keeper_runner: VoteKeeperRunner,
) {
println!("Parsing {json_fixture:?}");
// build mapping from model addresses to real addresses
let vote_keeper_runner = VoteKeeperRunner {
address_map: ADDRESSES
.iter()
.map(|&name| {
let pk = PrivateKey::generate(&mut rng).public_key();
(name.into(), Address::from_public_key(&pk))
})
.collect(),
};

let json = std::fs::read_to_string(&json_fixture).unwrap();
let trace = itf::trace_from_str::<State>(&json).unwrap();
trace.run_on(vote_keeper_runner).unwrap();
trace.run_on(vote_keeper_runner).unwrap();
}
}
26 changes: 3 additions & 23 deletions Code/itf/tests/votekeeper/runner.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
use std::collections::HashMap;

use rand::rngs::StdRng;
use rand::SeedableRng;

use malachite_common::{Context, Round, Value};
use malachite_itf::votekeeper::State;
use malachite_test::{Address, Height, PrivateKey, TestContext, Vote};
use malachite_test::{Address, Height, TestContext, Vote};
use malachite_vote::{
keeper::{Message, VoteKeeper},
ThresholdParams,
};

use itf::Runner as ItfRunner;
use rstest::fixture;

use super::utils::{check_votes, value_from_model, ADDRESSES};
use super::utils::{check_votes, value_from_model};

pub struct VoteKeeperRunner {
address_map: HashMap<String, Address>,
pub address_map: HashMap<String, Address>,
}

impl ItfRunner for VoteKeeperRunner {
Expand Down Expand Up @@ -181,19 +177,3 @@ impl ItfRunner for VoteKeeperRunner {
Ok(true)
}
}

#[fixture]
pub fn vote_keeper_runner() -> VoteKeeperRunner {
let mut rng = StdRng::seed_from_u64(0x42);

// build mapping from model addresses to real addresses
VoteKeeperRunner {
address_map: ADDRESSES
.iter()
.map(|&name| {
let pk = PrivateKey::generate(&mut rng).public_key();
(name.into(), Address::from_public_key(&pk))
})
.collect(),
}
}

0 comments on commit 996007e

Please sign in to comment.