Skip to content

Commit

Permalink
Add odra-test crate (#319)
Browse files Browse the repository at this point in the history
* Add odra-test crate
* Update Cargo.toml in examples
* Update build_schema bin
* Exclude module_schema function from wasm
* update justfile
* update templates
  • Loading branch information
kpob authored Jan 17, 2024
1 parent f789039 commit 08cf0a8
Show file tree
Hide file tree
Showing 46 changed files with 123 additions and 93 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ members = [
"odra-macros",
"odra-casper/wasm-env",
"odra-casper/test-vm",
"odra-test",
"try-from-macro"
# needs a refactor
# "odra-casper/livenet"
Expand Down
3 changes: 3 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ odra-modules = { path = "../modules", default-features = false }
sha3 = { version = "0.10.6", default-features = false }

[dev-dependencies]
odra-test = { path = "../odra-test" }
hex = "0.4.3"

[[bin]]
name = "odra_examples_build_contract"
path = "bin/build_contract.rs"
test = false

[[bin]]
name = "odra_examples_build_schema"
path = "bin/build_schema.rs"
test = false

[profile.release]
codegen-units = 1
Expand Down
6 changes: 3 additions & 3 deletions examples/bin/build_schema.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use odra::contract_def::ContractBlueprint;

#[cfg(not(target_arch = "wasm32"))]
extern "Rust" {
fn module_schema() -> ContractBlueprint;
fn module_schema() -> odra::contract_def::ContractBlueprint;
}

#[cfg(not(target_arch = "wasm32"))]
fn main() {
let schema = unsafe { module_schema() };
println!("{:#?}", schema);
Expand Down
2 changes: 1 addition & 1 deletion examples/src/contracts/owned_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub mod tests {

pub fn setup() -> OwnedTokenHostRef {
OwnedTokenDeployer::init(
&odra::test_env(),
&odra_test::env(),
String::from(NAME),
String::from(SYMBOL),
DECIMALS,
Expand Down
2 changes: 1 addition & 1 deletion examples/src/contracts/tlw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ mod test {
const ONE_DAY_IN_SECONDS: u64 = 60 * 60 * 24;

fn setup() -> (TimeLockWalletHostRef, Address, Address) {
let test_env = odra::test_env();
let test_env = odra_test::env();
(
TimeLockWalletDeployer::init(&test_env, ONE_DAY_IN_SECONDS),
test_env.get_account(0),
Expand Down
4 changes: 2 additions & 2 deletions examples/src/contracts/token_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ mod test {
const DECIMALS: u8 = 10;

fn setup() -> TokenManagerHostRef {
let test_env = odra::test_env();
let test_env = odra_test::env();
let mut contract = TokenManagerDeployer::init(&test_env);

contract.add_token(String::from(PLASCOIN), DECIMALS, String::from(PLS));
Expand Down Expand Up @@ -123,7 +123,7 @@ mod test {

#[test]
fn many_tokens_works() {
let test_env = odra::test_env();
let test_env = odra_test::env();
let mut contract = TokenManagerDeployer::init(&test_env);
let (user, balance) = (test_env.get_account(0), 111.into());
for i in 0..20 {
Expand Down
4 changes: 2 additions & 2 deletions examples/src/features/access_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub mod test {

#[test]
fn deploy_works() {
let env = odra::test_env();
let env = odra_test::env();
let contract = MockModeratedDeployer::init(&env);
let admin = env.get_account(0);

Expand Down Expand Up @@ -253,7 +253,7 @@ pub mod test {
}

fn setup(add_moderator: bool) -> (MockModeratedHostRef, Address, Address, Address) {
let env = odra::test_env();
let env = odra_test::env();
let mut contract = MockModeratedDeployer::init(&env);
// given admin who is a moderator and two users that are not moderators.
let (admin, user1, user2) = (env.get_account(0), env.get_account(1), env.get_account(2));
Expand Down
2 changes: 1 addition & 1 deletion examples/src/features/cross_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mod tests {

#[test]
fn test_cross_calls() {
let test_env = odra::test_env();
let test_env = odra_test::env();
let math_engine_contract = MathEngineDeployer::init(&test_env);
let cross_contract =
CrossContractDeployer::init(&test_env, *math_engine_contract.address());
Expand Down
2 changes: 1 addition & 1 deletion examples/src/features/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ mod tests {

#[test]
fn test_party() {
let test_env = odra::test_env();
let test_env = odra_test::env();
let party_contract = PartyContractDeployer::init(&test_env);
test_env.emitted_event(
&party_contract.address(),
Expand Down
4 changes: 2 additions & 2 deletions examples/src/features/handling_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ mod tests {

#[test]
fn test_owner() {
let test_env = odra::test_env();
let test_env = odra_test::env();
let owner = test_env.get_account(0);
let not_an_owner = test_env.get_account(1);

Expand All @@ -66,7 +66,7 @@ mod tests {

#[test]
fn test_owner_error() {
let test_env = odra::test_env();
let test_env = odra_test::env();
let owner = test_env.get_account(0);
let not_an_owner = test_env.get_account(1);

Expand Down
2 changes: 1 addition & 1 deletion examples/src/features/host_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ mod tests {

#[test]
fn host_test() {
let test_env = odra::test_env();
let test_env = odra_test::env();
let host_contract = HostContractDeployer::init(&test_env, "HostContract".to_string());
assert_eq!(host_contract.name(), "HostContract".to_string());
}
Expand Down
2 changes: 1 addition & 1 deletion examples/src/features/module_nesting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ mod tests {

#[test]
fn nested_odra_types() {
let test_env = odra::test_env();
let test_env = odra_test::env();
let mut nested_odra_types = NestedOdraTypesContractDeployer::init(&test_env);

// Storage is not set
Expand Down
2 changes: 1 addition & 1 deletion examples/src/features/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod tests {

#[test]
fn test_modules() {
let test_env = odra::test_env();
let test_env = odra_test::env();
let modules_contract = ModulesContractDeployer::init(&test_env);
assert_eq!(modules_contract.add_using_module(), 8);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/src/features/native_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mod tests {

#[test]
fn test_modules() {
let test_env = odra::test_env();
let test_env = odra_test::env();
let mut my_contract = PublicWalletDeployer::init(&test_env);
assert_eq!(test_env.balance_of(&my_contract.address()), U512::zero());

Expand Down
8 changes: 4 additions & 4 deletions examples/src/features/pauseable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ mod test {

#[test]
fn pause_works() {
let test_env = odra::test_env();
let test_env = odra_test::env();
let mut contract = PauseableCounterDeployer::init(&test_env);
let caller = test_env.get_account(0);

Expand All @@ -67,7 +67,7 @@ mod test {

#[test]
fn increment_only_if_unpaused() {
let test_env = odra::test_env();
let test_env = odra_test::env();
let mut contract = PauseableCounterDeployer::init(&test_env);
contract.increment();
contract.pause();
Expand All @@ -81,15 +81,15 @@ mod test {

#[test]
fn cannot_unpause_unpaused() {
let test_env = odra::test_env();
let test_env = odra_test::env();
let mut contract = PauseableCounterDeployer::init(&test_env);

assert_eq!(contract.try_unpause().unwrap_err(), PausedRequired.into());
}

#[test]
fn cannot_pause_paused() {
let test_env = odra::test_env();
let test_env = odra_test::env();
let mut contract = PauseableCounterDeployer::init(&test_env);
contract.pause();
assert_eq!(contract.try_pause().unwrap_err(), UnpausedRequired.into());
Expand Down
6 changes: 3 additions & 3 deletions examples/src/features/reentrancy_guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ mod test {

#[test]
fn non_reentrant_function_can_be_called() {
let mut contract = ReentrancyMockDeployer::init(&odra::test_env());
let mut contract = ReentrancyMockDeployer::init(&odra_test::env());
assert_eq!(contract.get_value(), 0);
contract.non_reentrant_count();
assert_eq!(contract.get_value(), 1);
}

#[test]
fn ref_recursion_not_allowed() {
let mut contract = ReentrancyMockDeployer::init(&odra::test_env());
let mut contract = ReentrancyMockDeployer::init(&odra_test::env());
assert_eq!(
contract.try_count_ref_recursive(11).unwrap_err(),
odra::ExecutionError::ReentrantCall.into()
Expand All @@ -65,7 +65,7 @@ mod test {

#[test]
fn local_recursion_allowed() {
let mut contract = ReentrancyMockDeployer::init(&odra::test_env());
let mut contract = ReentrancyMockDeployer::init(&odra_test::env());
contract.count_local_recursive(11);
assert_eq!(contract.get_value(), 11);
}
Expand Down
4 changes: 2 additions & 2 deletions examples/src/features/signature_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ mod test {

#[test]
fn signature_verification_works() {
let test_env = odra::test_env();
let test_env = odra_test::env();
let message = "Message to be signed";
let message_bytes = Bytes::from(message.as_bytes());
let account = test_env.get_account(0);
Expand Down Expand Up @@ -59,7 +59,7 @@ mod test {
let public_key_decoded = hex::decode(public_key_hex).unwrap();
let (public_key, _) = PublicKey::from_bytes(public_key_decoded.as_slice()).unwrap();

let signature_verifier = SignatureVerifierDeployer::init(&odra::test_env());
let signature_verifier = SignatureVerifierDeployer::init(&odra_test::env());
assert!(signature_verifier.verify_signature(message_bytes, signature_bytes, public_key));
}
}
2 changes: 1 addition & 1 deletion examples/src/features/storage/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ mod tests {

#[test]
fn init_test() {
let test_env = odra::test_env();
let test_env = odra_test::env();
let mut dog_contract = DogContract3Deployer::init(&test_env, "Mantus".to_string());
assert_eq!(dog_contract.walks_amount(), 0);
assert_eq!(dog_contract.walks_total_length(), 0);
Expand Down
2 changes: 1 addition & 1 deletion examples/src/features/storage/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ mod tests {

#[test]
fn visit_test() {
let test_env = odra::test_env();
let test_env = odra_test::env();
let mut dog_contract = DogContract2Deployer::init(&test_env, "Mantus".to_string());
assert_eq!(dog_contract.visits("Kuba".to_string()), 0);
dog_contract.visit("Kuba".to_string());
Expand Down
2 changes: 1 addition & 1 deletion examples/src/features/storage/variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ mod tests {

#[test]
fn init_test() {
let test_env = odra::test_env();
let test_env = odra_test::env();
let dog_contract = DogContractDeployer::init(&test_env, true, 10, "Mantus".to_string());
assert!(dog_contract.barks());
assert_eq!(dog_contract.weight(), 10);
Expand Down
4 changes: 2 additions & 2 deletions examples/src/features/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ mod tests {
use odra::prelude::*;

#[test]
fn test_env() {
let test_env = odra::test_env();
fn env() {
let test_env = odra_test::env();
test_env.set_caller(test_env.get_account(0));
let testing_contract = TestingContractDeployer::init(&test_env, "MyContract".to_string());
let creator = testing_contract.created_by();
Expand Down
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ check-lint: clippy
cd odra-casper/proxy-caller && cargo fmt -- --check
cd modules && cargo fmt -- --check
cd examples && cargo fmt -- --check
cd examples && cargo check
cd examples && cargo check --target wasm32-unknown-unknown --bin odra_examples_build_contract

install-cargo-odra:
cargo install cargo-odra --locked
Expand Down
3 changes: 3 additions & 0 deletions modules/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ odra = { path = "../odra", version = "0.8.0", default-features = false }
casper-event-standard = "0.4.1"
hex = { version = "0.4.3", default-features = false }

[dev-dependencies]
odra-test = { path = "../odra-test", version = "0.8.0" }

[[bin]]
name = "odra_modules_build_contract"
path = "bin/build_contract.rs"
Expand Down
8 changes: 4 additions & 4 deletions modules/src/access/ownable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,17 +331,17 @@ mod test {
}

fn setup_ownable() -> (OwnableHostRef, Address) {
let env = odra::test_env();
let env = odra_test::env();
(OwnableDeployer::init(&env), env.get_account(0))
}

fn setup_ownable_2_step() -> (Ownable2StepHostRef, Address) {
let env = odra::test_env();
let env = odra_test::env();
(Ownable2StepDeployer::init(&env), env.get_account(0))
}

fn setup_renounceable() -> (Vec<RenounceableHostRef>, Address) {
let env = odra::test_env();
let env = odra_test::env();
let ownable = OwnableDeployer::init(&env);
let ownable_2_step = Ownable2StepDeployer::init(&env);
let renouncable_ref = RenounceableHostRef::new(*ownable.address(), env.clone());
Expand All @@ -354,7 +354,7 @@ mod test {
}

fn setup_owned() -> (HostEnv, OwnableHostRef, Ownable2StepHostRef, Address) {
let env = odra::test_env();
let env = odra_test::env();
let ownable = OwnableDeployer::init(&env);
let ownable_2_step = Ownable2StepDeployer::init(&env);
(env.clone(), ownable, ownable_2_step, env.get_account(0))
Expand Down
2 changes: 1 addition & 1 deletion modules/src/erc1155_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ mod tests {
}

fn setup() -> TokenEnv {
let env = odra::test_env();
let env = odra_test::env();

TokenEnv {
env: env.clone(),
Expand Down
2 changes: 1 addition & 1 deletion modules/src/erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ mod tests {
const INITIAL_SUPPLY: u32 = 10_000;

fn setup() -> (HostEnv, Erc20HostRef) {
let env = odra::test_env();
let env = odra_test::env();
(
env.clone(),
Erc20Deployer::init(
Expand Down
2 changes: 1 addition & 1 deletion modules/src/erc721_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ mod tests {
}

fn setup() -> TokenEnv {
let env = odra::test_env();
let env = odra_test::env();
TokenEnv {
env: env.clone(),
token: Erc721TokenDeployer::init(
Expand Down
2 changes: 1 addition & 1 deletion modules/src/wrapped_native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ mod tests {
Address,
U512
) {
let env = odra::test_env();
let env = odra_test::env();
let token = WrappedNativeTokenDeployer::init(&env);
let account_1 = env.get_account(0);
let account_1_balance = env.balance_of(&account_1);
Expand Down
3 changes: 3 additions & 0 deletions odra-macros/src/ast/blueprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct BlueprintModItem {
#[derive(syn_derive::ToTokens)]
struct SchemaFnItem {
no_mangle_attr: syn::Attribute,
not_wasm32_attr: syn::Attribute,
sig: syn::Signature,
#[syn(braced)]
brace_token: syn::token::Brace,
Expand All @@ -49,6 +50,7 @@ impl TryFrom<&'_ ModuleImplIR> for SchemaFnItem {

Ok(Self {
no_mangle_attr: utils::attr::no_mangle(),
not_wasm32_attr: utils::attr::not_wasm32(),
sig: parse_quote!(fn #ident_module_schema() -> #ty_blueprint),
brace_token: Default::default(),
expr: utils::expr::new_blueprint(&module.module_ident()?)
Expand All @@ -72,6 +74,7 @@ mod test {
use super::*;

#[no_mangle]
#[cfg(not(target_arch = "wasm32"))]
fn module_schema() -> odra::contract_def::ContractBlueprint {
odra::contract_def::ContractBlueprint::new::<Erc20>()
}
Expand Down
Loading

0 comments on commit 08cf0a8

Please sign in to comment.