// This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. // SPDX-License-Identifier: Apache-2.0 // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use sp_core::Get; use super::{new_test_ext, BlockNumberFor, Config, Pallet, Runtime}; use crate::{ assert_noop, assert_ok, parameter_types, storage::generator::StorageValue, Blake2_128Concat, }; #[test] fn storage_alias_works() { new_test_ext().execute_with(|| { #[crate::storage_alias] type GenericData2 = StorageMap, BlockNumberFor>; assert_eq!(Pallet::::generic_data2(5), None); GenericData2::::insert(5, 5); assert_eq!(Pallet::::generic_data2(5), Some(5)); /// Some random docs that ensure that docs are accepted #[crate::storage_alias] pub type GenericData = StorageMap, BlockNumberFor>; #[crate::storage_alias] pub type GenericDataPallet = StorageMap, Blake2_128Concat, BlockNumberFor, BlockNumberFor>; }); } #[test] fn storage_value_mutate_exists_should_work() { new_test_ext().execute_with(|| { #[crate::storage_alias] pub type Value = StorageValue; assert!(!Value::exists()); Value::mutate_exists(|v| *v = Some(1)); assert!(Value::exists()); assert_eq!(Value::get(), Some(1)); // removed if mutated to `None` Value::mutate_exists(|v| *v = None); assert!(!Value::exists()); }); } #[test] fn storage_value_try_mutate_exists_should_work() { new_test_ext().execute_with(|| { #[crate::storage_alias] pub type Value = StorageValue; type TestResult = std::result::Result<(), &'static str>; assert!(!Value::exists()); // mutated if `Ok` assert_ok!(Value::try_mutate_exists(|v| -> TestResult { *v = Some(1); Ok(()) })); assert!(Value::exists()); assert_eq!(Value::get(), Some(1)); // no-op if `Err` assert_noop!( Value::try_mutate_exists(|v| -> TestResult { *v = Some(2); Err("nah") }), "nah" ); assert_eq!(Value::get(), Some(1)); // removed if mutated to`None` assert_ok!(Value::try_mutate_exists(|v| -> TestResult { *v = None; Ok(()) })); assert!(!Value::exists()); }); } #[docify::export] #[test] fn verbatim_attribute() { new_test_ext().execute_with(|| { // Declare the alias that will use the verbatim identifier as prefix. #[crate::storage_alias(verbatim)] pub type Value = StorageValue; // Check that it works as expected. Value::put(1); assert_eq!(1, Value::get().unwrap()); // The prefix is the one we declared above. assert_eq!(&b"Test"[..], Value::pallet_prefix()); }); } #[docify::export] #[test] fn pallet_name_attribute() { new_test_ext().execute_with(|| { // Declare the alias that will use the pallet name as prefix. #[crate::storage_alias(pallet_name)] pub type Value = StorageValue, u32>; // Check that it works as expected. Value::::put(1); assert_eq!(1, Value::::get().unwrap()); // The prefix is the pallet name. In this case the pallet name is `System` as declared in // `construct_runtime!`. assert_eq!(&b"System"[..], Value::::pallet_prefix()); }); } #[docify::export] #[test] fn dynamic_attribute() { new_test_ext().execute_with(|| { // First let's declare our prefix. // // It could be any type that, as long as it implements `Get<&'static str>`. parameter_types! { pub Prefix: &'static str = "Hello"; } // Declare the alias that will use the dynamic `Get` as prefix. #[crate::storage_alias(dynamic)] pub type Value> = StorageValue; // Check that it works as expected. Value::::put(1); assert_eq!(1, Value::::get().unwrap()); // The prefix is the one we declared above. assert_eq!(&b"Hello"[..], Value::::pallet_prefix()); }); } #[docify::export] #[test] fn storage_alias_guess() { new_test_ext().execute_with(|| { // The macro will use `Test` as prefix. #[crate::storage_alias] pub type Value = StorageValue; assert_eq!(&b"Test"[..], Value::pallet_prefix()); // The macro will use the pallet name as prefix. #[crate::storage_alias] pub type PalletValue = StorageValue, u32>; assert_eq!(&b"System"[..], PalletValue::::pallet_prefix()); }); } #[test] fn dynamic_attribute_without_generics_works() { new_test_ext().execute_with(|| { parameter_types! { pub Prefix: &'static str = "Hello"; } #[crate::storage_alias(dynamic)] pub type Value = StorageValue; Value::put(1); assert_eq!(1, Value::get().unwrap()) }); }