docs: 完成所有文档的中文翻译并应用到项目

This commit is contained in:
xuxiang
2026-01-28 00:12:54 +08:00
parent 0ced59a26b
commit e133f58e1c
76 changed files with 6808 additions and 6170 deletions

View File

@@ -1,60 +1,60 @@
---
description: Enforce TDD workflow for Go. Write table-driven tests first, then implement. Verify 80%+ coverage with go test -cover.
description: 强制执行 Go 的测试驱动开发TDD工作流。先编写表驱动测试Table-Driven Tests),然后进行实现。使用 go test -cover 验证 80% 以上的覆盖率。
---
# Go TDD Command
# Go TDD 命令
This command enforces test-driven development methodology for Go code using idiomatic Go testing patterns.
此命令使用地道的 Go 测试模式为 Go 代码强制执行测试驱动开发Test-Driven DevelopmentTDD方法论。
## What This Command Does
## 此命令的作用
1. **Define Types/Interfaces**: Scaffold function signatures first
2. **Write Table-Driven Tests**: Create comprehensive test cases (RED)
3. **Run Tests**: Verify tests fail for the right reason
4. **Implement Code**: Write minimal code to pass (GREEN)
5. **Refactor**: Improve while keeping tests green
6. **Check Coverage**: Ensure 80%+ coverage
1. **定义类型/接口**:首先搭建函数签名的支架
2. **编写表驱动测试(Table-Driven Tests**创建全面的测试用例RED/红灯)
3. **运行测试**:验证测试因预期的原因失败
4. **实现代码**:编写通过测试所需的最少代码(GREEN/绿灯)
5. **重构(Refactor**:在保持测试通过的同时优化代码
6. **检查覆盖率**:确保 80% 以上的覆盖率
## When to Use
## 何时使用
Use `/go-test` when:
- Implementing new Go functions
- Adding test coverage to existing code
- Fixing bugs (write failing test first)
- Building critical business logic
- Learning TDD workflow in Go
在以下场景使用 `/go-test`
- 实现新的 Go 函数
- 为现有代码增加测试覆盖率
- 修复 Bug先编写失败的测试
- 构建核心业务逻辑
- 在 Go 中学习 TDD 工作流Workflow
## TDD Cycle
## 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(循环) → 下一个测试用例
```
## Example Session
## 会话示例
```text
User: /go-test I need a function to validate email addresses
User: /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
@@ -70,13 +70,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},
@@ -100,7 +100,7 @@ func TestValidateEmail(t *testing.T) {
}
```
## Step 3: Run Tests - Verify FAIL
## 第 3 步:运行测试 - 验证失败
```bash
$ go test ./validator/...
@@ -112,9 +112,9 @@ $ go test ./validator/...
FAIL
```
Tests fail as expected (panic).
测试如预期般失败 (panic)
## Step 4: Implement Minimal Code (GREEN)
## 第 4 步:实现最少代码 (GREEN)
```go
// validator/email.go
@@ -143,7 +143,7 @@ func ValidateEmail(email string) error {
}
```
## Step 5: Run Tests - Verify PASS
## 第 5 步:运行测试 - 验证通过
```bash
$ go test ./validator/...
@@ -152,9 +152,9 @@ PASS
ok project/validator 0.003s
```
All tests passing!
所有测试通过!
## Step 6: Check Coverage
## 第 6 步:检查覆盖率
```bash
$ go test -cover ./validator/...
@@ -164,14 +164,14 @@ coverage: 100.0% of statements
ok project/validator 0.003s
```
Coverage: 100%
覆盖率:100%
## TDD Complete!
## TDD 完成!
```
## Test Patterns
## 测试模式
### Table-Driven Tests
### 表驱动测试(Table-Driven Tests
```go
tests := []struct {
name string
@@ -186,23 +186,23 @@ tests := []struct {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Function(tt.input)
// assertions
// 断言逻辑
})
}
```
### Parallel Tests
### 并行测试(Parallel Tests
```go
for _, tt := range tests {
tt := tt // Capture
tt := tt // 闭包捕获变量
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
// test body
// 测试主体
})
}
```
### Test Helpers
### 测试助手(Test Helpers
```go
func setupTestDB(t *testing.T) *sql.DB {
t.Helper()
@@ -212,57 +212,57 @@ func setupTestDB(t *testing.T) *sql.DB {
}
```
## Coverage Commands
## 覆盖率命令
```bash
# Basic coverage
# 基础覆盖率
go test -cover ./...
# Coverage profile
# 生成覆盖率分析文件
go test -coverprofile=coverage.out ./...
# View in browser
# 在浏览器中查看结果
go tool cover -html=coverage.out
# Coverage by function
# 按函数查看覆盖率
go tool cover -func=coverage.out
# With race detection
# 配合竞态检测运行
go test -race -cover ./...
```
## Coverage Targets
## 覆盖率目标
| Code Type | Target |
| 代码类型 | 目标 |
|-----------|--------|
| Critical business logic | 100% |
| Public APIs | 90%+ |
| General code | 80%+ |
| Generated code | Exclude |
| 核心业务逻辑 | 100% |
| 公共 API | 90%+ |
| 通用代码 | 80%+ |
| 生成的代码 | 排除 |
## TDD Best Practices
## TDD 最佳实践
**DO:**
- Write test FIRST, before any implementation
- Run tests after each change
- Use table-driven tests for comprehensive coverage
- Test behavior, not implementation details
- Include edge cases (empty, nil, max values)
**建议这样做:**
- **先**写测试,在进行任何实现之前
- 每次修改后都运行测试
- 使用表驱动测试以实现全面的覆盖
- 测试行为,而不是实现细节
- 包含边界情况空值、nil、最大值
**DON'T:**
- Write implementation before tests
- Skip the RED phase
- Test private functions directly
- Use `time.Sleep` in tests
- Ignore flaky tests
**不要这样做:**
- 在测试之前编写实现代码
- 跳过 RED红灯阶段
- 直接测试私有函数
- 在测试中使用 `time.Sleep`
- 忽略不稳定的测试Flaky tests
## Related Commands
## 相关命令
- `/go-build` - Fix build errors
- `/go-review` - Review code after implementation
- `/verify` - Run full verification loop
- `/go-build` - 修复构建错误
- `/go-review` - 实现后评审代码
- `/verify` - 运行完整验证循环
## Related
## 相关内容
- Skill: `skills/golang-testing/`
- Skill: `skills/tdd-workflow/`
- 技能(Skill`skills/golang-testing/`
- 技能(Skill`skills/tdd-workflow/`