Skip to content

Commit

Permalink
feat(std): support timestamp skipping in test cases (#569)
Browse files Browse the repository at this point in the history
<!--- Provide a general summary of your changes in the Title above -->
<!--- If you need to use a more detailed template, append the query
param template=detailed_pr_template.md to the URL -->

# Description

support timestamp skipping in test cases

# How has this been tested?

test case run by gnodev
  • Loading branch information
r3v4s authored Jun 13, 2024
1 parent 931f3d2 commit a22ce24
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/reference/stdlibs/std/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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
27 changes: 27 additions & 0 deletions examples/gno.land/r/x/skip_height_to_skip_time/skiptime_test.gno
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 @@ -57,6 +57,7 @@ func IsOriginCall(m *gno.Machine) bool {
func TestSkipHeights(m *gno.Machine, count int64) {
ctx := m.Context.(*TestExecContext)
ctx.Height += count
ctx.Timestamp += (count * 5)
m.Context = ctx
}

Expand Down

0 comments on commit a22ce24

Please sign in to comment.