-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(stdlibs/std)!: change TestSetPrevRealm to TestSetRealm (#2164)
This PR removes the `TestSetPrevRealm` and `TestSetPrevAddr`, added in #890, in favour of a new API `TestSetRealm`, which is more flexible in its behaviour. The function works by setting the result of `CurrentRealm()` for the frame in which the `TestSetRealm` function is called. Consequently, calling `PrevRealm` in any function called within will yield the value passed to `TestSetRealm`. `TestSetRealm` allows to transparently set "user" and "code" realms, by using two new costructors in the testing stdlibs, `std.NewUserRealm` and `std.NewCodeRealm`, allowing emulation of both kinds of callers. The call is not `TestSetPrevRealm` as it does not permanently "set" the value returned by PrevRealm, as if in a mock; it simply changes the Realm of the current frame, allowing "deep" calls to PrevRealm() to correctly return the value of the previous realm. <details><summary>Contributors' checklist...</summary> - [ ] Added new tests, or not needed, or not feasible - [ ] Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory - [ ] Updated the official documentation or not needed - [ ] No breaking changes were made, or a `BREAKING CHANGE: xxx` message was included in the description - [ ] Added references to related issues and PRs - [ ] Provided any useful hints for running manual tests - [ ] Added new benchmarks to [generated graphs](https://gnoland.github.io/benchmarks), if any. More info [here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md). </details>
- Loading branch information
Showing
18 changed files
with
297 additions
and
128 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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,48 @@ | ||
package main | ||
|
||
import ( | ||
"std" | ||
) | ||
|
||
func main() { | ||
PrintRealm() | ||
println(pad("CurrentRealm:"), std.CurrentRealm()) | ||
println(pad("PrevRealm:"), std.PrevRealm()) | ||
std.TestSetRealm(std.NewUserRealm("g1user")) | ||
PrintRealm() | ||
println(pad("CurrentRealm:"), std.CurrentRealm()) | ||
println(pad("PrevRealm:"), std.PrevRealm()) | ||
std.TestSetRealm(std.NewCodeRealm("gno.land/r/demo/users")) | ||
PrintRealm() | ||
println(pad("CurrentRealm:"), std.CurrentRealm()) | ||
println(pad("PrevRealm:"), std.PrevRealm()) | ||
} | ||
|
||
func pad(s string) string { | ||
for len(s) < 26 { | ||
s += " " | ||
} | ||
return s | ||
} | ||
|
||
func PrintRealm() { | ||
println(pad("PrintRealm: CurrentRealm:"), std.CurrentRealm()) | ||
println(pad("PrintRealm: PrevRealm:"), std.PrevRealm()) | ||
} | ||
|
||
// Because this is the context of a package, using PrintRealm() | ||
// should not change the output of the main function. | ||
|
||
// Output: | ||
// PrintRealm: CurrentRealm: (struct{("g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm" std.Address),("" string)} std.Realm) | ||
// PrintRealm: PrevRealm: (struct{("g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm" std.Address),("" string)} std.Realm) | ||
// CurrentRealm: (struct{("g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm" std.Address),("" string)} std.Realm) | ||
// PrevRealm: (struct{("g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm" std.Address),("" string)} std.Realm) | ||
// PrintRealm: CurrentRealm: (struct{("g1user" std.Address),("" string)} std.Realm) | ||
// PrintRealm: PrevRealm: (struct{("g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm" std.Address),("" string)} std.Realm) | ||
// CurrentRealm: (struct{("g1user" std.Address),("" string)} std.Realm) | ||
// PrevRealm: (struct{("g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm" std.Address),("" string)} std.Realm) | ||
// PrintRealm: CurrentRealm: (struct{("g17m4ga9t9dxn8uf06p3cahdavzfexe33ecg8v2s" std.Address),("gno.land/r/demo/users" string)} std.Realm) | ||
// PrintRealm: PrevRealm: (struct{("g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm" std.Address),("" string)} std.Realm) | ||
// CurrentRealm: (struct{("g17m4ga9t9dxn8uf06p3cahdavzfexe33ecg8v2s" std.Address),("gno.land/r/demo/users" string)} std.Realm) | ||
// PrevRealm: (struct{("g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm" std.Address),("" string)} std.Realm) |
Oops, something went wrong.