Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions tools/i18n-meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,19 @@
"ru": "-",
"ko": "-"
},
{
"name": "directory_stack.md",
"en": "In progress",
"zh-CN": "badd5aa21c3690769e81dfb416a69d2f8ea20fa5",
"fr": "-",
"de": "-",
"tr": "-",
"ja": "-",
"es": "-",
"pt-BR": "-",
"ru": "-",
"ko": "-"
},
{
"name": "environment.md",
"en": "Completed",
Expand Down Expand Up @@ -691,7 +704,7 @@
{
"name": "standard_library.md",
"en": "In progress",
"zh-CN": "-",
"zh-CN": "badd5aa21c3690769e81dfb416a69d2f8ea20fa5",
"fr": "-",
"de": "-",
"tr": "-",
Expand Down Expand Up @@ -743,7 +756,7 @@
{
"name": "testing.md",
"en": "In progress",
"zh-CN": "-",
"zh-CN": "badd5aa21c3690769e81dfb416a69d2f8ea20fa5",
"fr": "-",
"de": "-",
"tr": "-",
Expand Down
3 changes: 2 additions & 1 deletion zh-CN/book/directory_stack.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ dirs
# => ╭───┬────────┬─────────────────────────────────╮
# => │ # │ active │ path │
# => ├───┼────────┼─────────────────────────────────┤
# => │ 0 │ true │ /home/myuser/repo/nushell │
# => │ 0 │ true │ /home/myuser/src/repo/nushell │
# => ╰───┴────────┴─────────────────────────────────╯
```

Expand Down Expand Up @@ -91,6 +91,7 @@ dirs next
pwd
# => /home/myuser/src/repo/nushell
dirs goto 2
pwd
# => /home/myuser/src/repo/nu_scripts
```

Expand Down
44 changes: 31 additions & 13 deletions zh-CN/book/standard_library.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# 标准库 (预览版)
---
prev:
text: (没那么)高级
link: /zh-CN/book/advanced.md
---

Nushell 内置了一个用原生 Nu 编写的有用命令的标准库。默认情况下,标准库会在 Nushell 启动时加载到内存中(但不会自动导入)。
# 标准库

Nushell 内置了一个标准库,其中包含用原生 Nu 编写的实用命令。默认情况下,标准库会在 Nushell 启动时加载到内存中(但不会自动导入)。小型模块 `std/prelude` 是个例外:它会自动导入,提供 [`banner`](/commands/docs/banner.md) 和 [`pwd`](/commands/docs/pwd.md) 命令。

[[toc]]

Expand Down Expand Up @@ -60,12 +66,19 @@ use std *

- `use std/assert`: `assert` 及其子命令
- `use std/bench`: 基准测试命令 `bench`
- `use std/clip`: `clip copy52` 和 `clip paste52`,通过终端(OSC 52)复制到系统剪贴板或从中粘贴;以及 `clip prefix`,为待复制内容的每一行添加前缀
- `use std/config`: `config dark-theme` 和 `config light-theme`(用于 `$env.config.color_config` 的主题),以及 `config env-conversions`
- `use std/dirs`: 目录栈命令 `dirs` 及其子命令
- `use std/input`: `input display` 命令
- `use std/help`: 支持补全和其他功能的替代版 `help` 命令及其子命令
- `use std/iters`: 额外的 `iters` 前缀迭代命令
- `use std/iter`: 额外的 `iter` 前缀迭代命令,例如 `iter find` 和 `iter scan`
- `use std/log`: `log <subcommands>` 如 `log warning <msg>`
- `use std/math`: 数学常量如 `$math.E`。这些也可以像下面的形式 #2 那样作为定义导入
- `use std/random`: `random dice` 命令

::: tip
Nushell 还提供实验性的内置命令 `clip copy` 和 `clip paste`,它们直接使用操作系统的剪贴板,而不是 OSC 52。要试用它们,请用 `nu --experimental-options '[native-clip]'` 启动 Nushell。
:::

#### 2. 直接导入模块的定义(内容)

Expand All @@ -78,29 +91,34 @@ ls | to jsonl

通常使用 `use std/<submodule> *` (**带**通配符/`*`)导入的子模块:

- `use std/dt *`: 处理 `date` 值的额外命令
- `use std/dt *`: 处理 `datetime` 值的额外命令
- `use std/formats *`: 额外的 `to` 和 `from` 格式转换
- `use std/math *`: 不带前缀的数学常量,如 `$E`。注意上面的形式 #1 在阅读和维护代码时可能更易理解
- `use std/testing *`: 用于标记测试的 `@test`、`@ignore`、`@before-each`、`@before-all`、`@after-each` 和 `@after-all` 属性
- `use std/util *`: 各种实用命令,例如 `path add`、`repeat`、`null-device` 和 `structure`
- `use std/xml *`: 处理 XML 数据的额外命令

#### 3. `use std <submodule>`

也可以使用空格分隔的形式导入标准库子模块:

```nu
use std log
use std formats *
```

::: important
如[使用模块](./modules/using_modules.md#模块定义)中所述,这种形式(像 `use std *` 一样)会先将整个标准库加载到作用域中,然后再导入子模块。相比之下,上面的 #1 和 #2 中的斜杠分隔版本只会导入子模块,因此会快得多。

这种形式也不适用于包含同名命令的子模块。例如,`use std log` 只导入 `log` 命令本身,不会导入 `log info` 或其他子命令。请改用 `use std/log`。
:::

## 标准库候选模块

`std-rfc` 位于 [nushell 仓库](https://github.com/nushell/nushell/tree/main/crates/nu-std/std-rfc),作为标准库可能的添加项的暂存区。

如果你有兴趣为标准库添加内容,请通过 PR 提交代码到该仓库的 `std-rfc` 模块。我们也鼓励你安装这个模块并对即将成为候选的命令提供反馈。
`std-rfc` 随 Nushell 一同提供,因此可以像导入标准库子模块一样导入它的子模块,例如 `use std-rfc/str`。它目前包含 `conversions`、`date`、`iter`、`kv`、`path`、`pb`、`random`、`str`、`tables`、`url` 和 `xml` 子模块。

如果你有兴趣为标准库添加内容,请通过 PR 提交代码到该仓库的 `std-rfc` 模块。我们也鼓励你试用这些候选命令并提供反馈。

::: details 更多详情

Expand Down Expand Up @@ -132,17 +150,17 @@ use std formats *
nu --no-std-lib
```

在使用 `nu -c` 在子 shell 中运行命令时,这尤其有助于最小化开销。例如:
在使用 `nu -c` 在子 shell 中运行命令时,这尤其有助于减少开销。使用 `-c` 时,`$nu.startup-time` 表示 Nushell 在运行命令之前的启动耗时,因此可以比较两种方式:

```nu
nu --no-std-lib -n -c "$nu.startup-time"
# => 1ms 125µs 10ns
# => 9ms 650µs 250ns

nu -n -c "$nu.startup-time"
# => 4ms 889µs 576ns
# => 11ms 558µs 83ns
```

当以这种方式禁用时,你将无法导入标准库、它的任何子模块或使用它的任何命令。
当以这种方式禁用时,你将无法导入标准库、它的任何子模块或使用它的任何命令,包括 `std/prelude` 中的 `banner` 和 `pwd`。

## 在模块中使用 `std/log`

Expand All @@ -159,16 +177,16 @@ nu -n -c "$nu.startup-time"
view files
| enumerate | flatten
| where filename !~ '^std'
| where filename !~ '^entry'
| where filename !~ '^repl_entry'
| where {|file|
(view span $file.start $file.end) =~ 'use\W+std[^\/]'
(view span $file.start $file.end) =~ 'use\s+std(\s|;|$)'
}
```

编辑这些文件,使用上面[导入子模块](#导入子模块)部分推荐的语法。

::: note
如果 Nushell 库(例如来自 [nu_scripts 仓库](https://github.com/nushell/nu_scripts))、示例或文档使用了这种语法,请通过 issue 或 PR 报告。在 Nushell 0.99.0 发布后,这些将会逐步更新。
如果 Nushell 库(例如来自 [nu_scripts 仓库](https://github.com/nushell/nu_scripts))、示例或文档仍在使用这种语法,请通过 issue 或 PR 报告。

如果第三方模块使用了这种语法,请报告给作者/维护者进行更新。
:::
112 changes: 52 additions & 60 deletions zh-CN/book/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,43 @@

## 断言命令

Nushell 在标准库中提供了一组"断言"命令。
Nushell 在标准库中提供了一组“断言”命令。
你可以使用 `==`、`<=` 等内置的相等或大小比较运算符,或更复杂的命令来检查条件,并在预期条件不满足时手动抛出错误;不过,使用标准库提供的断言命令通常更方便!

我们确实可以使用内置的相等/顺序命令实现测试目的,如 `==` 或 `<=` 或更复杂的命令,并在预期条件失败时手动抛出错误,但使用标准库提供的内容可以说更容易!

在下文中,将假设 `std assert` 模块已在当前作用域中导入
下文假设 `std/assert` 模块已在当前作用域中导入:

```nu
use std/assert
```

每个断言的基础是 `std assert` 命令。如果条件不为真,它会生成一个错误。
所有断言都以 `assert` 命令为基础。如果条件不为真,它就会报错。

```nu
assert (1 == 2)
```

```
Error:
× Assertion failed.
╭─[entry #13:1:1]
1 │ assert (1 == 2)
· ───┬──
· ╰── It is not true.
╰────
# => Error: nu::shell::error
# =>
# => × Assertion failed.
# => ╭─[repl_entry #13:1:9]
# => 1 │ assert (1 == 2)
# => · ───┬──
# => · ╰── It is not true.
# => ╰────
```

可以选择设置一条消息来显示断言命令的意图、出了什么问题或预期是什么:

```nu
let a = 0
assert ($a == 19) $"The lockout code is wrong, received: ($a)"
```

```
Error:
× The lockout code is wrong, received: 13
╭─[entry #25:1:1]
1 │ let a = 0
2 │ assert ($a == 19) $"The lockout code is wrong, received: ($a)"
· ────┬───
· ╰── It is not true.
╰────
# => Error: nu::shell::error
# =>
# => × The lockout code is wrong, received: 0
# => ╭─[repl_entry #25:2:9]
# => 1 │ let a = 0
# => 2 │ assert ($a == 19) $"The lockout code is wrong, received: ($a)"
# => · ────┬───
# => · ╰── It is not true.
# => ╰────
```

有许多断言命令,它们的行为与基本命令完全相同,只是使用了适当的运算符。它们的附加价值是能够提供更好的错误消息。
Expand All @@ -54,16 +49,15 @@ Error:
let a = "foo"
let b = "bar"
assert ($b | str contains $a)
```

```
Error: × Assertion failed.
╭─[entry #5:3:8]
2 │ let b = "bar"
3 │ assert ($b | str contains $a)
· ───────────┬──────────
· ╰── It is not true.
╰────
# => Error: nu::shell::error
# =>
# => × Assertion failed.
# => ╭─[repl_entry #5:3:14]
# => 2 │ let b = "bar"
# => 3 │ assert ($b | str contains $a)
# => · ──────┬─────
# => · ╰── It is not true.
# => ╰────
```

而使用 `assert str contains`:
Expand All @@ -72,17 +66,16 @@ Error: × Assertion failed.
let a = "a needle"
let b = "haystack"
assert str contains $b $a
```

```
Error: × Assertion failed.
╭─[entry #7:3:21]
2 │ let b = "bar"
3 │ assert str contains $b $a
· ──┬──
· ╰─┤ This does not contain 'a needle'.
· │ value: "haystack"
╰────
# => Error: nu::shell::error
# =>
# => × `$left` does not have the correct length
# => ╭─[repl_entry #7:3:21]
# => 2 │ let b = "haystack"
# => 3 │ assert str contains $b $a
# => · ─┬ ─┬
# => · │ ╰── expected: a needle
# => · ╰── value: "haystack"
# => ╰────
```

一般来说,对于基本的 `assert` 命令,建议始终提供附加消息来显示出了什么问题。如果你不能使用任何内置的断言命令,你可以通过为 [`error make`](/commands/docs/error_make.md) 传递标签来为 `assert` 命令创建一个自定义的:
Expand All @@ -101,21 +94,20 @@ def "assert even" [number: int] {
```nu
let $a = 13
assert even $a
```

```
Error:
× Assertion failed.
╭─[entry #37:1:1]
1 │ assert even $a
· ─┬
· ╰── 13 is not an even number
╰────
# => Error: nu::shell::error
# =>
# => × Assertion failed.
# => ╭─[repl_entry #37:2:13]
# => 1 │ let $a = 13
# => 2 │ assert even $a
# => · ─┬
# => · ╰── 13 is not an even number
# => ╰────
```

## 运行测试

现在我们已经能够通过调用 `std assert` 中的命令来编写测试,能够运行它们并在出现问题时看到测试失败,在一切正确时看到测试通过,这将是很好的 :)
现在我们已经能够通过调用 `std/assert` 中的命令来编写测试,接下来就可以运行它们:有问题时测试失败,一切正确时测试通过 :)

### Nupm 包

Expand All @@ -142,7 +134,7 @@ Error:

```nu
# `fib n` 是第 n 个斐波那契数
export def fib [n: int] [ nothing -> int ] {
export def fib [n: int]: nothing -> int {
if $n == 0 {
return 0
} else if $n == 1 {
Expand Down Expand Up @@ -224,6 +216,6 @@ def "test show-ignored-test" [] {
}
```

这是一个简单的示例,但可以扩展以包含你可能期望的测试框架的许多功能,包括设置和拆卸函数以及跨文件的测试发现。
这是一个简单的示例,但可以扩展出测试框架中常见的许多功能,包括测试前的初始化、测试后的清理,以及跨文件发现测试。

[Nupm]: https://github.com/nushell/nupm
Loading