Skip to content

Commit 6221dd6

Browse files
lvan100lianghuan
authored andcommitted
docs(README): update readme doc
1 parent 458f151 commit 6221dd6

File tree

4 files changed

+568
-1
lines changed

4 files changed

+568
-1
lines changed

CODE_OF_CONDUCT.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Contributor Code of Conduct
2+
3+
Thank you for your interest in and support of the Go-Spring project!
4+
5+
To cultivate an open, respectful, inclusive, and professional community, we ask that all participants follow this Code
6+
of Conduct in every interaction—whether reporting issues, submitting code, engaging in discussions, or contributing in
7+
any other way.
8+
9+
## Our Commitment
10+
11+
We are committed to providing a harassment-free experience for everyone, regardless of age, background, identity, or
12+
experience level. We value and support diversity and inclusivity within our community.
13+
14+
## Encouraged Behavior
15+
16+
- Communicate with respect and courtesy.
17+
- Welcome and consider constructive feedback.
18+
- Appreciate diverse perspectives and technical choices.
19+
- Demonstrate patience and empathy in collaborations.
20+
- Contribute positively to a welcoming and supportive environment.
21+
22+
## Unacceptable Behavior
23+
24+
- Use of discriminatory, abusive, or offensive language or conduct.
25+
- Harassment, threats, or personal attacks.
26+
- Sharing explicit content or inappropriate links.
27+
- Deliberate disruption of constructive efforts.
28+
- Impersonation or violation of personal privacy.
29+
30+
## Enforcement
31+
32+
Project maintainers are responsible for upholding this Code of Conduct. They have the authority to remove, edit, or
33+
reject comments, commits, code, wiki edits, issues, or other contributions that violate these guidelines, and to take
34+
further action as necessary.
35+
36+
## Reporting Violations
37+
38+
If you witness or experience a violation of this Code of Conduct, please contact the maintainers via email or submit an
39+
anonymous issue. All reports will be handled with discretion and taken seriously.
40+
41+
---
42+
43+
# 贡献者行为准则
44+
45+
首先,感谢你关注并支持 Go-Spring 项目!
46+
47+
为营造一个开放、友善、包容和专业的社区氛围,我们制定了本行为准则。无论你是报告问题、提交代码、参与讨论,还是以其他形式参与项目,都请遵循以下准则。
48+
49+
## 我们的承诺
50+
51+
我们承诺为每一位参与者提供一个免受骚扰、歧视和攻击性行为干扰的环境。我们欢迎来自不同背景、经验和身份的贡献者,共同建设一个多元、包容的社区。
52+
53+
## 倡导的行为
54+
55+
- 保持尊重、礼貌的沟通方式;
56+
- 乐于接受建设性的意见与反馈;
57+
- 尊重不同的观点与技术选择;
58+
- 在协作中体现耐心与包容;
59+
- 积极参与,共建积极向上的社区氛围。
60+
61+
## 不被接受的行为
62+
63+
- 使用歧视性、侮辱性或攻击性的言语与行为;
64+
- 进行人身攻击、骚扰或威胁;
65+
- 发布淫秽内容或不当链接;
66+
- 蓄意干扰他人正常贡献;
67+
- 冒充他人或侵犯他人隐私。
68+
69+
## 执行与维护
70+
71+
项目维护者有权也有责任删除、修改或拒绝任何违反行为准则的评论、提交、代码、Wiki 编辑、Issue 或其他形式的贡献内容,必要时可采取进一步措施。
72+
73+
## 如何报告问题
74+
75+
若你遇到违反行为准则的情况,请通过电子邮件联系项目维护者,或通过 Issue 匿名举报。我们承诺认真对待每一份举报,并确保信息保密。

