From f99dfa59efc1dfa7d2e94ffbacd9f29fe96d9224 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Tue, 7 Mar 2023 17:28:29 +0900 Subject: [PATCH] feat: skip timestamp in test feat: add test.gno feat: TestSkipTimestamps now takes time.Duration feat: use time instead of std.GetTimestamp --- .../gno.land/r/demo/mtimestamp/mtimestamp.gno | 1 + .../r/demo/mtimestamp/mtimestamp_test.gno | 23 +++++++++++++++++++ gnovm/tests/imports.go | 23 +++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 examples/gno.land/r/demo/mtimestamp/mtimestamp.gno create mode 100644 examples/gno.land/r/demo/mtimestamp/mtimestamp_test.gno diff --git a/examples/gno.land/r/demo/mtimestamp/mtimestamp.gno b/examples/gno.land/r/demo/mtimestamp/mtimestamp.gno new file mode 100644 index 00000000000..6d1cacd7dcb --- /dev/null +++ b/examples/gno.land/r/demo/mtimestamp/mtimestamp.gno @@ -0,0 +1 @@ +package mtimestamp \ No newline at end of file diff --git a/examples/gno.land/r/demo/mtimestamp/mtimestamp_test.gno b/examples/gno.land/r/demo/mtimestamp/mtimestamp_test.gno new file mode 100644 index 00000000000..304a8920668 --- /dev/null +++ b/examples/gno.land/r/demo/mtimestamp/mtimestamp_test.gno @@ -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)) + } +} \ No newline at end of file diff --git a/gnovm/tests/imports.go b/gnovm/tests/imports.go index e4c95b79a5f..f8a5b302853 100644 --- a/gnovm/tests/imports.go +++ b/gnovm/tests/imports.go @@ -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