Playground
challenge.gno
package vintage
import (
"gno.land/p/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/flag"
"gno.land/r/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/hall"
)
var info = flag.Info{
Slug: "vintage",
Title: "Counterfeit Vintage",
Difficulty: flag.Hard,
Category: "interfaces",
Brief: "The appraiser certifies bottles older than 1900, but she only handles bottles from this " +
"cellar: every Bottle carries the cellar's seal, an unexported method no other realm can " +
"write. The house only pours the 2024. Get a bottle from before 1900 certified in your name.",
}
var captures = flag.NewLedger()
type challenge struct{}
func (challenge) Info() flag.Info { return info }
func (challenge) Captured(player address) bool { return captures.Captured(player) }
// Challenge describes this realm to a hall.
func Challenge() flag.Challenge { return challenge{} }
func init(cur realm) { hall.Enlist(cross(cur), challenge{}) }
gnomod.toml
module = "gno.land/r/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/vintage" gno = "0.9" [addpkg] creator = "g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf" height = 1457773 max_deposit = "100000000ugnot"
vintage.gno
// Package vintage is a Cellar challenge about trusting interface values.
package vintage
import (
"strconv"
"strings"
"gno.land/p/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/flag"
"gno.land/p/nt/markdown/sanitize/v0"
)
const maxLabel = 60
// Bottle is a bottle from this cellar. The seal method is unexported, so
// no other package can declare a type that implements Bottle.
type Bottle interface {
Vintage() int
Label() string
seal()
}
type bottle struct {
vintage int
label string
}
func (b *bottle) Vintage() int { return b.vintage }
func (b *bottle) Label() string { return b.label }
func (b *bottle) seal() {}
// Uncork pours the house wine.
func Uncork() Bottle { return &bottle{vintage: 2024, label: "House red"} }
var certified []string // labels of certified bottles, for the window
// Appraise certifies b if it was bottled before 1900.
func Appraise(cur realm, b Bottle) {
player := flag.MustPlayer(cur.Previous())
if b == nil {
panic("vintage: the appraiser needs a bottle")
}
if b.Vintage() >= 1900 {
panic("vintage: a " + strconv.Itoa(b.Vintage()) + " is young. Come back with something older")
}
label := b.Label()
if len(label) > maxLabel {
label = label[:maxLabel]
}
certified = append(certified, label+" ("+strconv.Itoa(b.Vintage())+")")
captures.Record(player)
}
// Render shows the appraiser's window.
func Render(_ string) string {
var b strings.Builder
b.WriteString(flag.Card(info, captures))
b.WriteString("\n## In the window\n\n")
if len(certified) == 0 {
b.WriteString("*No bottle has been certified.*\n")
}
for i := len(certified) - 1; i >= 0 && i >= len(certified)-10; i-- {
b.WriteString("- certified: " + sanitize.InlineText(certified[i]) + "\n")
}
b.WriteString("\n## Captured by\n\n" + flag.Captures(captures, 5))
return b.String()
}
vintage_test.gno
package vintage
import (
"testing"
"gno.land/p/nt/uassert/v0"
)
const ash = address("g1zkzjmyveh7euq83h5zhnhj6mg0d7kr35nphzd2")
func TestHouseWineIsTooYoung(cur realm, t *testing.T) {
testing.SetRealm(testing.NewUserRealm(ash))
uassert.AbortsContains(t, cur, "is young", func() { Appraise(cross(cur), Uncork()) })
uassert.AbortsContains(t, cur, "needs a bottle", func() { Appraise(cross(cur), nil) })
uassert.False(t, captures.Captured(ash))
}
func TestUncork(t *testing.T) {
b := Uncork()
uassert.Equal(t, 2024, b.Vintage())
uassert.Equal(t, "House red", b.Label())
}