-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make constant mock available for go1.20 and above
- Loading branch information
Showing
18 changed files
with
215 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
//go:build go1.20 | ||
// +build go1.20 | ||
|
||
package ctxt | ||
|
||
const EnableTrapUntypedConst = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
//go:build !go1.20 | ||
// +build !go1.20 | ||
|
||
package ctxt | ||
|
||
const EnableTrapUntypedConst = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# API Summary | ||
When passing a variable pointer to `Patch` and `Mock`, xgo will lookup for package level variables and setup trap for accessing to these variables. | ||
|
||
Constant can only be patched via `PatchByName(pkg,name,replacer)`. | ||
|
||
# Limitation | ||
1. Only variables and consts of main module will be available for patching, | ||
2. Constant patching requires go>=1.20. | ||
|
||
# Examples | ||
## `Patch` on variable | ||
```go | ||
package patch | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/xhd2015/xgo/runtime/mock" | ||
) | ||
|
||
var a int = 123 | ||
|
||
func TestPatchVarTest(t *testing.T) { | ||
mock.Patch(&a, func() int { | ||
return 456 | ||
}) | ||
b := a | ||
if b != 456 { | ||
t.Fatalf("expect patched varaibel a to be %d, actual: %d", 456, b) | ||
} | ||
} | ||
|
||
``` | ||
|
||
Check [../test/patch_const/patch_var_test.go](../test/patch_const/patch_var_test.go) for more cases. | ||
|
||
## `PatchByName` on constant | ||
```go | ||
package patch_const | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/xhd2015/xgo/runtime/mock" | ||
) | ||
|
||
const N = 50 | ||
|
||
func TestPatchInElseShouldWork(t *testing.T) { | ||
mock.PatchByName("github.com/xhd2015/xgo/runtime/test/patch_const", "N", func() int { | ||
return 5 | ||
}) | ||
b := N*4 | ||
|
||
if b != 20 { | ||
t.Fatalf("expect b to be %d,actual: %d", 20, b) | ||
} | ||
} | ||
``` | ||
|
||
Check [../test/patch_const/patch_const_test.go](../test/patch_const/patch_const_test.go) for more cases. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
package sub | ||
|
||
const N = 50 | ||
|
||
type NameSet map[string]bool |
Oops, something went wrong.