Skip to content

Commit

Permalink
chore: fixup
Browse files Browse the repository at this point in the history
Signed-off-by: moul <[email protected]>
  • Loading branch information
moul committed Nov 19, 2024
1 parent 85b0439 commit ba3ff33
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
42 changes: 42 additions & 0 deletions examples/gno.land/r/docs/add/add.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package add

import (
"strconv"
"time"

"gno.land/p/moul/txlink"
)

// Global variables to store the current number and last update timestamp
var (
number int
lastUpdate time.Time
)

// Add function to update the number and timestamp
func Add(n int) {
number += n
lastUpdate = time.Now()
}

// Render displays the current number value, last update timestamp, and a link to call Add with 42
func Render(path string) string {
// Display the current number and formatted last update time
result := "# Add Example\n\n"
result += "Current Number: " + strconv.Itoa(number) + "\n\n"
result += "Last Updated: " + formatTimestamp(lastUpdate) + "\n\n"

// Generate a transaction link to call Add with 42 as the default parameter
txLink := txlink.URL("Add", "n", "42")
result += "[Increase Number](" + txLink + ")\n"

return result
}

// Helper function to format the timestamp for readability
func formatTimestamp(timestamp time.Time) string {
if timestamp.IsZero() {
return "Never"
}
return timestamp.Format("2006-01-02 15:04:05")
}
44 changes: 44 additions & 0 deletions examples/gno.land/r/docs/add/add_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package add

import (
"testing"
)

func TestRenderAndAdd(t *testing.T) {
// Initial Render output
output := Render("")
expected := `# Add Example
Current Number: 0
Last Updated: Never
[Increase Number](/r/docs/add$help&func=Add&n=42)
`
if output != expected {
t.Errorf("Initial Render failed, got:\n%s", output)
}

// Call Add with a value of 10
Add(10)

// Call Add again with a value of -5
Add(-5)

// Render after two Add calls
finalOutput := Render("")

// Initial Render output
output = Render("")
expected = `# Add Example
Current Number: 5
Last Updated: 2009-02-13 23:31:30
[Increase Number](/r/docs/add$help&func=Add&n=42)
`
if output != expected {
t.Errorf("Final Render failed, got:\n%s", output)
}
}
3 changes: 3 additions & 0 deletions examples/gno.land/r/docs/add/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module gno.land/r/docs/add

require gno.land/p/moul/txlink v0.0.0-latest
1 change: 1 addition & 0 deletions examples/gno.land/r/docs/home/home.gno
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Explore various examples to learn more about Gno functionality and usage.
## Examples
- [Hello World](/r/docs/hello) - A simple introductory example.
- [Add](/r/docs/add) - An interactive example to update a number with transactions.
- [AVL Pager](/r/docs/avl_pager) - Paginate through AVL tree items.
- ...
Expand Down

0 comments on commit ba3ff33

Please sign in to comment.