Skip to content

Commit

Permalink
refactor: TestSkipHeights => t.SkipHeights + remove TestSkipHeights
Browse files Browse the repository at this point in the history
  • Loading branch information
hthieu1110 committed Feb 1, 2025
1 parent 055cad0 commit c8b4af7
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ func TestAuctionEnd(t *testing.T) {

// Auction ends
highestBid = 3
std.TestSkipHeights(500)
t.SkipHeights(500)
shouldNoPanic(t, AuctionEnd)
shouldEqual(t, ended, true)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ func TestFull(t *testing.T) {

// Auction ends
{
std.TestSkipHeights(150)
t.SkipHeights(150)
shouldPanic(t, AuctionEnd)
shouldEqual(t, ended, false)

std.TestSkipHeights(301)
t.SkipHeights(301)
shouldNoPanic(t, AuctionEnd)
shouldEqual(t, ended, true)

Expand Down
6 changes: 3 additions & 3 deletions docs/how-to-guides/porting-solidity-to-gno.md
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ func TestAuctionEnd(t *testing.T) {

// Auction ends
highestBid = 3
std.TestSkipHeights(500)
t.SkipHeights(500)
shouldNoPanic(t, AuctionEnd)
shouldEqual(t, ended, true)

Expand Down Expand Up @@ -666,11 +666,11 @@ func TestFull(t *testing.T) {

// Auction ends
{
std.TestSkipHeights(150)
t.SkipHeights(150)
shouldPanic(t, AuctionEnd)
shouldEqual(t, ended, false)

std.TestSkipHeights(301)
t.SkipHeights(301)
shouldNoPanic(t, AuctionEnd)
shouldEqual(t, ended, true)

Expand Down
4 changes: 2 additions & 2 deletions examples/gno.land/p/demo/entropy/entropy_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestInstanceValue(t *testing.T) {
t.Errorf("should have the same result: new=%s, base=%s", sameHeightResult, baseResult)
}

std.TestSkipHeights(1)
t.SkipHeights(1)
differentHeightEntropy := New()
differentHeightResult := computeValue(t, differentHeightEntropy)

Expand All @@ -44,7 +44,7 @@ func TestInstanceValue64(t *testing.T) {
t.Errorf("should have the same result: new=%s, base=%s", sameHeightResult, baseResult)
}

std.TestSkipHeights(1)
t.SkipHeights(1)
differentHeightEntropy := New()
differentHeightResult := computeValue64(t, differentHeightEntropy)

Expand Down
3 changes: 2 additions & 1 deletion examples/gno.land/p/demo/entropy/z_filetest.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"std"
"testing"

"gno.land/p/demo/entropy"
)
Expand All @@ -27,7 +28,7 @@ func main() {
println(r.Value())
println(r.Value64())

std.TestSkipHeights(1)
testing.SkipHeights(1)
println("---")
r = entropy.New()
println(r.Value())
Expand Down
2 changes: 1 addition & 1 deletion examples/gno.land/p/moul/memo/memo_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func TestMemoizeWithValidator(t *testing.T) {
}

// Wait
std.TestSkipHeights(10)
t.SkipHeights(10)

// Second call
result = m.MemoizeWithValidator(tt.key, fn, isValid)
Expand Down
7 changes: 3 additions & 4 deletions examples/gno.land/r/sys/validators/v2/validators_test.gno
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package validators

import (
"testing"

"std"
"testing"

"gno.land/p/demo/avl"
"gno.land/p/demo/testutils"
Expand Down Expand Up @@ -45,7 +44,7 @@ func TestValidators_AddRemove(t *testing.T) {
// Make sure the validator is added
uassert.True(t, vp.IsValidator(val.Address))

std.TestSkipHeights(1)
t.SkipHeights(1)
}

for i := initialHeight; i < initialHeight+int64(len(vals)); i++ {
Expand Down Expand Up @@ -79,7 +78,7 @@ func TestValidators_AddRemove(t *testing.T) {
// Make sure the validator is removed
uassert.False(t, vp.IsValidator(val.Address))

std.TestSkipHeights(1)
t.SkipHeights(1)
}

for i := initialRemoveHeight; i < initialRemoveHeight+int64(len(vals)); i++ {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestSkipHeights(t *testing.T) {
shouldEQ(t, oldNow, 1234567890)

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

shouldEQ(t, std.GetHeight()-oldHeight, 3)
shouldEQ(t, time.Now().Unix()-oldNow, 15)
Expand Down
19 changes: 15 additions & 4 deletions gnovm/stdlibs/testing/testing.gno
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"std"
"strconv"
"strings"
"time"
)

// ----------------------------------------
Expand Down Expand Up @@ -220,15 +221,19 @@ func (t *T) SetContext(ctx Context) {
}

func (t *T) SetPreviousRealm(prevRealm std.Realm) {
t.SetContext(Context{PreviousRealm: prevRealm})
SetPreviousRealm(prevRealm)
}

func (t *T) SetOriginSend(send std.Coins, spend std.Coins) {
t.SetContext(Context{OriginSend: send, OriginSpend: spend})
SetOriginSend(send, spend)
}

func (t *T) SetOriginPkgAddress(addr std.Address) {
t.SetContext(Context{OriginPkgAddress: addr})
SetOriginPkgAddress(addr)
}

func (t *T) SkipHeights(count int64) {
SkipHeights(count)
}

func (t *T) log(s string) {
Expand Down Expand Up @@ -414,7 +419,7 @@ func TestSetContext(ctx Context) {

if !ctx.Time.IsZero() {
timestamp = ctx.Time.Unix()
timestampNano = ctx.Time.UnixNano()
timestampNano = 0
}

testSetContext(
Expand Down Expand Up @@ -443,3 +448,9 @@ func SetOriginSend(send std.Coins, spend std.Coins) {
func SetOriginPkgAddress(addr std.Address) {
TestSetContext(Context{OriginPkgAddress: addr})
}

func SkipHeights(count int64) {
toHeight := std.GetHeight() + count
toTime := time.Now().Unix() + (count * 5)
TestSetContext(Context{Height: toHeight, Time: time.Unix(toTime, 0)})
}
22 changes: 0 additions & 22 deletions gnovm/tests/stdlibs/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions gnovm/tests/stdlibs/std/std.gno
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package std

func AssertOriginCall() // injected
func IsOriginCall() bool // injected
func TestSkipHeights(count int64) // injected
func AssertOriginCall() // injected
func IsOriginCall() bool // injected

// TestSetRealm sets the realm for the current frame.
// After calling TestSetRealm, calling CurrentRealm() in the test function will yield the value of
Expand Down
7 changes: 0 additions & 7 deletions gnovm/tests/stdlibs/std/std.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,6 @@ func IsOriginCall(m *gno.Machine) bool {
panic("unable to determine if test is a _test or a _filetest")
}

func TestSkipHeights(m *gno.Machine, count int64) {
ctx := m.Context.(*TestExecContext)
ctx.Height += count
ctx.Timestamp += (count * 5)
m.Context = ctx
}

func X_callerAt(m *gno.Machine, n int) string {
if n <= 0 {
m.Panic(typedString("GetCallerAt requires positive arg"))
Expand Down

0 comments on commit c8b4af7

Please sign in to comment.