Skip to content

Commit

Permalink
Make code generation deterministic so snapshot testing works
Browse files Browse the repository at this point in the history
  • Loading branch information
bugarela committed May 23, 2024
1 parent f4580da commit 3aec37b
Show file tree
Hide file tree
Showing 12 changed files with 330 additions and 321 deletions.
93 changes: 49 additions & 44 deletions src/test_generation/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,61 @@ use crate::types::{fallback_constructor, Context};
use itertools::Itertools;

pub fn translate_actions(ctx: Context) -> String {
let msgs = ctx.message_type_for_action.iter().map(|(action, ty)| {
if action == "instantiate" {
let msg_struct = ctx
.structs
.get("InstantiateMsg")
let msgs = ctx
.message_type_for_action
.iter()
// Sort items by name so that the generated code is deterministic
.sorted_by(|a, b| a.0.cmp(b.0))
.map(|(action, ty)| {
if action == "instantiate" {
let msg_struct = ctx
.structs
.get("InstantiateMsg")
.cloned()
.unwrap_or_default();
let msg_fields = msg_struct
.iter()
.map(|f| {
let body = init_value_for_type(&ctx, f.ty.clone());

format!("{}: {}", f.name, body)
})
.collect_vec();
let msg = format!("InstantiateMsg {{ {} }}", msg_fields.join(", "));
return translate_init(msg);
}
if action == "execute" || action == "instantiate" || action == "reply" {
return "".to_string();
}
let constructor = ctx
.constructors
.get(ty.as_str())
.cloned()
.unwrap_or_default();
let msg_fields = msg_struct
.unwrap_or_else(|| fallback_constructor(ty));

let nondet_picks = constructor
.fields
.iter()
.map(|f| {
let body = init_value_for_type(&ctx, f.ty.clone());
let body = type_conversion(
format!("nondet_picks.message_{}.clone().unwrap()", f.name),
f.ty.clone(),
);

format!("{}: {}", f.name, body)
format!(" let message_{} = {};", f.name, body)
})
.collect_vec();
let msg = format!("InstantiateMsg {{ {} }}", msg_fields.join(", "));
return translate_init(msg);
}
if action == "execute" || action == "instantiate" || action == "reply" {
return "".to_string();
}
let constructor = ctx
.constructors
.get(ty.as_str())
.cloned()
.unwrap_or_else(|| fallback_constructor(ty));

let nondet_picks = constructor
.fields
.iter()
.map(|f| {
let body = type_conversion(
format!("nondet_picks.message_{}.clone().unwrap()", f.name),
f.ty.clone(),
);

format!(" let message_{} = {};", f.name, body)
})
.collect_vec();

let fields = constructor
.fields
.iter()
.map(|f| format!("{}: message_{}", f.name, f.name))
.collect_vec()
.join(", ");
let msg = format!("{} {{ {} }}", constructor.name.replace('_', "::"), fields);

translate_action(action, msg, nondet_picks.clone())
});

let fields = constructor
.fields
.iter()
.map(|f| format!("{}: message_{}", f.name, f.name))
.collect_vec()
.join(", ");
let msg = format!("{} {{ {} }}", constructor.name.replace('_', "::"), fields);

translate_action(action, msg, nondet_picks.clone())
});

msgs.clone().join("\n")
}
Expand Down
30 changes: 17 additions & 13 deletions src/test_generation/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@ pub mod state_structs {
use itf::de::{self, As};
"
.to_string();
for (name, fields) in ctx.structs {
structs.push_str(
format_struct(
name,
fields
.iter()
.map(|f| (f.name.clone(), f.ty.clone()))
.collect_vec(),
false,
)
.as_str(),
);
}
ctx.structs
.iter()
// Sort items by name so that the generated code is deterministic
.sorted_by(|a, b| a.0.cmp(b.0))
.for_each(|(name, fields)| {
structs.push_str(
format_struct(
name.to_string(),
fields
.iter()
.map(|f| (f.name.clone(), f.ty.clone()))
.collect_vec(),
false,
)
.as_str(),
);
});
structs.push_str(
format_struct(
"ContractState".to_string(),
Expand Down
44 changes: 22 additions & 22 deletions tests/snapshots/integration_tests__ctf01.snap
Original file line number Diff line number Diff line change
Expand Up @@ -340,28 +340,6 @@ pub mod tests {
match action_taken.as_str() {


"withdraw_action" => {
let sender = Addr::unchecked(sender.unwrap());
let funds = funds_from_trace(amount, denom);

let message_ids = nondet_picks.message_ids.clone().unwrap().iter().map(|x| x.to_u64().unwrap().into()).collect();
let msg = ExecuteMsg::Withdraw { ids: message_ids };
println!("Message: {:?}", msg);
println!("Sender: {:?}", sender);
println!("Funds: {:?}", funds);

let res = app.execute_contract(
sender,
test_state.contract_addr.clone(),
&msg,
&funds,
);

compare_result(s.value.result.clone(), res)
}



"deposit_action" => {
let sender = Addr::unchecked(sender.unwrap());
let funds = funds_from_trace(amount, denom);
Expand Down Expand Up @@ -418,6 +396,28 @@ pub mod tests {
}



"withdraw_action" => {
let sender = Addr::unchecked(sender.unwrap());
let funds = funds_from_trace(amount, denom);

let message_ids = nondet_picks.message_ids.clone().unwrap().iter().map(|x| x.to_u64().unwrap().into()).collect();
let msg = ExecuteMsg::Withdraw { ids: message_ids };
println!("Message: {:?}", msg);
println!("Sender: {:?}", sender);
println!("Funds: {:?}", funds);

let res = app.execute_contract(
sender,
test_state.contract_addr.clone(),
&msg,
&funds,
);

compare_result(s.value.result.clone(), res)
}


_ => panic!("Invalid action taken"),
}
compare_state(&test_state, &app, &(s.value.clone()));
Expand Down
44 changes: 22 additions & 22 deletions tests/snapshots/integration_tests__ctf02.snap
Original file line number Diff line number Diff line change
Expand Up @@ -365,28 +365,6 @@ pub mod tests {
match action_taken.as_str() {


"unstake_action" => {
let sender = Addr::unchecked(sender.unwrap());
let funds = funds_from_trace(amount, denom);

let message_unlock_amount = nondet_picks.message_unlock_amount.clone().unwrap().to_u64().unwrap().into();
let msg = ExecuteMsg::Unstake { unlock_amount: message_unlock_amount };
println!("Message: {:?}", msg);
println!("Sender: {:?}", sender);
println!("Funds: {:?}", funds);

let res = app.execute_contract(
sender,
test_state.contract_addr.clone(),
&msg,
&funds,
);

compare_result(s.value.result.clone(), res)
}



"deposit_action" => {
let sender = Addr::unchecked(sender.unwrap());
let funds = funds_from_trace(amount, denom);
Expand Down Expand Up @@ -466,6 +444,28 @@ pub mod tests {



"unstake_action" => {
let sender = Addr::unchecked(sender.unwrap());
let funds = funds_from_trace(amount, denom);

let message_unlock_amount = nondet_picks.message_unlock_amount.clone().unwrap().to_u64().unwrap().into();
let msg = ExecuteMsg::Unstake { unlock_amount: message_unlock_amount };
println!("Message: {:?}", msg);
println!("Sender: {:?}", sender);
println!("Funds: {:?}", funds);

let res = app.execute_contract(
sender,
test_state.contract_addr.clone(),
&msg,
&funds,
);

compare_result(s.value.result.clone(), res)
}



"withdraw_action" => {
let sender = Addr::unchecked(sender.unwrap());
let funds = funds_from_trace(amount, denom);
Expand Down
Loading

0 comments on commit 3aec37b

Please sign in to comment.