@@ -874,6 +874,7 @@ pub mod tests {
874874
875875 decl_storage ! {
876876 trait Store for Module <T : Config > as Test {
877+ pub Value get( fn value) : u64 ;
877878 pub Data get( fn data) build( |_| vec![ ( 15u32 , 42u64 ) ] ) :
878879 map hasher( twox_64_concat) u32 => u64 ;
879880 pub OptionLinkedMap : map hasher( blake2_128_concat) u32 => Option <u32 >;
@@ -946,6 +947,61 @@ pub mod tests {
946947 } ) ;
947948 }
948949
950+ #[ test]
951+ fn storage_value_mutate_exists_should_work ( ) {
952+ new_test_ext ( ) . execute_with ( || {
953+ #[ crate :: storage_alias]
954+ pub type Value = StorageValue < Test , u32 > ;
955+
956+ assert ! ( !Value :: exists( ) ) ;
957+
958+ Value :: mutate_exists ( |v| * v = Some ( 1 ) ) ;
959+ assert ! ( Value :: exists( ) ) ;
960+ assert_eq ! ( Value :: get( ) , Some ( 1 ) ) ;
961+
962+ // removed if mutated to `None`
963+ Value :: mutate_exists ( |v| * v = None ) ;
964+ assert ! ( !Value :: exists( ) ) ;
965+ } ) ;
966+ }
967+
968+ #[ test]
969+ fn storage_value_try_mutate_exists_should_work ( ) {
970+ new_test_ext ( ) . execute_with ( || {
971+ #[ crate :: storage_alias]
972+ pub type Value = StorageValue < Test , u32 > ;
973+
974+ type TestResult = result:: Result < ( ) , & ' static str > ;
975+
976+ assert ! ( !Value :: exists( ) ) ;
977+
978+ // mutated if `Ok`
979+ assert_ok ! ( Value :: try_mutate_exists( |v| -> TestResult {
980+ * v = Some ( 1 ) ;
981+ Ok ( ( ) )
982+ } ) ) ;
983+ assert ! ( Value :: exists( ) ) ;
984+ assert_eq ! ( Value :: get( ) , Some ( 1 ) ) ;
985+
986+ // no-op if `Err`
987+ assert_noop ! (
988+ Value :: try_mutate_exists( |v| -> TestResult {
989+ * v = Some ( 2 ) ;
990+ Err ( "nah" )
991+ } ) ,
992+ "nah"
993+ ) ;
994+ assert_eq ! ( Value :: get( ) , Some ( 1 ) ) ;
995+
996+ // removed if mutated to`None`
997+ assert_ok ! ( Value :: try_mutate_exists( |v| -> TestResult {
998+ * v = None ;
999+ Ok ( ( ) )
1000+ } ) ) ;
1001+ assert ! ( !Value :: exists( ) ) ;
1002+ } ) ;
1003+ }
1004+
9491005 #[ test]
9501006 fn map_issue_3318 ( ) {
9511007 new_test_ext ( ) . execute_with ( || {
@@ -1257,6 +1313,13 @@ pub mod tests {
12571313 PalletStorageMetadata {
12581314 prefix : "Test" ,
12591315 entries : vec ! [
1316+ StorageEntryMetadata {
1317+ name: "Value" ,
1318+ modifier: StorageEntryModifier :: Default ,
1319+ ty: StorageEntryType :: Plain ( scale_info:: meta_type:: <u64 >( ) ) ,
1320+ default : vec![ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ,
1321+ docs: vec![ ] ,
1322+ } ,
12601323 StorageEntryMetadata {
12611324 name: "Data" ,
12621325 modifier: StorageEntryModifier :: Default ,
0 commit comments