Skip to content

Commit

Permalink
docs: add TestSetRealm reference docs (#2369)
Browse files Browse the repository at this point in the history
<!-- please provide a detailed description of the changes made in this
pull request. -->

## Description

This PR adds `TestSetRealm` docs, as well as related docs. It also fixes
up a few minor details.

Closes: #2273 

<details><summary>Contributors' checklist...</summary>

- [x] Added new tests, or not needed, or not feasible
- [x] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [x] Updated the official documentation or not needed
- [x] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [x] 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>

---------

Co-authored-by: Morgan <git@howl.moe>
  • Loading branch information
leohhhn and thehowl committed Jun 18, 2024
1 parent 5fdbce0 commit aa9c64a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 deletions.
77 changes: 73 additions & 4 deletions docs/reference/stdlibs/std/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ func TestSetOrigCaller(addr Address)
func TestSetOrigPkgAddr(addr Address)
func TestSetOrigSend(sent, spent Coins)
func TestIssueCoins(addr Address, coins Coins)
func TestSetRealm(realm Realm)
func NewUserRealm(address Address)
func NewCodeRealm(pkgPath string)
```

---

## TestSkipHeights

```go
func TestSkipHeights(count int64)
```
Expand All @@ -29,30 +33,34 @@ std.TestSkipHeights(100)
---

## TestSetOrigCaller

```go
func TestSetOrigCaller(addr Address)
```
Sets the current caller of the transaction to **addr**.

#### Usage
```go
std.TestSetOrigCaller("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
std.TestSetOrigCaller(std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"))
```
---

## TestSetOrigPkgAddr

```go
func TestSetOrigPkgAddr(addr Address)
```
Sets the current realm/package address to **addr**.
Sets the call entry realm address to **addr**.

#### Usage
```go
std.TestSetOrigPkgAddr("g1ecely4gjy0yl6s9kt409ll330q9hk2lj9ls3ec")
std.TestSetOrigPkgAddr(std.Address("g1ecely4gjy0yl6s9kt409ll330q9hk2lj9ls3ec"))
```

---

## TestSetOrigSend

```go
func TestSetOrigSend(sent, spent Coins)
```
Expand All @@ -65,17 +73,78 @@ std.TestSetOrigSend(sent, spent Coins)
---

## TestIssueCoins

```go
func TestIssueCoins(addr Address, coins Coins)
```

Issues testing context **coins** to **addr**.

#### Usage

```go
issue := std.Coins{{"coin1", 100}, {"coin2", 200}}
addr := "g1ecely4gjy0yl6s9kt409ll330q9hk2lj9ls3ec"
addr := std.Address("g1ecely4gjy0yl6s9kt409ll330q9hk2lj9ls3ec")
std.TestIssueCoins(addr, issue)
```

---

## TestSetRealm

```go
func TestSetRealm(rlm Realm)
```

Sets the realm for the current frame. After calling `TestSetRealm()`, calling
[`CurrentRealm()`](chain.md#currentrealm) in the same test function will yield the value of `rlm`, and
any `PrevRealm()` called from a function used after TestSetRealm will yield `rlm`.

Should be used in combination with [`NewUserRealm`](#newuserrealm) &
[`NewCodeRealm`](#newcoderealm).

#### Usage
```go
addr := std.Address("g1ecely4gjy0yl6s9kt409ll330q9hk2lj9ls3ec")
std.TestSetRealm(std.NewUserRealm(""))
// or
std.TestSetRealm(std.NewCodeRealm("gno.land/r/demo/users"))
```

---

## NewUserRealm

```go
func NewUserRealm(address Address) Realm
```

Creates a new user realm for testing purposes.

#### Usage
```go
addr := std.Address("g1ecely4gjy0yl6s9kt409ll330q9hk2lj9ls3ec")
userRealm := std.NewUserRealm(addr)
```

---

## NewCodeRealm

```go
func NewCodeRealm(pkgPath string)
```

Creates a new code realm for testing purposes.

#### Usage
```go
path := "gno.land/r/demo/boards"
codeRealm := std.NewCodeRealm(path)
```






Expand Down
2 changes: 1 addition & 1 deletion gnovm/tests/stdlibs/std/std.gno
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func TestSetOrigCaller(addr Address) { testSetOrigCaller(string(addr)) }
func TestSetOrigPkgAddr(addr Address) { testSetOrigPkgAddr(string(addr)) }

// TestSetRealm sets the realm for the current frame.
// After calling TestSetRealm, calling CurrentRealm() will yield the value of
// After calling TestSetRealm, calling CurrentRealm() in the test function will yield the value of
// rlm, while if a realm function is called, using PrevRealm() will yield rlm.
func TestSetRealm(rlm Realm) {
testSetRealm(string(rlm.addr), rlm.pkgPath)
Expand Down

0 comments on commit aa9c64a

Please sign in to comment.