Skip to content

Commit

Permalink
update build system tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
lijunchen committed Feb 7, 2024
1 parent 8ee845e commit 25b1667
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 48 deletions.
45 changes: 21 additions & 24 deletions build-system-tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,50 +271,47 @@ fib(10) = 55, fib(11) = 89
Hello, world!
```

## Adding tests
## Adding Tests

In the last, let's add some tests to verify our fib implementation.
Let's add some tests to verify our fib implementation. Add the following content in `lib/fib/a.mbt`:

First, inside the `lib/fib` directory, create a file named `fib_test.mbt` and paste the following code:

`lib/fib/fib_test.mbt`
`lib/fib/a.mbt`
```rust
fn assert_eq[T: Eq](lhs: T, rhs: T) {
fn assert_eq[T: Show + Eq](lhs: T, rhs: T) {
if lhs != rhs {
abort("")
abort("assert_eq failed.\n lhs: \(lhs)\n rhs: \(rhs)")
}
}

fn init {
test {
assert_eq(fib(1), 1)
assert_eq(fib2(2), 1)
assert_eq(fib(2), 1)
assert_eq(fib(3), 2)
assert_eq(fib2(4), 3)
assert_eq(fib(4), 3)
assert_eq(fib(5), 5)
}
```

This code tests the first five terms of the Fibonacci sequence.
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.

You can also write inline tests directly in lib/fib/a.mbt:
## Stand-alone test files

`lib/fib/a.mbt`
```rust
pub fn fib(n : Int) -> Int {
match n {
0 => 0
1 => 1
_ => fib(n - 1) + fib(n - 2)
}
}
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:

`lib/fib/fib_test.mbt`
```rust
test {
if fib(4) != 3 { abort("fib(4) != 3") }
assert_eq(fib(1), 1)
assert_eq(fib2(2), 1)
assert_eq(fib(3), 2)
assert_eq(fib2(4), 3)
assert_eq(fib(5), 5)
}
```

Finally, use the command `moon test` which scans the module, identifying and running all files ending with `_test.mbt`. If everything works, you'll see:
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:

```bash
$ moon test
Expand All @@ -325,4 +322,4 @@ Hello, world!
test main ... ok
```

Note that `main/main.mbt:init` is also executed here. We will improve the issue with tests and package initialization functions in the future.
Note that `main/main.mbt:init` is also executed here, and we will improve the issue of testing with package initialization functions in the future.
45 changes: 21 additions & 24 deletions zh-docs/build-system-tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,50 +268,47 @@ Hello, world!

## 添加测试

最后,让添加一些测试来验证我们的 fib 实现。
最后,让我们添加一些测试来验证我们的 fib 实现。`lib/fib/a.mbt` 中添加如下内容:

首先,在 `lib/fib` 目录下,创建一个名为 `fib_test.mbt` 的文件,并粘贴以下代码:

`lib/fib/fib_test.mbt`
`lib/fib/a.mbt`
```rust
fn assert_eq[T: Eq](lhs: T, rhs: T) {
fn assert_eq[T: Show + Eq](lhs: T, rhs: T) {
if lhs != rhs {
abort("")
abort("assert_eq failed.\n lhs: \(lhs)\n rhs: \(rhs)")
}
}

fn init {
test {
assert_eq(fib(1), 1)
assert_eq(fib2(2), 1)
assert_eq(fib(2), 1)
assert_eq(fib(3), 2)
assert_eq(fib2(4), 3)
assert_eq(fib(4), 3)
assert_eq(fib(5), 5)
}
```

这段代码测试了斐波那契序列的前五个项。
这段代码测试了斐波那契序列的前五个项。`test { ... }` 定义了一个内联测试块。内联测试块中的代码会在测试模式下被执行。

内联测试块会在非测试的编译模式下被丢弃(`moon build``moon run`),所以它们不会导致生成的代码大小膨胀。

你也可以直接在 `lib/fib/a.mbt` 中写inline测试:
## 独立的测试文件

除了内联测试,MoonBit 还支持独立的测试文件。名字以 `_test.mbt` 结尾的源文件会被视作独立的测试文件。它们只有在测试模式下才会加入到编译中。可以在这些独立测试文件中写内联测试和供测试使用的工具函数。
例如,可以在 `lib/fib` 目录下,创建一个名为 `fib_test.mbt` 的文件,并粘贴以下代码:

`lib/fib/a.mbt`
```rust
pub fn fib(n : Int) -> Int {
match n {
0 => 0
1 => 1
_ => fib(n - 1) + fib(n - 2)
}
}

`lib/fib/fib_test.mbt`
```rust
test {
if fib(4) != 3 { abort("fib(4) != 3") }
assert_eq(fib(1), 1)
assert_eq(fib2(2), 1)
assert_eq(fib(3), 2)
assert_eq(fib2(4), 3)
assert_eq(fib(5), 5)
}
```



最后,使用 `moon test` 命令,它扫描整个项目,识别并运行所有以 `_test.mbt` 结尾的文件。如果一切正常,你将看到:
最后,使用 `moon test` 命令,它扫描整个项目,识别并运行所有inline测试以及以 `_test.mbt` 结尾的文件。如果一切正常,你将看到:

```bash
$ moon test
Expand Down

0 comments on commit 25b1667

Please sign in to comment.