From 23f7173e816ae21c10170bb18e9da3300eee5822 Mon Sep 17 00:00:00 2001 From: jaekwon Date: Fri, 5 Jul 2024 15:35:27 -0700 Subject: [PATCH] add tests --- .../demo/tests/p_crossrealm/p_crossrealm.gno | 24 +++++++ .../r/demo/tests/crossrealm/crossrealm.gno | 29 ++++++++ .../testdata/gno_test/realm_boundmethod.txtar | 71 +++++++++++++++++++ gnovm/pkg/gnolang/values.go | 2 - gnovm/tests/files/zrealm_crossrealm14.gno | 17 +++++ 5 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 examples/gno.land/p/demo/tests/p_crossrealm/p_crossrealm.gno create mode 100644 examples/gno.land/r/demo/tests/crossrealm/crossrealm.gno create mode 100644 gnovm/cmd/gno/testdata/gno_test/realm_boundmethod.txtar create mode 100644 gnovm/tests/files/zrealm_crossrealm14.gno diff --git a/examples/gno.land/p/demo/tests/p_crossrealm/p_crossrealm.gno b/examples/gno.land/p/demo/tests/p_crossrealm/p_crossrealm.gno new file mode 100644 index 00000000000..6d46203e98c --- /dev/null +++ b/examples/gno.land/p/demo/tests/p_crossrealm/p_crossrealm.gno @@ -0,0 +1,24 @@ +package p_crossrealm + +type Stringer interface { + String() string +} + +type Container struct { + A int + B Stringer +} + +func (c *Container) Touch() *Container { + c.A += 1 + return c +} + +func (c *Container) Print() { + println("A:", c.A) + if c.B == nil { + println("B: undefined") + } else { + println("B:", c.B.String()) + } +} diff --git a/examples/gno.land/r/demo/tests/crossrealm/crossrealm.gno b/examples/gno.land/r/demo/tests/crossrealm/crossrealm.gno new file mode 100644 index 00000000000..97273f642de --- /dev/null +++ b/examples/gno.land/r/demo/tests/crossrealm/crossrealm.gno @@ -0,0 +1,29 @@ +package crossrealm + +import ( + "gno.land/p/demo/tests/p_crossrealm" + "gno.land/p/demo/ufmt" +) + +type LocalStruct struct { + A int +} + +func (ls *LocalStruct) String() string { + return ufmt.Sprintf("LocalStruct{%d}", ls.A) +} + +// local is saved locally in this realm +var local *LocalStruct + +func init() { + local = &LocalStruct{A: 123} +} + +// Make1 returns a local object wrapped by a p struct +func Make1() *p_crossrealm.Container { + return &p_crossrealm.Container{ + A: 1, + B: local, + } +} diff --git a/gnovm/cmd/gno/testdata/gno_test/realm_boundmethod.txtar b/gnovm/cmd/gno/testdata/gno_test/realm_boundmethod.txtar new file mode 100644 index 00000000000..9d935df74c2 --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_test/realm_boundmethod.txtar @@ -0,0 +1,71 @@ +# Set up GNOROOT in the current directory. +mkdir $WORK/gnovm +symlink $WORK/gnovm/stdlibs -> $GNOROOT/gnovm/stdlibs +env GNOROOT=$WORK + +gno test -v ./examples/gno.land/r/demo/realm2 + +stderr '=== RUN TestDo' +stderr '--- PASS: TestDo.*' + +stderr '=== RUN file/realm2_filetest.gno' +stderr '--- PASS: file/realm2_filetest.*' + +-- examples/gno.land/p/demo/counter/gno.mod -- +module gno.land/p/demo/counter + +-- examples/gno.land/p/demo/counter/counter.gno -- +package counter + +type Counter struct { + n int +} + +func (c *Counter) Inc() { + c.n++ +} + +-- examples/gno.land/r/demo/realm1/realm1.gno -- +package realm1 + +import "gno.land/p/demo/counter" + +var c = counter.Counter{} + +func GetCounter() *counter.Counter { + return &c +} + +-- examples/gno.land/r/demo/realm2/realm2.gno -- +package realm2 + +import ( + "gno.land/r/demo/realm1" +) + +func Do() { + realm1.GetCounter().Inc() +} + +-- examples/gno.land/r/demo/realm2/realm2_filetest.gno -- +// PKGPATH: gno.land/r/tests +package tests + +import "gno.land/r/demo/realm2" + +func main() { + realm2.Do() + println("OK") +} + +// Output: +// OK + +-- examples/gno.land/r/demo/realm2/realm2_test.gno -- +package realm2 + +import "testing" + +func TestDo(t *testing.T) { + Do() +} diff --git a/gnovm/pkg/gnolang/values.go b/gnovm/pkg/gnolang/values.go index d38c083428c..125ae13fbcf 100644 --- a/gnovm/pkg/gnolang/values.go +++ b/gnovm/pkg/gnolang/values.go @@ -192,7 +192,6 @@ const ( PointerIndexNative = -3 // Base is *NativeValue. ) -/* func (pv *PointerValue) GetBase(store Store) Object { switch cbase := pv.Base.(type) { case nil: @@ -207,7 +206,6 @@ func (pv *PointerValue) GetBase(store Store) Object { panic("should not happen") } } -*/ // cu: convert untyped; pass false for const definitions // TODO: document as something that enables into-native assignment. diff --git a/gnovm/tests/files/zrealm_crossrealm14.gno b/gnovm/tests/files/zrealm_crossrealm14.gno new file mode 100644 index 00000000000..23451e6f5d1 --- /dev/null +++ b/gnovm/tests/files/zrealm_crossrealm14.gno @@ -0,0 +1,17 @@ +// PKGPATH: gno.land/r/crossrealm_test +package crossrealm_test + +import ( + crossrealm "gno.land/r/demo/tests/crossrealm" +) + +func main() { + // even though we are running within a realm, + // we aren't storing the result of crossrealm.Make1(), + // so this should print fine. + crossrealm.Make1().Touch().Print() +} + +// Output: +// A: 2 +// B: LocalStruct{123}