From 84bdd4247463602f8c62ffb55232d30277adbcce Mon Sep 17 00:00:00 2001 From: Damir Shamanaev Date: Mon, 21 Oct 2024 18:27:10 +0300 Subject: [PATCH] better assert() --- .../move-basics/constants-shop-price.move | 2 +- .../sources/move-basics/control-flow.move | 22 +++++++-------- .../sources/move-basics/expression.move | 5 +--- .../samples/sources/move-basics/function.move | 6 ++-- .../samples/sources/move-basics/generics.move | 4 +-- .../move-basics/importing-module-members.move | 15 ---------- .../samples/sources/move-basics/option.move | 6 ++-- .../sources/move-basics/references.move | 4 +-- .../samples/sources/move-basics/string.move | 4 +-- .../sources/move-basics/struct-methods-2.move | 4 +-- .../sources/move-basics/struct-methods.move | 4 +-- .../samples/sources/move-basics/struct.move | 6 ++-- .../sources/move-basics/type-reflection.move | 2 +- .../samples/sources/move-basics/vector.move | 6 ++-- .../samples/sources/programmability/bcs.move | 18 ++++++------ .../programmability/collections-2.move | 6 ++-- .../programmability/collections-3.move | 2 +- .../sources/programmability/fast-path.move | 2 +- .../sources/programmability/publisher.move | 4 +-- .../sources/your-first-move/hello_world.move | 20 ++++++------- .../your-first-move/hello_world_debug.move | 28 +++++++++---------- .../your-first-move/hello_world_docs.move | 26 ++++++++--------- 22 files changed, 89 insertions(+), 107 deletions(-) delete mode 100644 packages/samples/sources/move-basics/importing-module-members.move diff --git a/packages/samples/sources/move-basics/constants-shop-price.move b/packages/samples/sources/move-basics/constants-shop-price.move index 1092bccb..165a5ff8 100644 --- a/packages/samples/sources/move-basics/constants-shop-price.move +++ b/packages/samples/sources/move-basics/constants-shop-price.move @@ -16,7 +16,7 @@ public struct Item {} /// Purchase an item from the shop. public fun purchase(coin: Coin): Item { - assert!(coin.value() == ITEM_PRICE, 0); + assert!(coin.value() == ITEM_PRICE); transfer::public_transfer(coin, SHOP_OWNER); diff --git a/packages/samples/sources/move-basics/control-flow.move b/packages/samples/sources/move-basics/control-flow.move index c34e5e33..27c71a8b 100644 --- a/packages/samples/sources/move-basics/control-flow.move +++ b/packages/samples/sources/move-basics/control-flow.move @@ -26,7 +26,7 @@ fun test_if_else() { 0 }; - assert!(y == 1, 0); + assert!(y == 1); } // ANCHOR_END: if_else // ANCHOR: while_loop @@ -50,9 +50,9 @@ fun while_loop(mut x: u8): u8 { #[test] fun test_while() { - assert!(while_loop(0) == 10, 0); // 10 times - assert!(while_loop(5) == 5, 0); // 5 times - assert!(while_loop(10) == 0, 0); // loop never executed + assert!(while_loop(0) == 10); // 10 times + assert!(while_loop(5) == 5); // 5 times + assert!(while_loop(10) == 0); // loop never executed } // ANCHOR_END: while_loop // ANCHOR: infinite_while @@ -66,7 +66,7 @@ fun test_infinite_while() { }; // This line will never be executed. - assert!(x == 5, 0); + assert!(x == 5); } // ANCHOR_END: infinite_while #[allow(dead_code)] @@ -81,7 +81,7 @@ fun test_infinite_loop() { }; // This line will never be executed. - assert!(x == 5, 0); + assert!(x == 5); } // ANCHOR_END: infinite_loop // ANCHOR: break_loop @@ -99,7 +99,7 @@ fun test_break_loop() { } }; - assert!(x == 5, 0); + assert!(x == 5); } // ANCHOR_END: break_loop // ANCHOR: continue_loop @@ -124,7 +124,7 @@ fun test_continue_loop() { } }; - assert!(x == 10, 0); // 10 + assert!(x == 10); // 10 } // ANCHOR_END: continue_loop // ANCHOR: return_statement @@ -144,8 +144,8 @@ fun is_positive(x: u8): bool { #[test] fun test_return() { - assert!(is_positive(5) == false, 0); - assert!(is_positive(0) == false, 0); - assert!(is_positive(1) == true, 0); + assert!(is_positive(5) == false); + assert!(is_positive(0) == false); + assert!(is_positive(1) == true); } // ANCHOR_END: return_statement diff --git a/packages/samples/sources/move-basics/expression.move b/packages/samples/sources/move-basics/expression.move index 2f3b7d13..c5cd9272 100644 --- a/packages/samples/sources/move-basics/expression.move +++ b/packages/samples/sources/move-basics/expression.move @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 #[allow(unused_variable)] -module book::expression { +module book::expression; #[test] fun expression_examples() { @@ -81,7 +81,4 @@ while (bool_expr) { expr; }; // loop is an expression, but returns `()` as well. loop { expr; break }; // ANCHOR_END: control_flow - - -} } diff --git a/packages/samples/sources/move-basics/function.move b/packages/samples/sources/move-basics/function.move index 3f8b96d7..e93aaaf1 100644 --- a/packages/samples/sources/move-basics/function.move +++ b/packages/samples/sources/move-basics/function.move @@ -15,7 +15,7 @@ public fun add(a: u64, b: u64): u64 { #[test] fun test_add() { let sum = add(1, 2); - assert!(sum == 3, 0); + assert!(sum == 3); } // ANCHOR_END: math @@ -36,8 +36,8 @@ fun get_name_and_age(): (vector, u8) { // Tuple must be destructured to access its elements. // Name and age are declared as immutable variables. let (name, age) = get_name_and_age(); -assert!(name == b"John", 0); -assert!(age == 25, 0); +assert!(name == b"John"); +assert!(age == 25); // ANCHOR_END: tuple_return_imm // ANCHOR: tuple_return_mut diff --git a/packages/samples/sources/move-basics/generics.move b/packages/samples/sources/move-basics/generics.move index 869e7196..7ef6bfaf 100644 --- a/packages/samples/sources/move-basics/generics.move +++ b/packages/samples/sources/move-basics/generics.move @@ -74,8 +74,8 @@ fun test_swap_type_params() { let Pair { first: pf1, second: ps1 } = pair1; // first1: u8, second1: bool let Pair { first: pf2, second: ps2 } = pair2; // first2: bool, second2: u8 - assert!(pf1 == ps2, 0x0); // 10 == 10 - assert!(ps1 == pf2, 0x0); // true == true + assert!(pf1 == ps2); // 10 == 10 + assert!(ps1 == pf2); // true == true } // ANCHOR_END: test_pair_swap diff --git a/packages/samples/sources/move-basics/importing-module-members.move b/packages/samples/sources/move-basics/importing-module-members.move deleted file mode 100644 index 91472676..00000000 --- a/packages/samples/sources/move-basics/importing-module-members.move +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright (c) Mysten Labs, Inc. -// SPDX-License-Identifier: Apache-2.0 - -#[allow(unused_use)] -// ANCHOR: members -module book::more_imports; - -use book::module_one::new; // imports the `new` function from the `module_one` module -use book::module_one::Character; // importing the `Character` struct from the `module_one` module - -/// Calls the `new` function from the `module_one` module. -public fun create_character(): Character { - new() -} -// ANCHOR_END: members diff --git a/packages/samples/sources/move-basics/option.move b/packages/samples/sources/move-basics/option.move index 593d4b71..5fd24139 100644 --- a/packages/samples/sources/move-basics/option.move +++ b/packages/samples/sources/move-basics/option.move @@ -31,15 +31,15 @@ public fun register( let mut opt = option::some(b"Alice"); // `option.is_some()` returns true if option contains a value. -assert!(opt.is_some(), 1); +assert!(opt.is_some()); // internal value can be `borrow`ed and `borrow_mut`ed. -assert!(opt.borrow() == &b"Alice", 0); +assert!(opt.borrow() == &b"Alice"); // `option.extract` takes the value out of the option, leaving the option empty. let inner = opt.extract(); // `option.is_none()` returns true if option is None. -assert!(opt.is_none(), 2); +assert!(opt.is_none()); // ANCHOR_END: usage } diff --git a/packages/samples/sources/move-basics/references.move b/packages/samples/sources/move-basics/references.move index 1326899c..f2b5e032 100644 --- a/packages/samples/sources/move-basics/references.move +++ b/packages/samples/sources/move-basics/references.move @@ -51,7 +51,7 @@ fun test_card() { enter_metro(&mut card); - assert!(is_valid(&card), 0); // read the card! + assert!(is_valid(&card)); // read the card! enter_metro(&mut card); // modify the card but don't move it enter_metro(&mut card); // modify the card but don't move it @@ -67,7 +67,7 @@ fun test_card_2024() { let mut card = purchase(); card.enter_metro(); // modify the card but don't move it - assert!(card.is_valid(), 0); // read the card! + assert!(card.is_valid()); // read the card! card.enter_metro(); // modify the card but don't move it card.enter_metro(); // modify the card but don't move it diff --git a/packages/samples/sources/move-basics/string.move b/packages/samples/sources/move-basics/string.move index 8cc50137..f1747c78 100644 --- a/packages/samples/sources/move-basics/string.move +++ b/packages/samples/sources/move-basics/string.move @@ -59,11 +59,11 @@ let hello = b"Hello".to_string(); // this is a valid UTF-8 string let hello = b"Hello".try_to_string(); -assert!(hello.is_some(), 0); // abort if the value is not valid UTF-8 +assert!(hello.is_some()); // abort if the value is not valid UTF-8 // this is not a valid UTF-8 string let invalid = b"\xFF".try_to_string(); -assert!(invalid.is_none(), 0); // abort if the value is valid UTF-8 +assert!(invalid.is_none()); // abort if the value is valid UTF-8 // ANCHOR_END: safe_utf8 } diff --git a/packages/samples/sources/move-basics/struct-methods-2.move b/packages/samples/sources/move-basics/struct-methods-2.move index a7923eef..4e80f6ec 100644 --- a/packages/samples/sources/move-basics/struct-methods-2.move +++ b/packages/samples/sources/move-basics/struct-methods-2.move @@ -36,9 +36,9 @@ public fun villain_health(villain: &Villain): u8 { villain.health } // Test the methods of the `Hero` and `Villain` structs. fun test_associated_methods() { let hero = new_hero(); - assert!(hero.health() == 100, 1); + assert!(hero.health() == 100); let villain = new_villain(); - assert!(villain.health() == 100, 3); + assert!(villain.health() == 100); } // ANCHOR_END: hero_and_villain diff --git a/packages/samples/sources/move-basics/struct-methods.move b/packages/samples/sources/move-basics/struct-methods.move index 3d8df5a8..a7164ec0 100644 --- a/packages/samples/sources/move-basics/struct-methods.move +++ b/packages/samples/sources/move-basics/struct-methods.move @@ -31,7 +31,7 @@ fun test_methods() { let mut hero = new(); hero.heal_spell(); - assert!(hero.health() == 110, 1); - assert!(hero.mana() == 90, 2); + assert!(hero.health() == 110); + assert!(hero.mana() == 90); } // ANCHOR_END: hero diff --git a/packages/samples/sources/move-basics/struct.move b/packages/samples/sources/move-basics/struct.move index 7e91e038..91e2e1f6 100644 --- a/packages/samples/sources/move-basics/struct.move +++ b/packages/samples/sources/move-basics/struct.move @@ -41,13 +41,13 @@ let mut artist = Artist { let artist_name = artist.name; // Access a field of the `Artist` struct. -assert!(artist.name == string::utf8(b"The Beatles"), 0); +assert!(artist.name == b"The Beatles".to_string()); // Mutate the `name` field of the `Artist` struct. -artist.name = string::utf8(b"Led Zeppelin"); +artist.name = b"Led Zeppelin".to_string(); // Check that the `name` field has been mutated. -assert!(artist.name == string::utf8(b"Led Zeppelin"), 1); +assert!(artist.name == b"Led Zeppelin".to_string()); // ANCHOR_END: access // ANCHOR: unpack diff --git a/packages/samples/sources/move-basics/type-reflection.move b/packages/samples/sources/move-basics/type-reflection.move index 3670db49..5acfd692 100644 --- a/packages/samples/sources/move-basics/type-reflection.move +++ b/packages/samples/sources/move-basics/type-reflection.move @@ -32,6 +32,6 @@ fun test_type_reflection() { let (type_name, module_name, _address_str) = do_i_know_you(); // - assert!(module_name == b"type_reflection".to_ascii_string(), 1); + assert!(module_name == b"type_reflection".to_ascii_string()); } // ANCHOR_END: main diff --git a/packages/samples/sources/move-basics/vector.move b/packages/samples/sources/move-basics/vector.move index db09f35b..4fba05be 100644 --- a/packages/samples/sources/move-basics/vector.move +++ b/packages/samples/sources/move-basics/vector.move @@ -23,13 +23,13 @@ let vv: vector> = vector[ // ANCHOR: methods let mut v = vector[10u8, 20, 30]; -assert!(v.length() == 3, 0); -assert!(!v.is_empty(), 1); +assert!(v.length() == 3); +assert!(!v.is_empty()); v.push_back(40); let last_value = v.pop_back(); -assert!(last_value == 40, 2); +assert!(last_value == 40); // ANCHOR_END: methods } } diff --git a/packages/samples/sources/programmability/bcs.move b/packages/samples/sources/programmability/bcs.move index 81565d85..17fd7dbd 100644 --- a/packages/samples/sources/programmability/bcs.move +++ b/packages/samples/sources/programmability/bcs.move @@ -47,7 +47,7 @@ custom_bytes.append(bcs::to_bytes(&b"hello, world!".to_string())); custom_bytes.append(bcs::to_bytes(&true)); // struct is just a sequence of fields, so the bytes should be the same! -assert!(&struct_bytes == &custom_bytes, 0); +assert!(&struct_bytes == &custom_bytes); // ANCHOR_END: encode_struct } @@ -61,12 +61,12 @@ let mut bcs = bcs::new(x"010000000000000000"); // Same bytes can be read differently, for example: Option let value: Option = bcs.peel_option_u64(); -assert!(value.is_some(), 0); -assert!(value.borrow() == &0, 1); +assert!(value.is_some()); +assert!(value.borrow() == &0); let remainder = bcs.into_remainder_bytes(); -assert!(remainder.length() == 0, 2); +assert!(remainder.length() == 0); // ANCHOR_END: decode // ANCHOR: chain_decode @@ -94,25 +94,25 @@ while (len > 0) { len = len - 1; }; -assert!(vec.length() == 1, 0); +assert!(vec.length() == 1); // ANCHOR_END: decode_vector // ANCHOR: decode_option let mut bcs = bcs::new(x"00"); let is_some = bcs.peel_bool(); -assert!(is_some == false, 0); +assert!(is_some == false); let mut bcs = bcs::new(x"0101"); let is_some = bcs.peel_bool(); let value = bcs.peel_u8(); -assert!(is_some == true, 1); -assert!(value == 1, 2); +assert!(is_some == true); +assert!(value == 1); // ANCHOR_END: decode_option // ANCHOR: decode_struct -// some bytes... +// some bytes... let mut bcs = bcs::new(x"0101010F0000000000F00000000000"); let (age, is_active, name) = ( diff --git a/packages/samples/sources/programmability/collections-2.move b/packages/samples/sources/programmability/collections-2.move index 9c5c28a6..d06ce27b 100644 --- a/packages/samples/sources/programmability/collections-2.move +++ b/packages/samples/sources/programmability/collections-2.move @@ -20,9 +20,9 @@ fun vec_set_playground() { set.insert(2); // add an item to the set set.insert(3); - assert!(set.contains(&1), 0); // check if an item is in the set - assert!(set.size() == 3, 1); // get the number of items in the set - assert!(!set.is_empty(), 2); // check if the set is empty + assert!(set.contains(&1)); // check if an item is in the set + assert!(set.size() == 3); // get the number of items in the set + assert!(!set.is_empty()); // check if the set is empty set.remove(&2); // remove an item from the set } diff --git a/packages/samples/sources/programmability/collections-3.move b/packages/samples/sources/programmability/collections-3.move index 05664e99..35e7066b 100644 --- a/packages/samples/sources/programmability/collections-3.move +++ b/packages/samples/sources/programmability/collections-3.move @@ -21,7 +21,7 @@ fun vec_map_playground() { map.insert(2, b"two".to_string()); // add a key-value pair to the map map.insert(3, b"three".to_string()); - assert!(map.contains(&2), 0); // check if a key is in the map + assert!(map.contains(&2)); // check if a key is in the map map.remove(&2); // remove a key-value pair from the map } diff --git a/packages/samples/sources/programmability/fast-path.move b/packages/samples/sources/programmability/fast-path.move index 78b302cf..6afa71ca 100644 --- a/packages/samples/sources/programmability/fast-path.move +++ b/packages/samples/sources/programmability/fast-path.move @@ -36,7 +36,7 @@ module book::coffee_machine { /// Put the cup back. This is a fast path operation. public fun put_back(cup: Cup) { let Cup { id, has_coffee: _ } = cup; - object::delete(id); + id.delete(); } } // ANCHOR_END: main diff --git a/packages/samples/sources/programmability/publisher.move b/packages/samples/sources/programmability/publisher.move index 20c1a4f3..1af4382a 100644 --- a/packages/samples/sources/programmability/publisher.move +++ b/packages/samples/sources/programmability/publisher.move @@ -36,11 +36,11 @@ let publisher = package::test_claim(USE_PUBLISHER {}, ctx); // ANCHOR: use_publisher // Checks if the type is from the same module, hence the `Publisher` has the // authority over it. -assert!(publisher.from_module(), 0); +assert!(publisher.from_module()); // Checks if the type is from the same package, hence the `Publisher` has the // authority over it. -assert!(publisher.from_package(), 0); +assert!(publisher.from_package()); // ANCHOR_END: use_publisher sui::test_utils::destroy(publisher); } diff --git a/packages/samples/sources/your-first-move/hello_world.move b/packages/samples/sources/your-first-move/hello_world.move index f852b18c..0f3ae515 100644 --- a/packages/samples/sources/your-first-move/hello_world.move +++ b/packages/samples/sources/your-first-move/hello_world.move @@ -1,16 +1,16 @@ // Copyright (c) Mysten Labs, Inc. // SPDX-License-Identifier: Apache-2.0 -module book::hello_world { - use std::string::String; +module book::hello_world; - public fun hello_world(): String { - b"Hello, World!".to_string() - } +use std::string::String; - #[test] - fun test_is_hello_world() { - let expected = b"Hello, World!".to_string(); - assert!(hello_world() == expected, 0) - } +public fun hello_world(): String { + b"Hello, World!".to_string() +} + +#[test] +fun test_is_hello_world() { + let expected = b"Hello, World!".to_string(); + assert!(hello_world() == expected) } diff --git a/packages/samples/sources/your-first-move/hello_world_debug.move b/packages/samples/sources/your-first-move/hello_world_debug.move index 00b85898..99de59e9 100644 --- a/packages/samples/sources/your-first-move/hello_world_debug.move +++ b/packages/samples/sources/your-first-move/hello_world_debug.move @@ -1,21 +1,21 @@ // Copyright (c) Mysten Labs, Inc. // SPDX-License-Identifier: Apache-2.0 -module book::hello_world_debug { - use std::string::String; - use std::debug; +module book::hello_world_debug; - public fun hello_world(): String { - let result = b"Hello, World!".to_string(); - debug::print(&result); - result - } +use std::string::String; +use std::debug; - #[test] - fun test_is_hello_world() { - let expected = b"Hello, World!".to_string(); - let actual = hello_world(); +public fun hello_world(): String { + let result = b"Hello, World!".to_string(); + debug::print(&result); + result +} + +#[test] +fun test_is_hello_world() { + let expected = b"Hello, World!".to_string(); + let actual = hello_world(); - assert!(actual == expected, 0) - } + assert!(actual == expected) } diff --git a/packages/samples/sources/your-first-move/hello_world_docs.move b/packages/samples/sources/your-first-move/hello_world_docs.move index d100e7ec..3ad5add2 100644 --- a/packages/samples/sources/your-first-move/hello_world_docs.move +++ b/packages/samples/sources/your-first-move/hello_world_docs.move @@ -2,20 +2,20 @@ // SPDX-License-Identifier: Apache-2.0 /// This module contains a function that returns a string "Hello, World!". -module book::hello_world_docs { - use std::string::String; +module book::hello_world_docs; - /// As the name says: returns a string "Hello, World!". - public fun hello_world(): String { - b"Hello, World!".to_string() - } +use std::string::String; - #[test] - /// This is a test for the `hello_world` function. - fun test_is_hello_world() { - let expected = b"Hello, World!".to_string(); - let actual = hello_world(); +/// As the name says: returns a string "Hello, World!". +public fun hello_world(): String { + b"Hello, World!".to_string() +} + +#[test] +/// This is a test for the `hello_world` function. +fun test_is_hello_world() { + let expected = b"Hello, World!".to_string(); + let actual = hello_world(); - assert!(actual == expected, 0) - } + assert!(actual == expected) }