CONTRIBUTING.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Contributing to Go-Spring
2+
3+
First of all, thank you for your interest in and support of the Go-Spring project!
4+
5+
We welcome all kinds of contributions, including reporting issues, improving documentation, fixing bugs, and developing
6+
new features. Please follow the guidelines below to contribute:
7+
8+
## Submitting Issues
9+
10+
- Before submitting, please search existing issues to avoid duplicates.
11+
- Provide clear reproduction steps, expected behavior, and actual results.
12+
- If available, include error logs and relevant environment information.
13+
14+
## Submitting Pull Requests
15+
16+
1. **Fork the repository and create a new branch**
17+
```bash
18+
git checkout -b feature/your-feature-name
19+
```
20+
21+
2. **Maintain consistent coding style**
22+
- Follow Go’s official style guidelines (use `gofmt`, `golint`, `go vet`)
23+
- It’s recommended to use [`golangci-lint`](https://github.com/golangci/golangci-lint) for local linting
24+
25+
3. **Write tests**
26+
- All new features must include unit tests
27+
- Use Go’s built-in `testing` package, and name test files as `xxx_test.go`
28+
29+
4. **Update documentation (if applicable)**
30+
31+
5. **Submit and create a Pull Request**
32+
- Clearly describe the purpose, changes made, and testing results
33+
- Link relevant issues (if any)
34+
35+
## Local Development Environment
36+
37+
- Go version: Latest stable release is recommended (e.g., `go1.21+`)
38+
- Use Go Modules for dependency management
39+
- Make sure all tests pass:
40+
```bash
41+
go test ./...
42+
```
43+
44+
## Contact Us
45+
46+
If you have any questions, feel free to open an issue or join the discussion forum.
47+
48+
Thank you for contributing!
49+
50+
---
51+
52+
# Contributing to Go-Spring
53+
54+
首先,感谢你关注并支持 Go-Spring 项目!
55+
56+
我们欢迎各种形式的贡献,包括但不限于 Issue 提交、文档完善、Bug 修复、功能开发等。请按照以下指引参与贡献:
57+
58+
## 提交 Issue
59+
60+
- 在提交前,请先搜索现有的 Issue,避免重复提交。
61+
- 请提供清晰的复现步骤、预期行为以及实际结果。
62+
- 如有错误日志或运行环境信息,请一并附上。
63+
64+
## 提交 Pull Request
65+
66+
1. **Fork 仓库并创建新分支**
67+
```bash
68+
git checkout -b feature/your-feature-name
69+
```
70+
71+
2. **保持一致的代码风格**
72+
- 遵循 Go 官方代码规范(使用 `gofmt``golint``go vet`
73+
- 推荐使用 [`golangci-lint`](https://github.com/golangci/golangci-lint) 进行本地代码检查
74+
75+
3. **编写测试用例**
76+
- 所有新功能必须配备单元测试
77+
- 使用 Go 内置的 `testing` 包,测试文件应命名为 `xxx_test.go`
78+
79+
4. **更新相关文档(如有变更)**
80+
81+
5. **提交并创建 Pull Request**
82+
- 说明 PR 的目的、变更内容、测试情况等
83+
- 关联相关 Issue(如有)
84+
85+
## 本地开发环境要求
86+
87+
- Go 版本:推荐使用最新版稳定版(如 `go1.21+`
88+
- 使用 Go Modules 进行依赖管理
89+
- 确保测试全部通过:
90+
```bash
91+
go test ./...
92+
```
93+
94+
## 联系我们
95+
96+
如有疑问,欢迎通过 Issue 与我们联系,或参与项目的讨论区。
97+
98+
感谢你的贡献!

README.md

Lines changed: 199 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,200 @@
11
# mock
2-
A modern and type-safe mocking library for Go with full support for generics.
2+
3+
[English](README.md) | [中文](README_CN.md)
4+
5+
**mock** is a modern, type-safe mocking library for the Go programming language, fully supporting generic programming.
6+
It provides a simple and easy-to-use interface that helps developers easily create and manage mock objects, thereby
7+
improving the quality and efficiency of unit testing. The library is designed to address the lack of type safety and the
8+
complexity of traditional mocking tools in Go.
9+
10+
## Key Features
11+
12+
* **Type Safety**: Utilizes Go 1.18+ generics to ensure compile-time safety and avoid runtime type errors
13+
* **Multiple Mocking Modes**:
14+
* `Handle` Mode: Directly handle function calls
15+
* `When/Return` Mode: Conditional mock returns
16+
* **Flexible Method Matching**: Supports different numbers and types of parameters and return values (up to 5 parameters
17+
and 5 return values)
18+
* **Context Support**: Provides integration with the `context` package, making it easier to test in distributed systems
19+
* **Auto Reset Functionality**: The `Manager` provides a `Reset` method to easily reset all mockers to their initial
20+
state
21+
* **Detailed Error Messages**: Offers clear error prompts when no matching mock code is found or when multiple matches
22+
exist
23+
24+
## Usage Example
25+
26+
Below is a simple usage example:
27+
28+
```go
29+
package mock_test
30+
31+
import (
32+
"context"
33+
"reflect"
34+
"testing"
35+
36+
"github.com/go-spring/mock"
37+
"github.com/go-spring/mock/internal/assert"
38+
)
39+
40+
type Trace struct {
41+
TraceId string
42+
}
43+
44+
type Request struct {
45+
Token string
46+
}
47+
48+
type Response struct {
49+
Message string
50+
}
51+
52+
type Client struct{}
53+
54+
var clientType = reflect.TypeFor[Client]()
55+
56+
func (c *Client) Get(ctx context.Context, req *Request, trace *Trace) (*Response, error) {
57+
if ret, ok := mock.InvokeContext(ctx, clientType, "Get", ctx, req, trace); ok {
58+
return mock.Unbox2[*Response, error](ret)
59+
}
60+
return &Response{Message: "9:xxx"}, nil
61+
}
62+
63+
// MockGet registers a mock implementation for the Get method.
64+
func MockGet(r *mock.Manager) *mock.Mocker32[context.Context, *Request, *Trace, *Response, error] {
65+
return mock.NewMocker32[context.Context, *Request, *Trace, *Response, error](r, clientType, "Get")
66+
}
67+
68+
func TestMockWithContext(t *testing.T) {
69+
var c Client
70+
71+
// Test case: Unmocked
72+
{
73+
resp, err := c.Get(t.Context(), &Request{}, &Trace{})
74+
assert.Nil(t, err)
75+
assert.Equal(t, resp.Message, "9:xxx")
76+
}
77+
78+
r := mock.NewManager()
79+
ctx := r.BindTo(t.Context())
80+
81+
// Test case: When && Return
82+
{
83+
r.Reset()
84+
MockGet(r).
85+
When(func(ctx context.Context, req *Request, trace *Trace) bool {
86+
return req.Token == "1:abc"
87+
}).
88+
Return(func() (resp *Response, err error) {
89+
return &Response{Message: "1:abc"}, nil
90+
})
91+
92+
resp, err := c.Get(ctx, &Request{Token: "1:abc"}, &Trace{})
93+
assert.Nil(t, err)
94+
assert.Equal(t, resp.Message, "1:abc")
95+
}
96+
97+
// Test case: Handle
98+
{
99+
r.Reset()
100+
MockGet(r).
101+
Handle(func(ctx context.Context, req *Request, trace *Trace) (resp *Response, err error) {
102+
return &Response{Message: "4:xyz"}, nil
103+
})
104+
105+
resp, err := c.Get(ctx, &Request{Token: "4:xyz"}, &Trace{})
106+
assert.Nil(t, err)
107+
assert.Equal(t, resp.Message, "4:xyz")
108+
}
109+
110+
// Test case: Invalid Handle
111+
{
112+
r.Reset()
113+
MockGet(r).Handle(nil)
114+
115+
resp, err := c.Get(ctx, &Request{}, &Trace{})
116+
assert.Nil(t, err)
117+
assert.Equal(t, resp.Message, "9:xxx")
118+
}
119+
}
120+
121+
type ClientInterface interface {
122+
Query(req *Request, trace *Trace) (*Response, error)
123+
}
124+
125+
// MockClient is a mock implementation of ClientInterface.
126+
type MockClient struct {
127+
r *mock.Manager
128+
}
129+
130+
var mockClientType = reflect.TypeFor[MockClient]()
131+
132+
// NewMockClient creates a new instance of MockClient.
133+
func NewMockClient(r *mock.Manager) *MockClient {
134+
return &MockClient{r}
135+
}
136+
137+
// Query mocks the Query method by invoking a registered mock implementation.
138+
func (c *MockClient) Query(req *Request, trace *Trace) (*Response, error) {
139+
if ret, ok := mock.Invoke(c.r, mockClientType, "Query", req, trace); ok {
140+
return mock.Unbox2[*Response, error](ret)
141+
}
142+
panic("mock error")
143+
}
144+
145+
// MockQuery registers a mock implementation for the Query method.
146+
func (c *MockClient) MockQuery() *mock.Mocker22[*Request, *Trace, *Response, error] {
147+
return mock.NewMocker22[*Request, *Trace, *Response, error](c.r, mockClientType, "Query")
148+
}
149+
150+
func TestMockNoContext(t *testing.T) {
151+
r := mock.NewManager()
152+
153+
var c ClientInterface
154+
mc := NewMockClient(r)
155+
c = mc
156+
157+
// Test case: When && Return
158+
{
159+
r.Reset()
160+
mc.MockQuery().
161+
When(func(req *Request, trace *Trace) bool {
162+
return req.Token == "1:abc"
163+
}).
164+
Return(func() (resp *Response, err error) {
165+
return &Response{Message: "1:abc"}, nil
166+
})
167+
168+
resp, err := c.Query(&Request{Token: "1:abc"}, &Trace{})
169+
assert.Nil(t, err)
170+
assert.Equal(t, resp.Message, "1:abc")
171+
}
172+
173+
// Test case: Handle
174+
{
175+
r.Reset()
176+
mc.MockQuery().
177+
Handle(func(req *Request, trace *Trace) (resp *Response, err error) {
178+
return &Response{Message: "4:xyz"}, nil
179+
})
180+
181+
resp, err := c.Query(&Request{Token: "4:xyz"}, &Trace{})
182+
assert.Nil(t, err)
183+
assert.Equal(t, resp.Message, "4:xyz")
184+
}
185+
186+
// Test case: Invalid Handle
187+
{
188+
r.Reset()
189+
mc.MockQuery().Handle(nil)
190+
191+
assert.Panic(t, func() {
192+
_, _ = c.Query(&Request{}, &Trace{})
193+
}, "mock error")
194+
}
195+
}
196+
```
197+
198+
## License
199+
200+
This project is licensed under the Apache License Version 2.0.

0 commit comments

Comments
 (0)