Skip to content

Commit

Permalink
better assert()
Browse files Browse the repository at this point in the history
  • Loading branch information
damirka committed Oct 21, 2024
1 parent 9ecaf8f commit 84bdd42
Show file tree
Hide file tree
Showing 22 changed files with 89 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public struct Item {}

/// Purchase an item from the shop.
public fun purchase(coin: Coin<SUI>): Item {
assert!(coin.value() == ITEM_PRICE, 0);
assert!(coin.value() == ITEM_PRICE);

transfer::public_transfer(coin, SHOP_OWNER);

Expand Down
22 changes: 11 additions & 11 deletions packages/samples/sources/move-basics/control-flow.move
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fun test_if_else() {
0
};

assert!(y == 1, 0);
assert!(y == 1);
}
// ANCHOR_END: if_else
// ANCHOR: while_loop
Expand All @@ -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
Expand All @@ -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)]
Expand All @@ -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
Expand All @@ -99,7 +99,7 @@ fun test_break_loop() {
}
};

assert!(x == 5, 0);
assert!(x == 5);
}
// ANCHOR_END: break_loop
// ANCHOR: continue_loop
Expand All @@ -124,7 +124,7 @@ fun test_continue_loop() {
}
};

assert!(x == 10, 0); // 10
assert!(x == 10); // 10
}
// ANCHOR_END: continue_loop
// ANCHOR: return_statement
Expand All @@ -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
5 changes: 1 addition & 4 deletions packages/samples/sources/move-basics/expression.move
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

#[allow(unused_variable)]
module book::expression {
module book::expression;

#[test]
fun expression_examples() {
Expand Down Expand Up @@ -81,7 +81,4 @@ while (bool_expr) { expr; };
// loop is an expression, but returns `()` as well.
loop { expr; break };
// ANCHOR_END: control_flow


}
}
6 changes: 3 additions & 3 deletions packages/samples/sources/move-basics/function.move
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -36,8 +36,8 @@ fun get_name_and_age(): (vector<u8>, 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
Expand Down
4 changes: 2 additions & 2 deletions packages/samples/sources/move-basics/generics.move
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 0 additions & 15 deletions packages/samples/sources/move-basics/importing-module-members.move

This file was deleted.

6 changes: 3 additions & 3 deletions packages/samples/sources/move-basics/option.move
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions packages/samples/sources/move-basics/references.move
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions packages/samples/sources/move-basics/string.move
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions packages/samples/sources/move-basics/struct-methods-2.move
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions packages/samples/sources/move-basics/struct-methods.move
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions packages/samples/sources/move-basics/struct.move
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/samples/sources/move-basics/type-reflection.move
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ fun test_type_reflection() {
let (type_name, module_name, _address_str) = do_i_know_you<MyType>();

//
assert!(module_name == b"type_reflection".to_ascii_string(), 1);
assert!(module_name == b"type_reflection".to_ascii_string());
}
// ANCHOR_END: main
6 changes: 3 additions & 3 deletions packages/samples/sources/move-basics/vector.move
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ let vv: vector<vector<u8>> = 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
}
}
Expand Down
18 changes: 9 additions & 9 deletions packages/samples/sources/programmability/bcs.move
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -61,12 +61,12 @@ let mut bcs = bcs::new(x"010000000000000000");
// Same bytes can be read differently, for example: Option<u64>
let value: Option<u64> = 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
Expand Down Expand Up @@ -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) = (
Expand Down
6 changes: 3 additions & 3 deletions packages/samples/sources/programmability/collections-2.move
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion packages/samples/sources/programmability/fast-path.move
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions packages/samples/sources/programmability/publisher.move
Original file line number Diff line number Diff line change
Expand Up @@ -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<Book>(), 0);
assert!(publisher.from_module<Book>());

// Checks if the type is from the same package, hence the `Publisher` has the
// authority over it.
assert!(publisher.from_package<Book>(), 0);
assert!(publisher.from_package<Book>());
// ANCHOR_END: use_publisher
sui::test_utils::destroy(publisher);
}
Expand Down
20 changes: 10 additions & 10 deletions packages/samples/sources/your-first-move/hello_world.move
Original file line number Diff line number Diff line change
@@ -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)
}
Loading

0 comments on commit 84bdd42

Please sign in to comment.