diff --git a/examples/gno.land/r/gnoland/faucet/admin.gno b/examples/gno.land/r/gnoland/faucet/admin.gno index 000adb8f310..a4916c5af02 100644 --- a/examples/gno.land/r/gnoland/faucet/admin.gno +++ b/examples/gno.land/r/gnoland/faucet/admin.gno @@ -44,7 +44,7 @@ func AdminAddController(addr std.Address) string { size := gControllers.Size() - if size >= gControllersSize { + if size >= gControllersMaxSize { return "can not add more controllers than allowed" } diff --git a/examples/gno.land/r/gnoland/faucet/faucet.gno b/examples/gno.land/r/gnoland/faucet/faucet.gno index c68f6d203f0..c9d8ed503d0 100644 --- a/examples/gno.land/r/gnoland/faucet/faucet.gno +++ b/examples/gno.land/r/gnoland/faucet/faucet.gno @@ -10,11 +10,11 @@ import ( var ( // configurable by admin. - gAdminAddr std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") - gControllers = avl.NewTree() - gControllersSize = 10 // limit it to 10 - gInPause = false - gMessage = "# Community Faucet.\n\n" + gAdminAddr std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + gControllers = avl.NewTree() + gControllersMaxSize = 10 // limit it to 10 + gInPause = false + gMessage = "# Community Faucet.\n\n" // internal vars, for stats. gTotalTransferred std.Coins @@ -52,11 +52,11 @@ func GetPerTransferLimit() int64 { return gLimit.Amount } -func Render(path string) string { +func Render(_ string) string { banker := std.GetBanker(std.BankerTypeRealmSend) balance := banker.GetCoins(std.GetOrigPkgAddr()) - output := path + gMessage + output := gMessage if gInPause { output += "Status: inactive.\n" } else { @@ -69,7 +69,7 @@ func Render(path string) string { output += ufmt.Sprintf("Admin: %s\n\n ", gAdminAddr.String()) output += ufmt.Sprintf("Controllers:\n\n ") - for i := 0; i < gControllersSize; i++ { + for i := 0; i < gControllers.Size(); i++ { _, v := gControllers.GetByIndex(i) output += ufmt.Sprintf("%s ", v.(std.Address)) } diff --git a/examples/gno.land/r/gnoland/faucet/z0_filetest.gno b/examples/gno.land/r/gnoland/faucet/z0_filetest.gno new file mode 100644 index 00000000000..d6c3273497f --- /dev/null +++ b/examples/gno.land/r/gnoland/faucet/z0_filetest.gno @@ -0,0 +1,27 @@ +package main + +import ( + "gno.land/r/gnoland/faucet" +) + +// assert render with empty path and no controllers +func main() { + println(faucet.Render("")) +} + +// Output: +// # Community Faucet. +// +// Status: active. +// Balance: 200000000ugnot. +// Total transfers: (in 0 times). +// +// Package address: g17rgsdnfxzza0sdfsdma37sdwxagsz378833ca4 +// +// Admin: g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 +// +// Controllers: +// +// +// +// Per request limit: 350000000ugnot diff --git a/examples/gno.land/r/gnoland/faucet/z1_filetest.gno b/examples/gno.land/r/gnoland/faucet/z1_filetest.gno new file mode 100644 index 00000000000..54fa750ba5e --- /dev/null +++ b/examples/gno.land/r/gnoland/faucet/z1_filetest.gno @@ -0,0 +1,27 @@ +package main + +import ( + "gno.land/r/gnoland/faucet" +) + +// assert render with a path and no controllers +func main() { + println(faucet.Render("path")) +} + +// Output: +// # Community Faucet. +// +// Status: active. +// Balance: 200000000ugnot. +// Total transfers: (in 0 times). +// +// Package address: g17rgsdnfxzza0sdfsdma37sdwxagsz378833ca4 +// +// Admin: g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 +// +// Controllers: +// +// +// +// Per request limit: 350000000ugnot diff --git a/examples/gno.land/r/gnoland/faucet/z2_filetest.gno b/examples/gno.land/r/gnoland/faucet/z2_filetest.gno new file mode 100644 index 00000000000..1791cd91989 --- /dev/null +++ b/examples/gno.land/r/gnoland/faucet/z2_filetest.gno @@ -0,0 +1,44 @@ +package main + +import ( + "std" + + "gno.land/p/demo/testutils" + "gno.land/r/gnoland/faucet" +) + +// assert render with empty path and 2 controllers +func main() { + var ( + adminaddr = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + controlleraddr1 = testutils.TestAddress("controller1") + controlleraddr2 = testutils.TestAddress("controller2") + ) + std.TestSetOrigCaller(adminaddr) + err := faucet.AdminAddController(controlleraddr1) + if err != nil { + panic(err) + } + err = faucet.AdminAddController(controlleraddr2) + if err != nil { + panic(err) + } + println(faucet.Render("")) +} + +// Output: +// # Community Faucet. +// +// Status: active. +// Balance: 200000000ugnot. +// Total transfers: (in 0 times). +// +// Package address: g17rgsdnfxzza0sdfsdma37sdwxagsz378833ca4 +// +// Admin: g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 +// +// Controllers: +// +// g1vdhkuarjdakxcetjx9047h6lta047h6lsdacav g1vdhkuarjdakxcetjxf047h6lta047h6lnrev3v +// +// Per request limit: 350000000ugnot diff --git a/examples/gno.land/r/gnoland/faucet/z3_filetest.gno b/examples/gno.land/r/gnoland/faucet/z3_filetest.gno new file mode 100644 index 00000000000..1dca56811a9 --- /dev/null +++ b/examples/gno.land/r/gnoland/faucet/z3_filetest.gno @@ -0,0 +1,56 @@ +package main + +import ( + "std" + + "gno.land/p/demo/testutils" + "gno.land/r/gnoland/faucet" +) + +// assert render with 2 controllers and 2 transfers +func main() { + var ( + adminaddr = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + controlleraddr1 = testutils.TestAddress("controller1") + controlleraddr2 = testutils.TestAddress("controller2") + testaddr1 = testutils.TestAddress("test1") + testaddr2 = testutils.TestAddress("test2") + ) + std.TestSetOrigCaller(adminaddr) + err := faucet.AdminAddController(controlleraddr1) + if err != nil { + panic(err) + } + err = faucet.AdminAddController(controlleraddr2) + if err != nil { + panic(err) + } + std.TestSetOrigCaller(controlleraddr1) + err = faucet.Transfer(testaddr1, 1000000) + if err != nil { + panic(err) + } + std.TestSetOrigCaller(controlleraddr2) + err = faucet.Transfer(testaddr1, 2000000) + if err != nil { + panic(err) + } + println(faucet.Render("")) +} + +// Output: +// # Community Faucet. +// +// Status: active. +// Balance: 197000000ugnot. +// Total transfers: 3000000ugnot (in 2 times). +// +// Package address: g17rgsdnfxzza0sdfsdma37sdwxagsz378833ca4 +// +// Admin: g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 +// +// Controllers: +// +// g1vdhkuarjdakxcetjx9047h6lta047h6lsdacav g1vdhkuarjdakxcetjxf047h6lta047h6lnrev3v +// +// Per request limit: 350000000ugnot diff --git a/gnovm/tests/file.go b/gnovm/tests/file.go index 10eb0ddb62c..a9f15ebcab3 100644 --- a/gnovm/tests/file.go +++ b/gnovm/tests/file.go @@ -3,6 +3,7 @@ package tests import ( "bytes" "fmt" + "go/ast" "go/parser" "go/token" "io" @@ -347,7 +348,7 @@ func wantedFromComment(p string) (directives []string, pkgPath, res, err, rops s return } for _, comments := range f.Comments { - text := comments.Text() + text := readComments(comments) if strings.HasPrefix(text, "PKGPATH:") { line := strings.SplitN(text, "\n", 2)[0] pkgPath = strings.TrimSpace(strings.TrimPrefix(line, "PKGPATH:")) @@ -386,6 +387,21 @@ func wantedFromComment(p string) (directives []string, pkgPath, res, err, rops s return } +// readComments returns //-style comments from cg, but without truncating empty +// lines like cg.Text(). +func readComments(cg *ast.CommentGroup) string { + var b strings.Builder + for _, c := range cg.List { + if len(c.Text) < 2 || c.Text[:2] != "//" { + // ignore no //-style comment + break + } + s := strings.TrimPrefix(c.Text[2:], " ") + b.WriteString(s + "\n") + } + return b.String() +} + // Replace comment in file with given output given directive. func replaceWantedInPlace(path string, directive string, output string) { bz := osm.MustReadFile(path)