@@ -431,6 +431,16 @@ test reduce_3() {
431431 result == 1
432432}
433433
434+ /// Promote an arbitrary list of assets into a `Value`. This function fails
435+ /// (i.e. halt the program execution) if:
436+ ///
437+ /// - there's any duplicate amongst `PolicyId`;
438+ /// - there's any duplicate amongst `AssetName`;
439+ /// - the `AssetName` aren't sorted in ascending lexicographic order; or
440+ /// - any asset quantity is null.
441+ ///
442+ /// This function is meant to turn arbitrary user-defined `Data` into safe `Value`,
443+ /// while checking for internal invariants.
434444pub fn from_asset_list (xs: List < (PolicyId , List < (AssetName , Int )> )> ) -> Value {
435445 xs
436446 |> list.foldr (
@@ -465,7 +475,7 @@ test from_asset_list_2() fail {
465475
466476test from_asset_list_3 () fail {
467477 let v = from_asset_list ([(#"33" , [(#"" , 0 )])])
468- v = = zero ()
478+ v ! = zero ()
469479}
470480
471481test from_asset_list_4 () {
@@ -483,15 +493,15 @@ test from_asset_list_6() fail {
483493 from_asset_list (
484494 [(#"33" , [(#"" , 1 ), (#"33" , 1 )]), (#"33" , [(#"" , 1 ), (#"33" , 1 )])],
485495 )
486- flatten (v) == [(# "33" , # "" , 1 ), (# "33" , # "33" , 1 )]
496+ v != zero ()
487497}
488498
489499test from_asset_list_7 () fail {
490500 let v =
491501 from_asset_list (
492502 [(#"33" , [(#"" , 1 ), (#"33" , 1 )]), (#"34" , [(#"" , 1 ), (#"" , 1 )])],
493503 )
494- flatten (v) == [(# "33" , # "" , 1 ), (# "33" , # "33" , 1 ), (# "34" , # "" , 1 )]
504+ v != zero ()
495505}
496506
497507test from_asset_list_8 () {
@@ -511,6 +521,23 @@ test from_asset_list_8() {
511521 ]
512522}
513523
524+ test from_asset_list_9 () {
525+ let v =
526+ from_asset_list (
527+ [
528+ (#"35" , [(#"" , 1 )]),
529+ (#"33" , [(#"" , 1 ), (#"33" , 1 )]),
530+ (#"34" , [(#"31" , 1 )]),
531+ ],
532+ )
533+ flatten (v) == [
534+ (#"33" , #"" , 1 ),
535+ (#"33" , #"33" , 1 ),
536+ (#"34" , #"31" , 1 ),
537+ (#"35" , #"" , 1 ),
538+ ]
539+ }
540+
514541/// Convert the value into a dictionary of dictionaries.
515542pub fn to_dict (self: Value ) -> Dict < PolicyId , Dict < AssetName , Int >> {
516543 self.inner
0 commit comments