render_test.gno
3.04 Kb · 91 lines
1package rpgroom
2
3import (
4 "strings"
5 "testing"
6
7 "gno.land/p/nt/testutils/v0"
8 "gno.land/p/nt/uassert/v0"
9)
10
11// ExampleRender pins the empty-room view. resetState() puts the realm back to
12// its init() shape, so the example does not depend on test ordering.
13func ExampleRender() {
14 resetState()
15 print(Render(""))
16 // Output:
17 // # RPG Room
18 //
19 // A single dungeon room with one monster in it at a time. Call `Spawn("name")` to enter, `Attack()` to fight the resident monster, `Rest()` to heal up, and `Revive()` if you fall.
20 //
21 // ## Current monster
22 //
23 // **Slime** — level 1
24 //
25 // `##########` 35/35 HP
26 //
27 // - Attack: 5
28 // - Monsters cleared so far: 0
29 //
30 // ## Leaderboard
31 //
32 // _No heroes yet. Be the first: `Spawn("name")`._
33}
34
35// TestRenderLeaderboard checks spawned heroes replace the empty notice and are
36// listed in rank order.
37func TestRenderLeaderboard(cur realm, t *testing.T) {
38 resetState()
39
40 testing.SetRealm(testing.NewUserRealm(testutils.TestAddress("alice")))
41 Spawn(cross(cur), "Alice the Bold")
42 testing.SetRealm(testing.NewUserRealm(testutils.TestAddress("bob")))
43 Spawn(cross(cur), "Bob the Brave")
44
45 out := Render("")
46 uassert.False(t, strings.Contains(out, "_No heroes yet"), "empty notice should be gone")
47 uassert.True(t, strings.Contains(out, "| # | Hero | Lvl | HP | Kills | Gold | Status |"),
48 "leaderboard header should render")
49 uassert.True(t, strings.Contains(out, "Alice the Bold"), "alice should be listed")
50 uassert.True(t, strings.Contains(out, "Bob the Brave"), "bob should be listed")
51 uassert.True(t, strings.Contains(out, "| alive |"), "a fresh hero is alive")
52}
53
54// TestRenderHeroPage checks the per-address detail route.
55func TestRenderHeroPage(cur realm, t *testing.T) {
56 resetState()
57 alice := testutils.TestAddress("alice")
58
59 testing.SetRealm(testing.NewUserRealm(alice))
60 Spawn(cross(cur), "Alice the Bold")
61
62 out := Render(alice.String())
63 uassert.True(t, strings.HasPrefix(out, "# Alice the Bold\n"), "hero page should be titled by name")
64 uassert.True(t, strings.Contains(out, "- Level: 1\n"), "level should be shown")
65 uassert.True(t, strings.Contains(out, "- Kills: 0\n"), "kills should be shown")
66 uassert.True(t, strings.Contains(out, "- Gold: 0\n"), "gold should be shown")
67 uassert.False(t, strings.Contains(out, "- Status: fallen"), "a live hero has no fallen status")
68}
69
70// TestRenderUnknownHero covers the not-found branch of the detail route.
71func TestRenderUnknownHero(t *testing.T) {
72 resetState()
73 stranger := testutils.TestAddress("stranger").String()
74
75 uassert.Equal(t, "> [!WARNING]\n> No hero found for `"+stranger+"`.\n", Render(stranger))
76}
77
78// TestRenderTrimsPath checks surrounding whitespace still routes to the room.
79func TestRenderTrimsPath(t *testing.T) {
80 resetState()
81 uassert.Equal(t, Render(""), Render(" "))
82}
83
84// TestRenderMonsterHPBar checks the monster's bar tracks damage.
85func TestRenderMonsterHPBar(t *testing.T) {
86 resetState()
87 monster.HP = monster.MaxHP / 2
88
89 uassert.True(t, strings.Contains(Render(""), hpBar(monster.HP, monster.MaxHP)),
90 "the room should render the monster's current HP bar")
91}