git.delta.rocks / unique-network / refs/commits / d339ca36aeb2

difftreelog

source

tests/flipper-src/lib.rs2.4 KiBsourcehistory
1#![cfg_attr(not(feature = "std"), no_std)]23use ink_lang as ink;45#[ink::contract(version = "0.1.0")]6mod flipper {7    use ink_core::storage;89    /// Defines the storage of your contract.10    /// Add new fields to the below struct in order11    /// to add new static storage fields to your contract.12    #[ink(storage)]13    struct Flipper {14        /// Stores a single `bool` value on the storage.15        value: storage::Value<bool>,16    }1718    impl Flipper {19        /// Constructor that initializes the `bool` value to the given `init_value`.20        #[ink(constructor)]21        fn new(&mut self, init_value: bool) {22            self.value.set(init_value);23        }2425        /// Constructor that initializes the `bool` value to `false`.26        ///27        /// Constructors can delegate to other constructors.28        #[ink(constructor)]29        fn default(&mut self) {30            self.new(false)31        }3233        /// A message that can be called on instantiated contracts.34        /// This one flips the value of the stored `bool` from `true`35        /// to `false` and vice versa.36        #[ink(message)]37        fn flip(&mut self) {38            *self.value = !self.get();39        }4041        /// Simply returns the current value of our `bool`.42        #[ink(message)]43        fn get(&self) -> bool {44            *self.value45        }46    }4748    /// Unit tests in Rust are normally defined within such a `#[cfg(test)]`49    /// module and test functions are marked with a `#[test]` attribute.50    /// The below code is technically just normal Rust code.51    #[cfg(test)]52    mod tests {53        /// Imports all the definitions from the outer scope so we can use them here.54        use super::*;5556        /// We test if the default constructor does its job.57        #[test]58        fn default_works() {59            // Note that even though we defined our `#[ink(constructor)]`60            // above as `&mut self` functions that return nothing we can call61            // them in test code as if they were normal Rust constructors62            // that take no `self` argument but return `Self`.63            let flipper = Flipper::default();64            assert_eq!(flipper.get(), false);65        }6667        /// We test a simple use case of our contract.68        #[test]69        fn it_works() {70            let mut flipper = Flipper::new(false);71            assert_eq!(flipper.get(), false);72            flipper.flip();73            assert_eq!(flipper.get(), true);74        }75    }76}