-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use generics to stub a single value with auto-reset (#21)
- Loading branch information
Showing
3 changed files
with
48 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package gostub | ||
|
||
// TestingT is a subset of the testing.TB interface used by gostub. | ||
type TestingT interface { | ||
Cleanup(func()) | ||
} | ||
|
||
// Value replaces the value at varPtr with stubVal. | ||
// The original value is reset at the end of the test via t.Cleanup | ||
// or can be reset using the returned function. | ||
func Value[T any](t TestingT, varPtr *T, stubVal T) (reset func()) { | ||
orig := *varPtr | ||
*varPtr = stubVal | ||
|
||
reset = func() { | ||
*varPtr = orig | ||
} | ||
t.Cleanup(reset) | ||
return reset | ||
} |