docs(zh-CN): translate code block(plain text) (#753)

Co-authored-by: neo <neo.dowithless@gmail.com>
This commit is contained in:
zdoc.app
2026-03-23 06:39:24 +08:00
committed by GitHub
parent fd2a8edb53
commit 4f6f587700
118 changed files with 1807 additions and 1835 deletions

View File

@@ -28,34 +28,34 @@ description: 为Go强制执行TDD工作流程。首先编写表驱动测试
## TDD 循环
```
RED → Write failing table-driven test
GREEN → Implement minimal code to pass
REFACTOR → Improve code, tests stay green
REPEAT → Next test case
RED → 编写失败的表格驱动测试
GREEN → 实现最小化代码以通过测试
REFACTOR → 改进代码,保持测试通过
REPEAT → 下一个测试用例
```
## 示例会话
````
User: /go-test I need a function to validate email addresses
用户: /go-test 我需要一个验证电子邮箱地址的函数
Agent:
# TDD Session: Email Validator
代理:
# TDD 会话: 电子邮件验证器
## Step 1: Define Interface
## 步骤 1: 定义接口
```go
// validator/email.go
package validator
// ValidateEmail checks if the given string is a valid email address.
// Returns nil if valid, error describing the issue otherwise.
// ValidateEmail 检查给定的字符串是否为有效的电子邮件地址。
// 如果有效则返回 nil否则返回描述问题的错误。
func ValidateEmail(email string) error {
panic("not implemented")
}
```
## Step 2: Write Table-Driven Tests (RED)
## 步骤 2: 编写表驱动测试 (RED)
```go
// validator/email_test.go
@@ -71,13 +71,13 @@ func TestValidateEmail(t *testing.T) {
email string
wantErr bool
}{
// Valid emails
// 有效邮箱
{"simple email", "user@example.com", false},
{"with subdomain", "user@mail.example.com", false},
{"with plus", "user+tag@example.com", false},
{"with dots", "first.last@example.com", false},
// Invalid emails
// 无效邮箱
{"empty string", "", true},
{"no at sign", "userexample.com", true},
{"no domain", "user@", true},
@@ -101,7 +101,7 @@ func TestValidateEmail(t *testing.T) {
}
```
## Step 3: Run Tests - Verify FAIL
## 步骤 3: 运行测试 - 验证失败
```bash
$ go test ./validator/...
@@ -113,9 +113,9 @@ $ go test ./validator/...
FAIL
```
Tests fail as expected (panic).
测试按预期失败 (panic)
## Step 4: Implement Minimal Code (GREEN)
## 步骤 4: 实现最小化代码 (GREEN)
```go
// validator/email.go
@@ -144,7 +144,7 @@ func ValidateEmail(email string) error {
}
```
## Step 5: Run Tests - Verify PASS
## 步骤 5: 运行测试 - 验证通过
```bash
$ go test ./validator/...
@@ -153,9 +153,9 @@ PASS
ok project/validator 0.003s
```
All tests passing!
所有测试通过!
## Step 6: Check Coverage
## 步骤 6: 检查覆盖率
```bash
$ go test -cover ./validator/...
@@ -165,9 +165,9 @@ coverage: 100.0% of statements
ok project/validator 0.003s
```
Coverage: 100%
覆盖率: 100%
## TDD Complete!
## TDD 完成!
````
## 测试模式