Skip to content

Commit

Permalink
feat: skip timestamp in test
Browse files Browse the repository at this point in the history
feat: add test.gno

feat: TestSkipTimestamps now takes time.Duration

feat: use time instead of std.GetTimestamp
  • Loading branch information
r3v4s committed Apr 11, 2023
1 parent 0eb5ff7 commit f99dfa5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/gno.land/r/demo/mtimestamp/mtimestamp.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package mtimestamp
23 changes: 23 additions & 0 deletions examples/gno.land/r/demo/mtimestamp/mtimestamp_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package mtimestamp

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


func TestSkipDuration(t *testing.T) {
// Duration 3hr 2min 1sec
myDur := time.Second * 1
myDur += time.Minute * 2
myDur += time.Hour * 3

start := time.Now()
std.TestSkipTimestamps(myDur)
end := time.Now()

if end.Sub(start) != myDur {
t.Errorf("time diff between start and end should be equal to myDur %v), but got %v", myDur, end.Sub(start))
}
}
23 changes: 23 additions & 0 deletions gnovm/tests/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,29 @@ func testPackageInjector(store gno.Store, pn *gno.PackageNode) {
m.Context = ctx
},
)
pn.DefineNative("TestSkipTimestamps",
gno.Flds( // params
"timestamp", gno.AnyT(), // NOTE: should be time.Duration
),
gno.Flds( // results
),
func(m *gno.Machine) {
arg0 := m.LastBlock().GetParams1().TV

d := arg0.GetInt64()
sec := d / int64(time.Second)
nano := d % int64(time.Second)

ctx := m.Context.(stdlibs.ExecContext)
ctx.Timestamp += sec
ctx.TimestampNano += nano
if ctx.TimestampNano >= int64(time.Second) {
ctx.Timestamp += 1
ctx.TimestampNano -= int64(time.Second)
}
m.Context = ctx
},
)
// TODO: move elsewhere.
pn.DefineNative("ClearStoreCache",
gno.Flds( // params
Expand Down

0 comments on commit f99dfa5

Please sign in to comment.