Skip to content

Commit

Permalink
feat: add JJOptimist's Homepage realm to examples (#3405)
Browse files Browse the repository at this point in the history
This pull request adds a new realm example to the Gno examples
repository—JJOptimist's Homepage.

The realm includes:
- Personal introduction and contact information
- Configuration management system
- Owner controls 
- Tests for ownership and rendering

---------

Co-authored-by: Leon Hudak <33522493+leohhhn@users.noreply.github.com>
  • Loading branch information
JJOptimist and leohhhn authored Feb 5, 2025
1 parent d7bfee2 commit e12b3f1
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 0 deletions.
32 changes: 32 additions & 0 deletions examples/gno.land/r/jjoptimist/home/config.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package home

import (
"std"

"gno.land/p/demo/ownable"
)

type Config struct {
Title string
Description string
Github string
}

var config = Config{
Title: "JJOptimist's Home Realm 🏠",
Description: "Exploring Gno and building on-chain",
Github: "jjoptimist",
}

var Ownable = ownable.NewWithAddress(std.Address("g16vfw3r7zuz43fhky3xfsuc2hdv9tnhvlkyn0nj"))

func GetConfig() Config {
return config
}

func UpdateConfig(newTitle, newDescription, newGithub string) {
Ownable.AssertCallerIsOwner()
config.Title = newTitle
config.Description = newDescription
config.Github = newGithub
}
1 change: 1 addition & 0 deletions examples/gno.land/r/jjoptimist/home/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/r/jjoptimist/home
82 changes: 82 additions & 0 deletions examples/gno.land/r/jjoptimist/home/home.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package home

import (
"std"
"strconv"
"time"

"gno.land/r/leon/hof"
)

const (
gnomeArt1 = ` /\
/ \
,,,,,
(o.o)
(\_/)
-"-"-`

gnomeArt2 = ` /\
/ \
,,,,,
(^.^)
(\_/)
-"-`

gnomeArt3 = ` /\
/ \
,,,,,
(*.*)
(\_/)
"-"-"`

gnomeArt4 = ` /\
/ \
,,,,,
(o.~)
(\_/)
-"-`
)

var creation time.Time

func getGnomeArt(height int64) string {
var art string
switch {
case height%7 == 0:
art = gnomeArt4 // winking gnome
case height%5 == 0:
art = gnomeArt3 // starry-eyed gnome
case height%3 == 0:
art = gnomeArt2 // happy gnome
default:
art = gnomeArt1 // regular gnome
}
return "```\n" + art + "\n```\n"
}

func init() {
creation = time.Now()
hof.Register()
}

func Render(path string) string {
height := std.GetHeight()

output := "# " + config.Title + "\n\n"

output += "## About Me\n"
output += "- 👋 Hi, I'm JJOptimist\n"
output += getGnomeArt(height)
output += "- 🌱 " + config.Description + "\n"

output += "## Contact\n"
output += "- 📫 GitHub: [" + config.Github + "](https://github.com/" + config.Github + ")\n"

output += "\n---\n"
output += "_Realm created: " + creation.Format("2006-01-02 15:04:05 UTC") + "_\n"
output += "_Owner: " + Ownable.Owner().String() + "_\n"
output += "_Current Block Height: " + strconv.Itoa(int(height)) + "_"

return output
}
60 changes: 60 additions & 0 deletions examples/gno.land/r/jjoptimist/home/home_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package home

import (
"strings"
"testing"
)

func TestConfig(t *testing.T) {
cfg := GetConfig()

if cfg.Title != "JJOptimist's Home Realm 🏠" {
t.Errorf("Expected title to be 'JJOptimist's Home Realm 🏠', got %s", cfg.Title)
}
if cfg.Description != "Exploring Gno and building on-chain" {
t.Errorf("Expected description to be 'Exploring Gno and building on-chain', got %s", cfg.Description)
}
if cfg.Github != "jjoptimist" {
t.Errorf("Expected github to be 'jjoptimist', got %s", cfg.Github)
}
}

func TestRender(t *testing.T) {
output := Render("")

// Test that required sections are present
if !strings.Contains(output, "# "+config.Title) {
t.Error("Rendered output missing title")
}
if !strings.Contains(output, "## About Me") {
t.Error("Rendered output missing About Me section")
}
if !strings.Contains(output, "## Contact") {
t.Error("Rendered output missing Contact section")
}
if !strings.Contains(output, config.Description) {
t.Error("Rendered output missing description")
}
if !strings.Contains(output, config.Github) {
t.Error("Rendered output missing github link")
}
}

func TestGetGnomeArt(t *testing.T) {
tests := []struct {
height int64
expected string
}{
{7, gnomeArt4}, // height divisible by 7
{5, gnomeArt3}, // height divisible by 5
{3, gnomeArt2}, // height divisible by 3
{2, gnomeArt1}, // default case
}

for _, tt := range tests {
art := getGnomeArt(tt.height)
if !strings.Contains(art, tt.expected) {
t.Errorf("For height %d, expected art containing %s, got %s", tt.height, tt.expected, art)
}
}
}

0 comments on commit e12b3f1

Please sign in to comment.