Skip to content

Commit

Permalink
tests: add a compare function that clones the TestEnvironment
Browse files Browse the repository at this point in the history
  • Loading branch information
csgui committed Feb 6, 2024
1 parent 1de927d commit 17f3f26
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 66 deletions.
70 changes: 9 additions & 61 deletions clar2wasm/src/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use clarity::vm::{eval_all, ClarityVersion, ContractContext, Value};
use crate::compile;
use crate::datastore::{BurnDatastore, Datastore, StacksConstants};

#[derive(Clone)]
pub struct TestEnvironment {
contract_contexts: HashMap<String, ContractContext>,
epoch: StacksEpochId,
Expand Down Expand Up @@ -263,35 +264,6 @@ pub fn evaluate(snippet: &str) -> Result<Option<Value>, ()> {
evaluate_at(snippet, StacksEpochId::latest(), ClarityVersion::latest()).map_err(|_| ())
}

/// Evaluate a Clarity snippet at the latest epoch and clarity version,
/// advancing the chain tip `count` time.
/// Returns an optional value -- the result of the evaluation.
#[allow(clippy::result_unit_err)]
pub fn evaluate_advancing_tip(snippet: &str, count: u32) -> Result<Option<Value>, ()> {
evaluate_advancing_tip_at(
snippet,
StacksEpochId::latest(),
ClarityVersion::latest(),
count,
)
.map_err(|_| ())
}

/// Evaluate a Clarity snippet at a specific epoch and version,
/// advancing the chain tip `count` times.
/// Returns an optional value -- the result of the evaluation.
pub fn evaluate_advancing_tip_at(
snippet: &str,
epoch: StacksEpochId,
version: ClarityVersion,
count: u32,
) -> Result<Option<Value>, Error> {
let mut env = TestEnvironment::new(epoch, version);
env.advance_chain_tip(count);

env.evaluate(snippet)
}

/// Interpret a Clarity snippet at a specific epoch and version.
/// Returns an optional value -- the result of the evaluation.
pub fn interpret_at(
Expand All @@ -310,35 +282,6 @@ pub fn interpret(snippet: &str) -> Result<Option<Value>, ()> {
interpret_at(snippet, StacksEpochId::latest(), ClarityVersion::latest()).map_err(|_| ())
}

/// Interpret a Clarity snippet at a specific epoch and version,
/// advancing the chain tip `count` times.
/// Returns an optional value -- the result of the evaluation.
pub fn interpret_advancing_tip_at(
snippet: &str,
epoch: StacksEpochId,
version: ClarityVersion,
count: u32,
) -> Result<Option<Value>, Error> {
let mut env = TestEnvironment::new(epoch, version);
env.advance_chain_tip(count);

env.interpret(snippet)
}

/// Interprets a Clarity snippet at the latest epoch and clarity version,
/// advancing the chain tip `count` times.
/// Returns an optional value -- the result of the evaluation.
#[allow(clippy::result_unit_err)]
pub fn interpret_advancing_tip(snippet: &str, count: u32) -> Result<Option<Value>, ()> {
interpret_advancing_tip_at(
snippet,
StacksEpochId::latest(),
ClarityVersion::latest(),
count,
)
.map_err(|_| ())
}

pub fn crosscheck(snippet: &str, expected: Result<Option<Value>, ()>) {
let compiled = evaluate_at(snippet, StacksEpochId::latest(), ClarityVersion::latest());
let interpreted = interpret(snippet);
Expand Down Expand Up @@ -366,9 +309,14 @@ pub fn crosscheck_compare_only(snippet: &str) {
crosscheck_compare_assertion(snippet, compiled, interpreted);
}

pub fn crosscheck_compare_advancing_tip_by(snippet: &str, count: u32) {
let compiled = evaluate_advancing_tip(snippet, count);
let interpreted = interpret_advancing_tip(snippet, count);
pub fn crosscheck_compare_only_advancing_tip(snippet: &str, count: u32) {
let mut compiler_env = TestEnvironment::new(StacksEpochId::latest(), ClarityVersion::latest());
compiler_env.advance_chain_tip(count);

let mut interpreter_env = compiler_env.clone();

let compiled = compiler_env.evaluate(snippet).map_err(|_| ());
let interpreted = interpreter_env.interpret(snippet).map_err(|_| ());

crosscheck_compare_assertion(snippet, compiled, interpreted);
}
Expand Down
8 changes: 3 additions & 5 deletions clar2wasm/tests/wasm-generation/blockinfo.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clar2wasm::tools::{crosscheck_compare_advancing_tip_by, crosscheck_compare_only};
use clar2wasm::tools::{crosscheck_compare_only, crosscheck_compare_only_advancing_tip};
use proptest::proptest;

use crate::uint;
Expand All @@ -23,9 +23,7 @@ proptest! {
#[test]
fn crossprop_blockinfo_within_controlled_range(block_height in 1..=STACKS_BLOCK_HEIGHT_LIMIT) {
for info in &BLOCK_INFO {
crosscheck_compare_advancing_tip_by(
&format!("(get-block-info? {info} u{block_height})"), 80
)
crosscheck_compare_only_advancing_tip(&format!("(get-block-info? {info} u{block_height})"), 80)
}
}
}
Expand All @@ -49,7 +47,7 @@ proptest! {
# [test]
fn crossprop_blockinfo_burnchain_within_controlled_range(block_height in 1..=BURN_BLOCK_HEIGHT_LIMIT) {
for info in &BURN_BLOCK_INFO {
crosscheck_compare_advancing_tip_by(
crosscheck_compare_only_advancing_tip(
&format!("(get-burn-block-info? {info} u{block_height})"), 80
)
}
Expand Down

0 comments on commit 17f3f26

Please sign in to comment.