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<T> =
StorageMap<System, Blake2_128Concat, BlockNumberFor<T>, BlockNumberFor<T>>;
assert_eq!(Pallet::<Runtime>::generic_data2(5), None);
GenericData2::<Runtime>::insert(5, 5);
assert_eq!(Pallet::<Runtime>::generic_data2(5), Some(5));
#[crate::storage_alias]
pub type GenericData<T> =
StorageMap<Test2, Blake2_128Concat, BlockNumberFor<T>, BlockNumberFor<T>>;
#[crate::storage_alias]
pub type GenericDataPallet<T: Config> =
StorageMap<Pallet<T>, Blake2_128Concat, BlockNumberFor<T>, BlockNumberFor<T>>;
});
}
#[test]
fn storage_value_mutate_exists_should_work() {
new_test_ext().execute_with(|| {
#[crate::storage_alias]
pub type Value = StorageValue<Test, u32>;
assert!(!Value::exists());
Value::mutate_exists(|v| *v = Some(1));
assert!(Value::exists());
assert_eq!(Value::get(), Some(1));
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<Test, u32>;
type TestResult = std::result::Result<(), &'static str>;
assert!(!Value::exists());
assert_ok!(Value::try_mutate_exists(|v| -> TestResult {
*v = Some(1);
Ok(())
}));
assert!(Value::exists());
assert_eq!(Value::get(), Some(1));
assert_noop!(
Value::try_mutate_exists(|v| -> TestResult {
*v = Some(2);
Err("nah")
}),
"nah"
);
assert_eq!(Value::get(), Some(1));
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(|| {
#[crate::storage_alias(verbatim)]
pub type Value = StorageValue<Test, u32>;
Value::put(1);
assert_eq!(1, Value::get().unwrap());
assert_eq!(&b"Test"[..], Value::pallet_prefix());
});
}
#[docify::export]
#[test]
fn pallet_name_attribute() {
new_test_ext().execute_with(|| {
#[crate::storage_alias(pallet_name)]
pub type Value<T: Config> = StorageValue<Pallet<T>, u32>;
Value::<Runtime>::put(1);
assert_eq!(1, Value::<Runtime>::get().unwrap());
assert_eq!(&b"System"[..], Value::<Runtime>::pallet_prefix());
});
}
#[docify::export]
#[test]
fn dynamic_attribute() {
new_test_ext().execute_with(|| {
parameter_types! {
pub Prefix: &'static str = "Hello";
}
#[crate::storage_alias(dynamic)]
pub type Value<T: Get<&'static str>> = StorageValue<T, u32>;
Value::<Prefix>::put(1);
assert_eq!(1, Value::<Prefix>::get().unwrap());
assert_eq!(&b"Hello"[..], Value::<Prefix>::pallet_prefix());
});
}
#[docify::export]
#[test]
fn storage_alias_guess() {
new_test_ext().execute_with(|| {
#[crate::storage_alias]
pub type Value = StorageValue<Test, u32>;
assert_eq!(&b"Test"[..], Value::pallet_prefix());
#[crate::storage_alias]
pub type PalletValue<T: Config> = StorageValue<Pallet<T>, u32>;
assert_eq!(&b"System"[..], PalletValue::<Runtime>::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<Prefix, u32>;
Value::put(1);
assert_eq!(1, Value::get().unwrap())
});
}