diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 00000000..34235bac --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,39 @@ +name: Run tests for Move packages + +# Set the VERSION variable to use when downloading the binary +# Currently, requires explicit setup, ideally it should be fetched +# automatically +# env: +# VERSION: 'v1.24.1' + +on: + pull_request: + +jobs: + download-binary: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Download Sui + uses: jaxxstorm/action-install-gh-release@v1.10.0 + with: + repo: MystenLabs/sui + platform: ubuntu + rename-to: sui-archive + cache: enable + extension: '.tgz' + # The code below is unpacking the .tgz because the action used above does + # not recognize the extension. The PR to the action repo is open and once + # it's in (and if) we'll be able to remove this. + - run: 'ls /opt/hostedtoolcache/' + - name: Unpack the archive (.tgz is not supported currently) + run: 'tar -xvzf /opt/hostedtoolcache/MystenLabs/sui/latest/ubuntu-x64/sui-archive' + - name: Move binaries to $PATH + run: mv sui* /opt/hostedtoolcache/MystenLabs/sui/latest/ubuntu-x64 + - run: chmod 0755 /opt/hostedtoolcache/MystenLabs/sui/latest/ubuntu-x64/sui + + # Run the tests in every directory using the latest mainnet binary + - run: 'sui move test --path packages/samples' + - run: 'sui move test --path packages/reference' + - run: 'sui move test --path packages/todo_list' + - run: 'sui move test --path packages/hello_world' diff --git a/book/src/SUMMARY.md b/book/src/SUMMARY.md index a90a1578..f7104af2 100644 --- a/book/src/SUMMARY.md +++ b/book/src/SUMMARY.md @@ -69,7 +69,7 @@ - [String](./move-basics/string.md) - [Control Flow](./move-basics/control-flow.md) - [Constants](./move-basics/constants.md) - - [Assert and Abort](./move-basics/assert-and-abort.md) + - [Aborting Execution](./move-basics/assert-and-abort.md) - [Function](./move-basics/function.md) - [Struct Methods](./move-basics/struct-methods.md) - [Visibility Modifiers](./move-basics/visibility.md) diff --git a/book/src/move-basics/assert-and-abort.md b/book/src/move-basics/assert-and-abort.md index f2dc156d..27aa0908 100644 --- a/book/src/move-basics/assert-and-abort.md +++ b/book/src/move-basics/assert-and-abort.md @@ -1,4 +1,4 @@ -# Assert and Abort +# Aborting Execution @@ -21,9 +21,9 @@ Links: --> A transaction can either succeed or fail. Successful execution applies all the changes made to -objects and on-chain data, and the transaction is committed to the blockchain. If a transaction -aborts, and changes are not applied. The `abort` keyword is used to abort a transaction and revert -the changes made so far. +objects and on-chain data, and the transaction is committed to the blockchain. Alternatively, if a +transaction aborts, the changes are not applied. The `abort` keyword is used to abort a transaction +and revert the changes made so far. > It is important to note that there is no catch mechanism in Move. If a transaction aborts, the > changes made so far are reverted, and the transaction is considered failed. @@ -31,8 +31,8 @@ the changes made so far. ## Abort The `abort` keyword is used to abort the execution of a transaction. It is used in combination with -an abort code, which will be returned to the caller of the transaction. The abort code is an integer -of type `u64` and can be any value. +an abort code, which will be returned to the caller of the transaction. The abort code is an +[integer](./primitive-types.md) of type `u64`. ```move {{#include ../../../packages/samples/sources/move-basics/assert-and-abort.move:abort}} @@ -45,7 +45,7 @@ The code above will, of course, abort with abort code `1`. The `assert!` macro is a built-in macro that can be used to assert a condition. If the condition is false, the transaction will abort with the given abort code. The `assert!` macro is a convenient way to abort a transaction if a condition is not met. The macro shortens the code otherwise written with -an `if` expression + `abort`. +an `if` expression + `abort`. The `code` argument is required and has to be a `u64` value. ```move {{#include ../../../packages/samples/sources/move-basics/assert-and-abort.move:assert}} @@ -53,10 +53,11 @@ an `if` expression + `abort`. ## Error constants -To make error codes more descriptive, it is a good practice to define error constants. Error -constants are defined as `const` declarations and are usually prefixed with `E` followed by a camel -case name. Error constants are no different from other constants and don't have special handling. So -their addition is purely a practice for better code readability. +To make error codes more descriptive, it is a good practice to define +[error constants](./constants.md). Error constants are defined as `const` declarations and are +usually prefixed with `E` followed by a camel case name. Error constants are no different from other +constants and don't have special handling, however, they are used to increase the readability of the +code and make it easier to understand the abort scenarios. ```move {{#include ../../../packages/samples/sources/move-basics/assert-and-abort.move:error_const}} diff --git a/book/src/move-basics/primitive-types.md b/book/src/move-basics/primitive-types.md index 6ff1fed6..b20a9cfa 100644 --- a/book/src/move-basics/primitive-types.md +++ b/book/src/move-basics/primitive-types.md @@ -29,8 +29,7 @@ Where: - `` - the value to be assigned to the variable ```move -let x: bool = true; -let mut y: u8 = 42; +{{#include ../../../packages/samples/sources/move-basics/primitive-types.move:variables_and_assignment}} ``` A mutable variable can be reassigned using the `=` operator. @@ -42,8 +41,7 @@ y = 43; Variables can also be shadowed by re-declaring. ```move -let x: u8 = 42; -let x: u8 = 43; +{{#include ../../../packages/samples/sources/move-basics/primitive-types.move:shadowing}} ``` ## Booleans @@ -53,8 +51,7 @@ The `bool` type represents a boolean value - yes or no, true or false. It has tw the type - the compiler can infer it from the value. ```move -let x = true; -let y = false; +{{#include ../../../packages/samples/sources/move-basics/primitive-types.move:boolean}} ``` Booleans are often used to store flags and to control the flow of the program. Please, refer to the @@ -72,10 +69,7 @@ Move supports unsigned integers of various sizes: from 8-bit to 256-bit. The int - `u256` - 256-bit ```move -let x: u8 = 42; -let y: u16 = 42; -// ... -let z: u256 = 42; +{{#include ../../../packages/samples/sources/move-basics/primitive-types.move:integers}} ``` Unlike booleans, integer types need to be inferred. In most of the cases, the compiler will infer @@ -84,9 +78,7 @@ infer the type and will require an explicit type annotation. It can either be pr assignment or by using a type suffix. ```move -// Both are equivalent -let x: u8 = 42; -let x = 42u8; +{{#include ../../../packages/samples/sources/move-basics/primitive-types.move:integer_explicit_type}} ``` ### Operations @@ -102,6 +94,9 @@ multiplication, division, and remainder. The syntax for these operations is: | % | modular division | The divisor is 0 | | / | truncating division | The divisor is 0 | +> For more operations, including bitwise operations, please refer to the +> [Move Reference](/reference/primitive-types/integers.html#bitwise). + The type of the operands _must match_, otherwise, the compiler will raise an error. The result of the operation will be of the same type as the operands. To perform operations on different types, the operands need to be cast to the same type. @@ -114,22 +109,19 @@ the operands need to be cast to the same type. Move supports explicit casting between integer types. The syntax for it is: ```move -( as ) + as ``` -Note, that it requires parentheses around the expression to prevent ambiguity. +Note, that it may require parentheses around the expression to prevent ambiguity. ```move -let x: u8 = 42; -let y: u16 = (x as u16); +{{#include ../../../packages/samples/sources/move-basics/primitive-types.move:cast_as}} ``` A more complex example, preventing overflow: ```move -let x: u8 = 255; -let y: u8 = 255; -let z: u16 = (x as u16) + ((y as u16) * 2); +{{#include ../../../packages/samples/sources/move-basics/primitive-types.move:overflow}} ``` ### Overflow diff --git a/packages/reference/sources/abilities.move b/packages/reference/sources/abilities.move index 7038c041..ce3b407b 100644 --- a/packages/reference/sources/abilities.move +++ b/packages/reference/sources/abilities.move @@ -1,6 +1,7 @@ // Copyright (c) Mysten Labs, Inc. // SPDX-License-Identifier: Apache-2.0 +#[allow(unused_field)] module ref::abilities { // ANCHOR: annotating_structs @@ -10,7 +11,7 @@ public struct MyVec(vector) has copy, drop, store; // ANCHOR_END: annotating_structs // ANCHOR: conditional_abilities -public struct Cup has copy, drop, store, key { item: T } +// public struct Cup has copy, drop, store, key { item: T } // ANCHOR_END: conditional_abilities } diff --git a/packages/samples/sources/move-basics/primitive-types.move b/packages/samples/sources/move-basics/primitive-types.move index dc1c757f..2c8d5b6b 100644 --- a/packages/samples/sources/move-basics/primitive-types.move +++ b/packages/samples/sources/move-basics/primitive-types.move @@ -3,4 +3,47 @@ module book::primitive_types { +#[test, allow(unused_variable, unused_let_mut)] +fun test_primitive_types() { + +// ANCHOR: variables_and_assignment +let x: bool = true; +let mut y: u8 = 42; +// ANCHOR_END: variables_and_assignment + +// ANCHOR: shadowing +let x: u8 = 42; +let x: u16 = 42; +// ANCHOR_END: shadowing + +// ANCHOR: boolean +let x = true; +let y = false; +// ANCHOR_END: boolean + +// ANCHOR: integers +let x: u8 = 42; +let y: u16 = 42; +// ... +let z: u256 = 42; +// ANCHOR_END: integers + +// ANCHOR: integer_explicit_type +// Both are equivalent +let x: u8 = 42; +let x = 42u8; +// ANCHOR_END: integer_explicit_type + +// ANCHOR: cast_as +let x: u8 = 42; +let y: u16 = x as u16; +let z = 2 * (x as u16); // ambiguous, requires parentheses +// ANCHOR_END: cast_as + +// ANCHOR: overflow +let x: u8 = 255; +let y: u8 = 255; +let z: u16 = (x as u16) + ((y as u16) * 2); +// ANCHOR_END: overflow +} } diff --git a/theme/head.hbs b/theme/head.hbs index 8391e1b9..f90c0a40 100644 --- a/theme/head.hbs +++ b/theme/head.hbs @@ -7,6 +7,5 @@ window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); - gtag('config', 'G-B1E7R0BHX4');