diff --git a/examples/dns/Cargo.toml b/examples/dns/Cargo.toml index 31a0c760a3b..b4eefb02f37 100644 --- a/examples/dns/Cargo.toml +++ b/examples/dns/Cargo.toml @@ -31,3 +31,4 @@ std = [ "scale-info/std", ] ink-as-dependency = [] +ink-experimental-engine = ["ink_env/ink-experimental-engine"] diff --git a/examples/dns/lib.rs b/examples/dns/lib.rs index 7dfe29cd1eb..79aef0ce318 100644 --- a/examples/dns/lib.rs +++ b/examples/dns/lib.rs @@ -176,6 +176,7 @@ mod dns { } } + #[cfg(not(feature = "ink-experimental-engine"))] #[cfg(test)] mod tests { use super::*; @@ -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::test::default_accounts::() + } + + fn set_next_caller(caller: AccountId) { + ink_env::test::set_caller::(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); + } + } }