Skip to content

Commit

Permalink
Add tests in experimental off-chain env for dns (#1156)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmichi authored Mar 2, 2022
1 parent ecfd20f commit 894e716
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/dns/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ std = [
"scale-info/std",
]
ink-as-dependency = []
ink-experimental-engine = ["ink_env/ink-experimental-engine"]
77 changes: 77 additions & 0 deletions examples/dns/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ mod dns {
}
}

#[cfg(not(feature = "ink-experimental-engine"))]
#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -261,4 +262,80 @@ mod dns {
assert_eq!(contract.get_address(name), accounts.bob);
}
}

#[cfg(feature = "ink-experimental-engine")]
#[cfg(test)]
mod tests_experimental_engine {
use super::*;
use ink_lang as ink;

fn default_accounts(
) -> ink_env::test::DefaultAccounts<ink_env::DefaultEnvironment> {
ink_env::test::default_accounts::<Environment>()
}

fn set_next_caller(caller: AccountId) {
ink_env::test::set_caller::<Environment>(caller);
}

#[ink::test]
fn register_works() {
let default_accounts = default_accounts();
let name = Hash::from([0x99; 32]);

set_next_caller(default_accounts.alice);
let mut contract = DomainNameService::new();

assert_eq!(contract.register(name), Ok(()));
assert_eq!(contract.register(name), Err(Error::NameAlreadyExists));
}

#[ink::test]
fn set_address_works() {
let accounts = default_accounts();
let name = Hash::from([0x99; 32]);

set_next_caller(accounts.alice);

let mut contract = DomainNameService::new();
assert_eq!(contract.register(name), Ok(()));

// Caller is not owner, `set_address` should fail.
set_next_caller(accounts.bob);
assert_eq!(
contract.set_address(name, accounts.bob),
Err(Error::CallerIsNotOwner)
);

// Caller is owner, set_address will be successful
set_next_caller(accounts.alice);
assert_eq!(contract.set_address(name, accounts.bob), Ok(()));
assert_eq!(contract.get_address(name), accounts.bob);
}

#[ink::test]
fn transfer_works() {
let accounts = default_accounts();
let name = Hash::from([0x99; 32]);

set_next_caller(accounts.alice);

let mut contract = DomainNameService::new();
assert_eq!(contract.register(name), Ok(()));

// Test transfer of owner.
assert_eq!(contract.transfer(name, accounts.bob), Ok(()));

// Owner is bob, alice `set_address` should fail.
assert_eq!(
contract.set_address(name, accounts.bob),
Err(Error::CallerIsNotOwner)
);

set_next_caller(accounts.bob);
// Now owner is bob, `set_address` should be successful.
assert_eq!(contract.set_address(name, accounts.bob), Ok(()));
assert_eq!(contract.get_address(name), accounts.bob);
}
}
}

0 comments on commit 894e716

Please sign in to comment.