https://github.com/for-acgn/monkey
monkey is a library to make patch for unit tests
https://github.com/for-acgn/monkey
monkey patch
Last synced: 8 months ago
JSON representation
monkey is a library to make patch for unit tests
- Host: GitHub
- URL: https://github.com/for-acgn/monkey
- Owner: For-ACGN
- License: mit
- Created: 2023-04-29T12:22:13.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-03-17T11:46:17.000Z (about 2 years ago)
- Last Synced: 2025-01-14T10:19:59.528Z (over 1 year ago)
- Topics: monkey, patch
- Language: Go
- Homepage:
- Size: 91.8 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# monkey
monkey is a library to make patch for unit tests, this repository modified some from [gomonkey](https://github.com/agiledragon/gomonkey).\
it can patch exported/unexported methods from exported/unexported structure.
## Example
### Patch function
```go
patch := func(a ...interface{}) (int, error) {
return fmt.Print("what?!")
}
pg := monkey.Patch(fmt.Println, patch)
defer pg.Unpatch()
// output: what?!
fmt.Println("hello!")
```
### Patch method
#### with receiver
```go
var r *bytes.Reader
patch := func(*bytes.Reader, b []byte) (int, error) {
return 0, nil
}
pg := monkey.PatchMethod(r, "Read", patch)
defer pg.Unpatch()
reader := bytes.NewReader([]byte("hello"))
buf := make([]byte, 1024)
// output: 0
fmt.Println(reader.Read(buf))
```
#### ignore receiver
```go
var r *bytes.Reader
patch := func(b []byte) (int, error) {
return 0, nil
}
pg := monkey.PatchMethod(r, "Read", patch)
defer pg.Unpatch()
reader := bytes.NewReader([]byte("hello"))
buf := make([]byte, 1024)
// output: 0
fmt.Println(reader.Read(buf))
```
## Original
https://github.com/bouk/monkey
https://github.com/agiledragon/gomonkey