Skip to content

Commit

Permalink
#414: more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-kuprianov committed Aug 6, 2020
1 parent a720487 commit 49ef36d
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 56 deletions.
41 changes: 1 addition & 40 deletions tendermint/tests/lite-model-based.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use std::{io};
use std::{fs, path::PathBuf};
use serde::Deserialize;
//use tempfile::tempdir;
use tendermint::{block::signed_header::SignedHeader, lite};
use tendermint::block::{Header};
use tendermint::lite::{TrustThresholdFraction, TrustedState};

mod utils;
use utils::{apalache::*, command::*, lite::*};
use utils::{*, apalache::*, command::*, lite::*};

type Trusted = lite::TrustedState<SignedHeader, Header>;

Expand Down Expand Up @@ -50,10 +48,6 @@ pub struct BlockVerdict {

const TEST_DIR: &str = "./tests/support/lite-model-based/";

fn read_file(dir: &str, file: &str) -> String {
fs::read_to_string(PathBuf::from(dir.to_owned() + file)).unwrap()
}

fn read_single_step_test(dir: &str, file: &str) -> SingleStepTestCase {
serde_json::from_str(read_file(dir, file).as_str()).unwrap()
}
Expand Down Expand Up @@ -107,39 +101,6 @@ fn single_step_test() {
run_single_step_test(&tc);
}

fn run_apalache_test(dir: &str, test: ApalacheTestCase) -> io::Result<CommandRun> {
let mut cmd = Command::new();
if let Some(timeout) = test.timeout {
cmd.program("timeout");
cmd.arg(&timeout.to_string());
cmd.arg("apalache-mc");
}
else {
cmd.program("apalache-mc");
}
cmd.arg("check");
cmd.arg_from_parts(vec!["--inv=", &test.test]);
if let Some(length) = test.length {
cmd.arg_from_parts(vec!["--length=", &length.to_string()]);
}
cmd.arg(&test.model);
if !dir.is_empty() {
cmd.current_dir(dir);
}
match cmd.spawn() {
Ok(run) => {
if run.status.success() {
Ok(run)
}
else {
Err(io::Error::new(io::ErrorKind::Interrupted, run.stdout.to_string()))
}
},
Err(e) => Err(e)
}
}


#[test]
fn apalache_test() {

Expand Down
2 changes: 1 addition & 1 deletion tendermint/tests/lite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tendermint::lite::{Requester, TrustThresholdFraction, TrustedState};
use tendermint::{block::signed_header::SignedHeader, evidence::Duration, lite, Hash, Time};

mod utils;
use utils::lite::*;
use utils::{*, lite::*};

#[derive(Deserialize, Clone, Debug)]
struct TestCases {
Expand Down
34 changes: 34 additions & 0 deletions tendermint/tests/utils/apalache.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use serde::Deserialize;
use std::{io};
use crate::utils::command::*;

#[derive(Deserialize, Clone, Debug)]
pub struct ApalacheTestBatch {
Expand All @@ -16,3 +18,35 @@ pub struct ApalacheTestCase {
pub length: Option<u64>,
pub timeout: Option<u64>,
}

pub fn run_apalache_test(dir: &str, test: ApalacheTestCase) -> io::Result<CommandRun> {
let mut cmd = Command::new();
if let Some(timeout) = test.timeout {
cmd.program("timeout");
cmd.arg(&timeout.to_string());
cmd.arg("apalache-mc");
}
else {
cmd.program("apalache-mc");
}
cmd.arg("check");
cmd.arg_from_parts(vec!["--inv=", &test.test]);
if let Some(length) = test.length {
cmd.arg_from_parts(vec!["--length=", &length.to_string()]);
}
cmd.arg(&test.model);
if !dir.is_empty() {
cmd.current_dir(dir);
}
match cmd.spawn() {
Ok(run) => {
if run.status.success() {
Ok(run)
}
else {
Err(io::Error::new(io::ErrorKind::Interrupted, run.stdout.to_string()))
}
},
Err(e) => Err(e)
}
}
16 changes: 1 addition & 15 deletions tendermint/tests/utils/lite.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anomaly::fail;
use async_trait::async_trait;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt::Debug;
use tendermint::block::Header;
Expand All @@ -10,20 +10,6 @@ use tendermint::{
block::signed_header::SignedHeader, evidence::Duration, lite, validator::Set, Time,
};

/// Test that a struct `T` can be:
///
/// - serialized to JSON
/// - parsed back from the serialized JSON of the previous step
/// - that the two parsed structs are equal according to their `PartialEq` impl
pub fn test_serialization_roundtrip<T>(obj: &T)
where
T: Debug + PartialEq + Serialize + DeserializeOwned,
{
let serialized = serde_json::to_string(obj).unwrap();
let parsed = serde_json::from_str(&serialized).unwrap();
assert_eq!(obj, &parsed);
}

#[derive(Deserialize, Clone, Debug)]
pub struct Initial {
pub signed_header: SignedHeader,
Expand Down
23 changes: 23 additions & 0 deletions tendermint/tests/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
use serde::{de::DeserializeOwned, Serialize};
use std::fmt::Debug;
use std::{fs, path::PathBuf};

pub mod apalache;
pub mod command;
pub mod lite;

/// Test that a struct `T` can be:
///
/// - serialized to JSON
/// - parsed back from the serialized JSON of the previous step
/// - that the two parsed structs are equal according to their `PartialEq` impl
pub fn test_serialization_roundtrip<T>(obj: &T)
where
T: Debug + PartialEq + Serialize + DeserializeOwned,
{
let serialized = serde_json::to_string(obj).unwrap();
let parsed = serde_json::from_str(&serialized).unwrap();
assert_eq!(obj, &parsed);
}

/// Read a file into a string
pub fn read_file(dir: &str, file: &str) -> String {
fs::read_to_string(PathBuf::from(dir.to_owned() + file)).unwrap()
}

0 comments on commit 49ef36d

Please sign in to comment.