Skip to content

Commit 7795b24

Browse files
committed
Add explainer to from_asset_list and one extra test
That last test show that we actually can provide policy ids out of order.
1 parent d9655fc commit 7795b24

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

lib/aiken/transaction/value.ak

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
434444
pub 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

466476
test from_asset_list_3() fail {
467477
let v = from_asset_list([(#"33", [(#"", 0)])])
468-
v == zero()
478+
v != zero()
469479
}
470480

471481
test 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

489499
test 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

497507
test 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.
515542
pub fn to_dict(self: Value) -> Dict<PolicyId, Dict<AssetName, Int>> {
516543
self.inner

0 commit comments

Comments
 (0)