Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a trace runner #9

Merged
merged 5 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## Unreleased

- Add a trace runner ([#9](https://github.com/informalsystems/itf-rs/pull/9))

## v0.2.0

*November 21st, 2023*
Expand Down
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/// Error type for the library.
#[derive(Debug)]
pub enum Error {
/// An error occured when deserializing the ITF-encoded JSON
/// An error occurred when deserializing the ITF-encoded JSON
Json(serde_json::Error),

/// An error occured when decoding an ITF value into a Rust value
/// An error occurred when decoding an ITF value into a Rust value
Decode(crate::de::Error),
}

Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ use serde::Deserialize;

pub mod de;
pub mod error;
pub mod runner;
pub mod state;
pub mod trace;

pub use error::Error;
pub use runner::Runner;
pub use state::State;
pub use trace::Trace;

Expand Down
58 changes: 58 additions & 0 deletions src/runner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use crate::Trace;

pub trait Runner {
type ActualState;
type Result;
type ExpectedState;
type Error;

fn init(&mut self, expected: &Self::ExpectedState) -> Result<Self::ActualState, Self::Error>;

fn step(
&mut self,
actual: &mut Self::ActualState,
expected: &Self::ExpectedState,
) -> Result<Self::Result, Self::Error>;

fn result_invariant(
&self,
result: &Self::Result,
expected: &Self::ExpectedState,
) -> Result<bool, Self::Error>;

fn state_invariant(
&self,
actual: &Self::ActualState,
expected: &Self::ExpectedState,
) -> Result<bool, Self::Error>;
}

impl<S> Trace<S> {
pub fn run_on<R, E>(&self, mut runner: R) -> Result<(), E>
where
R: Runner<ExpectedState = S, Error = E>,
{
if let Some(expected_init) = self.states.first() {
eprintln!("step: Initial");
let mut actual = runner.init(&expected_init.value)?;
assert!(
runner.state_invariant(&actual, &expected_init.value)?,
"State Invariant failed after Initialization"
);
for (i, expected) in self.states.iter().enumerate().skip(1) {
println!("step: {i}");
let result = runner.step(&mut actual, &expected.value)?;
assert!(
runner.result_invariant(&result, &expected.value)?,
"Result Invariant failed after step {i}",
);
assert!(
runner.state_invariant(&actual, &expected.value)?,
"State Invariant failed after step {i}",
);
}
}

Ok(())
}
}
2 changes: 1 addition & 1 deletion src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
use crate::error::Error;
use crate::value::Value;

/// Metada for an ITF [`State`].
/// Metadata for an ITF [`State`].
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Meta {
#[serde(default)]
Expand Down
4 changes: 2 additions & 2 deletions tests/regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ fn test_bigint_to_int() {

#[test]
fn test_deserialize_any() {
use itf::de::{As, Integer};
use itf::de::{As, Integer, Same};
use num_bigint::BigInt;
use std::collections::HashMap;

Expand Down Expand Up @@ -188,7 +188,7 @@ fn test_deserialize_any() {
_foo: HashMap<i64, BigInt>,
},
Bar {
#[serde(with = "As::<Vec<Vec<(Integer, Integer)>>>")]
#[serde(with = "As::<Vec<Vec<(Same, Integer)>>>")]
_bar: Vec<Vec<(BigInt, u64)>>,
},
}
Expand Down
Loading