Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support timestamp skipping in test cases #569

Merged
merged 4 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/reference/stdlibs/std/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func TestSkipHeights(count int64)
```
Modifies the block height variable by skipping **count** blocks.

It also increases block timestamp by 5 seconds for every single count

#### Usage
```go
std.TestSkipHeights(100)
Expand Down
10 changes: 5 additions & 5 deletions examples/gno.land/p/demo/rand/rand0_filetest.gno
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func main() {
// 177
// 802
// ---
// 450
// 78
// 777
// 15
// 339
// 269
// 233
// 591
// 936
// 908
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package skiptime
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package skiptime

import (
"std"
"testing"
"time"
)

func TestSkipHeights(t *testing.T) {
oldHeight := std.GetHeight()
shouldEQ(t, oldHeight, 123)

oldNow := time.Now().Unix()
shouldEQ(t, oldNow, 1234567890)

// skip 3 blocks == 15 seconds
std.TestSkipHeights(3)

shouldEQ(t, std.GetHeight()-oldHeight, 3)
shouldEQ(t, time.Now().Unix()-oldNow, 15)
}

func shouldEQ(t *testing.T, got, expected int64) {
if got != expected {
t.Fatalf("expected %d, got %d.", expected, got)
}
}
1 change: 1 addition & 0 deletions gnovm/tests/stdlibs/std/std.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func TestCurrentRealm(m *gno.Machine) string {
func TestSkipHeights(m *gno.Machine, count int64) {
ctx := m.Context.(std.ExecContext)
ctx.Height += count
ctx.Timestamp += (count * 5)
m.Context = ctx
}

Expand Down
Loading