Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add JJOptimist's Homepage realm to examples #3405

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions examples/gno.land/r/jjoptimist/config.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package home

import (
"std"
)

var (

leohhhn marked this conversation as resolved.
Show resolved Hide resolved
OWNER_ADDR = std.Address("g16vfw3r7zuz43fhky3xfsuc2hdv9tnhvlkyn0nj")

owner std.Address
leohhhn marked this conversation as resolved.
Show resolved Hide resolved

config = struct {
title string
description string
github string
}{
title: "JJOptimist's Home Realm 🏠",
description: "Exploring Gno and building on-chain",
github: "jjoptimist",
}
)

func init() {
owner = OWNER_ADDR
leohhhn marked this conversation as resolved.
Show resolved Hide resolved
}

func IsOwner(addr std.Address) bool {
return addr == owner
}

func GetConfig() struct {
title string
description string
github string
} {
leohhhn marked this conversation as resolved.
Show resolved Hide resolved
return config
}

func UpdateConfig(newTitle, newDescription, newGithub string) {
if !IsOwner(std.GetOrigCaller()) {
panic("not authorized")
}
config.title = newTitle
config.description = newDescription
config.github = newGithub
}

func GetOwner() std.Address {
return owner
}
5 changes: 5 additions & 0 deletions examples/gno.land/r/jjoptimist/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module gno.land/r/jjoptimist/home

require (
gno.land/p/demo/testutils v0.0.0
)
32 changes: 32 additions & 0 deletions examples/gno.land/r/jjoptimist/home.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package home

import (
"time"
)

var (
creation time.Time
)

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

func Render(path string) string {
cfg := GetConfig()

output := "# " + cfg.title + "\n\n"

output += "## About Me\n"
output += "- 👋 Hi, I'm JJOptimist\n"
output += "- 🌱 " + cfg.description + "\n"

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

output += "\n---\n"
output += "_Realm created: " + creation.Format("2006-01-02 15:04:05 MST") + "_\n"
output += "_Owner: " + GetOwner().String() + "_"

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

import (
"std"
"testing"
"time"
)

func TestOwnership(t *testing.T) {

leohhhn marked this conversation as resolved.
Show resolved Hide resolved
owner := GetOwner()
if owner.String() != "g16vfw3r7zuz43fhky3xfsuc2hdv9tnhvlkyn0nj" {
t.Errorf("Expected owner to be g16vfw3r7zuz43fhky3xfsuc2hdv9tnhvlkyn0nj, got %s", owner.String())
}


if !IsOwner(owner) {
t.Error("Owner address should return true for IsOwner check")
}


nonOwner := std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
if IsOwner(nonOwner) {
t.Error("Non-owner address should return false for IsOwner check")
}
}

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

// Test initial config values
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("")

// Random test for section
expectedSections := []string{
"# JJOptimist's Home Realm",
"## About Me",
"## Contact",
"GitHub: [jjoptimist]",
"_Owner: g16vfw3r7zuz43fhky3xfsuc2hdv9tnhvlkyn0nj_",
}

for _, section := range expectedSections {
if !contains(output, section) {
t.Errorf("Expected output to contain '%s'", section)
}
}
}


func contains(s, substr string) bool {
for i := 0; i < len(s)-len(substr)+1; i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}
Loading