Skip to content

Commit

Permalink
fix hello-world guide
Browse files Browse the repository at this point in the history
  • Loading branch information
damirka committed Apr 9, 2024
1 parent fcd1b6c commit 9c8ffa8
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 15 deletions.
6 changes: 5 additions & 1 deletion book/src/guides/upgradeability-practices.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ module book::upgradable {
}
```

<!--
## Using entry and friend functions
TODO: Add a section about entry and friend functions
-->

## Versioning objects

Expand Down Expand Up @@ -108,4 +110,6 @@ module book::versioned_config {

## Modular architecture

TODO: add two patterns for modular architecture: object capability (SuiFrens) and witness registry (SuiNS)
This section is coming soon!

<!-- TODO: add two patterns for modular architecture: object capability (SuiFrens) and witness registry (SuiNS) -->
34 changes: 20 additions & 14 deletions book/src/your-first-move/hello-world.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ sui move new hello_world

The `sui move` command gives access to the Move CLI - a built-in compiler, test runner and a utility for all things Move. The `new` command followed by the name of the package will create a new package in a new folder. In our case, the folder name is "hello_world".

```bash
$ cd hello_world
```

We can view the contents of the folder to see that the package was created successfully.

```bash
$ ls -l hello_world
Move.toml
sources
tests
```

## Directory Structure
Expand Down Expand Up @@ -119,7 +118,7 @@ module hello_world::hello_world {
/// Returns the "Hello World!" as a `String`.
public fun hello_world(): String {
b"Hello World!".to_string()
b"Hello, World!".to_string()
}
}
```
Expand All @@ -137,9 +136,6 @@ $ sui move build --path hello_world
<!-- The output would be: -->
<!-- TODO: insert out -->


## Running Tests

During the compilation, Move Compiler automatically creates a build folder where it places all fetched and compiled dependencies as well as the bytecode for the modules of the current package.

> If you're using a versioning system, such as Git, build folder should be ignored. For example, using a `.gitignore` file with `build` added to it.
Expand All @@ -149,9 +145,23 @@ $ tree build
Running Tests
```

## Running Tests

Before we get to testing, we should add a test. Move Compiler supports tests written in Move and provides the execution environment. The tests can be placed in both the source files and in the tests/ folder. Tests are marked with the #[test] attribute and are automatically discovered by the compiler. We explain tests in depth in the Testing section.

<!-- Replace the contents of the tests/hello_world_tests.move with the following content: -->
Replace the contents of the tests/hello_world_tests.move with the following content:

```move
#[test_only]
module hello_world::hello_world_tests {
use hello_world::hello_world;
#[test]
fun test_hello_world() {
assert!(hello_world::hello_world() == b"Hello, World!".to_string(), 0);
}
}
```

Here we import the hello_world module, and call its hello_world function to test that the output is indeed the string "Hello World!". Now, that we have tests in place, let's compile the package in the test mode and run tests. Move CLI has the test command for this:

Expand All @@ -165,13 +175,9 @@ $ sui move test --path hello_world

## Next Steps

In this section we explained the basics of the Move package: its structure, the manifest, the build and test flows. [On the next page](), we will write an application and see how the code is structured and what the language can do.
In this section we explained the basics of the Move package: its structure, the manifest, the build and test flows. [On the next page](./../hello-sui), we will write an application and see how the code is structured and what the language can do.

## Further Reading

- [Package Manifest](./../concepts/manifest.md) section
- Package in [The Move Reference](/reference/packages.html)

## Summary

In this chapter, we have created a new package, explained its structure and contents, and compiled it. We have also added a test and ran it. In the next chapter, we will write a simple application and see how the code is structured and what the language can do.
26 changes: 26 additions & 0 deletions packages/hello_world/Move.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# @generated by Move, please check-in and do not edit manually.

[move]
version = 1
manifest_digest = "60F2000F4D6DAD240EF371DB3BC521F7BD4C8B814BC59CA473B852B77FDBE53C"
deps_digest = "F8BBB0CCB2491CA29A3DF03D6F92277A4F3574266507ACD77214D37ECA3F3082"
dependencies = [
{ name = "Sui" },
]

[[move.package]]
name = "MoveStdlib"
source = { git = "https://github.com/MystenLabs/sui.git", rev = "framework/testnet", subdir = "crates/sui-framework/packages/move-stdlib" }

[[move.package]]
name = "Sui"
source = { git = "https://github.com/MystenLabs/sui.git", rev = "framework/testnet", subdir = "crates/sui-framework/packages/sui-framework" }

dependencies = [
{ name = "MoveStdlib" },
]

[move.toolchain-version]
compiler-version = "1.23.0"
edition = "legacy"
flavor = "sui"
37 changes: 37 additions & 0 deletions packages/hello_world/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[package]
name = "hello_world"
edition = "2024.beta" # edition = "legacy" to use legacy (pre-2024) Move
# license = "" # e.g., "MIT", "GPL", "Apache 2.0"
# authors = ["..."] # e.g., ["Joe Smith ([email protected])", "John Snow ([email protected])"]

[dependencies]
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/testnet" }

# For remote import, use the `{ git = "...", subdir = "...", rev = "..." }`.
# Revision can be a branch, a tag, and a commit hash.
# MyRemotePackage = { git = "https://some.remote/host.git", subdir = "remote/path", rev = "main" }

# For local dependencies use `local = path`. Path is relative to the package root
# Local = { local = "../path/to" }

# To resolve a version conflict and force a specific version for dependency
# override use `override = true`
# Override = { local = "../conflicting/version", override = true }

[addresses]
hello_world = "0x0"

# Named addresses will be accessible in Move as `@name`. They're also exported:
# for example, `std = "0x1"` is exported by the Standard Library.
# alice = "0xA11CE"

[dev-dependencies]
# The dev-dependencies section allows overriding dependencies for `--test` and
# `--dev` modes. You can introduce test-only dependencies here.
# Local = { local = "../path/to/dev-build" }

[dev-addresses]
# The dev-addresses section allows overwriting named addresses for the `--test`
# and `--dev` modes.
# alice = "0xB0B"

11 changes: 11 additions & 0 deletions packages/hello_world/sources/hello_world.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// The module `hello_world` under named address `hello_world`.
/// The named address is set in the `Move.toml`.
module hello_world::hello_world {
// Imports the `String` type from the Standard Library
use std::string::String;

/// Returns the "Hello World!" as a `String`.
public fun hello_world(): String {
b"Hello, World!".to_string()
}
}
9 changes: 9 additions & 0 deletions packages/hello_world/tests/hello_world_tests.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[test_only]
module hello_world::hello_world_tests {
use hello_world::hello_world;

#[test]
fun test_hello_world() {
assert!(hello_world::hello_world() == b"Hello, World!".to_string(), 0);
}
}

0 comments on commit 9c8ffa8

Please sign in to comment.