diff --git a/moonbit-docs/docs/build-system-tutorial.md b/moonbit-docs/docs/build-system-tutorial.md index be8181ba..c55efa6b 100644 --- a/moonbit-docs/docs/build-system-tutorial.md +++ b/moonbit-docs/docs/build-system-tutorial.md @@ -1,55 +1,75 @@ # MoonBit's Build System Tutorial -Moon is the build system for the MoonBit language, currently based on the [n2](https://github.com/evmar/n2) project. Moon supports parallel and incremental builds. Additionally, moon also supports managing and building third-party packages on [mooncakes.io](https://mooncakes.io/) +Moon is the build system for the MoonBit language, currently based on the +[n2](https://github.com/evmar/n2) project. Moon supports parallel and +incremental builds. Additionally, moon also supports managing and building +third-party packages on [mooncakes.io](https://mooncakes.io/) ## Prerequisites Before you begin with this tutorial, make sure you have installed the following: -1. **MoonBit CLI Tools**: Download it from the [https://www.moonbitlang.com/download/](https://www.moonbitlang.com/download/). This command line tool is needed for creating and managing MoonBit projects. +1. **MoonBit CLI Tools**: Download it from the +[https://www.moonbitlang.com/download/](https://www.moonbitlang.com/download/). +This command line tool is needed for creating and managing MoonBit projects. Use `moon help` to view the usage instructions. - ```bash + ```plaintext $ moon help - Moonbit's build system + The build system and package manager for MoonBit. - Usage: moon + Usage: moon [OPTIONS] Commands: - build Build the current package - check Check the current package, but don't build object files - run Run WebAssembly module - clean Remove the target directory - new Create a new moonbit package - bench Generate build matrix for benchmarking - fmt Format moonbit - version Print version info and exit - test Run the tests - login Log in to your account - register Register an account on mooncakes.io - publish Publish the current package - add Add a new dependency - remove Remove a dependency - tree Display the dependency tree - update Update index - doc Generate documentation - install Install dependencies - help Print this message or the help of the given subcommand(s) + new Create a new moonbit package + build Build the current package + check Check the current package, but don't build object files + run Run WebAssembly module + test Test the current package + clean Clean the target directory + fmt Format moonbit source code + doc Generate documentation + info Generate public interface (`.mbti`) files for all packages in the module + add Add a dependency + remove Remove a dependency + install Install dependencies + tree Display the dependency tree + login Log in to your account + register Register an account at mooncakes.io + publish Publish the current package + update Update the package registry index + coverage Code coverage utilities + generate-build-matrix Generate build matrix for benchmarking (legacy feature) + upgrade Upgrade toolchains + shell-completion Generate shell completion for bash/elvish/fish/pwsh/zsh to stdout + version Print version info and exit + help Print this message or the help of the given subcommand(s) Options: - -h, --help Print help + -C, --directory The source code directory. Defaults to the current directory + --target-dir The target directory. Defaults to `source_dir/target` + -q, --quiet Suppress output + -v, --verbose Increase verbosity + --trace Trace the execution of the program + --dry-run Do not actually run the command + -h, --help Print help ``` -2. **Moonbit Language** plugin in Visual Studio Code: You can install it from the VS Code marketplace. This plugin provides a rich development environment for MoonBit, including functionalities like syntax highlighting, code completion, and more. +2. **Moonbit Language** plugin in Visual Studio Code: You can install it from +the VS Code marketplace. This plugin provides a rich development environment for +MoonBit, including must-have functionalities like syntax highlighting, +intellisense, interactive debugging, testing, and more. -Once you have these prerequisites fulfilled, let's start by creating a new MoonBit module. +Once you have these prerequisites fulfilled, let's start by creating a new +MoonBit module. ## Creating a New Module -To create a new module, enter the `moon new` command in the terminal, and you will see the module creation wizard. By using all the default values, you can create a new module named `hello` in the `my-project` directory. +`moon` comes with a handy module creation wizard `moon new`. The default +settings are shown below: -```bash +```plaintext $ moon new Enter the path to create the project (. for current directory): my-project Select the create mode: exec @@ -59,11 +79,14 @@ Enter your license: Apache-2.0 Created my-project ``` -## Understanding the Module Directory Structure +This creates a new module named `hello` in `my-project`. The word _project_ is +used interchangeably with _module_. -After creating the new module, your directory structure should resemble the following: +## Understanding MoonBit's Module Structure -```bash +A typical module/project structure resembles the following: + +```plaintext my-project ├── README.md ├── lib @@ -76,34 +99,46 @@ my-project └── moon.mod.json ``` -Here's a brief explanation of the directory structure: +Here's a brief explanation of the structure: -- `lib` and `main` directories: These are the packages within the module. Each package can contain multiple `.mbt` files, which are the source code files for the MoonBit language. However, regardless of how many `.mbt` files a package has, they all share a common `moon.pkg.json` file. `lib/*_test.mbt` are separate test files in the `lib` package, where private members of the `lib` package can be accessed directly. These files are only included in the compilation in test mode, allowing for inline tests and utility functions for testing purposes to be written within these separate test files. +- `lib` and `main` directories: These are the packages within the module. Each +package may contain multiple moonbit source files (.mbt). A common +`moon.pkg.json` is shared around every source files within that package. -- `moon.pkg.json` is package descriptor. It defines the properties of the package, such as whether it is the main package and the packages it imports. +- `lib/*_test.mbt` are tests for the `lib` package. However members of `lib` +aren't directly visible to the tests, you'll need to explicitly use the +`@my-project.*` to access it. Tests are only included in +test compilation, allowing inline tests and utility functions for +testing purposes to be written within these separate test files. + +- `moon.pkg.json` is the package descriptor. It defines the properties of the +package, such as whether it is the main package and the packages it imports. - `main/moon.pkg.json`: ```json { - "is_main": true, + "is-main": true, "import": [ "username/hello/lib" ] } ``` - Here, "is_main: true" declares that the package needs to be linked by the build system into a wasm file. - + Here, `"is-main": true` declares that the package needs to be linked by the + build system into a wasm file. + - `lib/moon.pkg.json`: ```json {} ``` - This file is empty. Its purpose is simply to inform the build system that this folder is a package. - -- `moon.mod.json` is used to identify a directory as a MoonBit module. It contains the module's name: + This file is empty. Its purpose is simply to inform the build system that this + folder is a package. + +- `moon.mod.json` is used to declare a directory as a MoonBit module. It +contains the module's name: ```json { @@ -117,6 +152,9 @@ Here's a brief explanation of the directory structure: } ``` +Refer to the [json schema](./build-system-configuration.md) of module/package +descriptor for a complete specification. + ## Working with Packages Our `username/hello` module contains two packages: `lib` and `main`. @@ -125,7 +163,7 @@ The `lib` package contains `hello.mbt` and `hello_test.mbt` files: `hello.mbt` - ```moonbit + ```moonbit -f=hello.mbt pub fn hello() -> String { "Hello, world!" } @@ -133,17 +171,17 @@ The `lib` package contains `hello.mbt` and `hello_test.mbt` files: `hello_test.mbt` - ```moonbit + ```moonbit -f=hello.mbt test "hello" { if hello() != "Hello, world!" { - return Err("hello() != \"Hello, world!\"") + fail!("hello() != \"Hello, world!\"") } } ``` The `main` package contains a `main.mbt` file: - ```moonbit + ```moonbit no-check fn main { println(@lib.hello()) } @@ -151,13 +189,6 @@ The `main` package contains a `main.mbt` file: To execute the program, specify the path to the `main` package in the `moon run` command: -```bash -$ moon run ./main -Hello, world! -``` - -You can also omit `./` - ```bash $ moon run main Hello, world! @@ -172,8 +203,9 @@ Total tests: 1, passed: 1, failed: 0. ## Package Importing -In the MoonBit's build system, a module's name is used to reference its internal packages. -To import the `lib` package in `main/main.mbt`, you need to specify it in `main/moon.pkg.json`: +MoonBit's build system uses the name of a module to reference its internal +packages. To use `lib` within `main/main.mbt`, you need to specify it in +`main/moon.pkg.json`: ```json { @@ -184,21 +216,23 @@ To import the `lib` package in `main/main.mbt`, you need to specify it in `main/ } ``` -Here, `username/hello/lib` specifies importing the `username/hello/lib` package from the `username/hello` module, so you can use `@lib.hello()` in `main/main.mbt`. +(Although the intellisense will do that for you: just type `@lib` then enter.) -Note that the package name imported in `main/moon.pkg.json` is `username/hello/lib`, and `@lib` is used to refer to this package in `main/main.mbt`. The import here actually generates a default alias for the package name `username/hello/lib`. In the following sections, you will learn how to customize the alias for a package. - -## Creating and Using a New Package +Here, `username/hello/lib` specifies importing the `username/hello/lib` package +from the `username/hello` module, so you can use `@lib.hello()` in +`main/main.mbt`. -First, create a new directory named `fib` under `lib`: +Note that the package name imported in `main/moon.pkg.json` is +`username/hello/lib`, and `@lib` is used to refer to this package in +`main/main.mbt`. The import here actually generates a default alias for the +package name `username/hello/lib`. -```bash -mkdir lib/fib -``` +## Creating and Using a New Package -Now, you can create new files under `lib/fib`: +Suppose we have a new package named `fib` under `lib` and two moonbit source +`lib/fib/{a,b}.mbt`. -`a.mbt`: +In `a.mbt`: ```moonbit pub fn fib(n : Int) -> Int { @@ -210,7 +244,7 @@ pub fn fib(n : Int) -> Int { } ``` -`b.mbt`: +in `b.mbt`: ```moonbit pub fn fib2(num : Int) -> Int { @@ -226,13 +260,13 @@ pub fn fib2(num : Int) -> Int { } ``` -`moon.pkg.json`: +and `moon.pkg.json`: ```json {} ``` -After creating these files, your directory structure should look like this: +Our project structure should look like this now: ```bash my-project @@ -251,7 +285,8 @@ my-project └── moon.mod.json ``` -In the `main/moon.pkg.json` file, import the `username/hello/lib/fib` package and customize its alias to `my_awesome_fibonacci`: +In the `main/moon.pkg.json` file, import the `username/hello/lib/fib` package +and define an alias `my_awesome_fibonacci` for it: ```json { @@ -266,19 +301,19 @@ In the `main/moon.pkg.json` file, import the `username/hello/lib/fib` package an } ``` -This line imports the `fib` package, which is part of the `lib` package in the `hello` module. After doing this, you can use the `fib` package in `main/main.mbt`. Replace the file content of `main/main.mbt` to: +In `main/main.mbt`: -```moonbit +```moonbit no-check fn main { let a = @my_awesome_fibonacci.fib(10) let b = @my_awesome_fibonacci.fib2(11) - println("fib(10) = \(a), fib(11) = \(b)") + println("fib(10) = \{a}, fib(11) = \{b}") println(@lib.hello()) } ``` -To execute your program, specify the path to the `main` package: +Running `moon run main` again gives us the expected: ```bash $ moon run main @@ -288,14 +323,20 @@ Hello, world! ## Adding Tests -Let's add some tests to verify our fib implementation. Add the following content in `lib/fib/a.mbt`: +MoonBit differentiate between white-box and black-box tests. A white-box test +usually refers to an inline test block or a stand-alone `*_wbtest.mbt` source, intended for package developers to test their code, +whereas a black-box test refers to a `*_test.mbt` source, emulating package +users using current package. They may have different imports: a white-box test +automatically imports everything from `import` and `test-import` in +`moon.pkg.json`; a black-box test imports just the same as white-box but +with the addition of current package. -`lib/fib/a.mbt` +Let's add some inline tests to verify our fib implementation. In `lib/fib/a.mbt`: ```moonbit fn assert_eq[T: Show + Eq](lhs: T, rhs: T) -> Unit { if lhs != rhs { - abort("assert_eq failed.\n lhs: \(lhs)\n rhs: \(rhs)") + abort("assert_eq failed.\n\tlhs: \{lhs}\n\trhs: \{rhs}") } } @@ -308,13 +349,19 @@ test { } ``` -This code tests the first five terms of the Fibonacci sequence. `test { ... }` defines an inline test block. The code inside an inline test block is executed in test mode. +This code tests the first five terms of the Fibonacci sequence. `test { ... }` +defines an inline test block. The code inside an inline test block is executed +in test mode. -Inline test blocks are discarded in non-test compilation modes (`moon build` and `moon run`), so they won't cause the generated code size to bloat. +Inline test blocks are discarded in non-test compilation modes (`moon build` and +`moon run`), so they won't cause the generated code size to bloat. ## Stand-alone test files -Besides inline tests, MoonBit also supports stand-alone test files. Source files ending in `_test.mbt` are considered stand-alone test files. They will be included in test mode only. You can write inline tests and test utilities in these stand-alone test files. For example, inside the `lib/fib` directory, create a file named `fib_test.mbt` and paste the following code: +Stand-alone tests are included in test mode only as well. You can write inline +tests and test utilities in these stand-alone test files. For example, inside +the `lib/fib` directory, create a file named `fib_test.mbt` and paste the +following code: `lib/fib/fib_test.mbt` @@ -328,7 +375,9 @@ test { } ``` -Finally, use the `moon test` command, which scans the entire project, identifies, and runs all inline tests as well as files ending with `_test.mbt`. If everything is normal, you will see: +Now we use the `moon test` command, which scans the entire project, +identifies, and runs all inline tests as well as files ending with `_test.mbt` +or `_wbtest.mbt`. If everything is normal, you will see: ```bash $ moon test diff --git a/moonbit-docs/i18n/zh/docusaurus-plugin-content-docs/current/build-system-tutorial.md b/moonbit-docs/i18n/zh/docusaurus-plugin-content-docs/current/build-system-tutorial.md index 0cdd7594..01da1d6a 100644 --- a/moonbit-docs/i18n/zh/docusaurus-plugin-content-docs/current/build-system-tutorial.md +++ b/moonbit-docs/i18n/zh/docusaurus-plugin-content-docs/current/build-system-tutorial.md @@ -8,48 +8,58 @@ 1. **MoonBit CLI 工具**: 从[这里](https://www.moonbitlang.cn/download/)下载。该命令行工具用于创建和管理 MoonBit 项目。 - 使用 `moon help` 命令可查看使用说明。 - - ```bash - $ moon help - Moonbit's build system - - Usage: moon - - Commands: - build Build the current package - check Check the current package, but don't build object files - run Run WebAssembly module - clean Remove the target directory - new Create a new moonbit package - bench Generate build matrix for benchmarking - fmt Format moonbit - version Print version info and exit - test Run the tests - login Log in to your account - register Register an account on mooncakes.io - publish Publish the current package - add Add a new dependency - remove Remove a dependency - tree Display the dependency tree - update Update index - doc Generate documentation - install Install dependencies - help Print this message or the help of the given subcommand(s) - - Options: - -h, --help Print help - ``` - -2. **Moonbit Language** Visual Studio Code 插件: 可以从 VS Code 市场安装。该插件为 MoonBit 提供了丰富的开发环境,包括语法高亮、代码补全等功能。 + 使用 `moon help` 命令可查看使用说明。 + + ```plaintext + $ moon help + The build system and package manager for MoonBit. + + Usage: moon [OPTIONS] + + Commands: + new Create a new moonbit package + build Build the current package + check Check the current package, but don't build object files + run Run WebAssembly module + test Test the current package + clean Clean the target directory + fmt Format moonbit source code + doc Generate documentation + info Generate public interface (`.mbti`) files for all packages in the module + add Add a dependency + remove Remove a dependency + install Install dependencies + tree Display the dependency tree + login Log in to your account + register Register an account at mooncakes.io + publish Publish the current package + update Update the package registry index + coverage Code coverage utilities + generate-build-matrix Generate build matrix for benchmarking (legacy feature) + upgrade Upgrade toolchains + shell-completion Generate shell completion for bash/elvish/fish/pwsh/zsh to stdout + version Print version info and exit + help Print this message or the help of the given subcommand(s) + + Options: + -C, --directory The source code directory. Defaults to the current directory + --target-dir The target directory. Defaults to `source_dir/target` + -q, --quiet Suppress output + -v, --verbose Increase verbosity + --trace Trace the execution of the program + --dry-run Do not actually run the command + -h, --help Print help + ``` + +2. **Moonbit Language** Visual Studio Code 插件: 可以从 VS Code 市场安装。该插件为 MoonBit 提供了丰富的开发环境,包括语法高亮、代码补全、交互式除错和测试等功能。 安装完成后,让我们开始创建一个新的 MoonBit 模块。 ## 创建一个新模块 -要创建一个新模块,请在终端中输入 `moon new` 命令,您会看到模块创建向导。如果全部使用默认值即可在 `my-project` 目录下创建一个名为 `hello` 的新模块。 +`moon` 附带一个 `moon new` 模块创建向导。默认的配置是: -```bash +```plaintext $ moon new Enter the path to create the project (. for current directory): my-project Select the create mode: exec @@ -59,11 +69,13 @@ Enter your license: Apache-2.0 Created my-project ``` +向导会在 `my-project` 下创建一个新模块 `hello`。项目一词在这里与模块是互通的。 + ## 了解模块目录结构 -创建新模块后,目录结构应如下所示: +典型的模块/项目结构如下所示: -```bash +```plaintext my-project ├── README.md ├── lib @@ -78,31 +90,32 @@ my-project 这里简单解释一下目录结构: -- `lib` 和 `main` 目录:这些是模块中的包。每个包可以包含多个 `.mbt` 文件,这些文件是 MoonBit 语言的源代码文件。不过,无论包中有多少个 `.mbt` 文件,它们都共享一个公共的 `moon.pkg.json` 文件。`lib/*_test.mbt` 是 `lib` 包中独立的测试文件,在这些文件中可以直接访问 `lib` 包的私有成员。这些文件只有在测试模式下才会加入到编译中,可以在这些独立测试文件中写内联测试和供测试使用的工具函数。 +- `lib` 和 `main` 目录:这些是模块中的包。每个包可以包含多个 MoonBit 源文件(.mbt)。一个包中的所有源码都共享一个 `moon.pkg.json`。 + +- `lib/*_test.mbt` 是 `lib` 包中独立的测试文件。但 `lib` 的成员对测试是不可见的,需要显式地用 `@my-project.*` 来访问成员。这些文件只有在测试模式下才会编译,可以在这些独立测试文件中写内联测试和供测试使用的工具函数。 + +- `moon.pkg.json`:包描述文件,定义了包的属性,例如该包是否为 `main` 包,以及它所导入的包。 -- `moon.pkg.json` 文件:包描述符文件。它定义了包的属性,例如该包是否为 `main` 包,以及它所导入的包。 - `main/moon.pkg.json`: ```json { - "is_main": true, - "import": [ - "username/hello/lib" - ] + "is-main": true, + "import": ["username/hello/lib"] } ``` - 其中的 `"is_main: true"` 声明此包需要被构建系统链接为 `wasm` 文件。 + 其中的 `"is-main": true` 声明此包需要被构建系统链接为 `wasm` 文件。 - `lib/moon.pkg.json` - ```json - {} - ``` + ```json + {} + ``` 内容为空,其作用只是告诉构建系统该文件夹是一个包。 -- `moon.mod.json` 用于将目录标识为 MoonBit 模块。它包含模块的名称: +- `moon.mod.json` 用于将目录标记为 MoonBit 模块。它包含模块的名称: ```json { @@ -116,7 +129,9 @@ my-project } ``` -## 使用包 +模块/包描述文件的 [json schema](./build-system-configuration.md) 给出了全面的定义规范。 + +## 如何使用包 我们的 `username/hello` 模块包含两个包:`lib` 和 `main`。 @@ -124,7 +139,7 @@ my-project `hello.mbt` - ```moonbit + ```moonbit -f=hello.mbt pub fn hello() -> String { "Hello, world!" } @@ -132,72 +147,57 @@ my-project `hello_test.mbt` - ```moonbit + ```moonbit -f=hello.mbt test "hello" { if hello() != "Hello, world!" { - return Err("hello() != \"Hello, world!\"") + fail!("hello() != \"Hello, world!\"") } } ``` -- `main` 包含一个 `main.mbt` 文件: - - ```moonbit - fn main { - println(@lib.hello()) - } - ``` - -要执行程序,请在 `moon run` 命令中指定 `main` 包所在的路径: +`main` 包含一个 `main.mbt` 文件: -```bash -$ moon run ./main -Hello, world! +```moonbit no-check +fn main { + println(@lib.hello()) +} ``` -也可以省略 `./` +要执行代码,为 `moon run` 命令指定 `main` 包所在的路径: ```bash $ moon run main Hello, world! ``` -您可以使用 `moon test` 命令进行测试: +可以使用 `moon test` 命令进行测试: ```bash $ moon test Total tests: 1, passed: 1, failed: 0. ``` -## 包导入 +## 如何导入包 -在 MoonBit 的构建系统中,模块的名称用来引用其内部包。 +MoonBit 构建系统使用模块的名称用来引用其内部包。 要在 `main/main.mbt` 中导入 `lib` 包,需要在 `main/moon.pkg.json` 中指定: ```json { "is_main": true, - "import": [ - "username/hello/lib" - ] + "import": ["username/hello/lib"] } ``` -这里的 `username/hello/lib"` 指定了导入 `username/hello` 模块中的 `username/hello/lib` 包,这样你就能在 `main/main.mbt` 中使用 `@lib.hello()` 了。 +这里的 `username/hello/lib"` 指定导入 `username/hello` 模块中的 `username/hello/lib` 包,因此得以在 `main/main.mbt` 中使用 `@lib.hello()` 。 -注意,我们在 `main/moon.pkg.json` 中导入的包名是 `username/hello/lib`,在 `main/main.mbt` 中使用的是 `@lib` 来引用该包,这里的 `import` 其实是给包名 `username/hello/lib` 生成了一个默认的别名。之后你会学到如何自定义包的别名。 +注意,我们在 `main/moon.pkg.json` 中导入的包名是 `username/hello/lib`,在 `main/main.mbt` 中使用 `@lib` 来引用该包,这里的 `import` 其实是给包名 `username/hello/lib` 生成了一个默认的别名。 ## 创建和使用包 -首先,在 `lib` 目录下创建一个名为 `fib` 的新目录: +考虑在 `lib` 下创建一个新包 `fib`,并新建两个源码文件 `lib/fib/{a,b}.mbt`。 -```bash -mkdir lib/fib -``` - -然后在 `lib/fib` 下创建新文件: - -`a.mbt`: +`a.mbt`: ```moonbit pub fn fib(n : Int) -> Int { @@ -209,7 +209,7 @@ pub fn fib(n : Int) -> Int { } ``` -`b.mbt`: +`b.mbt`: ```moonbit pub fn fib2(num : Int) -> Int { @@ -225,15 +225,15 @@ pub fn fib2(num : Int) -> Int { } ``` -`moon.pkg.json`: +`moon.pkg.json`: ```json {} ``` -创建这些文件后,目录结构应如下所示: +现在项目结构应该如下所示: -``` +```plaintext my-project ├── README.md ├── lib @@ -250,7 +250,7 @@ my-project └── moon.mod.json ``` -在 `main/moon.pkg.json` 文件中,导入 `username/hello/lib/fib` 包,并定义其别名为 `my_awesome_fibonacci`: +修改 `main/moon.pkg.json`,导入 `username/hello/lib/fib` 包,并定义其别名为 `my_awesome_fibonacci`: ```json { @@ -265,19 +265,19 @@ my-project } ``` -这行代码导入了 `fib` 包,它是 `hello` 模块中 `lib` 包的一部分。完成后,你可以在 `main/main.mbt` 中使用 `fib` 包。将 `main/main.mbt` 文件的内容替换为: +`main/main.mbt`: -```moonbit +```moonbit no-check fn main { let a = @my_awesome_fibonacci.fib(10) let b = @my_awesome_fibonacci.fib2(11) - println("fib(10) = \(a), fib(11) = \(b)") + println("fib(10) = \{a}, fib(11) = \{b}") println(@lib.hello()) } ``` -要执行程序,请指定主包所在的路径: +运行 `moon run main` 能给出预期结果: ```bash $ moon run main @@ -287,14 +287,15 @@ Hello, world! ## 添加测试 -最后,我们添加一些测试来验证我们的 `fib` 实现。在 `lib/fib/a.mbt` 中添加以下内容: +MoonBit 区分白盒、黑盒测试。白盒测试指的是内联测试或一个独立的 `*_wbtest.mbt` 文件,模拟包开发者的测试场景;黑盒测试指的是 `*_test.mbt` 文件,模拟用户使用当前包的场景。 +可以为这两种测试导入不同的包:白盒测试会导入 `moon.pkg.json` 中的 `import` `test-import` 字段;黑盒测试则比白盒测试多导入一个当前包。 -`lib/fib/a.mbt` +不妨为 `fib` 添加一些内联测试来验证其正确性。`lib/fib/a.mbt`: ```moonbit fn assert_eq[T: Show + Eq](lhs: T, rhs: T) -> Unit { if lhs != rhs { - abort("assert_eq failed.\n lhs: \(lhs)\n rhs: \(rhs)") + abort("assert_eq failed.\n\tlhs: \{lhs}\n\trhs: \{rhs}") } } @@ -313,7 +314,9 @@ test { ## 独立的测试文件 -除了内联测试外,MoonBit 还支持独立的测试文件。名字以 `_test.mbt` 结尾的源文件会被视作独立的测试文件。它们只有在测试模式下才会加入到编译中。我们可以在这些独立测试文件中写内联测试,以及供测试使用的工具函数。例如,可以在 `lib/fib` 目录下创建一个名为 `fib_test.mbt` 的文件,并粘贴以下代码: +MoonBit 还支持独立的测试文件,这些文件只有在测试模式下才会加入到编译中。 +可以在这些独立测试文件中写内联测试,以及供测试使用的工具函数。例如可以在 +`lib/fib` 目录下创建一个名为 `fib_test.mbt` 的文件: `lib/fib/fib_test.mbt`: @@ -327,7 +330,7 @@ test { } ``` -最后,使用 `moon test` 命令,它会扫描整个项目,识别并运行所有的内联测试以及以 `_test.mbt` 结尾的文件。如果一切正常,你将看到: +现在可以用 `moon test`,扫描整个项目,识别并运行所有的内联测试以及所有以 `_test.mbt`/ `_wbtest.mbt` 结尾的文件。如果没有问题则输出: ```bash $ moon test