-
Notifications
You must be signed in to change notification settings - Fork 377
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: moul <[email protected]>
- Loading branch information
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters