Playground
contract.gno
// Package importdemo shows a realm importing and composing packages from
// ANOTHER namespace — the whole point of the demo.
//
// v2 imported gno.land/p/archive/dom, which is published on no chain we target
// (absent on both sapphire and pearl), so it type-checked locally but could
// never be deployed: `addpkg` failed with
// `could not import gno.land/p/archive/dom (unknown import path)`. v2 is
// archived (`ignore = true`); this version rebuilds the same thread-of-posts
// idea on foreign packages that are actually on chain — `p/nt/avl/v0` for
// ordered storage and `p/nt/ufmt/v0` for formatting.
//
// Deliberately keeps no realm state: Render builds the thread on each call, so
// output depends only on the code and the demo is reproducible.
package importdemo
import (
"strconv"
"strings"
"gno.land/p/nt/avl/v0"
"gno.land/p/nt/ufmt/v0"
)
// thread is a titled list of posts, kept in an avl.Tree so iteration is ordered
// by key rather than by insertion — the same guarantee the realm needs for a
// stable Render.
type thread struct {
name string
posts *avl.Tree // zero-padded index -> *post
n int
}
type post struct {
title string
body string
}
func newThread(name string) *thread {
return &thread{name: name, posts: avl.NewTree()}
}
// addPost appends a post. The key is zero-padded so avl's lexicographic order
// matches insertion order past 9 entries ("10" would otherwise sort before "9").
func (t *thread) addPost(title, body string) {
t.posts.Set(padIdx(t.n), &post{title: title, body: body})
t.n++
}
// padIdx renders i as a fixed-width, zero-padded key.
//
// Done by hand on purpose: gno's ufmt supports NO width or padding flags, so
// ufmt.Sprintf("%03d", 7) yields "7", not "007" — silently, with no error. Keys
// built that way sort as "0","1","10","11","2", which quietly reorders every
// thread past nine posts (TestOrderSurvivesPastNinePosts covers exactly this).
func padIdx(i int) string {
s := strconv.Itoa(i)
for len(s) < 3 {
s = "0" + s
}
return s
}
// String renders the thread as Markdown.
func (t *thread) String() string {
var b strings.Builder
b.WriteString(ufmt.Sprintf("# [thread] %s\n\n", t.name))
t.posts.Iterate("", "", func(_ string, v any) bool {
p := v.(*post)
b.WriteString(ufmt.Sprintf("## %s\n%s\n", p.title, p.body))
return false
})
return b.String()
}
// Render renders the demo thread for gnoweb.
func Render(path string) string {
t := newThread("Hello!")
t.addPost("Foo", "foo foo foo")
t.addPost("Bar", "bar bar bar")
return t.String()
}
contract_test.gno
package importdemo
import (
"strings"
"testing"
"gno.land/p/nt/uassert/v0"
)
func TestRender(t *testing.T) {
got := Render("")
expected := `
# [thread] Hello!
## Foo
foo foo foo
## Bar
bar bar bar
`
uassert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(got))
}
// Render must not depend on realm state: two calls have to agree, and the
// second must not accumulate the first one's posts.
func TestRenderIsPure(t *testing.T) {
uassert.Equal(t, Render(""), Render(""))
uassert.Equal(t, 2, strings.Count(Render(""), "## "))
}
// Posts come back in insertion order. The keys are zero-padded precisely so
// avl's lexicographic ordering does not put "10" before "9" past nine posts.
func TestOrderSurvivesPastNinePosts(t *testing.T) {
th := newThread("many")
for i := 0; i < 12; i++ {
th.addPost(ufmtIdx(i), "body")
}
out := th.String()
// post 9 must appear before post 10
if strings.Index(out, "## p9\n") > strings.Index(out, "## p10\n") {
t.Fatalf("insertion order lost past 9 posts:\n%s", out)
}
uassert.Equal(t, 12, strings.Count(out, "## "))
}
func ufmtIdx(i int) string {
return "p" + itoa(i)
}
func itoa(i int) string {
if i == 0 {
return "0"
}
var b []byte
for i > 0 {
b = append([]byte{byte('0' + i%10)}, b...)
i /= 10
}
return string(b)
}
gnomod.toml
module = "gno.land/r/nym-moultest123/demo/importdemo/v0" gno = "0.9" [addpkg] creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv" height = 186406 max_deposit = "5000000ugnot"
render_example_test.gno
package importdemo
// ExampleRender pins the realm's Render output as a testable example.
func ExampleRender() {
print(Render(""))
// Output:
// # [thread] Hello!
//
// ## Foo
// foo foo foo
// ## Bar
// bar bar bar
}